3
3
#include < DbgHelp.h>
4
4
#include " Logger.h"
5
5
6
- bool isExceptionRequierMiniDump (EXCEPTION_POINTERS* frame)
6
+ static std::vector<ReHamster::CrashFunc> s_crashFuncs;
7
+
8
+ static bool isExceptionRequierMiniDump (EXCEPTION_POINTERS* frame)
7
9
{
8
10
if (frame->ExceptionRecord ->ExceptionCode == EXCEPTION_BREAKPOINT ||
9
11
frame->ExceptionRecord ->ExceptionCode == EXCEPTION_SINGLE_STEP)
@@ -13,7 +15,7 @@ bool isExceptionRequierMiniDump(EXCEPTION_POINTERS* frame)
13
15
return true ;
14
16
}
15
17
16
- void WriteMiniDump (EXCEPTION_POINTERS* exception = nullptr )
18
+ static void WriteMiniDump (EXCEPTION_POINTERS* exception = nullptr )
17
19
{
18
20
//
19
21
// Credits https://stackoverflow.com/questions/5028781/how-to-write-a-sample-code-that-will-crash-and-produce-dump-file
@@ -57,7 +59,7 @@ void WriteMiniDump(EXCEPTION_POINTERS* exception = nullptr)
57
59
CloseHandle (hFile);
58
60
}
59
61
60
- void NotifyAboutException (EXCEPTION_POINTERS* exceptionInfoFrame)
62
+ static void NotifyAboutException (EXCEPTION_POINTERS* exceptionInfoFrame)
61
63
{
62
64
MessageBox (
63
65
NULL ,
@@ -84,21 +86,22 @@ void NotifyAboutException(EXCEPTION_POINTERS* exceptionInfoFrame)
84
86
MsgError (" ESP : 0x%x\n " , exceptionInfoFrame->ContextRecord ->Esp );
85
87
MsgError (" ******************************************************************************\n " );
86
88
89
+ // Extra stuff
90
+ for (auto & func : s_crashFuncs)
91
+ {
92
+ func ();
93
+ }
94
+
87
95
WriteMiniDump (exceptionInfoFrame);
88
96
}
89
97
90
- LONG WINAPI ExceptionFilterWin32 (EXCEPTION_POINTERS* exceptionInfoFrame)
98
+ static LONG WINAPI ExceptionFilterWin32 (EXCEPTION_POINTERS* exceptionInfoFrame)
91
99
{
92
- if (isExceptionRequierMiniDump ( exceptionInfoFrame) )
100
+ if (exceptionInfoFrame-> ExceptionRecord -> ExceptionCode < 0x80000000 )
93
101
{
94
- NotifyAboutException (exceptionInfoFrame) ;
102
+ return EXCEPTION_CONTINUE_EXECUTION ;
95
103
}
96
104
97
- return EXCEPTION_EXECUTE_HANDLER;
98
- }
99
-
100
- LONG WINAPI VectoredExceptionHandlerWin32 (EXCEPTION_POINTERS* exceptionInfoFrame)
101
- {
102
105
if (isExceptionRequierMiniDump (exceptionInfoFrame))
103
106
{
104
107
NotifyAboutException (exceptionInfoFrame);
@@ -126,18 +129,16 @@ namespace ReHamster
126
129
void CrashHandlerReporter::Install ()
127
130
{
128
131
_set_purecall_handler (PureCallhandler);
129
- m_prevHandler = reinterpret_cast <std::intptr_t >(SetUnhandledExceptionFilter (ExceptionFilterWin32));
132
+ m_vectored = reinterpret_cast <std::intptr_t >(AddVectoredExceptionHandler (0UL , ExceptionFilterWin32));
133
+ }
134
+
135
+ void CrashHandlerReporter::AddCrashFunc (CrashFunc onCrash)
136
+ {
137
+ s_crashFuncs.push_back (std::move (onCrash));
130
138
}
131
139
132
140
CrashHandlerReporter::~CrashHandlerReporter ()
133
141
{
134
- if (m_prevHandler == 0 )
135
- return ;
136
-
137
- SetUnhandledExceptionFilter (reinterpret_cast <LPTOP_LEVEL_EXCEPTION_FILTER>(m_prevHandler));
138
- if (!AddVectoredExceptionHandler (0UL , VectoredExceptionHandlerWin32))
139
- {
140
- MsgWarning (" AddVectoredExceptionHandler failed!\n " );
141
- }
142
+ RemoveVectoredContinueHandler (reinterpret_cast <PVOID>(m_vectored));
142
143
}
143
144
}
0 commit comments