Use 32-bit hook functions where possible. Cleaned up a couple of
[wine/multimedia.git] / win32 / except.c
blob86d23b3112900168aa77028bdf40679705dbb580
1 /*
2 * Win32 exception functions
4 * Copyright (c) 1996 Onno Hovers, (onno@stack.urc.tue.nl)
5 * Copyright (c) 1999 Alexandre Julliard
7 * Notes:
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
12 * reasons for this:
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
21 * documented!
25 #include <stdio.h>
26 #include "windef.h"
27 #include "winerror.h"
28 #include "ntddk.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "wine/exception.h"
32 #include "thread.h"
33 #include "stackframe.h"
34 #include "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;
57 if (nbargs && args)
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;
75 int len,len2;
77 switch(rec->ExceptionCode)
79 case EXCEPTION_INT_DIVIDE_BY_ZERO:
80 len = snprintf( buffer, size, "Unhandled division by zero" );
81 break;
82 case EXCEPTION_INT_OVERFLOW:
83 len = snprintf( buffer, size, "Unhandled overflow" );
84 break;
85 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
86 len = snprintf( buffer, size, "Unhandled array bounds" );
87 break;
88 case EXCEPTION_ILLEGAL_INSTRUCTION:
89 len = snprintf( buffer, size, "Unhandled illegal instruction" );
90 break;
91 case EXCEPTION_STACK_OVERFLOW:
92 len = snprintf( buffer, size, "Unhandled stack overflow" );
93 break;
94 case EXCEPTION_PRIV_INSTRUCTION:
95 len = snprintf( buffer, size, "Unhandled priviledged instruction" );
96 break;
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]);
102 else
103 len = snprintf( buffer, size, "Unhandled page fault");
104 break;
105 case EXCEPTION_DATATYPE_MISALIGNMENT:
106 len = snprintf( buffer, size, "Unhandled alignment" );
107 break;
108 case CONTROL_C_EXIT:
109 len = snprintf( buffer, size, "Unhandled ^C");
110 break;
111 case EXCEPTION_CRITICAL_SECTION_WAIT:
112 len = snprintf( buffer, size, "Critical section %08lx wait failed",
113 rec->ExceptionInformation[0]);
114 break;
115 case EXCEPTION_WINE_STUB:
116 len = snprintf( buffer, size, "Unimplemented function %s.%s called",
117 (char *)rec->ExceptionInformation[0], (char *)rec->ExceptionInformation[1] );
118 break;
119 case EXCEPTION_VM86_INTx:
120 len = snprintf( buffer, size, "Unhandled interrupt %02lx in vm86 mode",
121 rec->ExceptionInformation[0]);
122 break;
123 case EXCEPTION_VM86_STI:
124 len = snprintf( buffer, size, "Unhandled sti in vm86 mode");
125 break;
126 case EXCEPTION_VM86_PICRETURN:
127 len = snprintf( buffer, size, "Unhandled PIC return in vm86 mode");
128 break;
129 default:
130 len = snprintf( buffer, size, "Unhandled exception 0x%08lx", rec->ExceptionCode);
131 break;
133 if ((len<0) || (len>=size))
134 return -1;
135 #ifdef __i386__
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);
141 else
142 #endif
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))
147 return -1;
148 return len+len2;
152 /**********************************************************************
153 * send_debug_event
155 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
157 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
159 int ret;
160 HANDLE handle = 0;
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;
167 *rec_ptr = *rec;
168 *context_ptr = *context;
169 if (!SERVER_CALL()) handle = req->handle;
171 SERVER_END_VAR_REQ;
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);
182 ret = req->status;
184 SERVER_END_VAR_REQ;
185 NtClose( handle );
186 return ret;
190 /*******************************************************************
191 * UnhandledExceptionFilter (KERNEL32.@)
193 DWORD WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers)
195 char format[256];
196 char buffer[256];
197 HKEY hDbgConf;
198 DWORD bAuto = FALSE;
199 DWORD ret = EXCEPTION_EXECUTE_HANDLER;
200 int status;
202 /* send a last chance event to the debugger */
203 status = send_debug_event( epointers->ExceptionRecord, FALSE, epointers->ContextRecord );
204 switch (status)
206 case DBG_CONTINUE:
207 return EXCEPTION_CONTINUE_EXECUTION;
208 case DBG_EXCEPTION_NOT_HANDLED:
209 TerminateProcess( GetCurrentProcess(), epointers->ExceptionRecord->ExceptionCode );
210 break; /* not reached */
211 case 0: /* no debugger is present */
212 if (epointers->ExceptionRecord->ExceptionCode == CONTROL_C_EXIT)
214 /* do not launch the debugger on ^C, simply terminate the process */
215 TerminateProcess( GetCurrentProcess(), 1 );
217 break;
218 default:
219 FIXME("Unsupported yet debug continue value %d (please report)\n", status);
222 if (top_filter)
224 DWORD ret = top_filter( epointers );
225 if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
228 /* FIXME: Should check the current error mode */
230 if (!RegOpenKeyA(HKEY_LOCAL_MACHINE,
231 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug",
232 &hDbgConf)) {
233 DWORD type;
234 DWORD count;
236 count = sizeof(format);
237 if (RegQueryValueExA(hDbgConf, "Debugger", 0, &type, format, &count))
238 format[0] = 0;
240 count = sizeof(bAuto);
241 if (RegQueryValueExA(hDbgConf, "Auto", 0, &type, (char*)&bAuto, &count))
242 bAuto = TRUE;
243 else if (type == REG_SZ)
245 char autostr[10];
246 count = sizeof(autostr);
247 if (!RegQueryValueExA(hDbgConf, "Auto", 0, &type, autostr, &count))
248 bAuto = atoi(autostr);
250 RegCloseKey(hDbgConf);
251 } else {
252 /* format[0] = 0; */
253 strcpy(format, "debugger/winedbg %ld %ld");
256 if (!bAuto)
258 HMODULE mod = GetModuleHandleA( "user32.dll" );
259 MessageBoxA_funcptr pMessageBoxA = NULL;
260 if (mod) pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
261 if (pMessageBoxA)
263 format_exception_msg( epointers, buffer, sizeof(buffer) );
264 if (pMessageBoxA( 0, buffer, "Exception raised", MB_YESNO | MB_ICONHAND ) == IDNO)
266 TRACE("Killing process\n");
267 return EXCEPTION_EXECUTE_HANDLER;
272 if (format[0]) {
273 HANDLE hEvent;
274 PROCESS_INFORMATION info;
275 STARTUPINFOA startup;
276 OBJECT_ATTRIBUTES attr;
278 attr.Length = sizeof(attr);
279 attr.RootDirectory = 0;
280 attr.Attributes = OBJ_INHERIT;
281 attr.ObjectName = NULL;
282 attr.SecurityDescriptor = NULL;
283 attr.SecurityQualityOfService = NULL;
285 TRACE("Starting debugger (fmt=%s)\n", format);
286 NtCreateEvent( &hEvent, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
287 sprintf(buffer, format, GetCurrentProcessId(), hEvent);
288 memset(&startup, 0, sizeof(startup));
289 startup.cb = sizeof(startup);
290 startup.dwFlags = STARTF_USESHOWWINDOW;
291 startup.wShowWindow = SW_SHOWNORMAL;
292 if (CreateProcessA(NULL, buffer, NULL, NULL,
293 TRUE, 0, NULL, NULL, &startup, &info)) {
294 WaitForSingleObject(hEvent, INFINITE);
295 ret = EXCEPTION_CONTINUE_SEARCH;
296 } else {
297 ERR("Couldn't start debugger (%s) (%ld)\n"
298 "Read the Wine Developers Guide on how to set up winedbg or another debugger\n",
299 buffer, GetLastError());
301 CloseHandle(hEvent);
302 } else {
303 ERR("No standard debugger defined in the registry => no debugging session\n");
306 return ret;
310 /***********************************************************************
311 * SetUnhandledExceptionFilter (KERNEL32.@)
313 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
314 LPTOP_LEVEL_EXCEPTION_FILTER filter )
316 LPTOP_LEVEL_EXCEPTION_FILTER old = top_filter;
317 top_filter = filter;
318 return old;
322 /**************************************************************************
323 * FatalAppExitA (KERNEL32.@)
325 void WINAPI FatalAppExitA( UINT action, LPCSTR str )
327 HMODULE mod = GetModuleHandleA( "user32.dll" );
328 MessageBoxA_funcptr pMessageBoxA = NULL;
330 WARN("AppExit\n");
332 if (mod) pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
333 if (pMessageBoxA) pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
334 else ERR( "%s\n", debugstr_a(str) );
335 ExitProcess(0);
339 /**************************************************************************
340 * FatalAppExitW (KERNEL32.@)
342 void WINAPI FatalAppExitW( UINT action, LPCWSTR str )
344 HMODULE mod = GetModuleHandleA( "user32.dll" );
345 MessageBoxW_funcptr pMessageBoxW = NULL;
347 WARN("AppExit\n");
349 if (mod) pMessageBoxW = (MessageBoxW_funcptr)GetProcAddress( mod, "MessageBoxW" );
350 if (pMessageBoxW) pMessageBoxW( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
351 else ERR( "%s\n", debugstr_w(str) );
352 ExitProcess(0);