]> git.saurik.com Git - wxWidgets.git/blob - src/msw/crashrpt.cpp
DC headers don't need to be in gtk/private.h
[wxWidgets.git] / src / msw / crashrpt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/crashrpt.cpp
3 // Purpose: code to generate crash dumps (minidumps)
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 13.07.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_CRASHREPORT
28
29 #ifndef WX_PRECOMP
30 #endif //WX_PRECOMP
31
32 #include "wx/msw/debughlp.h"
33 #include "wx/msw/crashrpt.h"
34
35 // ----------------------------------------------------------------------------
36 // classes
37 // ----------------------------------------------------------------------------
38
39 // low level wxBusyCursor replacement: we use Win32 API directly here instead
40 // of going through wxWidgets calls as this could be dangerous
41 class BusyCursor
42 {
43 public:
44 BusyCursor()
45 {
46 HCURSOR hcursorBusy = ::LoadCursor(NULL, IDC_WAIT);
47 m_hcursorOld = ::SetCursor(hcursorBusy);
48 }
49
50 ~BusyCursor()
51 {
52 if ( m_hcursorOld )
53 {
54 ::SetCursor(m_hcursorOld);
55 }
56 }
57
58 private:
59 HCURSOR m_hcursorOld;
60 };
61
62 // the real crash report generator
63 class wxCrashReportImpl
64 {
65 public:
66 wxCrashReportImpl(const wxChar *filename);
67
68 bool Generate(int flags, EXCEPTION_POINTERS *ep);
69
70 ~wxCrashReportImpl()
71 {
72 if ( m_hFile != INVALID_HANDLE_VALUE )
73 {
74 ::CloseHandle(m_hFile);
75 }
76 }
77
78 private:
79
80 // formatted output to m_hFile
81 void Output(const wxChar *format, ...);
82
83 // output end of line
84 void OutputEndl() { Output(_T("\r\n")); }
85
86 // the handle of the report file
87 HANDLE m_hFile;
88 };
89
90 // ----------------------------------------------------------------------------
91 // globals
92 // ----------------------------------------------------------------------------
93
94 // the file name where the report about exception is written
95 //
96 // we use fixed buffer to avoid (big) dynamic allocations when the program
97 // crashes
98 static wxChar gs_reportFilename[MAX_PATH];
99
100 // this is defined in msw/main.cpp
101 extern EXCEPTION_POINTERS *wxGlobalSEInformation;
102
103 // ============================================================================
104 // implementation
105 // ============================================================================
106
107 // ----------------------------------------------------------------------------
108 // wxCrashReportImpl
109 // ----------------------------------------------------------------------------
110
111 wxCrashReportImpl::wxCrashReportImpl(const wxChar *filename)
112 {
113 m_hFile = ::CreateFile
114 (
115 filename,
116 GENERIC_WRITE,
117 0, // no sharing
118 NULL, // default security
119 CREATE_ALWAYS,
120 FILE_FLAG_WRITE_THROUGH,
121 NULL // no template file
122 );
123 }
124
125 void wxCrashReportImpl::Output(const wxChar *format, ...)
126 {
127 va_list argptr;
128 va_start(argptr, format);
129
130 DWORD cbWritten;
131
132 wxString s = wxString::FormatV(format, argptr);
133
134 wxCharBuffer buf(s.mb_str(wxConvUTF8));
135 ::WriteFile(m_hFile, buf.data(), strlen(buf.data()), &cbWritten, 0);
136
137 va_end(argptr);
138 }
139
140 bool wxCrashReportImpl::Generate(int flags, EXCEPTION_POINTERS *ep)
141 {
142 if ( m_hFile == INVALID_HANDLE_VALUE )
143 return false;
144
145 #if wxUSE_DBGHELP
146 if ( !ep )
147 ep = wxGlobalSEInformation;
148
149 if ( !ep )
150 {
151 Output(_T("Context for crash report generation not available."));
152 return false;
153 }
154
155 // show to the user that we're doing something...
156 BusyCursor busyCursor;
157
158 // user-specified crash report flags override those specified by the
159 // programmer
160 TCHAR envFlags[64];
161 DWORD dwLen = ::GetEnvironmentVariable
162 (
163 _T("WX_CRASH_FLAGS"),
164 envFlags,
165 WXSIZEOF(envFlags)
166 );
167
168 int flagsEnv;
169 if ( dwLen && dwLen < WXSIZEOF(envFlags) &&
170 wxSscanf(envFlags, _T("%d"), &flagsEnv) == 1 )
171 {
172 flags = flagsEnv;
173 }
174
175 if ( wxDbgHelpDLL::Init() )
176 {
177 MINIDUMP_EXCEPTION_INFORMATION minidumpExcInfo;
178
179 minidumpExcInfo.ThreadId = ::GetCurrentThreadId();
180 minidumpExcInfo.ExceptionPointers = ep;
181 minidumpExcInfo.ClientPointers = FALSE; // in our own address space
182
183 // do generate the dump
184 MINIDUMP_TYPE dumpFlags;
185 if ( flags & wxCRASH_REPORT_LOCALS )
186 {
187 // the only way to get local variables is to dump the entire
188 // process memory space -- but this makes for huge (dozens or
189 // even hundreds of Mb) files
190 dumpFlags = MiniDumpWithFullMemory;
191 }
192 else if ( flags & wxCRASH_REPORT_GLOBALS )
193 {
194 // MiniDumpWriteDump() has the option for dumping just the data
195 // segment which contains all globals -- exactly what we need
196 dumpFlags = MiniDumpWithDataSegs;
197 }
198 else // minimal dump
199 {
200 // the file size is not much bigger than when using MiniDumpNormal
201 // if we use the flags below, but the minidump is much more useful
202 // as it contains the values of many (but not all) local variables
203 dumpFlags = (MINIDUMP_TYPE)(MiniDumpScanMemory
204 #if _MSC_VER > 1300
205 |MiniDumpWithIndirectlyReferencedMemory
206 #endif
207 );
208 }
209
210 if ( !wxDbgHelpDLL::MiniDumpWriteDump
211 (
212 ::GetCurrentProcess(),
213 ::GetCurrentProcessId(),
214 m_hFile, // file to write to
215 dumpFlags, // kind of dump to craete
216 &minidumpExcInfo,
217 NULL, // no extra user-defined data
218 NULL // no callbacks
219 ) )
220 {
221 Output(_T("MiniDumpWriteDump() failed."));
222
223 return false;
224 }
225
226 return true;
227 }
228 else // dbghelp.dll couldn't be loaded
229 {
230 Output(wxDbgHelpDLL::GetErrorMessage());
231 }
232 #else // !wxUSE_DBGHELP
233 wxUnusedVar(flags);
234 wxUnusedVar(ep);
235
236 Output(_T("Support for crash report generation was not included ")
237 _T("in this wxWidgets version."));
238 #endif // wxUSE_DBGHELP/!wxUSE_DBGHELP
239
240 return false;
241 }
242
243 // ----------------------------------------------------------------------------
244 // wxCrashReport
245 // ----------------------------------------------------------------------------
246
247 /* static */
248 void wxCrashReport::SetFileName(const wxChar *filename)
249 {
250 wxStrncpy(gs_reportFilename, filename, WXSIZEOF(gs_reportFilename) - 1);
251 gs_reportFilename[WXSIZEOF(gs_reportFilename) - 1] = _T('\0');
252 }
253
254 /* static */
255 const wxChar *wxCrashReport::GetFileName()
256 {
257 return gs_reportFilename;
258 }
259
260 /* static */
261 bool wxCrashReport::Generate(int flags, EXCEPTION_POINTERS *ep)
262 {
263 wxCrashReportImpl impl(gs_reportFilename);
264
265 return impl.Generate(flags, ep);
266 }
267
268 /* static */
269 bool wxCrashReport::GenerateNow(int flags)
270 {
271 bool rc = false;
272
273 __try
274 {
275 RaiseException(0x1976, 0, 0, NULL);
276 }
277 __except( rc = Generate(flags, (EXCEPTION_POINTERS *)GetExceptionInformation()),
278 EXCEPTION_CONTINUE_EXECUTION )
279 {
280 // never executed because of EXCEPTION_CONTINUE_EXECUTION above
281 }
282
283 return rc;
284 }
285
286 // ----------------------------------------------------------------------------
287 // wxCrashContext
288 // ----------------------------------------------------------------------------
289
290 wxCrashContext::wxCrashContext(_EXCEPTION_POINTERS *ep)
291 {
292 wxZeroMemory(*this);
293
294 if ( !ep )
295 {
296 wxCHECK_RET( wxGlobalSEInformation, _T("no exception info available") );
297 ep = wxGlobalSEInformation;
298 }
299
300 // TODO: we could also get the operation (read/write) and address for which
301 // it failed for EXCEPTION_ACCESS_VIOLATION code
302 const EXCEPTION_RECORD& rec = *ep->ExceptionRecord;
303 code = rec.ExceptionCode;
304 addr = rec.ExceptionAddress;
305
306 #ifdef __INTEL__
307 const CONTEXT& ctx = *ep->ContextRecord;
308 regs.eax = ctx.Eax;
309 regs.ebx = ctx.Ebx;
310 regs.ecx = ctx.Ecx;
311 regs.edx = ctx.Edx;
312 regs.esi = ctx.Esi;
313 regs.edi = ctx.Edi;
314
315 regs.ebp = ctx.Ebp;
316 regs.esp = ctx.Esp;
317 regs.eip = ctx.Eip;
318
319 regs.cs = ctx.SegCs;
320 regs.ds = ctx.SegDs;
321 regs.es = ctx.SegEs;
322 regs.fs = ctx.SegFs;
323 regs.gs = ctx.SegGs;
324 regs.ss = ctx.SegSs;
325
326 regs.flags = ctx.EFlags;
327 #endif // __INTEL__
328 }
329
330 wxString wxCrashContext::GetExceptionString() const
331 {
332 wxString s;
333
334 #define CASE_EXCEPTION( x ) case EXCEPTION_##x: s = _T(#x); break
335
336 switch ( code )
337 {
338 CASE_EXCEPTION(ACCESS_VIOLATION);
339 CASE_EXCEPTION(DATATYPE_MISALIGNMENT);
340 CASE_EXCEPTION(BREAKPOINT);
341 CASE_EXCEPTION(SINGLE_STEP);
342 CASE_EXCEPTION(ARRAY_BOUNDS_EXCEEDED);
343 CASE_EXCEPTION(FLT_DENORMAL_OPERAND);
344 CASE_EXCEPTION(FLT_DIVIDE_BY_ZERO);
345 CASE_EXCEPTION(FLT_INEXACT_RESULT);
346 CASE_EXCEPTION(FLT_INVALID_OPERATION);
347 CASE_EXCEPTION(FLT_OVERFLOW);
348 CASE_EXCEPTION(FLT_STACK_CHECK);
349 CASE_EXCEPTION(FLT_UNDERFLOW);
350 CASE_EXCEPTION(INT_DIVIDE_BY_ZERO);
351 CASE_EXCEPTION(INT_OVERFLOW);
352 CASE_EXCEPTION(PRIV_INSTRUCTION);
353 CASE_EXCEPTION(IN_PAGE_ERROR);
354 CASE_EXCEPTION(ILLEGAL_INSTRUCTION);
355 CASE_EXCEPTION(NONCONTINUABLE_EXCEPTION);
356 CASE_EXCEPTION(STACK_OVERFLOW);
357 CASE_EXCEPTION(INVALID_DISPOSITION);
358 CASE_EXCEPTION(GUARD_PAGE);
359 CASE_EXCEPTION(INVALID_HANDLE);
360
361 default:
362 // unknown exception, ask NTDLL for the name
363 if ( !::FormatMessage
364 (
365 FORMAT_MESSAGE_IGNORE_INSERTS |
366 FORMAT_MESSAGE_FROM_HMODULE,
367 ::GetModuleHandle(_T("NTDLL.DLL")),
368 code,
369 0,
370 wxStringBuffer(s, 1024),
371 1024,
372 0
373 ) )
374 {
375 s.Printf(_T("UNKNOWN_EXCEPTION(%d)"), code);
376 }
377 }
378
379 #undef CASE_EXCEPTION
380
381 return s;
382 }
383
384 #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
385