Add backtrace print to fatal logvisor errors

This commit is contained in:
Jack Andersen 2016-09-07 20:13:49 -10:00
parent 48dc4c40c7
commit ac88aa934d
2 changed files with 99 additions and 4 deletions

View File

@ -15,6 +15,8 @@ extern "C" void logvisorBp();
namespace logvisor
{
void logvisorAbort();
#if _WIN32 && UNICODE
#define LOG_UCS2 1
#endif
@ -114,6 +116,11 @@ void RegisterConsoleLogger();
*/
void RegisterFileLogger(const char* filepath);
/**
* @brief Register signal handlers with system for common client exceptions
*/
void RegisterStandardExceptions();
#if _WIN32
/**
* @brief Spawn an application-owned cmd.exe window for displaying console output
@ -170,7 +177,7 @@ public:
if (severity == Error || severity == Fatal)
logvisorBp();
if (severity == Fatal)
abort();
logvisorAbort();
else if (severity == Error)
++ErrorCount;
}
@ -204,7 +211,7 @@ public:
}
if (severity == Fatal)
abort();
logvisorAbort();
else if (severity == Error)
++ErrorCount;
}

View File

@ -7,19 +7,19 @@
#endif
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#else
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include <fcntl.h>
#include <chrono>
#include <mutex>
#include <thread>
#include <unordered_map>
#include <stdio.h>
#include <inttypes.h>
#include <signal.h>
#include "logvisor/logvisor.hpp"
/* ANSI sequences */
@ -39,6 +39,7 @@ void logvisorBp() {}
namespace logvisor
{
static Module Log("logvisor");
static std::unordered_map<std::thread::id, const char*> ThreadMap;
void RegisterThreadName(const char* name)
@ -66,6 +67,86 @@ void RegisterThreadName(const char* name)
#endif
}
#if _WIN32
#include <DbgHelp.h>
#pragma comment(lib, "Dbghelp.lib")
static void logvisorAbort()
{
unsigned int i;
void* stack[100];
unsigned short frames;
SYMBOL_INFO* symbol;
HANDLE process;
process = GetCurrentProcess();
SymInitialize(process, NULL, TRUE);
frames = CaptureStackBackTrace(0, 100, stack, NULL);
symbol = (SYMBOL_INFO*)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1);
symbol->MaxNameLen = 255;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
for (i = 0; i < frames; i++)
{
SymFromAddr(process, (DWORD64)(stack[i]), 0, symbol);
printf("%i: %s - 0x%0llX", frames - i - 1, symbol->Name, symbol->Address);
DWORD dwDisplacement;
IMAGEHLP_LINE64 line;
SymSetOptions(SYMOPT_LOAD_LINES);
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
if (SymGetLineFromAddr64(process, (DWORD64)(stack[i]), &dwDisplacement, &line))
{
// SymGetLineFromAddr64 returned success
printf(" LINE %d\n", line.LineNumber);
}
else
{
printf("\n");
}
}
free(symbol);
// If you caught one of the above signals, it is likely you just
// want to quit your program right now.
abort();
}
#else
#include <execinfo.h>
void logvisorAbort()
{
void* array[128];
size_t size = backtrace(array, 128);
char** strings = backtrace_symbols(array, size);
for (size_t i = 0; i < size; i++)
printf("%s\n", strings[i]);
free(strings);
abort();
}
#endif
static void AbortHandler(int signum)
{
switch (signum)
{
case SIGSEGV:
Log.report(logvisor::Fatal, "Segmentation Fault");
case SIGILL:
Log.report(logvisor::Fatal, "Bad Execution");
case SIGFPE:
Log.report(logvisor::Fatal, "Floating Point Exception");
default:
Log.report(logvisor::Fatal, "unknown signal %d", signum);
}
}
std::vector<std::unique_ptr<ILogger>> MainLoggers;
std::atomic_size_t ErrorCount(0);
static std::chrono::steady_clock MonoClock;
@ -314,6 +395,13 @@ void CreateWin32Console()
}
#endif
void RegisterStandardExceptions()
{
signal(SIGSEGV, AbortHandler);
signal(SIGILL, AbortHandler);
signal(SIGFPE, AbortHandler);
}
struct FileLogger : public ILogger
{
FILE* fp;