Include the GUIDs in ddrawi.h in the libwine_uuid.a library.
[wine.git] / win32 / except.c
blob696b8e51644b82395f53bbd43cb8307a31a9a8cd
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 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" );
80 break;
81 case EXCEPTION_INT_OVERFLOW:
82 sprintf( buffer, "Unhandled overflow" );
83 break;
84 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
85 sprintf( buffer, "Unhandled array bounds" );
86 break;
87 case EXCEPTION_ILLEGAL_INSTRUCTION:
88 sprintf( buffer, "Unhandled illegal instruction" );
89 break;
90 case EXCEPTION_STACK_OVERFLOW:
91 sprintf( buffer, "Unhandled stack overflow" );
92 break;
93 case EXCEPTION_PRIV_INSTRUCTION:
94 sprintf( buffer, "Unhandled priviledged instruction" );
95 break;
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]);
101 else
102 sprintf( buffer, "Unhandled page fault");
103 break;
104 case EXCEPTION_DATATYPE_MISALIGNMENT:
105 sprintf( buffer, "Unhandled alignment" );
106 break;
107 case CONTROL_C_EXIT:
108 sprintf( buffer, "Unhandled ^C");
109 break;
110 case EXCEPTION_CRITICAL_SECTION_WAIT:
111 sprintf( buffer, "Critical section %08lx wait failed",
112 rec->ExceptionInformation[0]);
113 break;
114 case EXCEPTION_WINE_STUB:
115 sprintf( buffer, "Unimplemented function %s.%s called",
116 (char *)rec->ExceptionInformation[0], (char *)rec->ExceptionInformation[1] );
117 break;
118 case EXCEPTION_VM86_INTx:
119 sprintf( buffer, "Unhandled interrupt %02lx in vm86 mode",
120 rec->ExceptionInformation[0]);
121 break;
122 case EXCEPTION_VM86_STI:
123 sprintf( buffer, "Unhandled sti in vm86 mode");
124 break;
125 case EXCEPTION_VM86_PICRETURN:
126 sprintf( buffer, "Unhandled PIC return in vm86 mode");
127 break;
128 default:
129 sprintf( buffer, "Unhandled exception 0x%08lx", rec->ExceptionCode);
130 break;
132 #ifdef __i386__
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 );
136 else
137 #endif
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 /**********************************************************************
145 * send_debug_event
147 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
149 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
151 int ret;
152 HANDLE handle = 0;
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;
159 *rec_ptr = *rec;
160 *context_ptr = *context;
161 if (!SERVER_CALL()) handle = req->handle;
163 SERVER_END_VAR_REQ;
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);
174 ret = req->status;
176 SERVER_END_VAR_REQ;
177 NtClose( handle );
178 return ret;
182 /*******************************************************************
183 * UnhandledExceptionFilter (KERNEL32.@)
185 DWORD WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers)
187 char format[256];
188 char buffer[256];
189 HKEY hDbgConf;
190 DWORD bAuto = FALSE;
191 DWORD ret = EXCEPTION_EXECUTE_HANDLER;
192 int status;
194 /* send a last chance event to the debugger */
195 status = send_debug_event( epointers->ExceptionRecord, FALSE, epointers->ContextRecord );
196 switch (status)
198 case DBG_CONTINUE:
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 );
209 break;
210 default:
211 FIXME("Unsupported yet debug continue value %d (please report)\n", status);
214 if (top_filter)
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",
224 &hDbgConf)) {
225 DWORD type;
226 DWORD count;
228 count = sizeof(format);
229 if (RegQueryValueExA(hDbgConf, "Debugger", 0, &type, format, &count))
230 format[0] = 0;
232 count = sizeof(bAuto);
233 if (RegQueryValueExA(hDbgConf, "Auto", 0, &type, (char*)&bAuto, &count))
234 bAuto = TRUE;
235 else if (type == REG_SZ)
237 char autostr[10];
238 count = sizeof(autostr);
239 if (!RegQueryValueExA(hDbgConf, "Auto", 0, &type, autostr, &count))
240 bAuto = atoi(autostr);
242 RegCloseKey(hDbgConf);
243 } else {
244 /* format[0] = 0; */
245 strcpy(format, "debugger/winedbg %ld %ld");
248 if (!bAuto)
250 HMODULE mod = GetModuleHandleA( "user32.dll" );
251 MessageBoxA_funcptr pMessageBoxA = NULL;
252 if (mod) pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
253 if (pMessageBoxA)
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;
264 if (format[0]) {
265 HANDLE hEvent;
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;
288 } else {
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());
293 CloseHandle(hEvent);
294 } else {
295 ERR("No standard debugger defined in the registry => no debugging session\n");
298 return ret;
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;
309 top_filter = filter;
310 return old;
314 /**************************************************************************
315 * FatalAppExitA (KERNEL32.@)
317 void WINAPI FatalAppExitA( UINT action, LPCSTR str )
319 HMODULE mod = GetModuleHandleA( "user32.dll" );
320 MessageBoxA_funcptr pMessageBoxA = NULL;
322 WARN("AppExit\n");
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) );
327 ExitProcess(0);
331 /**************************************************************************
332 * FatalAppExitW (KERNEL32.@)
334 void WINAPI FatalAppExitW( UINT action, LPCWSTR str )
336 HMODULE mod = GetModuleHandleA( "user32.dll" );
337 MessageBoxW_funcptr pMessageBoxW = NULL;
339 WARN("AppExit\n");
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) );
344 ExitProcess(0);