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 */
206 FIXME("Unsupported yet debug continue value %d (please report)\n", status);
211 DWORD ret = top_filter( epointers );
212 if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
215 /* FIXME: Should check the current error mode */
217 if (!RegOpenKeyA(HKEY_LOCAL_MACHINE,
218 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug",
223 count = sizeof(format);
224 if (RegQueryValueExA(hDbgConf, "Debugger", 0, &type, format, &count))
227 count = sizeof(bAuto);
228 if (RegQueryValueExA(hDbgConf, "Auto", 0, &type, (char*)&bAuto, &count))
230 else if (type == REG_SZ)
233 count = sizeof(autostr);
234 if (!RegQueryValueExA(hDbgConf, "Auto", 0, &type, autostr, &count))
235 bAuto = atoi(autostr);
237 RegCloseKey(hDbgConf);
240 strcpy(format, "debugger/winedbg %ld %ld");
245 HMODULE mod = GetModuleHandleA( "user32.dll" );
246 MessageBoxA_funcptr pMessageBoxA = NULL;
247 if (mod) pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
250 format_exception_msg( epointers, buffer );
251 if (pMessageBoxA( 0, buffer, "Error", MB_YESNO | MB_ICONHAND ) == IDNO)
253 TRACE("Killing process\n");
254 return EXCEPTION_EXECUTE_HANDLER;
261 PROCESS_INFORMATION info;
262 STARTUPINFOA startup;
263 OBJECT_ATTRIBUTES attr;
265 attr.Length = sizeof(attr);
266 attr.RootDirectory = 0;
267 attr.Attributes = OBJ_INHERIT;
268 attr.ObjectName = NULL;
269 attr.SecurityDescriptor = NULL;
270 attr.SecurityQualityOfService = NULL;
272 TRACE("Starting debugger (fmt=%s)\n", format);
273 NtCreateEvent( &hEvent, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
274 sprintf(buffer, format, GetCurrentProcessId(), hEvent);
275 memset(&startup, 0, sizeof(startup));
276 startup.cb = sizeof(startup);
277 startup.dwFlags = STARTF_USESHOWWINDOW;
278 startup.wShowWindow = SW_SHOWNORMAL;
279 if (CreateProcessA(NULL, buffer, NULL, NULL,
280 TRUE, 0, NULL, NULL, &startup, &info)) {
281 WaitForSingleObject(hEvent, INFINITE);
282 ret = EXCEPTION_CONTINUE_SEARCH;
284 ERR("Couldn't start debugger (%s) (%ld)\n"
285 "Read the Wine Developers Guide on how to set up winedbg or another debugger\n",
286 buffer, GetLastError());
290 ERR("No standard debugger defined in the registry => no debugging session\n");
297 /***********************************************************************
298 * SetUnhandledExceptionFilter (KERNEL32.@)
300 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
301 LPTOP_LEVEL_EXCEPTION_FILTER filter )
303 LPTOP_LEVEL_EXCEPTION_FILTER old = top_filter;
309 /**************************************************************************
310 * FatalAppExitA (KERNEL32.@)
312 void WINAPI FatalAppExitA( UINT action, LPCSTR str )
314 HMODULE mod = GetModuleHandleA( "user32.dll" );
315 MessageBoxA_funcptr pMessageBoxA = NULL;
319 if (mod) pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
320 if (pMessageBoxA) pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
321 else ERR( "%s\n", debugstr_a(str) );
326 /**************************************************************************
327 * FatalAppExitW (KERNEL32.@)
329 void WINAPI FatalAppExitW( UINT action, LPCWSTR str )
331 HMODULE mod = GetModuleHandleA( "user32.dll" );
332 MessageBoxW_funcptr pMessageBoxW = NULL;
336 if (mod) pMessageBoxW = (MessageBoxW_funcptr)GetProcAddress( mod, "MessageBoxW" );
337 if (pMessageBoxW) pMessageBoxW( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
338 else ERR( "%s\n", debugstr_w(str) );