Last active
September 12, 2018 22:45
-
-
Save 0x1F9F1/09262dfff855ee752222a6de90cfe708 to your computer and use it in GitHub Desktop.
A raii based SEH to C++ exception translater
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
#pragma once | |
#define WIN32_LEAN_AND_MEAN | |
#include <Windows.h> | |
#include <stdexcept> | |
#include <cstdio> | |
class scoped_seh | |
{ | |
protected: | |
static void translate_seh(unsigned int code, EXCEPTION_POINTERS* ep) | |
{ | |
char buffer[2048]; | |
std::snprintf(buffer, sizeof(buffer), | |
#if defined(_WIN64) | |
"Unhandled exception 0x%08X at 0x%016llX\n" | |
"RAX = 0x%016llX RBX = 0x%016llX RCX = 0x%016llX RDX = 0x%016llX\n" | |
"RSP = 0x%016llX RBP = 0x%016llX RSI = 0x%016llX RDI = 0x%016llX\n" | |
"R8 = 0x%016llX R9 = 0x%016llX R10 = 0x%016llX R11 = 0x%016llX\n" | |
"R12 = 0x%016llX R13 = 0x%016llX R14 = 0x%016llX R15 = 0x%016llX\n", | |
code, | |
reinterpret_cast<DWORD64>(ep->ExceptionRecord->ExceptionAddress), | |
ep->ContextRecord->Rax, ep->ContextRecord->Rbx, ep->ContextRecord->Rcx, ep->ContextRecord->Rdx, | |
ep->ContextRecord->Rsp, ep->ContextRecord->Rbp, ep->ContextRecord->Rsi, ep->ContextRecord->Rdi, | |
ep->ContextRecord->R8, ep->ContextRecord->R9, ep->ContextRecord->R10, ep->ContextRecord->R11, | |
ep->ContextRecord->R12, ep->ContextRecord->R13, ep->ContextRecord->R14, ep->ContextRecord->R15 | |
#elif defined(_WIN32) | |
"Unhandled exception 0x%08X at 0x%08X\n" | |
"EAX = 0x%08lX EBX = 0x%08lX ECX = 0x%08lX EDX = 0x%08lX\n" | |
"ESP = 0x%08lX EBP = 0x%08lX ESI = 0x%08lX EDI = 0x%08lX\n", | |
code, | |
reinterpret_cast<DWORD>(ep->ExceptionRecord->ExceptionAddress), | |
ep->ContextRecord->Eax, ep->ContextRecord->Ebx, ep->ContextRecord->Ecx, ep->ContextRecord->Edx, | |
ep->ContextRecord->Esp, ep->ContextRecord->Ebp, ep->ContextRecord->Esi, ep->ContextRecord->Edi | |
#endif | |
); | |
throw std::runtime_error(buffer); | |
} | |
_se_translator_function _old_se_translator; | |
public: | |
scoped_seh() | |
: _old_se_translator(_set_se_translator(translate_seh)) | |
{ } | |
~scoped_seh() | |
{ | |
_set_se_translator(_old_se_translator); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment