-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExceptionHandler.hpp
167 lines (142 loc) · 4.08 KB
/
ExceptionHandler.hpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#pragma once
#ifndef _EH32_INCLUDE
#define _EH32_INCLUDE
#include <Windows.h>
#include <DbgHelp.h>
#include <Psapi.h>
#include <Zydis/Zydis.h>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <map>
#include <cstdint>
#include <algorithm>
#include <memory>
#if !defined(XSTATE_AVX512_KMASK) && !defined(XSTATE_AVX512_ZMM_H) && !defined(XSTATE_AVX512_ZMM)
#define XSTATE_AVX512_KMASK (5)
#define XSTATE_AVX512_ZMM_H (6)
#define XSTATE_AVX512_ZMM (7)
#endif
#ifndef XSTATE_MASK_AVX512
#define XSTATE_MASK_AVX512 ((1ull << (XSTATE_AVX512_KMASK)) | \
(1ull << (XSTATE_AVX512_ZMM_H)) | \
(1ull << (XSTATE_AVX512_ZMM)))
#endif
#define EH_EVENTNAME "ExceptionHandler Built" " @ " __TIME__ " " __DATE__
#define EH_REPORTSIZE 16384
#if defined(_M_ARM64)
static const char* const Arm64IndexToNameMap[] = {
"X0",
"X1",
"X2",
"X3",
"X4",
"X5",
"X6",
"X7",
"X8",
"X9",
"X10",
"X11",
"X12",
"X13",
"X14",
"X15",
"X16",
"X17",
"X18",
"X19",
"X20",
"X21",
"X22",
"X23",
"X24",
"X25",
"X26",
"X27",
"X28",
"Fp",
"Lr",
};
#endif
namespace PeParser
{
std::uintptr_t get_image_base(std::uintptr_t module_base);
}
std::string SStr_format(const char* fmt, ...);
#define EH_STRREG(EHREG) {#EHREG, EHREG, sizeof(decltype(EHREG))}
namespace ExceptionManager
{
struct EHRegister
{
std::string reg_name;
std::int64_t reg_value;
std::size_t reg_size;
EHRegister(std::string name, std::uint64_t value, std::size_t size)
: reg_name(name), reg_value(value), reg_size(size) { }
};
struct EHStackWalkLine
{
uintptr_t address = 0;
uintptr_t module_base_address = 0;
std::string module_name = "";
std::string source_file_name = "";
std::string function_symbol = "";
std::int32_t line = -1;
};
struct EHCompiledReport
{
std::vector<EHStackWalkLine> complete_callstack;
std::vector<EHRegister> register_list;
std::uint32_t eh_exception_code = 0;
std::uintptr_t eh_fault_address = 0;
//std::string eh_psuedo_name = "";
std::string eh_cpp_exception_symbol = "";
std::string eh_cpp_exception_message = "";
bool should_ignore = false;
};
struct EHFinishedReport
{
char* report_string;
size_t report_size;
bool clipped;
bool should_free;
EHFinishedReport(char* report_string, size_t report_size, bool clipped, bool should_free)
: report_string(report_string), report_size(report_size), clipped(clipped), should_free(should_free) { }
EHFinishedReport() :
report_string(NULL), report_size(0), clipped(false), should_free(false) { }
};
typedef void(*eh_receiver_callback)(EHFinishedReport);
typedef EHFinishedReport(*eh_processor_callback)(EHCompiledReport);
struct EHSettings
{
std::vector<std::uintptr_t> blacklist_code;
std::vector<std::string> blacklist_sym;
std::string prog_name = "";
uintptr_t prog_base; /* handle */
uintptr_t prog_size;
eh_receiver_callback recv_callback;
eh_processor_callback proc_callback;
char* report_dst;
size_t report_dst_size;
bool is_prog_dll;
bool use_seh;
bool use_veh;
};
extern EHSettings g_ehsettings;
extern char g_ehreportbuffer[EH_REPORTSIZE];
void Init(EHSettings* settings);
EHFinishedReport DefaultProcessor(EHCompiledReport report);
void DefaultHandler(EHFinishedReport report);
std::string getBack(const std::string& s, char delim);
bool IsXStatePresent();
std::string ResolveModuleFromAddress(DWORD Address);
EHCompiledReport GenerateReport(PEXCEPTION_POINTERS pExceptionRecord);
PCHAR GetExceptionSymbol(PEXCEPTION_POINTERS pExceptionRecord);
PCHAR GetExceptionMessage(PEXCEPTION_POINTERS pExceptionRecord);
BOOL ProcessException(bool isVEH, PEXCEPTION_POINTERS pExceptionRecord);
LONG WINAPI TopLevelExceptionHandler(PEXCEPTION_POINTERS pExceptionRecord);
LONG WINAPI VectoredExceptionHandler(PEXCEPTION_POINTERS pExceptionRecord);
};
#endif