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;
29 # define GET_IP(context) ((LPVOID)(context)->Eip)
31 # error You must define GET_IP for this CPU
34 /* Default hook for built-in debugger */
35 static DWORD default_hook( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first )
39 DPRINTF( "stopping process due to unhandled exception %08lx.\n",
43 return 0; /* not handled */
45 static DEBUGHOOK debug_hook = default_hook;
47 /*******************************************************************
48 * EXC_SetDebugEventHook
49 * EXC_GetDebugEventHook
51 * Set/Get the hook for the built-in debugger.
53 * FIXME: the built-in debugger should use the normal debug events.
55 void EXC_SetDebugEventHook( DEBUGHOOK hook )
59 DEBUGHOOK EXC_GetDebugEventHook(void)
64 /*******************************************************************
67 * Handler for exceptions happening inside a handler.
69 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
70 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
72 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
73 return ExceptionContinueSearch;
74 /* We shouldn't get here so we store faulty frame in dispatcher */
75 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
76 return ExceptionNestedException;
80 /*******************************************************************
83 * Handler for exceptions happening inside an unwind handler.
85 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
86 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
88 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
89 return ExceptionContinueSearch;
90 /* We shouldn't get here so we store faulty frame in dispatcher */
91 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
92 return ExceptionCollidedUnwind;
96 /*******************************************************************
99 * Call an exception handler, setting up an exception frame to catch exceptions
100 * happening during the handler execution.
101 * Please do not change the first 4 parameters order in any way - some exceptions handlers
102 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
104 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
105 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
106 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
108 EXC_NESTED_FRAME newframe;
111 newframe.frame.Handler = nested_handler;
112 newframe.prevFrame = frame;
113 EXC_push_frame( &newframe.frame );
114 TRACE( "calling handler at %p code=%lx flags=%lx\n",
115 handler, record->ExceptionCode, record->ExceptionFlags );
116 ret = handler( record, frame, context, dispatcher );
117 TRACE( "handler returned %lx\n", ret );
118 EXC_pop_frame( &newframe.frame );
123 /*******************************************************************
124 * EXC_DefaultHandling
126 * Default handling for exceptions. Called when we didn't find a suitable handler.
128 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
130 if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
131 (DEBUG_SendExceptionEvent( rec, FALSE ) == DBG_CONTINUE))
132 return; /* continue execution */
134 if (debug_hook( rec, context, FALSE ) == DBG_CONTINUE)
135 return; /* continue execution */
137 if (rec->ExceptionFlags & EH_STACK_INVALID)
138 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
139 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
140 ERR("Process attempted to continue execution after noncontinuable exception.\n");
142 ERR("Unhandled exception code %lx flags %lx addr %p\n",
143 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
144 TerminateProcess( GetCurrentProcess(), 1 );
148 /***********************************************************************
149 * RtlRaiseException (NTDLL.464)
151 void WINAPI REGS_FUNC(RtlRaiseException)( EXCEPTION_RECORD *rec, CONTEXT *context )
153 PEXCEPTION_FRAME frame, dispatch, nested_frame;
154 EXCEPTION_RECORD newrec;
157 TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
159 if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
160 (DEBUG_SendExceptionEvent( rec, TRUE ) == DBG_CONTINUE))
161 return; /* continue execution */
163 if (debug_hook( rec, context, TRUE ) == DBG_CONTINUE)
164 return; /* continue execution */
166 frame = NtCurrentTeb()->except;
168 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
170 /* Check frame address */
171 if (((void*)frame < NtCurrentTeb()->stack_low) ||
172 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
175 rec->ExceptionFlags |= EH_STACK_INVALID;
180 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
181 if (frame == nested_frame)
183 /* no longer nested */
185 rec->ExceptionFlags &= ~EH_NESTED_CALL;
190 case ExceptionContinueExecution:
191 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
192 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
193 newrec.ExceptionFlags = EH_NONCONTINUABLE;
194 newrec.ExceptionRecord = rec;
195 newrec.NumberParameters = 0;
196 RtlRaiseException( &newrec ); /* never returns */
198 case ExceptionContinueSearch:
200 case ExceptionNestedException:
201 if (nested_frame < dispatch) nested_frame = dispatch;
202 rec->ExceptionFlags |= EH_NESTED_CALL;
205 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
206 newrec.ExceptionFlags = EH_NONCONTINUABLE;
207 newrec.ExceptionRecord = rec;
208 newrec.NumberParameters = 0;
209 RtlRaiseException( &newrec ); /* never returns */
214 EXC_DefaultHandling( rec, context );
218 /*******************************************************************
219 * RtlUnwind (KERNEL32.590) (NTDLL.518)
221 void WINAPI REGS_FUNC(RtlUnwind)( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
222 PEXCEPTION_RECORD pRecord, DWORD returnEax,
225 EXCEPTION_RECORD record, newrec;
226 PEXCEPTION_FRAME frame, dispatch;
229 context->Eax = returnEax;
232 /* build an exception record, if we do not have one */
235 record.ExceptionCode = STATUS_UNWIND;
236 record.ExceptionFlags = 0;
237 record.ExceptionRecord = NULL;
238 record.ExceptionAddress = GET_IP(context);
239 record.NumberParameters = 0;
243 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
245 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
247 /* get chain of exception frames */
248 frame = NtCurrentTeb()->except;
249 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
251 /* Check frame address */
252 if (pEndFrame && (frame > pEndFrame))
254 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
255 newrec.ExceptionFlags = EH_NONCONTINUABLE;
256 newrec.ExceptionRecord = pRecord;
257 newrec.NumberParameters = 0;
258 RtlRaiseException( &newrec ); /* never returns */
260 if (((void*)frame < NtCurrentTeb()->stack_low) ||
261 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
264 newrec.ExceptionCode = STATUS_BAD_STACK;
265 newrec.ExceptionFlags = EH_NONCONTINUABLE;
266 newrec.ExceptionRecord = pRecord;
267 newrec.NumberParameters = 0;
268 RtlRaiseException( &newrec ); /* never returns */
272 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
273 frame->Handler, EXC_UnwindHandler ))
275 case ExceptionContinueSearch:
277 case ExceptionCollidedUnwind:
281 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
282 newrec.ExceptionFlags = EH_NONCONTINUABLE;
283 newrec.ExceptionRecord = pRecord;
284 newrec.NumberParameters = 0;
285 RtlRaiseException( &newrec ); /* never returns */
288 frame = EXC_pop_frame( frame );
293 /*******************************************************************
294 * NtRaiseException (NTDLL.175)
297 * DWORD WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first );
299 void WINAPI REGS_FUNC(NtRaiseException)( EXCEPTION_RECORD *rec, CONTEXT *ctx,
300 BOOL first, CONTEXT *context )
302 REGS_FUNC(RtlRaiseException)( rec, ctx );
307 /***********************************************************************
308 * RtlRaiseStatus (NTDLL.465)
310 * Raise an exception with ExceptionCode = status
312 void WINAPI RtlRaiseStatus( NTSTATUS status )
314 EXCEPTION_RECORD ExceptionRec;
316 ExceptionRec.ExceptionCode = status;
317 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
318 ExceptionRec.ExceptionRecord = NULL;
319 ExceptionRec.NumberParameters = 0;
320 RtlRaiseException( &ExceptionRec );
324 /***********************************************************************
325 * DebugBreak (KERNEL32.181)
327 void WINAPI REGS_FUNC(DebugBreak)( CONTEXT *context )
329 EXCEPTION_RECORD rec;
331 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
332 rec.ExceptionFlags = 0;
333 rec.ExceptionRecord = NULL;
334 rec.NumberParameters = 0;
335 REGS_FUNC(RtlRaiseException)( &rec, context );
339 /***********************************************************************
340 * DebugBreak16 (KERNEL.203)
342 void WINAPI DebugBreak16( CONTEXT86 *context )
345 REGS_FUNC(DebugBreak)( context );
346 #endif /* defined(__i386__) */