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
)
154 SERVER_START_VAR_REQ( queue_exception_event
, sizeof(*rec
) + sizeof(*context
) )
156 CONTEXT
*context_ptr
= server_data_ptr(req
);
157 EXCEPTION_RECORD
*rec_ptr
= (EXCEPTION_RECORD
*)(context_ptr
+ 1);
158 req
->first
= first_chance
;
160 *context_ptr
= *context
;
161 if (!SERVER_CALL()) handle
= req
->handle
;
164 if (!handle
) return 0; /* no debugger present or other error */
166 /* No need to wait on the handle since the process gets suspended
167 * once the event is passed to the debugger, so when we get back
168 * here the event has been continued already.
170 SERVER_START_VAR_REQ( get_exception_status
, sizeof(*context
) )
172 req
->handle
= handle
;
173 if (!SERVER_CALL()) *context
= *(CONTEXT
*)server_data_ptr(req
);
182 /*******************************************************************
183 * UnhandledExceptionFilter (KERNEL32.@)
185 DWORD WINAPI
UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers
)
191 DWORD ret
= EXCEPTION_EXECUTE_HANDLER
;
194 /* send a last chance event to the debugger */
195 status
= send_debug_event( epointers
->ExceptionRecord
, FALSE
, epointers
->ContextRecord
);
199 return EXCEPTION_CONTINUE_EXECUTION
;
200 case DBG_EXCEPTION_NOT_HANDLED
:
201 TerminateProcess( GetCurrentProcess(), epointers
->ExceptionRecord
->ExceptionCode
);
202 break; /* not reached */
203 case 0: /* no debugger is present */
204 if (epointers
->ExceptionRecord
->ExceptionCode
== CONTROL_C_EXIT
)
206 /* do not launch the debugger on ^C, simply terminate the process */
207 TerminateProcess( GetCurrentProcess(), 1 );
211 FIXME("Unsupported yet debug continue value %d (please report)\n", status
);
216 DWORD ret
= top_filter( epointers
);
217 if (ret
!= EXCEPTION_CONTINUE_SEARCH
) return ret
;
220 /* FIXME: Should check the current error mode */
222 if (!RegOpenKeyA(HKEY_LOCAL_MACHINE
,
223 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug",
228 count
= sizeof(format
);
229 if (RegQueryValueExA(hDbgConf
, "Debugger", 0, &type
, format
, &count
))
232 count
= sizeof(bAuto
);
233 if (RegQueryValueExA(hDbgConf
, "Auto", 0, &type
, (char*)&bAuto
, &count
))
235 else if (type
== REG_SZ
)
238 count
= sizeof(autostr
);
239 if (!RegQueryValueExA(hDbgConf
, "Auto", 0, &type
, autostr
, &count
))
240 bAuto
= atoi(autostr
);
242 RegCloseKey(hDbgConf
);
245 strcpy(format
, "debugger/winedbg %ld %ld");
250 HMODULE mod
= GetModuleHandleA( "user32.dll" );
251 MessageBoxA_funcptr pMessageBoxA
= NULL
;
252 if (mod
) pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
255 format_exception_msg( epointers
, buffer
);
256 if (pMessageBoxA( 0, buffer
, "Error", MB_YESNO
| MB_ICONHAND
) == IDNO
)
258 TRACE("Killing process\n");
259 return EXCEPTION_EXECUTE_HANDLER
;
266 PROCESS_INFORMATION info
;
267 STARTUPINFOA startup
;
268 OBJECT_ATTRIBUTES attr
;
270 attr
.Length
= sizeof(attr
);
271 attr
.RootDirectory
= 0;
272 attr
.Attributes
= OBJ_INHERIT
;
273 attr
.ObjectName
= NULL
;
274 attr
.SecurityDescriptor
= NULL
;
275 attr
.SecurityQualityOfService
= NULL
;
277 TRACE("Starting debugger (fmt=%s)\n", format
);
278 NtCreateEvent( &hEvent
, EVENT_ALL_ACCESS
, &attr
, FALSE
, FALSE
);
279 sprintf(buffer
, format
, GetCurrentProcessId(), hEvent
);
280 memset(&startup
, 0, sizeof(startup
));
281 startup
.cb
= sizeof(startup
);
282 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
283 startup
.wShowWindow
= SW_SHOWNORMAL
;
284 if (CreateProcessA(NULL
, buffer
, NULL
, NULL
,
285 TRUE
, 0, NULL
, NULL
, &startup
, &info
)) {
286 WaitForSingleObject(hEvent
, INFINITE
);
287 ret
= EXCEPTION_CONTINUE_SEARCH
;
289 ERR("Couldn't start debugger (%s) (%ld)\n"
290 "Read the Wine Developers Guide on how to set up winedbg or another debugger\n",
291 buffer
, GetLastError());
295 ERR("No standard debugger defined in the registry => no debugging session\n");
302 /***********************************************************************
303 * SetUnhandledExceptionFilter (KERNEL32.@)
305 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI
SetUnhandledExceptionFilter(
306 LPTOP_LEVEL_EXCEPTION_FILTER filter
)
308 LPTOP_LEVEL_EXCEPTION_FILTER old
= top_filter
;
314 /**************************************************************************
315 * FatalAppExitA (KERNEL32.@)
317 void WINAPI
FatalAppExitA( UINT action
, LPCSTR str
)
319 HMODULE mod
= GetModuleHandleA( "user32.dll" );
320 MessageBoxA_funcptr pMessageBoxA
= NULL
;
324 if (mod
) pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
325 if (pMessageBoxA
) pMessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
326 else ERR( "%s\n", debugstr_a(str
) );
331 /**************************************************************************
332 * FatalAppExitW (KERNEL32.@)
334 void WINAPI
FatalAppExitW( UINT action
, LPCWSTR str
)
336 HMODULE mod
= GetModuleHandleA( "user32.dll" );
337 MessageBoxW_funcptr pMessageBoxW
= NULL
;
341 if (mod
) pMessageBoxW
= (MessageBoxW_funcptr
)GetProcAddress( mod
, "MessageBoxW" );
342 if (pMessageBoxW
) pMessageBoxW( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
343 else ERR( "%s\n", debugstr_w(str
) );