Added default data formats for c_dfDIJoystick and c_dfDIJoystick2.
[wine/wine-gecko.git] / dlls / ntdll / exception.c
blob090795ef0ec94e0f7e750df5bde3d184ea9e3377
1 /*
2 * NT exception handling routines
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <signal.h>
27 #include <stdarg.h>
29 #include "ntstatus.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "thread.h"
33 #include "winternl.h"
34 #include "wine/exception.h"
35 #include "wine/server.h"
36 #include "wine/list.h"
37 #include "wine/debug.h"
38 #include "excpt.h"
39 #include "ntdll_misc.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(seh);
43 /* Exception record for handling exceptions happening inside exception handlers */
44 typedef struct
46 EXCEPTION_REGISTRATION_RECORD frame;
47 EXCEPTION_REGISTRATION_RECORD *prevFrame;
48 } EXC_NESTED_FRAME;
50 typedef struct
52 struct list entry;
53 PVECTORED_EXCEPTION_HANDLER func;
54 } VECTORED_HANDLER;
56 static struct list vectored_handlers = LIST_INIT(vectored_handlers);
58 static CRITICAL_SECTION vectored_handlers_section;
59 static CRITICAL_SECTION_DEBUG critsect_debug =
61 0, 0, &vectored_handlers_section,
62 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
63 0, 0, { 0, (DWORD)(__FILE__ ": vectored_handlers_section") }
65 static CRITICAL_SECTION vectored_handlers_section = { &critsect_debug, -1, 0, 0, 0, 0 };
67 #ifdef __i386__
68 # define GET_IP(context) ((LPVOID)(context)->Eip)
69 #elif defined(__sparc__)
70 # define GET_IP(context) ((LPVOID)(context)->pc)
71 #elif defined(__powerpc__)
72 # define GET_IP(context) ((LPVOID)(context)->Iar)
73 #else
74 # error You must define GET_IP for this CPU
75 #endif
78 /*******************************************************************
79 * EXC_RaiseHandler
81 * Handler for exceptions happening inside a handler.
83 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
84 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
86 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
87 return ExceptionContinueSearch;
88 /* We shouldn't get here so we store faulty frame in dispatcher */
89 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
90 return ExceptionNestedException;
94 /*******************************************************************
95 * EXC_UnwindHandler
97 * Handler for exceptions happening inside an unwind handler.
99 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
100 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
102 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
103 return ExceptionContinueSearch;
104 /* We shouldn't get here so we store faulty frame in dispatcher */
105 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
106 return ExceptionCollidedUnwind;
110 /*******************************************************************
111 * EXC_CallHandler
113 * Call an exception handler, setting up an exception frame to catch exceptions
114 * happening during the handler execution.
115 * Please do not change the first 4 parameters order in any way - some exceptions handlers
116 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
118 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
119 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
120 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
122 EXC_NESTED_FRAME newframe;
123 DWORD ret;
125 newframe.frame.Handler = nested_handler;
126 newframe.prevFrame = frame;
127 __wine_push_frame( &newframe.frame );
128 TRACE( "calling handler at %p code=%lx flags=%lx\n",
129 handler, record->ExceptionCode, record->ExceptionFlags );
130 ret = handler( record, frame, context, dispatcher );
131 TRACE( "handler returned %lx\n", ret );
132 __wine_pop_frame( &newframe.frame );
133 return ret;
137 /**********************************************************************
138 * send_debug_event
140 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
142 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
144 int ret;
145 HANDLE handle = 0;
147 if (!NtCurrentTeb()->Peb->BeingDebugged) return 0; /* no debugger present */
149 SERVER_START_REQ( queue_exception_event )
151 req->first = first_chance;
152 wine_server_add_data( req, context, sizeof(*context) );
153 wine_server_add_data( req, rec, sizeof(*rec) );
154 if (!wine_server_call( req )) handle = reply->handle;
156 SERVER_END_REQ;
157 if (!handle) return 0;
159 /* No need to wait on the handle since the process gets suspended
160 * once the event is passed to the debugger, so when we get back
161 * here the event has been continued already.
163 SERVER_START_REQ( get_exception_status )
165 req->handle = handle;
166 wine_server_set_reply( req, context, sizeof(*context) );
167 wine_server_call( req );
168 ret = reply->status;
170 SERVER_END_REQ;
171 NtClose( handle );
172 return ret;
176 /**********************************************************************
177 * call_vectored_handlers
179 * Call the vectored handlers chain.
181 static LONG call_vectored_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
183 struct list *ptr;
184 LONG ret = EXCEPTION_CONTINUE_SEARCH;
185 EXCEPTION_POINTERS except_ptrs;
187 except_ptrs.ExceptionRecord = rec;
188 except_ptrs.ContextRecord = context;
190 RtlEnterCriticalSection( &vectored_handlers_section );
191 LIST_FOR_EACH( ptr, &vectored_handlers )
193 VECTORED_HANDLER *handler = LIST_ENTRY( ptr, VECTORED_HANDLER, entry );
194 ret = handler->func( &except_ptrs );
195 if (ret == EXCEPTION_CONTINUE_EXECUTION) break;
197 RtlLeaveCriticalSection( &vectored_handlers_section );
198 return ret;
202 /*******************************************************************
203 * EXC_DefaultHandling
205 * Default handling for exceptions. Called when we didn't find a suitable handler.
207 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
209 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
211 if (rec->ExceptionFlags & EH_STACK_INVALID)
212 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
213 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
214 ERR("Process attempted to continue execution after noncontinuable exception.\n");
215 else
216 ERR("Unhandled exception code %lx flags %lx addr %p\n",
217 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
218 NtTerminateProcess( NtCurrentProcess(), 1 );
222 /***********************************************************************
223 * RtlRaiseException (NTDLL.@)
225 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
227 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
228 EXCEPTION_RECORD newrec;
229 DWORD res, c;
231 TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
232 for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
233 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
234 FIXME( "call to unimplemented function %s.%s\n",
235 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
236 #ifdef __i386__
237 else
239 TRACE(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
240 context->Eax, context->Ebx, context->Ecx,
241 context->Edx, context->Esi, context->Edi );
242 TRACE(" ebp=%08lx esp=%08lx cs=%04lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
243 context->Ebp, context->Esp, context->SegCs, context->SegDs,
244 context->SegEs, context->SegFs, context->SegGs, context->EFlags );
246 #endif
248 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
250 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION) return;
252 frame = NtCurrentTeb()->Tib.ExceptionList;
253 nested_frame = NULL;
254 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
256 /* Check frame address */
257 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
258 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
259 (int)frame & 3)
261 rec->ExceptionFlags |= EH_STACK_INVALID;
262 break;
265 /* Call handler */
266 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
267 if (frame == nested_frame)
269 /* no longer nested */
270 nested_frame = NULL;
271 rec->ExceptionFlags &= ~EH_NESTED_CALL;
274 switch(res)
276 case ExceptionContinueExecution:
277 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
278 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
279 newrec.ExceptionFlags = EH_NONCONTINUABLE;
280 newrec.ExceptionRecord = rec;
281 newrec.NumberParameters = 0;
282 RtlRaiseException( &newrec ); /* never returns */
283 break;
284 case ExceptionContinueSearch:
285 break;
286 case ExceptionNestedException:
287 if (nested_frame < dispatch) nested_frame = dispatch;
288 rec->ExceptionFlags |= EH_NESTED_CALL;
289 break;
290 default:
291 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
292 newrec.ExceptionFlags = EH_NONCONTINUABLE;
293 newrec.ExceptionRecord = rec;
294 newrec.NumberParameters = 0;
295 RtlRaiseException( &newrec ); /* never returns */
296 break;
298 frame = frame->Prev;
300 EXC_DefaultHandling( rec, context );
303 /**********************************************************************/
305 #ifdef DEFINE_REGS_ENTRYPOINT
306 DEFINE_REGS_ENTRYPOINT( RtlRaiseException, EXC_RtlRaiseException, 4, 4 );
307 #else
308 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
310 CONTEXT context;
311 memset( &context, 0, sizeof(context) );
312 EXC_RtlRaiseException( rec, &context );
314 #endif
317 /*******************************************************************
318 * RtlUnwind (NTDLL.@)
320 void WINAPI EXC_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID unusedEip,
321 PEXCEPTION_RECORD pRecord, PVOID returnEax, CONTEXT *context )
323 EXCEPTION_RECORD record, newrec;
324 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
326 #ifdef __i386__
327 context->Eax = (DWORD)returnEax;
328 #endif
330 /* build an exception record, if we do not have one */
331 if (!pRecord)
333 record.ExceptionCode = STATUS_UNWIND;
334 record.ExceptionFlags = 0;
335 record.ExceptionRecord = NULL;
336 record.ExceptionAddress = GET_IP(context);
337 record.NumberParameters = 0;
338 pRecord = &record;
341 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
343 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
345 /* get chain of exception frames */
346 frame = NtCurrentTeb()->Tib.ExceptionList;
347 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
349 /* Check frame address */
350 if (pEndFrame && (frame > pEndFrame))
352 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
353 newrec.ExceptionFlags = EH_NONCONTINUABLE;
354 newrec.ExceptionRecord = pRecord;
355 newrec.NumberParameters = 0;
356 RtlRaiseException( &newrec ); /* never returns */
358 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
359 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
360 (int)frame & 3)
362 newrec.ExceptionCode = STATUS_BAD_STACK;
363 newrec.ExceptionFlags = EH_NONCONTINUABLE;
364 newrec.ExceptionRecord = pRecord;
365 newrec.NumberParameters = 0;
366 RtlRaiseException( &newrec ); /* never returns */
369 /* Call handler */
370 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
371 frame->Handler, EXC_UnwindHandler ))
373 case ExceptionContinueSearch:
374 break;
375 case ExceptionCollidedUnwind:
376 frame = dispatch;
377 break;
378 default:
379 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
380 newrec.ExceptionFlags = EH_NONCONTINUABLE;
381 newrec.ExceptionRecord = pRecord;
382 newrec.NumberParameters = 0;
383 RtlRaiseException( &newrec ); /* never returns */
384 break;
386 frame = __wine_pop_frame( frame );
390 /**********************************************************************/
392 #ifdef DEFINE_REGS_ENTRYPOINT
393 DEFINE_REGS_ENTRYPOINT( RtlUnwind, EXC_RtlUnwind, 16, 16 );
394 #else
395 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID unusedEip,
396 PEXCEPTION_RECORD pRecord, PVOID returnEax )
398 CONTEXT context;
399 memset( &context, 0, sizeof(context) );
400 EXC_RtlUnwind( pEndFrame, unusedEip, pRecord, returnEax, &context );
402 #endif
405 /*******************************************************************
406 * NtRaiseException (NTDLL.@)
408 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
409 BOOL first, CONTEXT *context )
411 EXC_RtlRaiseException( rec, ctx );
412 *context = *ctx;
415 #ifdef DEFINE_REGS_ENTRYPOINT
416 DEFINE_REGS_ENTRYPOINT( NtRaiseException, EXC_NtRaiseException, 12, 12 );
417 #else
418 void WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first )
420 CONTEXT context;
421 memset( &context, 0, sizeof(context) );
422 EXC_NtRaiseException( rec, ctx, first, &context );
424 #endif
427 /***********************************************************************
428 * RtlRaiseStatus (NTDLL.@)
430 * Raise an exception with ExceptionCode = status
432 void WINAPI RtlRaiseStatus( NTSTATUS status )
434 EXCEPTION_RECORD ExceptionRec;
436 ExceptionRec.ExceptionCode = status;
437 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
438 ExceptionRec.ExceptionRecord = NULL;
439 ExceptionRec.NumberParameters = 0;
440 RtlRaiseException( &ExceptionRec );
444 /*******************************************************************
445 * RtlAddVectoredExceptionHandler (NTDLL.@)
447 PVOID WINAPI RtlAddVectoredExceptionHandler( ULONG first, PVECTORED_EXCEPTION_HANDLER func )
449 VECTORED_HANDLER *handler = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler) );
450 if (handler)
452 handler->func = func;
453 RtlEnterCriticalSection( &vectored_handlers_section );
454 if (first) list_add_head( &vectored_handlers, &handler->entry );
455 else list_add_tail( &vectored_handlers, &handler->entry );
456 RtlLeaveCriticalSection( &vectored_handlers_section );
458 return handler;
462 /*******************************************************************
463 * RtlRemoveVectoredExceptionHandler (NTDLL.@)
465 ULONG WINAPI RtlRemoveVectoredExceptionHandler( PVOID handler )
467 struct list *ptr;
468 ULONG ret = FALSE;
470 RtlEnterCriticalSection( &vectored_handlers_section );
471 LIST_FOR_EACH( ptr, &vectored_handlers )
473 if (ptr == &((VECTORED_HANDLER *)handler)->entry)
475 list_remove( ptr );
476 ret = TRUE;
477 break;
480 RtlLeaveCriticalSection( &vectored_handlers_section );
481 if (ret) RtlFreeHeap( GetProcessHeap(), 0, handler );
482 return ret;
486 /*************************************************************
487 * __wine_exception_handler (NTDLL.@)
489 * Exception handler for exception blocks declared in Wine code.
491 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
492 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
494 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
496 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
497 return ExceptionContinueSearch;
498 if (wine_frame->u.filter)
500 EXCEPTION_POINTERS ptrs;
501 ptrs.ExceptionRecord = record;
502 ptrs.ContextRecord = context;
503 switch(wine_frame->u.filter( &ptrs ))
505 case EXCEPTION_CONTINUE_SEARCH:
506 return ExceptionContinueSearch;
507 case EXCEPTION_CONTINUE_EXECUTION:
508 return ExceptionContinueExecution;
509 case EXCEPTION_EXECUTE_HANDLER:
510 break;
511 default:
512 MESSAGE( "Invalid return value from exception filter\n" );
513 assert( FALSE );
516 /* hack to make GetExceptionCode() work in handler */
517 wine_frame->ExceptionCode = record->ExceptionCode;
518 wine_frame->ExceptionRecord = wine_frame;
520 RtlUnwind( frame, 0, record, 0 );
521 __wine_pop_frame( frame );
522 siglongjmp( wine_frame->jmp, 1 );
526 /*************************************************************
527 * __wine_finally_handler (NTDLL.@)
529 * Exception handler for try/finally blocks declared in Wine code.
531 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
532 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
534 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
536 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
537 wine_frame->u.finally_func( FALSE );
539 return ExceptionContinueSearch;