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