2 * Win32 exception functions
4 * Copyright (c) 1996 Onno Hovers, (onno@stack.urc.tue.nl)
5 * Copyright (c) 1999 Alexandre Julliard
8 * What really happens behind the scenes of those new
9 * __try{...}__except(..){....} and
10 * __try{...}__finally{...}
11 * statements is simply not documented by Microsoft. There could be different
13 * One reason could be that they try to hide the fact that exception
14 * handling in Win32 looks almost the same as in OS/2 2.x.
15 * Another reason could be that Microsoft does not want others to write
16 * binary compatible implementations of the Win32 API (like us).
18 * Whatever the reason, THIS SUCKS!! Ensuring portability or future
19 * compatibility may be valid reasons to keep some things undocumented.
20 * But exception handling is so basic to Win32 that it should be
31 #include "wine/exception.h"
33 #include "stackframe.h"
34 #include "wine/server.h"
35 #include "debugtools.h"
37 DEFAULT_DEBUG_CHANNEL(seh
);
39 static PTOP_LEVEL_EXCEPTION_FILTER top_filter
;
41 typedef INT (WINAPI
*MessageBoxA_funcptr
)(HWND
,LPCSTR
,LPCSTR
,UINT
);
42 typedef INT (WINAPI
*MessageBoxW_funcptr
)(HWND
,LPCWSTR
,LPCWSTR
,UINT
);
44 /*******************************************************************
45 * RaiseException (KERNEL32.@)
47 void WINAPI
RaiseException( DWORD code
, DWORD flags
, DWORD nbargs
, const LPDWORD args
)
49 EXCEPTION_RECORD record
;
51 /* Compose an exception record */
53 record
.ExceptionCode
= code
;
54 record
.ExceptionFlags
= flags
& EH_NONCONTINUABLE
;
55 record
.ExceptionRecord
= NULL
;
56 record
.ExceptionAddress
= RaiseException
;
59 if (nbargs
> EXCEPTION_MAXIMUM_PARAMETERS
) nbargs
= EXCEPTION_MAXIMUM_PARAMETERS
;
60 record
.NumberParameters
= nbargs
;
61 memcpy( record
.ExceptionInformation
, args
, nbargs
* sizeof(*args
) );
63 else record
.NumberParameters
= 0;
65 RtlRaiseException( &record
);
69 /*******************************************************************
70 * format_exception_msg
72 static int format_exception_msg( const EXCEPTION_POINTERS
*ptr
, char *buffer
, int size
)
74 const EXCEPTION_RECORD
*rec
= ptr
->ExceptionRecord
;
77 switch(rec
->ExceptionCode
)
79 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
80 len
= snprintf( buffer
, size
, "Unhandled division by zero" );
82 case EXCEPTION_INT_OVERFLOW
:
83 len
= snprintf( buffer
, size
, "Unhandled overflow" );
85 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
86 len
= snprintf( buffer
, size
, "Unhandled array bounds" );
88 case EXCEPTION_ILLEGAL_INSTRUCTION
:
89 len
= snprintf( buffer
, size
, "Unhandled illegal instruction" );
91 case EXCEPTION_STACK_OVERFLOW
:
92 len
= snprintf( buffer
, size
, "Unhandled stack overflow" );
94 case EXCEPTION_PRIV_INSTRUCTION
:
95 len
= snprintf( buffer
, size
, "Unhandled priviledged instruction" );
97 case EXCEPTION_ACCESS_VIOLATION
:
98 if (rec
->NumberParameters
== 2)
99 len
= snprintf( buffer
, size
, "Unhandled page fault on %s access to 0x%08lx",
100 rec
->ExceptionInformation
[0] ? "write" : "read",
101 rec
->ExceptionInformation
[1]);
103 len
= snprintf( buffer
, size
, "Unhandled page fault");
105 case EXCEPTION_DATATYPE_MISALIGNMENT
:
106 len
= snprintf( buffer
, size
, "Unhandled alignment" );
109 len
= snprintf( buffer
, size
, "Unhandled ^C");
111 case EXCEPTION_CRITICAL_SECTION_WAIT
:
112 len
= snprintf( buffer
, size
, "Critical section %08lx wait failed",
113 rec
->ExceptionInformation
[0]);
115 case EXCEPTION_WINE_STUB
:
116 len
= snprintf( buffer
, size
, "Unimplemented function %s.%s called",
117 (char *)rec
->ExceptionInformation
[0], (char *)rec
->ExceptionInformation
[1] );
119 case EXCEPTION_VM86_INTx
:
120 len
= snprintf( buffer
, size
, "Unhandled interrupt %02lx in vm86 mode",
121 rec
->ExceptionInformation
[0]);
123 case EXCEPTION_VM86_STI
:
124 len
= snprintf( buffer
, size
, "Unhandled sti in vm86 mode");
126 case EXCEPTION_VM86_PICRETURN
:
127 len
= snprintf( buffer
, size
, "Unhandled PIC return in vm86 mode");
130 len
= snprintf( buffer
, size
, "Unhandled exception 0x%08lx", rec
->ExceptionCode
);
133 if ((len
<0) || (len
>=size
))
136 if (ptr
->ContextRecord
->SegCs
!= __get_cs())
137 len2
= snprintf(buffer
+len
, size
-len
,
138 " at address 0x%04lx:0x%08lx.\nDo you wish to debug it ?",
139 ptr
->ContextRecord
->SegCs
,
140 (DWORD
)ptr
->ExceptionRecord
->ExceptionAddress
);
143 len2
= snprintf(buffer
+len
, size
-len
,
144 " at address 0x%08lx.\nDo you wish to debug it ?",
145 (DWORD
)ptr
->ExceptionRecord
->ExceptionAddress
);
146 if ((len2
<0) || (len
>=size
-len
))
152 /**********************************************************************
155 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
157 static int send_debug_event( EXCEPTION_RECORD
*rec
, int first_chance
, CONTEXT
*context
)
162 SERVER_START_VAR_REQ( queue_exception_event
, sizeof(*rec
) + sizeof(*context
) )
164 CONTEXT
*context_ptr
= server_data_ptr(req
);
165 EXCEPTION_RECORD
*rec_ptr
= (EXCEPTION_RECORD
*)(context_ptr
+ 1);
166 req
->first
= first_chance
;
168 *context_ptr
= *context
;
169 if (!SERVER_CALL()) handle
= req
->handle
;
172 if (!handle
) return 0; /* no debugger present or other error */
174 /* No need to wait on the handle since the process gets suspended
175 * once the event is passed to the debugger, so when we get back
176 * here the event has been continued already.
178 SERVER_START_VAR_REQ( get_exception_status
, sizeof(*context
) )
180 req
->handle
= handle
;
181 if (!SERVER_CALL()) *context
= *(CONTEXT
*)server_data_ptr(req
);
189 /******************************************************************
192 * Does the effective debugger startup according to 'format'
194 static BOOL
start_debugger(PEXCEPTION_POINTERS epointers
, HANDLE hEvent
)
198 PROCESS_INFORMATION info
;
199 STARTUPINFOA startup
;
203 if (!RegOpenKeyA(HKEY_LOCAL_MACHINE
,
204 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug", &hDbgConf
)) {
208 count
= sizeof(format
);
209 if (RegQueryValueExA(hDbgConf
, "Debugger", 0, &type
, format
, &count
))
212 count
= sizeof(bAuto
);
213 if (RegQueryValueExA(hDbgConf
, "Auto", 0, &type
, (char*)&bAuto
, &count
))
215 else if (type
== REG_SZ
)
218 count
= sizeof(autostr
);
219 if (!RegQueryValueExA(hDbgConf
, "Auto", 0, &type
, autostr
, &count
))
220 bAuto
= atoi(autostr
);
222 RegCloseKey(hDbgConf
);
224 /* try a default setup... */
225 strcpy( format
, "debugger/winedbg %ld %ld" );
230 HMODULE mod
= GetModuleHandleA( "user32.dll" );
231 MessageBoxA_funcptr pMessageBoxA
= NULL
;
233 if (mod
) pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
236 format_exception_msg( epointers
, buffer
, sizeof(buffer
) );
237 if (pMessageBoxA( 0, buffer
, "Exception raised", MB_YESNO
| MB_ICONHAND
) == IDNO
)
239 TRACE("Killing process\n");
245 TRACE("Starting debugger (fmt=%s)\n", format
);
246 sprintf(buffer
, format
, GetCurrentProcessId(), hEvent
);
247 memset(&startup
, 0, sizeof(startup
));
248 startup
.cb
= sizeof(startup
);
249 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
250 startup
.wShowWindow
= SW_SHOWNORMAL
;
251 if (CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, 0, NULL
, NULL
, &startup
, &info
)) {
252 /* wait for debugger to come up... */
253 WaitForSingleObject(hEvent
, INFINITE
);
256 ERR("Couldn't start debugger (%s) (%ld)\n"
257 "Read the Wine Developers Guide on how to set up winedbg or another debugger\n",
258 buffer
, GetLastError());
262 /******************************************************************
263 * start_debugger_atomic
265 * starts the debugger is an atomic way:
266 * - either the debugger is not started and it is started
267 * - either the debugger has already been started by an other thread
268 * - either the debugger couldn't be started
270 * returns TRUE for the two first condition, FALSE for the last
272 static int start_debugger_atomic(PEXCEPTION_POINTERS epointers
)
274 static HANDLE hRunOnce
/* = 0 */;
278 OBJECT_ATTRIBUTES attr
;
281 attr
.Length
= sizeof(attr
);
282 attr
.RootDirectory
= 0;
283 attr
.Attributes
= OBJ_INHERIT
;
284 attr
.ObjectName
= NULL
;
285 attr
.SecurityDescriptor
= NULL
;
286 attr
.SecurityQualityOfService
= NULL
;
288 /* ask for manual reset, so that once the debugger is started, every thread will be
291 NtCreateEvent( &hEvent
, EVENT_ALL_ACCESS
, &attr
, TRUE
, FALSE
);
292 if (InterlockedCompareExchange( (LPLONG
)&hRunOnce
, hEvent
, 0 ) == 0)
294 /* ok, our event has been set... we're the winning thread */
295 BOOL ret
= start_debugger( epointers
, hRunOnce
);
300 /* so that the other threads won't be stuck */
301 NtSetEvent( hRunOnce
, &tmp
);
306 /* someone beat us here... */
307 CloseHandle( hEvent
);
310 /* and wait for the winner to have actually created the debugger */
311 WaitForSingleObject( hRunOnce
, INFINITE
);
312 /* in fact, here, we only know that someone has tried to start the debugger, we'll know
313 * by reposting the exception if it has actually attached to the current process
319 /*******************************************************************
320 * UnhandledExceptionFilter (KERNEL32.@)
322 DWORD WINAPI
UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers
)
327 for (loop
= 0; loop
<= 1; loop
++)
329 /* send a last chance event to the debugger */
330 status
= send_debug_event( epointers
->ExceptionRecord
, FALSE
, epointers
->ContextRecord
);
334 return EXCEPTION_CONTINUE_EXECUTION
;
335 case DBG_EXCEPTION_NOT_HANDLED
:
336 TerminateProcess( GetCurrentProcess(), epointers
->ExceptionRecord
->ExceptionCode
);
337 break; /* not reached */
338 case 0: /* no debugger is present */
339 if (epointers
->ExceptionRecord
->ExceptionCode
== CONTROL_C_EXIT
)
341 /* do not launch the debugger on ^C, simply terminate the process */
342 TerminateProcess( GetCurrentProcess(), 1 );
344 /* second try, the debugger isn't present... */
345 if (loop
== 1) return EXCEPTION_EXECUTE_HANDLER
;
348 FIXME("Unsupported yet debug continue value %d (please report)\n", status
);
349 return EXCEPTION_EXECUTE_HANDLER
;
352 /* should only be there when loop == 0 */
356 DWORD ret
= top_filter( epointers
);
357 if (ret
!= EXCEPTION_CONTINUE_SEARCH
) return ret
;
360 /* FIXME: Should check the current error mode */
362 if (!start_debugger_atomic( epointers
))
363 return EXCEPTION_EXECUTE_HANDLER
;
364 /* now that we should have a debugger attached, try to resend event */
367 return EXCEPTION_EXECUTE_HANDLER
;
371 /***********************************************************************
372 * SetUnhandledExceptionFilter (KERNEL32.@)
374 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI
SetUnhandledExceptionFilter(
375 LPTOP_LEVEL_EXCEPTION_FILTER filter
)
377 LPTOP_LEVEL_EXCEPTION_FILTER old
= top_filter
;
383 /**************************************************************************
384 * FatalAppExitA (KERNEL32.@)
386 void WINAPI
FatalAppExitA( UINT action
, LPCSTR str
)
388 HMODULE mod
= GetModuleHandleA( "user32.dll" );
389 MessageBoxA_funcptr pMessageBoxA
= NULL
;
393 if (mod
) pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
394 if (pMessageBoxA
) pMessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
395 else ERR( "%s\n", debugstr_a(str
) );
400 /**************************************************************************
401 * FatalAppExitW (KERNEL32.@)
403 void WINAPI
FatalAppExitW( UINT action
, LPCWSTR str
)
405 HMODULE mod
= GetModuleHandleA( "user32.dll" );
406 MessageBoxW_funcptr pMessageBoxW
= NULL
;
410 if (mod
) pMessageBoxW
= (MessageBoxW_funcptr
)GetProcAddress( mod
, "MessageBoxW" );
411 if (pMessageBoxW
) pMessageBoxW( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
412 else ERR( "%s\n", debugstr_w(str
) );