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 * RtlRaiseException (NTDLL.464)
145 void WINAPI REGS_FUNC(RtlRaiseException)( EXCEPTION_RECORD *rec, CONTEXT *context )
147 PEXCEPTION_FRAME frame, dispatch, nested_frame;
148 EXCEPTION_RECORD newrec;
151 TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
153 if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
154 (DEBUG_SendExceptionEvent( rec, TRUE ) == DBG_CONTINUE))
155 return; /* continue execution */
157 if (debug_hook( rec, context, TRUE ) == DBG_CONTINUE)
158 return; /* continue execution */
160 frame = NtCurrentTeb()->except;
162 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
164 /* Check frame address */
165 if (((void*)frame < NtCurrentTeb()->stack_low) ||
166 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
169 rec->ExceptionFlags |= EH_STACK_INVALID;
174 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
175 if (frame == nested_frame)
177 /* no longer nested */
179 rec->ExceptionFlags &= ~EH_NESTED_CALL;
184 case ExceptionContinueExecution:
185 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
186 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
187 newrec.ExceptionFlags = EH_NONCONTINUABLE;
188 newrec.ExceptionRecord = rec;
189 newrec.NumberParameters = 0;
190 RtlRaiseException( &newrec ); /* never returns */
192 case ExceptionContinueSearch:
194 case ExceptionNestedException:
195 if (nested_frame < dispatch) nested_frame = dispatch;
196 rec->ExceptionFlags |= EH_NESTED_CALL;
199 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
200 newrec.ExceptionFlags = EH_NONCONTINUABLE;
201 newrec.ExceptionRecord = rec;
202 newrec.NumberParameters = 0;
203 RtlRaiseException( &newrec ); /* never returns */
208 EXC_DefaultHandling( rec, context );
212 /*******************************************************************
213 * RtlUnwind (KERNEL32.590) (NTDLL.518)
215 void WINAPI REGS_FUNC(RtlUnwind)( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
216 PEXCEPTION_RECORD pRecord, DWORD returnEax,
219 EXCEPTION_RECORD record, newrec;
220 PEXCEPTION_FRAME frame, dispatch;
223 context->Eax = returnEax;
226 /* build an exception record, if we do not have one */
229 record.ExceptionCode = STATUS_UNWIND;
230 record.ExceptionFlags = 0;
231 record.ExceptionRecord = NULL;
232 record.ExceptionAddress = GET_IP(context);
233 record.NumberParameters = 0;
237 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
239 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
241 /* get chain of exception frames */
242 frame = NtCurrentTeb()->except;
243 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
245 /* Check frame address */
246 if (pEndFrame && (frame > pEndFrame))
248 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
249 newrec.ExceptionFlags = EH_NONCONTINUABLE;
250 newrec.ExceptionRecord = pRecord;
251 newrec.NumberParameters = 0;
252 RtlRaiseException( &newrec ); /* never returns */
254 if (((void*)frame < NtCurrentTeb()->stack_low) ||
255 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
258 newrec.ExceptionCode = STATUS_BAD_STACK;
259 newrec.ExceptionFlags = EH_NONCONTINUABLE;
260 newrec.ExceptionRecord = pRecord;
261 newrec.NumberParameters = 0;
262 RtlRaiseException( &newrec ); /* never returns */
266 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
267 frame->Handler, EXC_UnwindHandler ))
269 case ExceptionContinueSearch:
271 case ExceptionCollidedUnwind:
275 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
276 newrec.ExceptionFlags = EH_NONCONTINUABLE;
277 newrec.ExceptionRecord = pRecord;
278 newrec.NumberParameters = 0;
279 RtlRaiseException( &newrec ); /* never returns */
282 frame = EXC_pop_frame( frame );
287 /*******************************************************************
288 * NtRaiseException (NTDLL.175)
291 * DWORD WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first );
293 void WINAPI REGS_FUNC(NtRaiseException)( EXCEPTION_RECORD *rec, CONTEXT *ctx,
294 BOOL first, CONTEXT *context )
296 REGS_FUNC(RtlRaiseException)( rec, ctx );
301 /***********************************************************************
302 * RtlRaiseStatus (NTDLL.465)
304 * Raise an exception with ExceptionCode = status
306 void WINAPI RtlRaiseStatus( NTSTATUS status )
308 EXCEPTION_RECORD ExceptionRec;
310 ExceptionRec.ExceptionCode = status;
311 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
312 ExceptionRec.ExceptionRecord = NULL;
313 ExceptionRec.NumberParameters = 0;
314 RtlRaiseException( &ExceptionRec );
318 /***********************************************************************
319 * DebugBreak (KERNEL32.181)
321 void WINAPI REGS_FUNC(DebugBreak)( CONTEXT *context )
323 EXCEPTION_RECORD rec;
325 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
326 rec.ExceptionFlags = 0;
327 rec.ExceptionRecord = NULL;
328 rec.NumberParameters = 0;
329 REGS_FUNC(RtlRaiseException)( &rec, context );
333 /***********************************************************************
334 * DebugBreak16 (KERNEL.203)
336 void WINAPI DebugBreak16( CONTEXT86 *context )
339 REGS_FUNC(DebugBreak)( context );
340 #endif /* defined(__i386__) */