forked from duilib/duilib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniDumper.cpp
319 lines (270 loc) · 11.2 KB
/
MiniDumper.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
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
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <tchar.h>
#include <dbghelp.h>
#include "miniDumper.h"
#ifdef UNICODE
#define _tcssprintf wsprintf
#define tcsplitpath _wsplitpath
#else
#define _tcssprintf sprintf
#define tcsplitpath _splitpath
#endif
const int USER_DATA_BUFFER_SIZE = 4096;
//-----------------------------------------------------------------------------
// GLOBALS
//-----------------------------------------------------------------------------
CMiniDumper* CMiniDumper::s_pMiniDumper = NULL;
LPCRITICAL_SECTION CMiniDumper::s_pCriticalSection = NULL;
// Based on dbghelp.h
typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess,
DWORD dwPid,
HANDLE hFile,
MINIDUMP_TYPE DumpType,
CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
//-----------------------------------------------------------------------------
// Name: CMiniDumper()
// Desc: Constructor
//-----------------------------------------------------------------------------
CMiniDumper::CMiniDumper( bool bPromptUserForMiniDump )
{
// Our CMiniDumper should act alone as a singleton.
assert( !s_pMiniDumper );
s_pMiniDumper = this;
m_bPromptUserForMiniDump = bPromptUserForMiniDump;
// The SetUnhandledExceptionFilter function enables an application to
// supersede the top-level exception handler of each thread and process.
// After calling this function, if an exception occurs in a process
// that is not being debugged, and the exception makes it to the
// unhandled exception filter, that filter will call the exception
// filter function specified by the lpTopLevelExceptionFilter parameter.
::SetUnhandledExceptionFilter( unhandledExceptionHandler );
// Since DBGHELP.dll is not inherently thread-safe, making calls into it
// from more than one thread simultaneously may yield undefined behavior.
// This means that if your application has multiple threads, or is
// called by multiple threads in a non-synchronized manner, you need to
// make sure that all calls into DBGHELP.dll are isolated via a global
// critical section.
s_pCriticalSection = new CRITICAL_SECTION;
if( s_pCriticalSection )
InitializeCriticalSection( s_pCriticalSection );
}
//-----------------------------------------------------------------------------
// Name: ~CMiniDumper()
// Desc: Destructor
//-----------------------------------------------------------------------------
CMiniDumper::~CMiniDumper( void )
{
if( s_pCriticalSection )
{
DeleteCriticalSection( s_pCriticalSection );
delete s_pCriticalSection;
}
}
//-----------------------------------------------------------------------------
// Name: unhandledExceptionHandler()
// Desc: Call-back filter function for unhandled exceptions
//-----------------------------------------------------------------------------
LONG CMiniDumper::unhandledExceptionHandler( _EXCEPTION_POINTERS *pExceptionInfo )
{
if( !s_pMiniDumper )
return EXCEPTION_CONTINUE_SEARCH;
return s_pMiniDumper->writeMiniDump( pExceptionInfo );
}
//-----------------------------------------------------------------------------
// Name: setMiniDumpFileName()
// Desc:
//-----------------------------------------------------------------------------
void CMiniDumper::setMiniDumpFileName( void )
{
time_t currentTime;
time( ¤tTime );
_tcssprintf( m_szMiniDumpPath,
_T( "%s%s.%ld.dmp" ),
m_szAppPath,
m_szAppBaseName,
currentTime );
}
//-----------------------------------------------------------------------------
// Name: getImpersonationToken()
// Desc: The method acts as a potential workaround for the fact that the
// current thread may not have a token assigned to it, and if not, the
// process token is received.
//-----------------------------------------------------------------------------
bool CMiniDumper::getImpersonationToken( HANDLE* phToken )
{
*phToken = NULL;
if( !OpenThreadToken( GetCurrentThread(),
TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES,
TRUE,
phToken) )
{
if( GetLastError() == ERROR_NO_TOKEN )
{
// No impersonation token for the current thread is available.
// Let's go for the process token instead.
if( !OpenProcessToken( GetCurrentProcess(),
TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES,
phToken) )
return false;
}
else
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// Name: enablePrivilege()
// Desc: Since a MiniDump contains a lot of meta-data about the OS and
// application state at the time of the dump, it is a rather privileged
// operation. This means we need to set the SeDebugPrivilege to be able
// to call MiniDumpWriteDump.
//-----------------------------------------------------------------------------
BOOL CMiniDumper::enablePrivilege( LPCTSTR pszPriv, HANDLE hToken, TOKEN_PRIVILEGES* ptpOld )
{
BOOL bOk = FALSE;
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
bOk = LookupPrivilegeValue( 0, pszPriv, &tp.Privileges[0].Luid );
if( bOk )
{
DWORD cbOld = sizeof(*ptpOld);
bOk = AdjustTokenPrivileges( hToken, FALSE, &tp, cbOld, ptpOld, &cbOld );
}
return (bOk && (ERROR_NOT_ALL_ASSIGNED != GetLastError()));
}
//-----------------------------------------------------------------------------
// Name: restorePrivilege()
// Desc:
//-----------------------------------------------------------------------------
BOOL CMiniDumper::restorePrivilege( HANDLE hToken, TOKEN_PRIVILEGES* ptpOld )
{
BOOL bOk = AdjustTokenPrivileges(hToken, FALSE, ptpOld, 0, NULL, NULL);
return ( bOk && (ERROR_NOT_ALL_ASSIGNED != GetLastError()) );
}
//-----------------------------------------------------------------------------
// Name: writeMiniDump()
// Desc:
//-----------------------------------------------------------------------------
LONG CMiniDumper::writeMiniDump( _EXCEPTION_POINTERS *pExceptionInfo )
{
LONG retval = EXCEPTION_CONTINUE_SEARCH;
m_pExceptionInfo = pExceptionInfo;
HANDLE hImpersonationToken = NULL;
if( !getImpersonationToken( &hImpersonationToken ) )
return FALSE;
// You have to find the right dbghelp.dll.
// Look next to the EXE first since the one in System32 might be old (Win2k)
HMODULE hDll = NULL;
TCHAR szDbgHelpPath[MAX_PATH];
if( GetModuleFileName( NULL, m_szAppPath, _MAX_PATH ) )
{
TCHAR *pSlash = _tcsrchr( m_szAppPath, '\\' );
if( pSlash )
{
_tcscpy_s( m_szAppBaseName, pSlash + 1);
*(pSlash+1) = 0;
}
_tcscpy_s( szDbgHelpPath, m_szAppPath );
_tcscat_s( szDbgHelpPath, _T("DBGHELP.DLL") );
hDll = ::LoadLibrary( szDbgHelpPath );
}
if( hDll == NULL )
{
// If we haven't found it yet - try one more time.
hDll = ::LoadLibrary( _T("DBGHELP.DLL") );
}
LPCTSTR szResult = NULL;
if( hDll )
{
// Get the address of the MiniDumpWriteDump function, which writes
// user-mode mini-dump information to a specified file.
MINIDUMPWRITEDUMP MiniDumpWriteDump =
(MINIDUMPWRITEDUMP)::GetProcAddress( hDll, "MiniDumpWriteDump" );
if( MiniDumpWriteDump != NULL )
{
TCHAR szScratch[USER_DATA_BUFFER_SIZE];
setMiniDumpFileName();
// Ask the user if he or she wants to save a mini-dump file...
_tcssprintf( szScratch,
_T("There was an unexpected error:\n\nWould you ")
_T("like to create a mini-dump file?\n\n%s " ),
m_szMiniDumpPath);
// Create the mini-dump file...
HANDLE hFile = ::CreateFile( m_szMiniDumpPath,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );
if( hFile != INVALID_HANDLE_VALUE )
{
_MINIDUMP_EXCEPTION_INFORMATION ExInfo;
ExInfo.ThreadId = ::GetCurrentThreadId();
ExInfo.ExceptionPointers = pExceptionInfo;
ExInfo.ClientPointers = NULL;
// We need the SeDebugPrivilege to be able to run MiniDumpWriteDump
TOKEN_PRIVILEGES tp;
BOOL bPrivilegeEnabled = enablePrivilege( SE_DEBUG_NAME, hImpersonationToken, &tp );
BOOL bOk;
// DBGHELP.dll is not thread-safe, so we need to restrict access...
EnterCriticalSection( s_pCriticalSection );
{
// Write out the mini-dump data to the file...
bOk = MiniDumpWriteDump( GetCurrentProcess(),
GetCurrentProcessId(),
hFile,
MiniDumpNormal,
&ExInfo,
NULL,
NULL );
}
LeaveCriticalSection( s_pCriticalSection );
// Restore the privileges when done
if( bPrivilegeEnabled )
restorePrivilege( hImpersonationToken, &tp );
if( bOk )
{
szResult = NULL;
retval = EXCEPTION_EXECUTE_HANDLER;
}
else
{
_tcssprintf( szScratch,
_T("Failed to save the mini-dump file to '%s' (error %d)"),
m_szMiniDumpPath,
GetLastError() );
szResult = szScratch;
}
::CloseHandle( hFile );
}
else
{
_tcssprintf( szScratch,
_T("Failed to create the mini-dump file '%s' (error %d)"),
m_szMiniDumpPath,
GetLastError() );
szResult = szScratch;
}
}
else
{
szResult = _T( "Call to GetProcAddress failed to find MiniDumpWriteDump. ")
_T("The DBGHELP.DLL is possibly outdated." );
}
}
else
{
szResult = _T( "Call to LoadLibrary failed to find DBGHELP.DLL." );
}
if( szResult && m_bPromptUserForMiniDump )
::MessageBox( NULL, szResult, NULL, MB_OK );
TerminateProcess( GetCurrentProcess(), 0 );
return retval;
}