2 * NT exception handling routines
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
16 #include "wine/exception.h"
17 #include "stackframe.h"
19 #include "wine/server.h"
20 #include "debugtools.h"
21 #include "msvcrt/excpt.h"
23 DEFAULT_DEBUG_CHANNEL(seh);
25 /* Exception record for handling exceptions happening inside exception handlers */
28 EXCEPTION_FRAME frame;
29 EXCEPTION_FRAME *prevFrame;
33 # define GET_IP(context) ((LPVOID)(context)->Eip)
36 # define GET_IP(context) ((LPVOID)(context)->pc)
39 # error You must define GET_IP for this CPU
42 extern void SIGNAL_Unblock(void);
44 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
45 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME, LPVOID,
46 PEXCEPTION_RECORD, DWORD, PCONTEXT );
47 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
50 /*******************************************************************
53 * Handler for exceptions happening inside a handler.
55 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
56 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
58 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
59 return ExceptionContinueSearch;
60 /* We shouldn't get here so we store faulty frame in dispatcher */
61 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
62 return ExceptionNestedException;
66 /*******************************************************************
69 * Handler for exceptions happening inside an unwind handler.
71 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
72 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
74 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
75 return ExceptionContinueSearch;
76 /* We shouldn't get here so we store faulty frame in dispatcher */
77 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
78 return ExceptionCollidedUnwind;
82 /*******************************************************************
85 * Call an exception handler, setting up an exception frame to catch exceptions
86 * happening during the handler execution.
87 * Please do not change the first 4 parameters order in any way - some exceptions handlers
88 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
90 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
91 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
92 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
94 EXC_NESTED_FRAME newframe;
97 newframe.frame.Handler = nested_handler;
98 newframe.prevFrame = frame;
99 __wine_push_frame( &newframe.frame );
100 TRACE( "calling handler at %p code=%lx flags=%lx\n",
101 handler, record->ExceptionCode, record->ExceptionFlags );
102 ret = handler( record, frame, context, dispatcher );
103 TRACE( "handler returned %lx\n", ret );
104 __wine_pop_frame( &newframe.frame );
109 /**********************************************************************
112 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
114 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
119 SERVER_START_REQ( queue_exception_event )
121 req->first = first_chance;
122 wine_server_add_data( req, context, sizeof(*context) );
123 wine_server_add_data( req, rec, sizeof(*rec) );
124 if (!wine_server_call( req )) handle = reply->handle;
127 if (!handle) return 0; /* no debugger present or other error */
129 /* No need to wait on the handle since the process gets suspended
130 * once the event is passed to the debugger, so when we get back
131 * here the event has been continued already.
133 SERVER_START_REQ( get_exception_status )
135 req->handle = handle;
136 wine_server_set_reply( req, context, sizeof(*context) );
137 wine_server_call( req );
146 /*******************************************************************
147 * EXC_DefaultHandling
149 * Default handling for exceptions. Called when we didn't find a suitable handler.
151 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
153 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
155 if (rec->ExceptionFlags & EH_STACK_INVALID)
156 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
157 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
158 ERR("Process attempted to continue execution after noncontinuable exception.\n");
160 ERR("Unhandled exception code %lx flags %lx addr %p\n",
161 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
162 NtTerminateProcess( NtCurrentProcess(), 1 );
166 /***********************************************************************
167 * RtlRaiseException (NTDLL.@)
169 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * );
170 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
172 PEXCEPTION_FRAME frame, dispatch, nested_frame;
173 EXCEPTION_RECORD newrec;
176 TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
177 for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
178 if (rec->ExceptionCode == EXCEPTION_WINE_STUB) TRACE(" stub=%s\n", (char*)rec->ExceptionInformation[1]);
180 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
182 SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
184 frame = NtCurrentTeb()->except;
186 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
188 /* Check frame address */
189 if (((void*)frame < NtCurrentTeb()->stack_low) ||
190 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
193 rec->ExceptionFlags |= EH_STACK_INVALID;
198 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
199 if (frame == nested_frame)
201 /* no longer nested */
203 rec->ExceptionFlags &= ~EH_NESTED_CALL;
208 case ExceptionContinueExecution:
209 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
210 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
211 newrec.ExceptionFlags = EH_NONCONTINUABLE;
212 newrec.ExceptionRecord = rec;
213 newrec.NumberParameters = 0;
214 RtlRaiseException( &newrec ); /* never returns */
216 case ExceptionContinueSearch:
218 case ExceptionNestedException:
219 if (nested_frame < dispatch) nested_frame = dispatch;
220 rec->ExceptionFlags |= EH_NESTED_CALL;
223 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
224 newrec.ExceptionFlags = EH_NONCONTINUABLE;
225 newrec.ExceptionRecord = rec;
226 newrec.NumberParameters = 0;
227 RtlRaiseException( &newrec ); /* never returns */
232 EXC_DefaultHandling( rec, context );
236 /*******************************************************************
237 * RtlUnwind (NTDLL.@)
239 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
240 PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD );
241 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
242 PEXCEPTION_RECORD pRecord, DWORD returnEax,
245 EXCEPTION_RECORD record, newrec;
246 PEXCEPTION_FRAME frame, dispatch;
249 context->Eax = returnEax;
252 /* build an exception record, if we do not have one */
255 record.ExceptionCode = STATUS_UNWIND;
256 record.ExceptionFlags = 0;
257 record.ExceptionRecord = NULL;
258 record.ExceptionAddress = GET_IP(context);
259 record.NumberParameters = 0;
263 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
265 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
267 /* get chain of exception frames */
268 frame = NtCurrentTeb()->except;
269 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
271 /* Check frame address */
272 if (pEndFrame && (frame > pEndFrame))
274 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
275 newrec.ExceptionFlags = EH_NONCONTINUABLE;
276 newrec.ExceptionRecord = pRecord;
277 newrec.NumberParameters = 0;
278 RtlRaiseException( &newrec ); /* never returns */
280 if (((void*)frame < NtCurrentTeb()->stack_low) ||
281 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
284 newrec.ExceptionCode = STATUS_BAD_STACK;
285 newrec.ExceptionFlags = EH_NONCONTINUABLE;
286 newrec.ExceptionRecord = pRecord;
287 newrec.NumberParameters = 0;
288 RtlRaiseException( &newrec ); /* never returns */
292 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
293 frame->Handler, EXC_UnwindHandler ))
295 case ExceptionContinueSearch:
297 case ExceptionCollidedUnwind:
301 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
302 newrec.ExceptionFlags = EH_NONCONTINUABLE;
303 newrec.ExceptionRecord = pRecord;
304 newrec.NumberParameters = 0;
305 RtlRaiseException( &newrec ); /* never returns */
308 frame = __wine_pop_frame( frame );
313 /*******************************************************************
314 * NtRaiseException (NTDLL.@)
316 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
317 EXCEPTION_RECORD *, CONTEXT *, BOOL );
318 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
319 BOOL first, CONTEXT *context )
321 EXC_RtlRaiseException( rec, ctx );
326 /***********************************************************************
327 * RtlRaiseStatus (NTDLL.@)
329 * Raise an exception with ExceptionCode = status
331 void WINAPI RtlRaiseStatus( NTSTATUS status )
333 EXCEPTION_RECORD ExceptionRec;
335 ExceptionRec.ExceptionCode = status;
336 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
337 ExceptionRec.ExceptionRecord = NULL;
338 ExceptionRec.NumberParameters = 0;
339 RtlRaiseException( &ExceptionRec );
343 /*************************************************************
344 * __wine_exception_handler (NTDLL.@)
346 * Exception handler for exception blocks declared in Wine code.
348 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
349 CONTEXT *context, LPVOID pdispatcher )
351 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
353 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
354 return ExceptionContinueSearch;
355 if (wine_frame->u.filter)
357 EXCEPTION_POINTERS ptrs;
358 ptrs.ExceptionRecord = record;
359 ptrs.ContextRecord = context;
360 switch(wine_frame->u.filter( &ptrs ))
362 case EXCEPTION_CONTINUE_SEARCH:
363 return ExceptionContinueSearch;
364 case EXCEPTION_CONTINUE_EXECUTION:
365 return ExceptionContinueExecution;
366 case EXCEPTION_EXECUTE_HANDLER:
369 MESSAGE( "Invalid return value from exception filter\n" );
373 /* hack to make GetExceptionCode() work in handler */
374 wine_frame->ExceptionCode = record->ExceptionCode;
375 wine_frame->ExceptionRecord = wine_frame;
377 RtlUnwind( frame, 0, record, 0 );
378 __wine_pop_frame( frame );
379 longjmp( wine_frame->jmp, 1 );
383 /*************************************************************
384 * __wine_finally_handler (NTDLL.@)
386 * Exception handler for try/finally blocks declared in Wine code.
388 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
389 CONTEXT *context, LPVOID pdispatcher )
391 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
393 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
394 return ExceptionContinueSearch;
395 wine_frame->u.finally_func( FALSE );
396 return ExceptionContinueSearch;