-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #1165
- Loading branch information
Showing
10 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
void S_Log_Init(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "specific/s_log.h" | ||
|
||
#include "log.h" | ||
|
||
#include <backtrace-supported.h> | ||
#include <backtrace.h> | ||
#include <signal.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static void S_Log_ErrorCallback(void *data, const char *msg, int errnum); | ||
static int S_Log_FullTrace( | ||
void *data, uintptr_t pc, const char *filename, int lineno, | ||
const char *function); | ||
static void S_Log_SignalHandler(int sig); | ||
|
||
static void S_Log_ErrorCallback(void *data, const char *msg, int errnum) | ||
{ | ||
LOG_ERROR("%s", msg); | ||
} | ||
|
||
static int S_Log_FullTrace( | ||
void *data, uintptr_t pc, const char *filename, int lineno, | ||
const char *function) | ||
{ | ||
if (filename) { | ||
LOG_INFO( | ||
"0x%08X: %s (%s:%d)", pc, function ? function : "???", | ||
filename ? filename : "???", lineno); | ||
} else { | ||
LOG_INFO("0x%08X: %s", pc, function ? function : "???"); | ||
} | ||
return 0; | ||
} | ||
|
||
static void S_Log_SignalHandler(int sig) | ||
{ | ||
LOG_ERROR("== CRASH REPORT =="); | ||
LOG_INFO("SIGNAL: %d", sig); | ||
LOG_INFO("STACK TRACE:"); | ||
struct backtrace_state *state = backtrace_create_state( | ||
NULL, BACKTRACE_SUPPORTS_THREADS, S_Log_ErrorCallback, NULL); | ||
backtrace_full(state, 0, S_Log_FullTrace, S_Log_ErrorCallback, NULL); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
void S_Log_Init(void) | ||
{ | ||
signal(SIGSEGV, S_Log_SignalHandler); | ||
signal(SIGFPE, S_Log_SignalHandler); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "specific/s_log.h" | ||
|
||
void S_Log_Init(void) | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#include "specific/s_log.h" | ||
|
||
#include "filesystem.h" | ||
#include "log.h" | ||
|
||
#include <process.h> | ||
#include <signal.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <windows.h> | ||
#include <dbghelp.h> | ||
#include <tlhelp32.h> | ||
|
||
static void S_Log_CreateMiniDump(EXCEPTION_POINTERS *ex); | ||
static void S_Log_LogStackTraces(EXCEPTION_POINTERS *ex); | ||
|
||
static void S_Log_CreateMiniDump(EXCEPTION_POINTERS *ex) | ||
{ | ||
char *full_path = File_GetFullPath("TR1X.dmp"); | ||
HANDLE handle = CreateFile( | ||
full_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, | ||
NULL); | ||
MINIDUMP_EXCEPTION_INFORMATION dump_info; | ||
dump_info.ExceptionPointers = ex; | ||
dump_info.ThreadId = GetCurrentThreadId(); | ||
dump_info.ClientPointers = TRUE; | ||
MiniDumpWriteDump( | ||
GetCurrentProcess(), GetCurrentProcessId(), handle, MiniDumpNormal, | ||
&dump_info, NULL, NULL); | ||
CloseHandle(handle); | ||
|
||
LOG_INFO("Crash dump info put in %s", full_path); | ||
} | ||
|
||
static void S_Log_LogStackTraces(EXCEPTION_POINTERS *ex) | ||
{ | ||
LOG_ERROR("== CRASH REPORT =="); | ||
|
||
HANDLE thread = GetCurrentThread(); | ||
HANDLE process = GetCurrentProcess(); | ||
|
||
CONTEXT context = {}; | ||
context.ContextFlags = CONTEXT_FULL; | ||
|
||
if (thread != GetCurrentThread()) { | ||
SuspendThread(thread); | ||
if (GetThreadContext(thread, &context) == FALSE) { | ||
printf("Failed to get context\n"); | ||
return; | ||
} | ||
ResumeThread(thread); | ||
} | ||
RtlCaptureContext(&context); | ||
|
||
SymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_UNDNAME); | ||
|
||
if (!SymInitialize(process, 0, TRUE)) { | ||
LOG_ERROR("Failed to call SymInitialize"); | ||
return; | ||
} | ||
|
||
DWORD image; | ||
STACKFRAME64 stackframe; | ||
ZeroMemory(&stackframe, sizeof(STACKFRAME64)); | ||
|
||
#ifdef _M_IX86 | ||
image = IMAGE_FILE_MACHINE_I386; | ||
stackframe.AddrPC.Offset = context.Eip; | ||
stackframe.AddrPC.Mode = AddrModeFlat; | ||
stackframe.AddrFrame.Offset = context.Ebp; | ||
stackframe.AddrFrame.Mode = AddrModeFlat; | ||
stackframe.AddrStack.Offset = context.Esp; | ||
stackframe.AddrStack.Mode = AddrModeFlat; | ||
#elif _M_X64 | ||
image = IMAGE_FILE_MACHINE_AMD64; | ||
stackframe.AddrPC.Offset = context.Rip; | ||
stackframe.AddrPC.Mode = AddrModeFlat; | ||
stackframe.AddrFrame.Offset = context.Rsp; | ||
stackframe.AddrFrame.Mode = AddrModeFlat; | ||
stackframe.AddrStack.Offset = context.Rsp; | ||
stackframe.AddrStack.Mode = AddrModeFlat; | ||
#endif | ||
|
||
while (StackWalk64( | ||
image, process, thread, &stackframe, &context, 0, | ||
SymFunctionTableAccess64, SymGetModuleBase64, 0)) { | ||
char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)]; | ||
PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer; | ||
|
||
symbol->SizeOfStruct = sizeof(SYMBOL_INFO); | ||
symbol->MaxNameLen = MAX_SYM_NAME; | ||
|
||
DWORD64 displacement64 = 0; | ||
if (SymFromAddr( | ||
process, stackframe.AddrPC.Offset, &displacement64, symbol)) { | ||
IMAGEHLP_LINE64 line; | ||
DWORD displacement32; | ||
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); | ||
if (SymGetLineFromAddr64( | ||
process, stackframe.AddrPC.Offset, &displacement32, | ||
&line)) { | ||
LOG_INFO( | ||
"0x%08llX: %s (%s:%d)", symbol->Address, symbol->Name, | ||
line.FileName, line.LineNumber); | ||
} else { | ||
LOG_INFO("0x%08llX: %s", symbol->Address, symbol->Name); | ||
} | ||
} else { | ||
LOG_INFO("0x%08llX: ???", symbol->Address); | ||
} | ||
} | ||
|
||
SymCleanup(process); | ||
} | ||
|
||
LONG WINAPI S_Log_CrashHandler(EXCEPTION_POINTERS *ex) | ||
{ | ||
S_Log_CreateMiniDump(ex); | ||
S_Log_LogStackTraces(ex); | ||
return EXCEPTION_EXECUTE_HANDLER; | ||
} | ||
|
||
void S_Log_Init(void) | ||
{ | ||
SetUnhandledExceptionFilter(S_Log_CrashHandler); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters