2 * NT exception handling routines
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
13 #include "wine/exception.h"
14 #include "stackframe.h"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(seh)
20 /* Exception record for handling exceptions happening inside exception handlers */
23 EXCEPTION_FRAME frame;
24 EXCEPTION_FRAME *prevFrame;
28 /* Default hook for built-in debugger */
29 static DWORD default_hook( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first )
33 DPRINTF( "stopping process due to unhandled exception %08lx.\n",
37 return 0; /* not handled */
39 static DEBUGHOOK debug_hook = default_hook;
41 /*******************************************************************
42 * EXC_SetDebugEventHook
43 * EXC_GetDebugEventHook
45 * Set/Get the hook for the built-in debugger.
47 * FIXME: the built-in debugger should use the normal debug events.
49 void EXC_SetDebugEventHook( DEBUGHOOK hook )
53 DEBUGHOOK EXC_GetDebugEventHook(void)
58 /*******************************************************************
61 * Handler for exceptions happening inside a handler.
63 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
64 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
66 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
67 return ExceptionContinueSearch;
68 /* We shouldn't get here so we store faulty frame in dispatcher */
69 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
70 return ExceptionNestedException;
74 /*******************************************************************
77 * Handler for exceptions happening inside an unwind handler.
79 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
80 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
82 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
83 return ExceptionContinueSearch;
84 /* We shouldn't get here so we store faulty frame in dispatcher */
85 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
86 return ExceptionCollidedUnwind;
90 /*******************************************************************
93 * Call an exception handler, setting up an exception frame to catch exceptions
94 * happening during the handler execution.
95 * Please do not change the first 4 parameters order in any way - some exceptions handlers
96 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
98 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
99 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
100 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
102 EXC_NESTED_FRAME newframe;
105 newframe.frame.Handler = nested_handler;
106 newframe.prevFrame = frame;
107 EXC_push_frame( &newframe.frame );
108 TRACE( "calling handler at %p code=%lx flags=%lx\n",
109 handler, record->ExceptionCode, record->ExceptionFlags );
110 ret = handler( record, frame, context, dispatcher );
111 TRACE( "handler returned %lx\n", ret );
112 EXC_pop_frame( &newframe.frame );
117 /*******************************************************************
118 * EXC_DefaultHandling
120 * Default handling for exceptions. Called when we didn't find a suitable handler.
122 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
124 if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
125 (DEBUG_SendExceptionEvent( rec, FALSE ) == DBG_CONTINUE))
126 return; /* continue execution */
128 if (debug_hook( rec, context, FALSE ) == DBG_CONTINUE)
129 return; /* continue execution */
131 if (rec->ExceptionFlags & EH_STACK_INVALID)
132 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
133 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
134 ERR("Process attempted to continue execution after noncontinuable exception.\n");
136 ERR("Unhandled exception code %lx flags %lx addr %p\n",
137 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
138 TerminateProcess( GetCurrentProcess(), 1 );
142 /***********************************************************************
143 * EXC_RtlRaiseException / RtlRaiseException (NTDLL.464)
145 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * )
146 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
148 PEXCEPTION_FRAME frame, dispatch, nested_frame;
149 EXCEPTION_RECORD newrec;
152 TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
154 if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
155 (DEBUG_SendExceptionEvent( rec, TRUE ) == DBG_CONTINUE))
156 return; /* continue execution */
158 if (debug_hook( rec, context, TRUE ) == DBG_CONTINUE)
159 return; /* continue execution */
161 frame = NtCurrentTeb()->except;
163 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
165 /* Check frame address */
166 if (((void*)frame < NtCurrentTeb()->stack_low) ||
167 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
170 rec->ExceptionFlags |= EH_STACK_INVALID;
175 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
176 if (frame == nested_frame)
178 /* no longer nested */
180 rec->ExceptionFlags &= ~EH_NESTED_CALL;
185 case ExceptionContinueExecution:
186 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
187 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
188 newrec.ExceptionFlags = EH_NONCONTINUABLE;
189 newrec.ExceptionRecord = rec;
190 newrec.NumberParameters = 0;
191 RtlRaiseException( &newrec ); /* never returns */
193 case ExceptionContinueSearch:
195 case ExceptionNestedException:
196 if (nested_frame < dispatch) nested_frame = dispatch;
197 rec->ExceptionFlags |= EH_NESTED_CALL;
200 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
201 newrec.ExceptionFlags = EH_NONCONTINUABLE;
202 newrec.ExceptionRecord = rec;
203 newrec.NumberParameters = 0;
204 RtlRaiseException( &newrec ); /* never returns */
209 EXC_DefaultHandling( rec, context );
213 /*******************************************************************
214 * EXC_RtlUnwind / RtlUnwind (KERNEL32.590) (NTDLL.518)
216 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
217 PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD )
218 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
219 PEXCEPTION_RECORD pRecord, DWORD returnEax,
222 EXCEPTION_RECORD record, newrec;
223 PEXCEPTION_FRAME frame, dispatch;
226 context->Eax = returnEax;
229 /* build an exception record, if we do not have one */
232 record.ExceptionCode = STATUS_UNWIND;
233 record.ExceptionFlags = 0;
234 record.ExceptionRecord = NULL;
235 record.ExceptionAddress = GET_IP(context);
236 record.NumberParameters = 0;
240 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
242 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
244 /* get chain of exception frames */
245 frame = NtCurrentTeb()->except;
246 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
248 /* Check frame address */
249 if (pEndFrame && (frame > pEndFrame))
251 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
252 newrec.ExceptionFlags = EH_NONCONTINUABLE;
253 newrec.ExceptionRecord = pRecord;
254 newrec.NumberParameters = 0;
255 RtlRaiseException( &newrec ); /* never returns */
257 if (((void*)frame < NtCurrentTeb()->stack_low) ||
258 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
261 newrec.ExceptionCode = STATUS_BAD_STACK;
262 newrec.ExceptionFlags = EH_NONCONTINUABLE;
263 newrec.ExceptionRecord = pRecord;
264 newrec.NumberParameters = 0;
265 RtlRaiseException( &newrec ); /* never returns */
269 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
270 frame->Handler, EXC_UnwindHandler ))
272 case ExceptionContinueSearch:
274 case ExceptionCollidedUnwind:
278 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
279 newrec.ExceptionFlags = EH_NONCONTINUABLE;
280 newrec.ExceptionRecord = pRecord;
281 newrec.NumberParameters = 0;
282 RtlRaiseException( &newrec ); /* never returns */
285 frame = EXC_pop_frame( frame );
290 /*******************************************************************
291 * EXC_NtRaiseException / NtRaiseException (NTDLL.175)
293 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
294 EXCEPTION_RECORD *, CONTEXT *, BOOL )
295 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
296 BOOL first, CONTEXT *context )
298 EXC_RtlRaiseException( rec, ctx );
303 /***********************************************************************
304 * RtlRaiseStatus (NTDLL.465)
306 * Raise an exception with ExceptionCode = status
308 void WINAPI RtlRaiseStatus( NTSTATUS status )
310 EXCEPTION_RECORD ExceptionRec;
312 ExceptionRec.ExceptionCode = status;
313 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
314 ExceptionRec.ExceptionRecord = NULL;
315 ExceptionRec.NumberParameters = 0;
316 RtlRaiseException( &ExceptionRec );
320 /***********************************************************************
321 * EXC_DebugBreak / DebugBreak (KERNEL32.181)
323 DEFINE_REGS_ENTRYPOINT_0( DebugBreak, EXC_DebugBreak )
324 void WINAPI EXC_DebugBreak( CONTEXT *context )
326 EXCEPTION_RECORD rec;
328 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
329 rec.ExceptionFlags = 0;
330 rec.ExceptionRecord = NULL;
331 rec.NumberParameters = 0;
332 EXC_RtlRaiseException( &rec, context );
336 /***********************************************************************
337 * DebugBreak16 (KERNEL.203)
339 void WINAPI DebugBreak16( CONTEXT86 *context )
342 EXC_DebugBreak( context );
343 #endif /* defined(__i386__) */