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 CALLBACK 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 CALLBACK 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.
102 static DWORD EXC_CallHandler( PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler,
103 EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
104 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
106 EXC_NESTED_FRAME newframe;
109 newframe.frame.Handler = nested_handler;
110 newframe.prevFrame = frame;
111 EXC_push_frame( &newframe.frame );
112 TRACE( "calling handler at %p code=%lx flags=%lx\n",
113 handler, record->ExceptionCode, record->ExceptionFlags );
114 ret = handler( record, frame, context, dispatcher );
115 TRACE( "handler returned %lx\n", ret );
116 EXC_pop_frame( &newframe.frame );
121 /*******************************************************************
122 * EXC_DefaultHandling
124 * Default handling for exceptions. Called when we didn't find a suitable handler.
126 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
128 if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
129 (DEBUG_SendExceptionEvent( rec, FALSE ) == DBG_CONTINUE))
130 return; /* continue execution */
132 if (debug_hook( rec, context, FALSE ) == DBG_CONTINUE)
133 return; /* continue execution */
135 if (rec->ExceptionFlags & EH_STACK_INVALID)
136 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
137 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
138 ERR("Process attempted to continue execution after noncontinuable exception.\n");
140 ERR("Unhandled exception code %lx flags %lx addr %p\n",
141 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
142 TerminateProcess( GetCurrentProcess(), 1 );
146 /***********************************************************************
147 * RtlRaiseException (NTDLL.464)
149 void WINAPI REGS_FUNC(RtlRaiseException)( EXCEPTION_RECORD *rec, CONTEXT *context )
151 PEXCEPTION_FRAME frame, dispatch, nested_frame;
152 EXCEPTION_RECORD newrec;
155 TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
157 if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
158 (DEBUG_SendExceptionEvent( rec, TRUE ) == DBG_CONTINUE))
159 return; /* continue execution */
161 if (debug_hook( rec, context, TRUE ) == DBG_CONTINUE)
162 return; /* continue execution */
164 frame = NtCurrentTeb()->except;
166 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
168 /* Check frame address */
169 if (((void*)frame < NtCurrentTeb()->stack_low) ||
170 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
173 rec->ExceptionFlags |= EH_STACK_INVALID;
178 res = EXC_CallHandler( frame->Handler, EXC_RaiseHandler, rec, frame, context, &dispatch );
179 if (frame == nested_frame)
181 /* no longer nested */
183 rec->ExceptionFlags &= ~EH_NESTED_CALL;
188 case ExceptionContinueExecution:
189 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
190 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
191 newrec.ExceptionFlags = EH_NONCONTINUABLE;
192 newrec.ExceptionRecord = rec;
193 newrec.NumberParameters = 0;
194 RtlRaiseException( &newrec ); /* never returns */
196 case ExceptionContinueSearch:
198 case ExceptionNestedException:
199 if (nested_frame < dispatch) nested_frame = dispatch;
200 rec->ExceptionFlags |= EH_NESTED_CALL;
203 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
204 newrec.ExceptionFlags = EH_NONCONTINUABLE;
205 newrec.ExceptionRecord = rec;
206 newrec.NumberParameters = 0;
207 RtlRaiseException( &newrec ); /* never returns */
212 EXC_DefaultHandling( rec, context );
216 /*******************************************************************
217 * RtlUnwind (KERNEL32.590) (NTDLL.518)
219 void WINAPI REGS_FUNC(RtlUnwind)( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
220 PEXCEPTION_RECORD pRecord, DWORD returnEax,
223 EXCEPTION_RECORD record, newrec;
224 PEXCEPTION_FRAME frame, dispatch;
227 context->Eax = returnEax;
230 /* build an exception record, if we do not have one */
233 record.ExceptionCode = STATUS_UNWIND;
234 record.ExceptionFlags = 0;
235 record.ExceptionRecord = NULL;
236 record.ExceptionAddress = GET_IP(context);
237 record.NumberParameters = 0;
241 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
243 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
245 /* get chain of exception frames */
246 frame = NtCurrentTeb()->except;
247 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
249 /* Check frame address */
250 if (pEndFrame && (frame > pEndFrame))
252 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
253 newrec.ExceptionFlags = EH_NONCONTINUABLE;
254 newrec.ExceptionRecord = pRecord;
255 newrec.NumberParameters = 0;
256 RtlRaiseException( &newrec ); /* never returns */
258 if (((void*)frame < NtCurrentTeb()->stack_low) ||
259 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
262 newrec.ExceptionCode = STATUS_BAD_STACK;
263 newrec.ExceptionFlags = EH_NONCONTINUABLE;
264 newrec.ExceptionRecord = pRecord;
265 newrec.NumberParameters = 0;
266 RtlRaiseException( &newrec ); /* never returns */
270 switch(EXC_CallHandler( frame->Handler, EXC_UnwindHandler, pRecord,
271 frame, context, &dispatch ))
273 case ExceptionContinueSearch:
275 case ExceptionCollidedUnwind:
279 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
280 newrec.ExceptionFlags = EH_NONCONTINUABLE;
281 newrec.ExceptionRecord = pRecord;
282 newrec.NumberParameters = 0;
283 RtlRaiseException( &newrec ); /* never returns */
286 frame = EXC_pop_frame( frame );
291 /*******************************************************************
292 * NtRaiseException (NTDLL.175)
295 * DWORD WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first );
297 void WINAPI REGS_FUNC(NtRaiseException)( EXCEPTION_RECORD *rec, CONTEXT *ctx,
298 BOOL first, CONTEXT *context )
300 REGS_FUNC(RtlRaiseException)( rec, ctx );
305 /***********************************************************************
306 * RtlRaiseStatus (NTDLL.465)
308 * Raise an exception with ExceptionCode = status
310 void WINAPI RtlRaiseStatus( NTSTATUS status )
312 EXCEPTION_RECORD ExceptionRec;
314 ExceptionRec.ExceptionCode = status;
315 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
316 ExceptionRec.ExceptionRecord = NULL;
317 ExceptionRec.NumberParameters = 0;
318 RtlRaiseException( &ExceptionRec );
322 /***********************************************************************
323 * DebugBreak (KERNEL32.181)
325 void WINAPI REGS_FUNC(DebugBreak)( CONTEXT *context )
327 EXCEPTION_RECORD rec;
329 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
330 rec.ExceptionFlags = 0;
331 rec.ExceptionRecord = NULL;
332 rec.NumberParameters = 0;
333 REGS_FUNC(RtlRaiseException)( &rec, context );
337 /***********************************************************************
338 * DebugBreak16 (KERNEL.203)
340 void WINAPI DebugBreak16( CONTEXT86 *context )
343 REGS_FUNC(DebugBreak)( context );
344 #endif /* defined(__i386__) */