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"
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 void format_exception_msg( const EXCEPTION_POINTERS
*ptr
, char *buffer
)
74 const EXCEPTION_RECORD
*rec
= ptr
->ExceptionRecord
;
76 switch(rec
->ExceptionCode
)
78 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
79 sprintf( buffer
, "Unhandled division by zero" );
81 case EXCEPTION_INT_OVERFLOW
:
82 sprintf( buffer
, "Unhandled overflow" );
84 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
85 sprintf( buffer
, "Unhandled array bounds" );
87 case EXCEPTION_ILLEGAL_INSTRUCTION
:
88 sprintf( buffer
, "Unhandled illegal instruction" );
90 case EXCEPTION_STACK_OVERFLOW
:
91 sprintf( buffer
, "Unhandled stack overflow" );
93 case EXCEPTION_PRIV_INSTRUCTION
:
94 sprintf( buffer
, "Unhandled priviledged instruction" );
96 case EXCEPTION_ACCESS_VIOLATION
:
97 if (rec
->NumberParameters
== 2)
98 sprintf( buffer
, "Unhandled page fault on %s access to 0x%08lx",
99 rec
->ExceptionInformation
[0] ? "write" : "read",
100 rec
->ExceptionInformation
[1]);
102 sprintf( buffer
, "Unhandled page fault");
104 case EXCEPTION_DATATYPE_MISALIGNMENT
:
105 sprintf( buffer
, "Unhandled alignment" );
108 sprintf( buffer
, "Unhandled ^C");
110 case EXCEPTION_CRITICAL_SECTION_WAIT
:
111 sprintf( buffer
, "Critical section %08lx wait failed",
112 rec
->ExceptionInformation
[0]);
114 case EXCEPTION_WINE_STUB
:
115 sprintf( buffer
, "Unimplemented function %s.%s called",
116 (char *)rec
->ExceptionInformation
[0], (char *)rec
->ExceptionInformation
[1] );
118 case EXCEPTION_VM86_INTx
:
119 sprintf( buffer
, "Unhandled interrupt %02lx in vm86 mode",
120 rec
->ExceptionInformation
[0]);
122 case EXCEPTION_VM86_STI
:
123 sprintf( buffer
, "Unhandled sti in vm86 mode");
125 case EXCEPTION_VM86_PICRETURN
:
126 sprintf( buffer
, "Unhandled PIC return in vm86 mode");
129 sprintf( buffer
, "Unhandled exception 0x%08lx", rec
->ExceptionCode
);
133 if (ptr
->ContextRecord
->SegCs
!= __get_cs())
134 sprintf( buffer
+strlen(buffer
), " at address 0x%04lx:0x%08lx.\n",
135 ptr
->ContextRecord
->SegCs
, (DWORD
)ptr
->ExceptionRecord
->ExceptionAddress
);
138 sprintf( buffer
+strlen(buffer
), " at address 0x%08lx.\n",
139 (DWORD
)ptr
->ExceptionRecord
->ExceptionAddress
);
140 strcat( buffer
, "Do you wish to debug it ?" );
144 /**********************************************************************
147 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
149 static int send_debug_event( EXCEPTION_RECORD
*rec
, int first_chance
, CONTEXT
*context
)
156 struct queue_exception_event_request
*req
= server_alloc_req( sizeof(*req
),
157 sizeof(*rec
)+sizeof(*context
) );
158 CONTEXT
*context_ptr
= server_data_ptr(req
);
159 EXCEPTION_RECORD
*rec_ptr
= (EXCEPTION_RECORD
*)(context_ptr
+ 1);
160 req
->first
= first_chance
;
162 *context_ptr
= *context
;
163 if (!server_call_noerr( REQ_QUEUE_EXCEPTION_EVENT
)) handle
= req
->handle
;
166 if (!handle
) return 0; /* no debugger present or other error */
168 /* No need to wait on the handle since the process gets suspended
169 * once the event is passed to the debugger, so when we get back
170 * here the event has been continued already.
174 struct get_exception_status_request
*req
= server_alloc_req( sizeof(*req
), sizeof(*context
) );
175 req
->handle
= handle
;
176 if (!server_call_noerr( REQ_GET_EXCEPTION_STATUS
))
177 *context
= *(CONTEXT
*)server_data_ptr(req
);
186 /*******************************************************************
187 * UnhandledExceptionFilter (KERNEL32.@)
189 DWORD WINAPI
UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers
)
195 DWORD ret
= EXCEPTION_EXECUTE_HANDLER
;
198 /* send a last chance event to the debugger */
199 status
= send_debug_event( epointers
->ExceptionRecord
, FALSE
, epointers
->ContextRecord
);
203 return EXCEPTION_CONTINUE_EXECUTION
;
204 case DBG_EXCEPTION_NOT_HANDLED
:
205 TerminateProcess( GetCurrentProcess(), epointers
->ExceptionRecord
->ExceptionCode
);
206 break; /* not reached */
207 case 0: /* no debugger is present */
210 FIXME("Unsupported yet debug continue value %d (please report)\n", status
);
215 DWORD ret
= top_filter( epointers
);
216 if (ret
!= EXCEPTION_CONTINUE_SEARCH
) return ret
;
219 /* FIXME: Should check the current error mode */
221 if (!RegOpenKeyA(HKEY_LOCAL_MACHINE
,
222 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug",
227 count
= sizeof(format
);
228 if (RegQueryValueExA(hDbgConf
, "Debugger", 0, &type
, format
, &count
))
231 count
= sizeof(bAuto
);
232 if (RegQueryValueExA(hDbgConf
, "Auto", 0, &type
, (char*)&bAuto
, &count
))
234 else if (type
== REG_SZ
)
237 count
= sizeof(autostr
);
238 if (!RegQueryValueExA(hDbgConf
, "Auto", 0, &type
, autostr
, &count
))
239 bAuto
= atoi(autostr
);
241 RegCloseKey(hDbgConf
);
244 strcpy(format
, "debugger/winedbg %ld %ld");
249 HMODULE mod
= GetModuleHandleA( "user32.dll" );
250 MessageBoxA_funcptr pMessageBoxA
= NULL
;
251 if (mod
) pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
254 format_exception_msg( epointers
, buffer
);
255 if (pMessageBoxA( 0, buffer
, "Error", MB_YESNO
| MB_ICONHAND
) == IDNO
)
257 TRACE("Killing process\n");
258 return EXCEPTION_EXECUTE_HANDLER
;
265 PROCESS_INFORMATION info
;
266 STARTUPINFOA startup
;
267 OBJECT_ATTRIBUTES attr
;
269 attr
.Length
= sizeof(attr
);
270 attr
.RootDirectory
= 0;
271 attr
.Attributes
= OBJ_INHERIT
;
272 attr
.ObjectName
= NULL
;
273 attr
.SecurityDescriptor
= NULL
;
274 attr
.SecurityQualityOfService
= NULL
;
276 TRACE("Starting debugger (fmt=%s)\n", format
);
277 NtCreateEvent( &hEvent
, EVENT_ALL_ACCESS
, &attr
, FALSE
, FALSE
);
278 sprintf(buffer
, format
, GetCurrentProcessId(), hEvent
);
279 memset(&startup
, 0, sizeof(startup
));
280 startup
.cb
= sizeof(startup
);
281 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
282 startup
.wShowWindow
= SW_SHOWNORMAL
;
283 if (CreateProcessA(NULL
, buffer
, NULL
, NULL
,
284 TRUE
, 0, NULL
, NULL
, &startup
, &info
)) {
285 WaitForSingleObject(hEvent
, INFINITE
);
286 ret
= EXCEPTION_CONTINUE_SEARCH
;
288 ERR("Couldn't start debugger (%s) (%ld)\n"
289 "Read the Wine Developers Guide on how to set up winedbg or another debugger\n",
290 buffer
, GetLastError());
294 ERR("No standard debugger defined in the registry => no debugging session\n");
301 /***********************************************************************
302 * SetUnhandledExceptionFilter (KERNEL32.@)
304 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI
SetUnhandledExceptionFilter(
305 LPTOP_LEVEL_EXCEPTION_FILTER filter
)
307 LPTOP_LEVEL_EXCEPTION_FILTER old
= top_filter
;
313 /**************************************************************************
314 * FatalAppExitA (KERNEL32.@)
316 void WINAPI
FatalAppExitA( UINT action
, LPCSTR str
)
318 HMODULE mod
= GetModuleHandleA( "user32.dll" );
319 MessageBoxA_funcptr pMessageBoxA
= NULL
;
323 if (mod
) pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
324 if (pMessageBoxA
) pMessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
325 else ERR( "%s\n", debugstr_a(str
) );
330 /**************************************************************************
331 * FatalAppExitW (KERNEL32.@)
333 void WINAPI
FatalAppExitW( UINT action
, LPCWSTR str
)
335 HMODULE mod
= GetModuleHandleA( "user32.dll" );
336 MessageBoxW_funcptr pMessageBoxW
= NULL
;
340 if (mod
) pMessageBoxW
= (MessageBoxW_funcptr
)GetProcAddress( mod
, "MessageBoxW" );
341 if (pMessageBoxW
) pMessageBoxW( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
342 else ERR( "%s\n", debugstr_w(str
) );