2022-06-07 06:02:23 +00:00
|
|
|
|
|
|
|
#include <_os.h>
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include<Windows.h>
|
2022-06-19 05:24:36 +00:00
|
|
|
#include<DbgHelp.h>
|
2022-06-07 06:02:23 +00:00
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stddef.h>
|
2022-06-19 05:24:36 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2022-06-16 12:36:35 +00:00
|
|
|
|
|
|
|
#define _countof(arr) (sizeof (arr) / sizeof ((arr)[0]))
|
2022-06-07 06:02:23 +00:00
|
|
|
|
|
|
|
typedef struct SignalMapping {
|
|
|
|
DWORD code;
|
|
|
|
int signal;
|
|
|
|
} SignalMapping;
|
|
|
|
|
|
|
|
static SignalMapping map[] = {
|
|
|
|
{EXCEPTION_ACCESS_VIOLATION, SIGSEGV},
|
|
|
|
{EXCEPTION_IN_PAGE_ERROR, SIGSEGV},
|
|
|
|
{EXCEPTION_ARRAY_BOUNDS_EXCEEDED, SIGSEGV},
|
|
|
|
{EXCEPTION_DATATYPE_MISALIGNMENT, SIGALIGN},
|
|
|
|
{EXCEPTION_BREAKPOINT, SIGBREAK},
|
|
|
|
{EXCEPTION_FLT_DENORMAL_OPERAND, SIGFPE},
|
|
|
|
{EXCEPTION_FLT_DIVIDE_BY_ZERO, SIGFPE},
|
|
|
|
{EXCEPTION_FLT_INEXACT_RESULT, SIGFPE},
|
|
|
|
{EXCEPTION_FLT_INVALID_OPERATION, SIGFPE},
|
|
|
|
{EXCEPTION_FLT_OVERFLOW, SIGFPE},
|
|
|
|
{EXCEPTION_FLT_STACK_CHECK, SIGFPE},
|
|
|
|
{EXCEPTION_FLT_UNDERFLOW, SIGFPE},
|
|
|
|
{EXCEPTION_ILLEGAL_INSTRUCTION, SIGILL},
|
|
|
|
{EXCEPTION_INT_DIVIDE_BY_ZERO, SIGFPE},
|
|
|
|
{EXCEPTION_INT_OVERFLOW, SIGFPE},
|
|
|
|
{EXCEPTION_PRIV_INSTRUCTION, SIGILL},
|
|
|
|
{EXCEPTION_SINGLE_STEP, SIGSTEP},
|
|
|
|
};
|
|
|
|
|
2022-06-07 06:15:47 +00:00
|
|
|
static LONG _win32_handler(EXCEPTION_POINTERS *ExceptionInfo) {
|
2022-06-07 06:02:23 +00:00
|
|
|
EXCEPTION_RECORD *exception = ExceptionInfo->ExceptionRecord;
|
|
|
|
DWORD code = exception->ExceptionCode;
|
|
|
|
int signal = -1;
|
|
|
|
for(int mapping = 0; mapping != _countof(map); ++mapping) {
|
|
|
|
if(code == map[mapping].code) {
|
|
|
|
signal = map[mapping].signal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(signal != -1) {
|
|
|
|
raise(signal);
|
|
|
|
}
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
|
2022-06-19 05:24:36 +00:00
|
|
|
void _os_print_stack_trace() {
|
|
|
|
HANDLE process = GetCurrentProcess();
|
|
|
|
SymInitialize(process, NULL, TRUE);
|
|
|
|
|
|
|
|
void *stack[128];
|
|
|
|
USHORT frames = CaptureStackBackTrace(2, 128, stack, NULL);
|
|
|
|
|
|
|
|
SYMBOL_INFO* symbol = calloc(sizeof(SYMBOL_INFO)+256, 1);
|
|
|
|
symbol->MaxNameLen = 255;
|
|
|
|
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
|
|
|
|
for(size_t i = 0; i < frames; i++) {
|
|
|
|
SymFromAddr(process, (DWORD64)stack[i], 0, symbol);
|
|
|
|
if(strcmp(symbol->Name, "BaseThreadInitThunk") == 0) break;
|
|
|
|
if(strcmp(symbol->Name, "mainCRTStartup") == 0) break;
|
|
|
|
printf(" %u: 0x%"PRIx64" (%s)\n",
|
|
|
|
(int)(frames-i-1),
|
|
|
|
symbol->Address,
|
|
|
|
symbol->Name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
free(symbol);
|
|
|
|
}
|
|
|
|
|
2022-06-07 06:02:23 +00:00
|
|
|
void _os_init_eh() {
|
|
|
|
void *res = AddVectoredExceptionHandler(1, &_win32_handler);
|
|
|
|
if(res == NULL) {
|
|
|
|
ExitProcess(-69420);
|
|
|
|
}
|
|
|
|
}
|