-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebug.cpp
136 lines (107 loc) · 5.12 KB
/
Debug.cpp
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
#include "Globals.h"
#include "Debug.h"
BOOL EvaluateAssert(BOOL expression,LPCSTR expressionString,LPCSTR function,LPCSTR file,LONG line)
{
if(expression)
return FALSE;
static TCHAR message[4096];
wsprintf(message,TEXT("Function: %s\n\nExpression: %s\n\nFile: %s (Line %d)\n\nDo you want to break into the debugger? Selecting No will exit the application."),function,expressionString,file,line);
switch(MessageBox(NULL,message,TEXT("Assertion Failed"),MB_YESNOCANCEL|MB_ICONEXCLAMATION|MB_TASKMODAL))
{
case IDYES:
return TRUE;
case IDCANCEL:
return FALSE;
}
exit(2);
}
VOID Trace(LPCTSTR format,...)
{
ASSERT(format);
static TCHAR buffer[4096];
va_list args;
va_start(args,format);
wvsprintf(buffer,format,args);
va_end(args);
OutputDebugString(buffer);
}
typedef enum _MINIDUMP_TYPE {
MiniDumpNormal = 0x00000000,
MiniDumpWithDataSegs = 0x00000001,
MiniDumpWithFullMemory = 0x00000002,
MiniDumpWithHandleData = 0x00000004,
MiniDumpFilterMemory = 0x00000008,
MiniDumpScanMemory = 0x00000010,
MiniDumpWithUnloadedModules = 0x00000020,
MiniDumpWithIndirectlyReferencedMemory = 0x00000040,
MiniDumpFilterModulePaths = 0x00000080,
MiniDumpWithProcessThreadData = 0x00000100,
MiniDumpWithPrivateReadWriteMemory = 0x00000200,
MiniDumpWithoutOptionalData = 0x00000400,
MiniDumpWithFullMemoryInfo = 0x00000800,
MiniDumpWithThreadInfo = 0x00001000,
MiniDumpWithCodeSegs = 0x00002000,
MiniDumpWithoutAuxiliaryState = 0x00004000,
MiniDumpWithFullAuxiliaryState = 0x00008000,
MiniDumpValidTypeFlags = 0x0000ffff,
} MINIDUMP_TYPE;
typedef struct _MINIDUMP_EXCEPTION_INFORMATION {
DWORD ThreadId;
PEXCEPTION_POINTERS ExceptionPointers;
BOOL ClientPointers;
} MINIDUMP_EXCEPTION_INFORMATION, *PMINIDUMP_EXCEPTION_INFORMATION;
typedef
BOOL
(WINAPI * LPMINIDUMPWRITEDUMP) (
IN HANDLE hProcess,
IN DWORD ProcessId,
IN HANDLE hFile,
IN MINIDUMP_TYPE DumpType,
IN CONST LPVOID NotUsed1, // IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, OPTIONAL
IN CONST LPVOID NotUsed2, // IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, OPTIONAL
IN CONST LPVOID NotUsed3 // IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam OPTIONAL
);
LONG WINAPI DumpException(LPEXCEPTION_POINTERS exceptionInfo)
{
HMODULE hLibrary = LoadLibrary(TEXT("dbghelp.dll"));
LPMINIDUMPWRITEDUMP miniDumpWriteDump = (LPMINIDUMPWRITEDUMP)GetProcAddress(hLibrary,"MiniDumpWriteDump");
if(!hLibrary || !miniDumpWriteDump)
{
FreeLibrary(hLibrary);
TCHAR message[512];
wsprintf(message,TEXT("The program needs to close because of a unhandled 0x%08lX exception which occurred at 0x%08lX.\n")\
TEXT("No additional information about the problem could be stored. We apologize for the inconvenience."),exceptionInfo->ExceptionRecord->ExceptionCode,exceptionInfo->ExceptionRecord->ExceptionAddress);
MessageBox(GetDesktopWindow(),message,TEXT("Unhandled Exception"),MB_OK|MB_ICONEXCLAMATION|MB_TASKMODAL);
return EXCEPTION_EXECUTE_HANDLER;
}
// Just create the dump file in the current working directory
TCHAR dump[MAX_PATH] = {TEXT("Dump.dmp")};
HANDLE hFile = CreateFile(dump,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,NULL,NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
FreeLibrary(hLibrary);
TCHAR message[512];
wsprintf(message,TEXT("The program needs to close because of a unhandled 0x%08lX exception which occurred at 0x%08lX.\n")\
TEXT("A file '%s' containing more information about the ")\
TEXT("exception could not be written to the current directory.\nPlease ")\
TEXT("check if you have write permissions in the current directory. We apologize for the inconvenience."),exceptionInfo->ExceptionRecord->ExceptionCode,exceptionInfo->ExceptionRecord->ExceptionAddress,dump);
MessageBox(GetDesktopWindow(),message,TEXT("Unhandled Exception"),MB_OK|MB_ICONEXCLAMATION|MB_TASKMODAL);
return EXCEPTION_EXECUTE_HANDLER;
}
MINIDUMP_EXCEPTION_INFORMATION info = {0};
info.ClientPointers = TRUE;
info.ExceptionPointers = exceptionInfo;
info.ThreadId = GetCurrentThreadId();
miniDumpWriteDump(GetCurrentProcess(),GetCurrentProcessId(),hFile,MiniDumpWithFullMemory,&info,NULL,NULL);
FlushFileBuffers(hFile);
CloseHandle(hFile);
FreeLibrary(hLibrary);
TCHAR message[512];
wsprintf(message,TEXT("The program needs to close because of a unhandled 0x%08lX exception which occurred at 0x%08lX.\n")\
TEXT("A file '%s' containing more information about the ")\
TEXT("exception has been written to the current directory.\nPlease ")\
TEXT("send us this file to help us in finding and repairing ")\
TEXT("the problem. We apologize for the inconvenience."),exceptionInfo->ExceptionRecord->ExceptionCode,exceptionInfo->ExceptionRecord->ExceptionAddress,dump);
MessageBox(GetDesktopWindow(),message,TEXT("Unhandled Exception"),MB_OK|MB_ICONEXCLAMATION|MB_TASKMODAL);
return EXCEPTION_EXECUTE_HANDLER;
}