Add shell support for deleting files using the Delete key.
[wine/dcerpc.git] / win32 / except.c
blobaaa3933c3bca65154d41849e222fa5d0c402a2a5
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_REQ
156 struct queue_exception_event_request *req = server_alloc_req( sizeof(*req),
157 sizeof(*rec)+sizeof(*context) );
158 CONTEXT *context_ptr = server_data_ptr(req);
159 EXCEPTION_RECORD *rec_ptr = (EXCEPTION_RECORD *)(context_ptr + 1);
160 req->first = first_chance;
161 *rec_ptr = *rec;
162 *context_ptr = *context;
163 if (!server_call_noerr( REQ_QUEUE_EXCEPTION_EVENT )) handle = req->handle;
165 SERVER_END_REQ;
166 if (!handle) return 0; /* no debugger present or other error */
168 /* No need to wait on the handle since the process gets suspended
169 * once the event is passed to the debugger, so when we get back
170 * here the event has been continued already.
172 SERVER_START_REQ
174 struct get_exception_status_request *req = server_alloc_req( sizeof(*req), sizeof(*context) );
175 req->handle = handle;
176 if (!server_call_noerr( REQ_GET_EXCEPTION_STATUS ))
177 *context = *(CONTEXT *)server_data_ptr(req);
178 ret = req->status;
180 SERVER_END_REQ;
181 NtClose( handle );
182 return ret;
186 /*******************************************************************
187 * UnhandledExceptionFilter (KERNEL32.@)
189 DWORD WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers)
191 char format[256];
192 char buffer[256];
193 HKEY hDbgConf;
194 DWORD bAuto = FALSE;
195 DWORD ret = EXCEPTION_EXECUTE_HANDLER;
196 int status;
198 /* send a last chance event to the debugger */
199 status = send_debug_event( epointers->ExceptionRecord, FALSE, epointers->ContextRecord );
200 switch (status)
202 case DBG_CONTINUE:
203 return EXCEPTION_CONTINUE_EXECUTION;
204 case DBG_EXCEPTION_NOT_HANDLED:
205 TerminateProcess( GetCurrentProcess(), epointers->ExceptionRecord->ExceptionCode );
206 break; /* not reached */
207 case 0: /* no debugger is present */
208 break;
209 default:
210 FIXME("Unsupported yet debug continue value %d (please report)\n", status);
213 if (top_filter)
215 DWORD ret = top_filter( epointers );
216 if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
219 /* FIXME: Should check the current error mode */
221 if (!RegOpenKeyA(HKEY_LOCAL_MACHINE,
222 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug",
223 &hDbgConf)) {
224 DWORD type;
225 DWORD count;
227 count = sizeof(format);
228 if (RegQueryValueExA(hDbgConf, "Debugger", 0, &type, format, &count))
229 format[0] = 0;
231 count = sizeof(bAuto);
232 if (RegQueryValueExA(hDbgConf, "Auto", 0, &type, (char*)&bAuto, &count))
233 bAuto = TRUE;
234 else if (type == REG_SZ)
236 char autostr[10];
237 count = sizeof(autostr);
238 if (!RegQueryValueExA(hDbgConf, "Auto", 0, &type, autostr, &count))
239 bAuto = atoi(autostr);
241 RegCloseKey(hDbgConf);
242 } else {
243 /* format[0] = 0; */
244 strcpy(format, "debugger/winedbg %ld %ld");
247 if (!bAuto)
249 HMODULE mod = GetModuleHandleA( "user32.dll" );
250 MessageBoxA_funcptr pMessageBoxA = NULL;
251 if (mod) pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
252 if (pMessageBoxA)
254 format_exception_msg( epointers, buffer );
255 if (pMessageBoxA( 0, buffer, "Error", MB_YESNO | MB_ICONHAND ) == IDNO)
257 TRACE("Killing process\n");
258 return EXCEPTION_EXECUTE_HANDLER;
263 if (format[0]) {
264 HANDLE hEvent;
265 PROCESS_INFORMATION info;
266 STARTUPINFOA startup;
267 OBJECT_ATTRIBUTES attr;
269 attr.Length = sizeof(attr);
270 attr.RootDirectory = 0;
271 attr.Attributes = OBJ_INHERIT;
272 attr.ObjectName = NULL;
273 attr.SecurityDescriptor = NULL;
274 attr.SecurityQualityOfService = NULL;
276 TRACE("Starting debugger (fmt=%s)\n", format);
277 NtCreateEvent( &hEvent, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
278 sprintf(buffer, format, GetCurrentProcessId(), hEvent);
279 memset(&startup, 0, sizeof(startup));
280 startup.cb = sizeof(startup);
281 startup.dwFlags = STARTF_USESHOWWINDOW;
282 startup.wShowWindow = SW_SHOWNORMAL;
283 if (CreateProcessA(NULL, buffer, NULL, NULL,
284 TRUE, 0, NULL, NULL, &startup, &info)) {
285 WaitForSingleObject(hEvent, INFINITE);
286 ret = EXCEPTION_CONTINUE_SEARCH;
287 } else {
288 ERR("Couldn't start debugger (%s) (%ld)\n"
289 "Read the Wine Developers Guide on how to set up winedbg or another debugger\n",
290 buffer, GetLastError());
292 CloseHandle(hEvent);
293 } else {
294 ERR("No standard debugger defined in the registry => no debugging session\n");
297 return ret;
301 /***********************************************************************
302 * SetUnhandledExceptionFilter (KERNEL32.@)
304 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
305 LPTOP_LEVEL_EXCEPTION_FILTER filter )
307 LPTOP_LEVEL_EXCEPTION_FILTER old = top_filter;
308 top_filter = filter;
309 return old;
313 /**************************************************************************
314 * FatalAppExitA (KERNEL32.@)
316 void WINAPI FatalAppExitA( UINT action, LPCSTR str )
318 HMODULE mod = GetModuleHandleA( "user32.dll" );
319 MessageBoxA_funcptr pMessageBoxA = NULL;
321 WARN("AppExit\n");
323 if (mod) pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
324 if (pMessageBoxA) pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
325 else ERR( "%s\n", debugstr_a(str) );
326 ExitProcess(0);
330 /**************************************************************************
331 * FatalAppExitW (KERNEL32.@)
333 void WINAPI FatalAppExitW( UINT action, LPCWSTR str )
335 HMODULE mod = GetModuleHandleA( "user32.dll" );
336 MessageBoxW_funcptr pMessageBoxW = NULL;
338 WARN("AppExit\n");
340 if (mod) pMessageBoxW = (MessageBoxW_funcptr)GetProcAddress( mod, "MessageBoxW" );
341 if (pMessageBoxW) pMessageBoxW( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
342 else ERR( "%s\n", debugstr_w(str) );
343 ExitProcess(0);