-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrash_handler.hpp
More file actions
147 lines (123 loc) · 6.46 KB
/
crash_handler.hpp
File metadata and controls
147 lines (123 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef LINUXIFY_CRASH_HANDLER_HPP
#define LINUXIFY_CRASH_HANDLER_HPP
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <exception>
#include <psapi.h>
#include "shell_streams.hpp"
namespace CrashHandler {
inline std::string getExceptionName(DWORD code) {
switch(code) {
case EXCEPTION_ACCESS_VIOLATION: return "ACCESS_VIOLATION";
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: return "ARRAY_BOUNDS_EXCEEDED";
case EXCEPTION_BREAKPOINT: return "BREAKPOINT";
case EXCEPTION_DATATYPE_MISALIGNMENT: return "DATATYPE_MISALIGNMENT";
case EXCEPTION_FLT_DENORMAL_OPERAND: return "FLT_DENORMAL_OPERAND";
case EXCEPTION_FLT_DIVIDE_BY_ZERO: return "FLT_DIVIDE_BY_ZERO";
case EXCEPTION_FLT_INEXACT_RESULT: return "FLT_INEXACT_RESULT";
case EXCEPTION_FLT_INVALID_OPERATION: return "FLT_INVALID_OPERATION";
case EXCEPTION_FLT_OVERFLOW: return "FLT_OVERFLOW";
case EXCEPTION_FLT_STACK_CHECK: return "FLT_STACK_CHECK";
case EXCEPTION_FLT_UNDERFLOW: return "FLT_UNDERFLOW";
case EXCEPTION_ILLEGAL_INSTRUCTION: return "ILLEGAL_INSTRUCTION";
case EXCEPTION_IN_PAGE_ERROR: return "IN_PAGE_ERROR";
case EXCEPTION_INT_DIVIDE_BY_ZERO: return "INT_DIVIDE_BY_ZERO";
case EXCEPTION_INT_OVERFLOW: return "INT_OVERFLOW";
case EXCEPTION_INVALID_DISPOSITION: return "INVALID_DISPOSITION";
case EXCEPTION_NONCONTINUABLE_EXCEPTION: return "NONCONTINUABLE_EXCEPTION";
case EXCEPTION_PRIV_INSTRUCTION: return "PRIV_INSTRUCTION";
case EXCEPTION_SINGLE_STEP: return "SINGLE_STEP";
case EXCEPTION_STACK_OVERFLOW: return "STACK_OVERFLOW";
default: return "UNKNOWN_EXCEPTION";
}
}
inline void printCrashReport(const std::string& reason, PVOID address = nullptr) {
// Force restore console mode to ensure we can see the error
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hIn, &mode);
mode |= ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
SetConsoleMode(hIn, mode);
ShellIO::sout << ShellIO::endl;
ShellIO::sout << ShellIO::Color::Bold << ShellIO::Color::Red
<< "==========================================" << ShellIO::endl;
ShellIO::sout << " FATAL ERROR: LINUXIFY HAS CRASHED" << ShellIO::endl;
ShellIO::sout << "==========================================" << ShellIO::Color::Reset << ShellIO::endl;
ShellIO::sout << ShellIO::Color::LightRed << "Reason: " << reason << ShellIO::Color::Reset << ShellIO::endl;
if (address) {
std::stringstream ss;
ss << "Address: 0x" << std::hex << std::uppercase << (uintptr_t)address;
// Try to find module
HMODULE hMod = NULL;
if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)address, &hMod)) {
char modPath[MAX_PATH];
if (GetModuleFileNameA(hMod, modPath, MAX_PATH)) {
std::string modName = std::string(modPath);
size_t lastSlash = modName.find_last_of("\\/");
if (lastSlash != std::string::npos) modName = modName.substr(lastSlash + 1);
uintptr_t offset = (uintptr_t)address - (uintptr_t)hMod;
ss << " (" << modName << " + 0x" << offset << ")";
}
}
ShellIO::sout << ss.str() << ShellIO::endl;
}
ShellIO::sout << ShellIO::endl << "Stack Trace:" << ShellIO::endl;
// Simple stack trace
PVOID stack[64];
WORD frames = CaptureStackBackTrace(0, 64, stack, NULL);
for (WORD i = 0; i < frames; i++) {
std::stringstream ss;
ss << "[" << std::setw(2) << std::setfill('0') << i << "] 0x"
<< std::hex << std::uppercase << (uintptr_t)stack[i];
HMODULE hMod = NULL;
if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)stack[i], &hMod)) {
char modPath[MAX_PATH];
if (GetModuleFileNameA(hMod, modPath, MAX_PATH)) {
std::string modName = std::string(modPath);
size_t lastSlash = modName.find_last_of("\\/");
if (lastSlash != std::string::npos) modName = modName.substr(lastSlash + 1);
uintptr_t offset = (uintptr_t)stack[i] - (uintptr_t)hMod;
ss << " (" << modName << " + 0x" << offset << ")";
}
}
ShellIO::sout << ss.str() << ShellIO::endl;
}
ShellIO::sout << ShellIO::endl;
ShellIO::sout << "The application will now terminate." << ShellIO::endl;
ShellIO::sout.flush();
}
inline LONG WINAPI UnhandledExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo) {
DWORD code = pExceptionInfo->ExceptionRecord->ExceptionCode;
PVOID addr = pExceptionInfo->ExceptionRecord->ExceptionAddress;
std::stringstream ss;
ss << getExceptionName(code) << " (0x" << std::hex << std::uppercase << code << ")";
printCrashReport(ss.str(), addr);
return EXCEPTION_EXECUTE_HANDLER; // Proceed to termination
}
inline void TerminateHandler() {
std::string reason = "Unhandled C++ Exception (std::terminate)";
std::exception_ptr ptr = std::current_exception();
if (ptr) {
try {
std::rethrow_exception(ptr);
} catch (const std::exception& e) {
reason = std::string("Unhandled Exception: ") + e.what();
} catch (...) {
reason = "Unknown C++ Exception";
}
}
printCrashReport(reason);
std::abort();
}
inline void init() {
SetUnhandledExceptionFilter(UnhandledExceptionHandler);
std::set_terminate(TerminateHandler);
}
}
#endif // LINUXIFY_CRASH_HANDLER_HPP