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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
#ifdef _WIN32
#include "windowscrashhandler.h"
#include "../defines.h"
#include <tchar.h>
#include <stdio.h>
#include "StackWalker.h"
// minidump
#include <windows.h>
#include <TlHelp32.h>
//#include <tchar.h>
#include <dbghelp.h>
//#include <stdio.h>
#include <crtdbg.h>
#include <signal.h>
#include <locale>
#include <codecvt>
#include <ctime>
#include <sstream>
#include "../common/common.h"
#ifdef _MSC_VER
#if _MSC_VER < 1400
#define strcpy_s(dst, len, src) strcpy(dst, src)
#define strncpy_s(dst, len, src, maxLen) strncpy(dst, len, src)
#define strcat_s(dst, len, src) strcat(dst, src)
#define _snprintf_s _snprintf
#define _tcscat_s _tcscat
#endif
#endif
#ifdef __MINGW32__
#define strcpy_s(dst, len, src) strcpy(dst, src)
#define strncpy_s(dst, len, src, maxLen) strncpy(dst, src, len)
#define strcat_s(dst, len, src) strcat(dst, src)
#define _snprintf_s _snprintf
#endif
#define CR_SEH_EXCEPTION 0 //!< SEH exception.
#define CR_CPP_TERMINATE_CALL 1 //!< C++ terminate() call.
#define CR_CPP_UNEXPECTED_CALL 2 //!< C++ unexpected() call.
#define CR_CPP_PURE_CALL 3 //!< C++ pure virtual function call (VS .NET and later).
#define CR_CPP_NEW_OPERATOR_ERROR 4 //!< C++ new operator fault (VS .NET and later).
#define CR_CPP_SECURITY_ERROR 5 //!< Buffer overrun error (VS .NET only).
#define CR_CPP_INVALID_PARAMETER 6 //!< Invalid parameter exception (VS 2005 and later).
#define CR_CPP_SIGABRT 7 //!< C++ SIGABRT signal (abort).
#define CR_CPP_SIGFPE 8 //!< C++ SIGFPE signal (flotating point exception).
#define CR_CPP_SIGILL 9 //!< C++ SIGILL signal (illegal instruction).
#define CR_CPP_SIGINT 10 //!< C++ SIGINT signal (CTRL+C).
#define CR_CPP_SIGSEGV 11 //!< C++ SIGSEGV signal (invalid storage access).
#define CR_CPP_SIGTERM 12 //!< C++ SIGTERM signal (termination request).
#define STRINGIZE_(x) #x
#define STRINGIZE(x) STRINGIZE_(x)
#ifndef _AddressOfReturnAddress
// Taken from: http://msdn.microsoft.com/en-us/library/s975zw7k(VS.71).aspx
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif
// _ReturnAddress and _AddressOfReturnAddress should be prototyped before use
EXTERNC void * _AddressOfReturnAddress(void);
EXTERNC void * _ReturnAddress(void);
#endif
#pragma GCC diagnostic ignored "-Wcast-function-type"
namespace Debug {
class StackWalkerWithCallback : public StackWalker
{
public:
StackWalkerWithCallback(const std::function<void(const char * const)> &callback):
StackWalker(RetrieveVerbose | SymBuildPath),
m_callback(callback)
{ }
protected:
virtual void OnOutput(LPCSTR szText) override {
m_callback(szText);
}
private:
std::function<void(const char * const)> m_callback;
};
BOOL PreventSetUnhandledExceptionFilter()
{
HMODULE hKernel32 = LoadLibrary(_T("kernel32.dll"));
if (hKernel32 == NULL) return FALSE;
void* pOrgEntry = reinterpret_cast<void*>(GetProcAddress(hKernel32, "SetUnhandledExceptionFilter"));
if (pOrgEntry == NULL) return FALSE;
#ifdef _M_IX86
// Code for x86:
// 33 C0 xor eax,eax
// C2 04 00 ret 4
unsigned char szExecute[] = { 0x33, 0xC0, 0xC2, 0x04, 0x00 };
#elif _M_X64
// 33 C0 xor eax,eax
// C3 ret
unsigned char szExecute[] = { 0x33, 0xC0, 0xC3 };
#else
#error "The following code only works for x86 and x64!"
#endif
SIZE_T bytesWritten = 0;
BOOL bRet = WriteProcessMemory(GetCurrentProcess(),
pOrgEntry, szExecute, sizeof(szExecute), &bytesWritten);
return bRet;
}
BOOL CALLBACK MyMiniDumpCallback(
PVOID pParam,
const PMINIDUMP_CALLBACK_INPUT pInput,
PMINIDUMP_CALLBACK_OUTPUT pOutput
)
{
BOOL bRet = FALSE;
// Check parameters
if( pInput == 0 )
return FALSE;
if( pOutput == 0 )
return FALSE;
// Process the callbacks
WindowsCrashHandler *handler = (WindowsCrashHandler*)pParam;
switch( pInput->CallbackType )
{
case IncludeModuleCallback:
{
// Include the module into the dump
bRet = TRUE;
}
break;
case IncludeThreadCallback:
{
// Include the thread into the dump
bRet = TRUE;
}
break;
case ModuleCallback:
{
// Are data sections available for this module ?
if( pOutput->ModuleWriteFlags & ModuleWriteDataSeg )
{
// Yes, they are, but do we need them?
if( !handler->isDataSectionNeeded( pInput->Module.FullPath ) )
{
pOutput->ModuleWriteFlags &= (~ModuleWriteDataSeg);
}
}
if( !(pOutput->ModuleWriteFlags & ModuleReferencedByMemory) )
{
// No, it does not - exclude it
pOutput->ModuleWriteFlags &= (~ModuleWriteModule);
}
bRet = TRUE;
}
break;
case ThreadCallback:
{
// Include all thread information into the minidump
bRet = TRUE;
}
break;
case ThreadExCallback:
{
// Include this information
bRet = TRUE;
}
break;
case MemoryCallback:
{
// We do not include any information here -> return FALSE
bRet = FALSE;
}
break;
case CancelCallback:
break;
}
return bRet;
}
void DoHandleCrash() {
WindowsCrashHandler &handler = WindowsCrashHandler::getInstance();
handler.handleCrash();
}
// http://groups.google.com/group/crashrpt/browse_thread/thread/a1dbcc56acb58b27/fbd0151dd8e26daf?lnk=gst&q=stack+overflow#fbd0151dd8e26daf
// Thread procedure doing the dump for stack overflow.
DWORD WINAPI StackOverflowThreadFunction(LPVOID) {
DoHandleCrash();
TerminateProcess(GetCurrentProcess(), CHILLOUT_EXIT_CODE);
return 0;
}
static LONG WINAPI SehHandler(EXCEPTION_POINTERS*) {
#ifdef _DEBUG
fprintf(stderr, "Chillout SehHandler");
#endif
DoHandleCrash();
TerminateProcess(GetCurrentProcess(), CHILLOUT_EXIT_CODE);
// unreachable
return EXCEPTION_EXECUTE_HANDLER;
}
// The following code is intended to fix the issue with 32-bit applications in 64-bit environment.
// http://support.microsoft.com/kb/976038/en-us
// http://code.google.com/p/crashrpt/issues/detail?id=104
void EnableCrashingOnCrashes() {
typedef BOOL (WINAPI *tGetPolicy)(LPDWORD lpFlags);
typedef BOOL (WINAPI *tSetPolicy)(DWORD dwFlags);
static const DWORD EXCEPTION_SWALLOWING = 0x1;
const HMODULE kernel32 = LoadLibraryA("kernel32.dll");
const tGetPolicy pGetPolicy = reinterpret_cast<tGetPolicy>(GetProcAddress(kernel32, "GetProcessUserModeExceptionPolicy"));
const tSetPolicy pSetPolicy = reinterpret_cast<tSetPolicy>(GetProcAddress(kernel32, "SetProcessUserModeExceptionPolicy"));
if(pGetPolicy && pSetPolicy)
{
DWORD dwFlags;
if(pGetPolicy(&dwFlags))
{
// Turn off the filter
pSetPolicy(dwFlags & ~EXCEPTION_SWALLOWING);
}
}
}
WindowsCrashHandler::WindowsCrashHandler() {
m_oldSehHandler = NULL;
#if _MSC_VER>=1300
m_prevPurec = NULL;
m_prevNewHandler = NULL;
#endif
#if _MSC_VER>=1300 && _MSC_VER<1400
m_prevSec = NULL;
#endif
#if _MSC_VER>=1400
m_prevInvpar = NULL;
#endif
m_prevSigABRT = NULL;
m_prevSigINT = NULL;
m_prevSigTERM = NULL;
}
void WindowsCrashHandler::setup(const std::wstring &appName, const std::wstring &dumpsDir) {
m_appName = appName;
if (!appName.empty() && !dumpsDir.empty()) {
std::wstring path = dumpsDir;
while ((path.size() > 1) &&
(path[path.size() - 1] == L'\\')) {
path.erase(path.size() - 1);
}
}
EnableCrashingOnCrashes();
setProcessExceptionHandlers();
setThreadExceptionHandlers();
}
void WindowsCrashHandler::teardown() {
unsetProcessExceptionHandlers();
unsetThreadExceptionHandlers();
}
void WindowsCrashHandler::setCrashCallback(const std::function<void()> &callback) {
m_crashCallback = callback;
}
void WindowsCrashHandler::setBacktraceCallback(const std::function<void(const char * const)> &callback) {
m_backtraceCallback = callback;
}
void WindowsCrashHandler::handleCrash() {
std::lock_guard<std::mutex> guard(m_crashMutex);
(void)guard;
if (m_crashCallback) {
m_crashCallback();
}
// Terminate process
TerminateProcess(GetCurrentProcess(), CHILLOUT_EXIT_CODE);
}
bool WindowsCrashHandler::isDataSectionNeeded(const WCHAR* pModuleName) {
if( pModuleName == 0 ) {
_ASSERTE( _T("Parameter is null.") );
return false;
}
// Extract the module name
WCHAR szFileName[_MAX_FNAME] = L"";
_wsplitpath( pModuleName, NULL, NULL, szFileName, NULL );
// Compare the name with the list of known names and decide
// if contains app name in its path
if( wcsstr( pModuleName, m_appName.c_str() ) != 0 ) {
return true;
} else if( _wcsicmp( szFileName, L"ntdll" ) == 0 ) {
return true;
} else if( wcsstr( szFileName, L"Qt5" ) != 0 ) {
return true;
}
// Complete
return false;
}
void WindowsCrashHandler::setProcessExceptionHandlers() {
//SetErrorMode(SEM_NOGPFAULTERRORBOX | SEM_FAILCRITICALERRORS);
m_oldSehHandler = SetUnhandledExceptionFilter(SehHandler);
#if defined _M_X64 || defined _M_IX86
if (m_oldSehHandler)
PreventSetUnhandledExceptionFilter();
#endif
#if _MSC_VER>=1300
// Catch pure virtual function calls.
// Because there is one _purecall_handler for the whole process,
// calling this function immediately impacts all threads. The last
// caller on any thread sets the handler.
// http://msdn.microsoft.com/en-us/library/t296ys27.aspx
m_prevPurec = _set_purecall_handler(PureCallHandler);
// Catch new operator memory allocation exceptions
//_set_new_mode(1); // Force malloc() to call new handler too
m_prevNewHandler = _set_new_handler(NewHandler);
#endif
#if _MSC_VER>=1400
// Catch invalid parameter exceptions.
m_prevInvpar = _set_invalid_parameter_handler(InvalidParameterHandler);
#endif
#if _MSC_VER>=1300 && _MSC_VER<1400
// Catch buffer overrun exceptions
// The _set_security_error_handler is deprecated in VC8 C++ run time library
m_prevSec = _set_security_error_handler(SecurityHandler);
#endif
#if _MSC_VER>=1400
_set_abort_behavior(0, _WRITE_ABORT_MSG);
_set_abort_behavior(_CALL_REPORTFAULT, _CALL_REPORTFAULT);
#endif
#if defined(_MSC_VER)
// Disable all of the possible ways Windows conspires to make automated
// testing impossible.
// ::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
// ::_set_error_mode(_OUT_TO_STDERR);
// _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
// _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
// _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
// _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
// _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
// _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
m_crtReportHook = _CrtSetReportHook(CrtReportHook);
#endif
// Set up C++ signal handlers
SetConsoleCtrlHandler(ConsoleCtrlHandler, true);
// Catch an abnormal program termination
m_prevSigABRT = signal(SIGABRT, SigabrtHandler);
// Catch illegal instruction handler
m_prevSigINT = signal(SIGINT, SigintHandler);
// Catch a termination request
m_prevSigINT = signal(SIGTERM, SigtermHandler);
}
void WindowsCrashHandler::unsetProcessExceptionHandlers() {
#if _MSC_VER>=1300
if(m_prevPurec!=NULL) {
_set_purecall_handler(m_prevPurec);
}
if(m_prevNewHandler!=NULL) {
_set_new_handler(m_prevNewHandler);
}
#endif
#if _MSC_VER>=1400
if(m_prevInvpar!=NULL) {
_set_invalid_parameter_handler(m_prevInvpar);
}
#endif //_MSC_VER>=1400
#if _MSC_VER>=1300 && _MSC_VER<1400
if(m_prevSec!=NULL)
_set_security_error_handler(m_prevSec);
#endif //_MSC_VER<1400
if(m_prevSigABRT!=NULL) {
signal(SIGABRT, m_prevSigABRT);
}
if(m_prevSigINT!=NULL) {
signal(SIGINT, m_prevSigINT);
}
if(m_prevSigTERM!=NULL) {
signal(SIGTERM, m_prevSigTERM);
}
// Reset SEH exception filter
if (m_oldSehHandler) {
SetUnhandledExceptionFilter(m_oldSehHandler);
}
m_oldSehHandler = NULL;
}
int WindowsCrashHandler::setThreadExceptionHandlers()
{
DWORD dwThreadId = GetCurrentThreadId();
std::lock_guard<std::mutex> guard(m_threadHandlersMutex);
auto it = m_threadExceptionHandlers.find(dwThreadId);
if (it != m_threadExceptionHandlers.end()) {
// handlers are already set for the thread
return 1; // failed
}
ThreadExceptionHandlers handlers;
// Catch terminate() calls.
// In a multithreaded environment, terminate functions are maintained
// separately for each thread. Each new thread needs to install its own
// terminate function. Thus, each thread is in charge of its own termination handling.
// http://msdn.microsoft.com/en-us/library/t6fk7h29.aspx
handlers.m_prevTerm = std::set_terminate(TerminateHandler);
// Catch unexpected() calls.
// In a multithreaded environment, unexpected functions are maintained
// separately for each thread. Each new thread needs to install its own
// unexpected function. Thus, each thread is in charge of its own unexpected handling.
// http://msdn.microsoft.com/en-us/library/h46t5b69.aspx
handlers.m_prevUnexp = std::set_unexpected(UnexpectedHandler);
// Catch a floating point error
typedef void (*sigh)(int);
handlers.m_prevSigFPE = signal(SIGFPE, (sigh)SigfpeHandler);
// Catch an illegal instruction
handlers.m_prevSigILL = signal(SIGILL, SigillHandler);
// Catch illegal storage access errors
handlers.m_prevSigSEGV = signal(SIGSEGV, SigsegvHandler);
m_threadExceptionHandlers[dwThreadId] = handlers;
return 0;
}
int WindowsCrashHandler::unsetThreadExceptionHandlers() {
DWORD dwThreadId = GetCurrentThreadId();
std::lock_guard<std::mutex> guard(m_threadHandlersMutex);
(void)guard;
auto it = m_threadExceptionHandlers.find(dwThreadId);
if (it == m_threadExceptionHandlers.end()) {
return 1;
}
ThreadExceptionHandlers* handlers = &(it->second);
if(handlers->m_prevTerm!=NULL) {
std::set_terminate(handlers->m_prevTerm);
}
if(handlers->m_prevUnexp!=NULL) {
std::set_unexpected(handlers->m_prevUnexp);
}
if(handlers->m_prevSigFPE!=NULL) {
signal(SIGFPE, handlers->m_prevSigFPE);
}
if(handlers->m_prevSigILL!=NULL) {
signal(SIGINT, handlers->m_prevSigILL);
}
if(handlers->m_prevSigSEGV!=NULL) {
signal(SIGSEGV, handlers->m_prevSigSEGV);
}
// Remove from the list
m_threadExceptionHandlers.erase(it);
return 0;
}
int __cdecl WindowsCrashHandler::CrtReportHook(int nReportType, char* szMsg, int* pnRet) {
(void)szMsg;
switch (nReportType) {
case _CRT_WARN:
case _CRT_ERROR:
case _CRT_ASSERT:
// Put some debug code here
break;
}
if (pnRet) {
*pnRet = 0;
}
return TRUE;
}
// CRT terminate() call handler
void __cdecl WindowsCrashHandler::TerminateHandler() {
// Abnormal program termination (terminate() function was called)
DoHandleCrash();
}
// CRT unexpected() call handler
void __cdecl WindowsCrashHandler::UnexpectedHandler() {
// Unexpected error (unexpected() function was called)
DoHandleCrash();
}
// CRT Pure virtual method call handler
void __cdecl WindowsCrashHandler::PureCallHandler() {
// Pure virtual function call
DoHandleCrash();
}
// CRT buffer overrun handler. Available in CRT 7.1 only
#if _MSC_VER>=1300 && _MSC_VER<1400
void __cdecl WindowsCrashHandler::SecurityHandler(int code, void *x)
{
// Security error (buffer overrun).
code;
x;
EXCEPTION_POINTERS* pExceptionPtrs = (PEXCEPTION_POINTERS)_pxcptinfoptrs;
if (pExceptionPtrs == nullptr) {
GetExceptionPointers(CR_CPP_SECURITY_ERROR, &pExceptionPtrs);
}
DoHandleCrash(pExceptionPtrs);
}
#endif
#if _MSC_VER>=1400
// CRT invalid parameter handler
void __cdecl WindowsCrashHandler::InvalidParameterHandler(
const wchar_t* expression,
const wchar_t* function,
const wchar_t* file,
unsigned int line,
uintptr_t pReserved) {
pReserved;
expression;
function;
file;
line;
// fwprintf(stderr, L"Invalid parameter detected in function %s."
// L" File: %s Line: %d\n", function, file, line);
// Retrieve exception information
EXCEPTION_POINTERS* pExceptionPtrs = NULL;
GetExceptionPointers(CR_CPP_INVALID_PARAMETER, &pExceptionPtrs);
DoHandleCrash(pExceptionPtrs);
}
#endif
// CRT new operator fault handler
int __cdecl WindowsCrashHandler::NewHandler(size_t) {
// 'new' operator memory allocation exception
DoHandleCrash();
// Unreacheable code
return 0;
}
// CRT SIGABRT signal handler
void WindowsCrashHandler::SigabrtHandler(int) {
// Caught SIGABRT C++ signal
DoHandleCrash();
}
// CRT SIGFPE signal handler
void WindowsCrashHandler::SigfpeHandler(int /*code*/, int subcode) {
// Floating point exception (SIGFPE)
(void)subcode;
DoHandleCrash();
}
// CRT sigill signal handler
void WindowsCrashHandler::SigillHandler(int) {
// Illegal instruction (SIGILL)
DoHandleCrash();
}
// CRT sigint signal handler
void WindowsCrashHandler::SigintHandler(int) {
// Interruption (SIGINT)
DoHandleCrash();
}
// CRT SIGSEGV signal handler
void WindowsCrashHandler::SigsegvHandler(int) {
// Invalid storage access (SIGSEGV)
DoHandleCrash();
}
// CRT SIGTERM signal handler
void WindowsCrashHandler::SigtermHandler(int) {
// Termination request (SIGTERM)
DoHandleCrash();
}
// CRT SIGTERM signal handler
BOOL WINAPI WindowsCrashHandler::ConsoleCtrlHandler(DWORD type) {
// Termination request (CTRL_C_EVENT)
switch (type)
{
case CTRL_C_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
case CTRL_CLOSE_EVENT:
DoHandleCrash();
break;
}
return false;
}
}
#endif
|