1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/crashrpt.cpp
3 // Purpose: code to generate crash dumps (minidumps)
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/msw/debughlp.h"
33 #include "wx/msw/crashrpt.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // low level wxBusyCursor replacement: we use Win32 API directly here instead
40 // of going through wxWidgets calls as this could be dangerous
46 HCURSOR hcursorBusy
= ::LoadCursor(NULL
, IDC_WAIT
);
47 m_hcursorOld
= ::SetCursor(hcursorBusy
);
54 ::SetCursor(m_hcursorOld
);
62 // the real crash report generator
63 class wxCrashReportImpl
66 wxCrashReportImpl(const wxChar
*filename
);
68 bool Generate(int flags
, EXCEPTION_POINTERS
*ep
);
72 if ( m_hFile
!= INVALID_HANDLE_VALUE
)
74 ::CloseHandle(m_hFile
);
80 // formatted output to m_hFile
81 void Output(const wxChar
*format
, ...);
84 void OutputEndl() { Output(wxT("\r\n")); }
86 // the handle of the report file
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 // the file name where the report about exception is written
96 // we use fixed buffer to avoid (big) dynamic allocations when the program
98 static wxChar gs_reportFilename
[MAX_PATH
];
100 // this is defined in msw/main.cpp
101 extern EXCEPTION_POINTERS
*wxGlobalSEInformation
;
103 // ============================================================================
105 // ============================================================================
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 wxCrashReportImpl::wxCrashReportImpl(const wxChar
*filename
)
113 m_hFile
= ::CreateFile
118 NULL
, // default security
120 FILE_FLAG_WRITE_THROUGH
,
121 NULL
// no template file
125 void wxCrashReportImpl::Output(const wxChar
*format
, ...)
128 va_start(argptr
, format
);
132 wxString s
= wxString::FormatV(format
, argptr
);
134 wxCharBuffer
buf(s
.mb_str(wxConvUTF8
));
135 ::WriteFile(m_hFile
, buf
.data(), strlen(buf
.data()), &cbWritten
, 0);
140 bool wxCrashReportImpl::Generate(int flags
, EXCEPTION_POINTERS
*ep
)
142 if ( m_hFile
== INVALID_HANDLE_VALUE
)
147 ep
= wxGlobalSEInformation
;
151 Output(wxT("Context for crash report generation not available."));
155 // show to the user that we're doing something...
156 BusyCursor busyCursor
;
158 // user-specified crash report flags override those specified by the
161 DWORD dwLen
= ::GetEnvironmentVariable
163 wxT("WX_CRASH_FLAGS"),
169 if ( dwLen
&& dwLen
< WXSIZEOF(envFlags
) &&
170 wxSscanf(envFlags
, wxT("%d"), &flagsEnv
) == 1 )
175 if ( wxDbgHelpDLL::Init() )
177 MINIDUMP_EXCEPTION_INFORMATION minidumpExcInfo
;
179 minidumpExcInfo
.ThreadId
= ::GetCurrentThreadId();
180 minidumpExcInfo
.ExceptionPointers
= ep
;
181 minidumpExcInfo
.ClientPointers
= FALSE
; // in our own address space
183 // do generate the dump
184 MINIDUMP_TYPE dumpFlags
;
185 if ( flags
& wxCRASH_REPORT_LOCALS
)
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
;
192 else if ( flags
& wxCRASH_REPORT_GLOBALS
)
194 // MiniDumpWriteDump() has the option for dumping just the data
195 // segment which contains all globals -- exactly what we need
196 dumpFlags
= MiniDumpWithDataSegs
;
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
205 |MiniDumpWithIndirectlyReferencedMemory
210 if ( !wxDbgHelpDLL::MiniDumpWriteDump
212 ::GetCurrentProcess(),
213 ::GetCurrentProcessId(),
214 m_hFile
, // file to write to
215 dumpFlags
, // kind of dump to craete
217 NULL
, // no extra user-defined data
221 Output(wxT("MiniDumpWriteDump() failed."));
228 else // dbghelp.dll couldn't be loaded
230 Output(wxT("%s"), wxDbgHelpDLL::GetErrorMessage().c_str());
232 #else // !wxUSE_DBGHELP
236 Output(wxT("Support for crash report generation was not included ")
237 wxT("in this wxWidgets version."));
238 #endif // wxUSE_DBGHELP/!wxUSE_DBGHELP
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
248 void wxCrashReport::SetFileName(const wxString
& filename
)
250 wxStrlcpy(gs_reportFilename
, filename
.wx_str(), WXSIZEOF(gs_reportFilename
));
254 wxString
wxCrashReport::GetFileName()
256 return gs_reportFilename
;
260 bool wxCrashReport::Generate(int flags
, EXCEPTION_POINTERS
*ep
)
262 wxCrashReportImpl
impl(gs_reportFilename
);
264 return impl
.Generate(flags
, ep
);
268 bool wxCrashReport::GenerateNow(int flags
)
274 RaiseException(0x1976, 0, 0, NULL
);
276 __except( rc
= Generate(flags
, (EXCEPTION_POINTERS
*)GetExceptionInformation()),
277 EXCEPTION_CONTINUE_EXECUTION
)
279 // never executed because of EXCEPTION_CONTINUE_EXECUTION above
285 // ----------------------------------------------------------------------------
287 // ----------------------------------------------------------------------------
289 wxCrashContext::wxCrashContext(_EXCEPTION_POINTERS
*ep
)
295 wxCHECK_RET( wxGlobalSEInformation
, wxT("no exception info available") );
296 ep
= wxGlobalSEInformation
;
299 // TODO: we could also get the operation (read/write) and address for which
300 // it failed for EXCEPTION_ACCESS_VIOLATION code
301 const EXCEPTION_RECORD
& rec
= *ep
->ExceptionRecord
;
302 code
= rec
.ExceptionCode
;
303 addr
= rec
.ExceptionAddress
;
306 const CONTEXT
& ctx
= *ep
->ContextRecord
;
325 regs
.flags
= ctx
.EFlags
;
329 wxString
wxCrashContext::GetExceptionString() const
333 #define CASE_EXCEPTION( x ) case EXCEPTION_##x: s = wxT(#x); break
337 CASE_EXCEPTION(ACCESS_VIOLATION
);
338 CASE_EXCEPTION(DATATYPE_MISALIGNMENT
);
339 CASE_EXCEPTION(BREAKPOINT
);
340 CASE_EXCEPTION(SINGLE_STEP
);
341 CASE_EXCEPTION(ARRAY_BOUNDS_EXCEEDED
);
342 CASE_EXCEPTION(FLT_DENORMAL_OPERAND
);
343 CASE_EXCEPTION(FLT_DIVIDE_BY_ZERO
);
344 CASE_EXCEPTION(FLT_INEXACT_RESULT
);
345 CASE_EXCEPTION(FLT_INVALID_OPERATION
);
346 CASE_EXCEPTION(FLT_OVERFLOW
);
347 CASE_EXCEPTION(FLT_STACK_CHECK
);
348 CASE_EXCEPTION(FLT_UNDERFLOW
);
349 CASE_EXCEPTION(INT_DIVIDE_BY_ZERO
);
350 CASE_EXCEPTION(INT_OVERFLOW
);
351 CASE_EXCEPTION(PRIV_INSTRUCTION
);
352 CASE_EXCEPTION(IN_PAGE_ERROR
);
353 CASE_EXCEPTION(ILLEGAL_INSTRUCTION
);
354 CASE_EXCEPTION(NONCONTINUABLE_EXCEPTION
);
355 CASE_EXCEPTION(STACK_OVERFLOW
);
356 CASE_EXCEPTION(INVALID_DISPOSITION
);
357 CASE_EXCEPTION(GUARD_PAGE
);
358 CASE_EXCEPTION(INVALID_HANDLE
);
361 // unknown exception, ask NTDLL for the name
362 if ( !::FormatMessage
364 FORMAT_MESSAGE_IGNORE_INSERTS
|
365 FORMAT_MESSAGE_FROM_HMODULE
,
366 ::GetModuleHandle(wxT("NTDLL.DLL")),
369 wxStringBuffer(s
, 1024),
374 s
.Printf(wxT("UNKNOWN_EXCEPTION(%d)"), code
);
378 #undef CASE_EXCEPTION
383 #endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT