Made exception_event_request non-blocking, and added
[wine/multimedia.git] / win32 / except.c
blob07e87e5877275605a7f305b9ca01a3694bd610d4
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 "wine/exception.h"
30 #include "callback.h"
31 #include "thread.h"
32 #include "stackframe.h"
33 #include "server.h"
34 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(seh);
38 static PTOP_LEVEL_EXCEPTION_FILTER top_filter;
41 /*******************************************************************
42 * RaiseException (KERNEL32.418)
44 void WINAPI RaiseException( DWORD code, DWORD flags, DWORD nbargs, const LPDWORD args )
46 EXCEPTION_RECORD record;
48 /* Compose an exception record */
50 record.ExceptionCode = code;
51 record.ExceptionFlags = flags & EH_NONCONTINUABLE;
52 record.ExceptionRecord = NULL;
53 record.ExceptionAddress = RaiseException;
54 if (nbargs && args)
56 if (nbargs > EXCEPTION_MAXIMUM_PARAMETERS) nbargs = EXCEPTION_MAXIMUM_PARAMETERS;
57 record.NumberParameters = nbargs;
58 memcpy( record.ExceptionInformation, args, nbargs * sizeof(*args) );
60 else record.NumberParameters = 0;
62 RtlRaiseException( &record );
66 /*******************************************************************
67 * format_exception_msg
69 static void format_exception_msg( const EXCEPTION_POINTERS *ptr, char *buffer )
71 const EXCEPTION_RECORD *rec = ptr->ExceptionRecord;
73 switch(rec->ExceptionCode)
75 case EXCEPTION_INT_DIVIDE_BY_ZERO:
76 sprintf( buffer, "Unhandled division by zero" );
77 break;
78 case EXCEPTION_INT_OVERFLOW:
79 sprintf( buffer, "Unhandled overflow" );
80 break;
81 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
82 sprintf( buffer, "Unhandled array bounds" );
83 break;
84 case EXCEPTION_ILLEGAL_INSTRUCTION:
85 sprintf( buffer, "Unhandled illegal instruction" );
86 break;
87 case EXCEPTION_STACK_OVERFLOW:
88 sprintf( buffer, "Unhandled stack overflow" );
89 break;
90 case EXCEPTION_PRIV_INSTRUCTION:
91 sprintf( buffer, "Unhandled priviledged instruction" );
92 break;
93 case EXCEPTION_ACCESS_VIOLATION:
94 if (rec->NumberParameters == 2)
95 sprintf( buffer, "Unhandled page fault on %s access to 0x%08lx",
96 rec->ExceptionInformation[0] ? "write" : "read",
97 rec->ExceptionInformation[1]);
98 else
99 sprintf( buffer, "Unhandled page fault");
100 break;
101 case EXCEPTION_DATATYPE_MISALIGNMENT:
102 sprintf( buffer, "Unhandled alignment" );
103 break;
104 case CONTROL_C_EXIT:
105 sprintf( buffer, "Unhandled ^C");
106 break;
107 case EXCEPTION_CRITICAL_SECTION_WAIT:
108 sprintf( buffer, "Critical section %08lx wait failed",
109 rec->ExceptionInformation[0]);
110 break;
111 case EXCEPTION_WINE_STUB:
112 sprintf( buffer, "Unimplemented function %s.%s called",
113 (char *)rec->ExceptionInformation[0], (char *)rec->ExceptionInformation[1] );
114 break;
115 case EXCEPTION_VM86_INTx:
116 sprintf( buffer, "Unhandled interrupt %02lx in vm86 mode",
117 rec->ExceptionInformation[0]);
118 break;
119 case EXCEPTION_VM86_STI:
120 sprintf( buffer, "Unhandled sti in vm86 mode");
121 break;
122 case EXCEPTION_VM86_PICRETURN:
123 sprintf( buffer, "Unhandled PIC return in vm86 mode");
124 break;
125 default:
126 sprintf( buffer, "Unhandled exception 0x%08lx", rec->ExceptionCode);
127 break;
129 #ifdef __i386__
130 if (ptr->ContextRecord->SegCs != __get_cs())
131 sprintf( buffer+strlen(buffer), " at address 0x%04lx:0x%08lx.\n",
132 ptr->ContextRecord->SegCs, (DWORD)ptr->ExceptionRecord->ExceptionAddress );
133 else
134 #endif
135 sprintf( buffer+strlen(buffer), " at address 0x%08lx.\n",
136 (DWORD)ptr->ExceptionRecord->ExceptionAddress );
137 strcat( buffer, "Do you wish to debug it ?" );
141 /**********************************************************************
142 * send_debug_event
144 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
146 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
148 int ret;
149 HANDLE handle = 0;
151 SERVER_START_REQ
153 struct queue_exception_event_request *req = server_alloc_req( sizeof(*req),
154 sizeof(*rec)+sizeof(*context) );
155 CONTEXT *context_ptr = server_data_ptr(req);
156 EXCEPTION_RECORD *rec_ptr = (EXCEPTION_RECORD *)(context_ptr + 1);
157 req->first = first_chance;
158 *rec_ptr = *rec;
159 *context_ptr = *context;
160 if (!server_call_noerr( REQ_QUEUE_EXCEPTION_EVENT )) handle = req->handle;
162 SERVER_END_REQ;
163 if (!handle) return 0; /* no debugger present or other error */
165 /* No need to wait on the handle since the process gets suspended
166 * once the event is passed to the debugger, so when we get back
167 * here the event has been continued already.
169 SERVER_START_REQ
171 struct get_exception_status_request *req = server_alloc_req( sizeof(*req), sizeof(*context) );
172 req->handle = handle;
173 if (!server_call_noerr( REQ_GET_EXCEPTION_STATUS ))
174 *context = *(CONTEXT *)server_data_ptr(req);
175 ret = req->status;
177 SERVER_END_REQ;
178 NtClose( handle );
179 return ret;
183 /*******************************************************************
184 * UnhandledExceptionFilter (KERNEL32.537)
186 DWORD WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers)
188 char format[256];
189 char buffer[256];
190 HKEY hDbgConf;
191 DWORD bAuto = FALSE;
192 DWORD ret = EXCEPTION_EXECUTE_HANDLER;
193 int status;
195 /* send a last chance event to the debugger */
196 status = send_debug_event( epointers->ExceptionRecord, FALSE, epointers->ContextRecord );
197 switch (status)
199 case DBG_CONTINUE:
200 return EXCEPTION_CONTINUE_EXECUTION;
201 case DBG_EXCEPTION_NOT_HANDLED:
202 TerminateProcess( GetCurrentProcess(), epointers->ExceptionRecord->ExceptionCode );
203 break; /* not reached */
204 case 0: /* no debugger is present */
205 break;
206 default:
207 FIXME("Unsupported yet debug continue value %d (please report)\n", status);
210 if (top_filter)
212 DWORD ret = top_filter( epointers );
213 if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
216 /* FIXME: Should check the current error mode */
218 if (!RegOpenKeyA(HKEY_LOCAL_MACHINE,
219 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug",
220 &hDbgConf)) {
221 DWORD type;
222 DWORD count;
224 count = sizeof(format);
225 if (RegQueryValueExA(hDbgConf, "Debugger", 0, &type, format, &count))
226 format[0] = 0;
228 count = sizeof(bAuto);
229 if (RegQueryValueExA(hDbgConf, "Auto", 0, &type, (char*)&bAuto, &count))
230 bAuto = TRUE;
231 else if (type == REG_SZ)
233 char autostr[10];
234 count = sizeof(autostr);
235 if (!RegQueryValueExA(hDbgConf, "Auto", 0, &type, autostr, &count))
236 bAuto = atoi(autostr);
238 RegCloseKey(hDbgConf);
239 } else {
240 /* format[0] = 0; */
241 strcpy(format, "debugger/winedbg %ld %ld");
244 if (!bAuto && Callout.MessageBoxA) {
245 format_exception_msg( epointers, buffer );
246 if (Callout.MessageBoxA( 0, buffer, "Error", MB_YESNO | MB_ICONHAND ) == IDNO) {
247 TRACE("Killing process\n");
248 return EXCEPTION_EXECUTE_HANDLER;
252 if (format[0]) {
253 HANDLE hEvent;
254 PROCESS_INFORMATION info;
255 STARTUPINFOA startup;
256 OBJECT_ATTRIBUTES attr;
258 attr.Length = sizeof(attr);
259 attr.RootDirectory = 0;
260 attr.Attributes = OBJ_INHERIT;
261 attr.ObjectName = NULL;
262 attr.SecurityDescriptor = NULL;
263 attr.SecurityQualityOfService = NULL;
265 TRACE("Starting debugger (fmt=%s)\n", format);
266 NtCreateEvent( &hEvent, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
267 sprintf(buffer, format, GetCurrentProcessId(), hEvent);
268 memset(&startup, 0, sizeof(startup));
269 startup.cb = sizeof(startup);
270 startup.dwFlags = STARTF_USESHOWWINDOW;
271 startup.wShowWindow = SW_SHOWNORMAL;
272 if (CreateProcessA(NULL, buffer, NULL, NULL,
273 TRUE, 0, NULL, NULL, &startup, &info)) {
274 WaitForSingleObject(hEvent, INFINITE);
275 ret = EXCEPTION_CONTINUE_SEARCH;
276 } else {
277 ERR("Couldn't start debugger (%s) (%ld)\n"
278 "Read the Wine Developers Guide on how to set up winedbg or another debugger\n",
279 buffer, GetLastError());
281 CloseHandle(hEvent);
282 } else {
283 ERR("No standard debugger defined in the registry => no debugging session\n");
286 return ret;
290 /***********************************************************************
291 * SetUnhandledExceptionFilter (KERNEL32.516)
293 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
294 LPTOP_LEVEL_EXCEPTION_FILTER filter )
296 LPTOP_LEVEL_EXCEPTION_FILTER old = top_filter;
297 top_filter = filter;
298 return old;
302 /**************************************************************************
303 * FatalAppExitA (KERNEL32.108)
305 void WINAPI FatalAppExitA( UINT action, LPCSTR str )
307 WARN("AppExit\n");
308 if (Callout.MessageBoxA)
309 Callout.MessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
310 else
311 ERR( "%s\n", debugstr_a(str) );
312 ExitProcess(0);
316 /**************************************************************************
317 * FatalAppExitW (KERNEL32.109)
319 void WINAPI FatalAppExitW( UINT action, LPCWSTR str )
321 WARN("AppExit\n");
322 if (Callout.MessageBoxW)
323 Callout.MessageBoxW( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
324 else
325 ERR( "%s\n", debugstr_w(str) );
326 ExitProcess(0);