Moved GET_IP macro to winnt.h
[wine] / dlls / ntdll / exception.c
1 /*
2  * NT exception handling routines
3  * 
4  * Copyright 1999 Turchanov Sergey
5  * Copyright 1999 Alexandre Julliard
6  */
7
8 #include <signal.h>
9 #include "winnt.h"
10 #include "ntddk.h"
11 #include "process.h"
12 #include "global.h"
13 #include "wine/exception.h"
14 #include "stackframe.h"
15 #include "miscemu.h"
16 #include "debugtools.h"
17
18 DEFAULT_DEBUG_CHANNEL(seh)
19
20 /* Exception record for handling exceptions happening inside exception handlers */
21 typedef struct
22 {
23     EXCEPTION_FRAME frame;
24     EXCEPTION_FRAME *prevFrame;
25 } EXC_NESTED_FRAME;
26
27  
28 /* Default hook for built-in debugger */
29 static DWORD default_hook( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first )
30 {
31     if (!first)
32     {
33         DPRINTF( "stopping process due to unhandled exception %08lx.\n",
34                  rec->ExceptionCode );
35         raise( SIGSTOP );
36     }
37     return 0;  /* not handled */
38 }
39 static DEBUGHOOK debug_hook = default_hook;
40
41 /*******************************************************************
42  *         EXC_SetDebugEventHook
43  *         EXC_GetDebugEventHook
44  *
45  * Set/Get the hook for the built-in debugger.
46  *
47  * FIXME: the built-in debugger should use the normal debug events.
48  */
49 void EXC_SetDebugEventHook( DEBUGHOOK hook )
50 {
51     debug_hook = hook;
52 }
53 DEBUGHOOK EXC_GetDebugEventHook(void)
54 {
55     return debug_hook;
56 }
57
58 /*******************************************************************
59  *         EXC_RaiseHandler
60  *
61  * Handler for exceptions happening inside a handler.
62  */
63 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
64                                CONTEXT *context, EXCEPTION_FRAME **dispatcher )
65 {
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;
71 }
72
73
74 /*******************************************************************
75  *         EXC_UnwindHandler
76  *
77  * Handler for exceptions happening inside an unwind handler.
78  */
79 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
80                                 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
81 {
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;
87 }
88
89
90 /*******************************************************************
91  *         EXC_CallHandler
92  *
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
97  */
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)
101 {
102     EXC_NESTED_FRAME newframe;
103     DWORD ret;
104
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 );
113     return ret;
114 }
115
116
117 /*******************************************************************
118  *         EXC_DefaultHandling
119  *
120  * Default handling for exceptions. Called when we didn't find a suitable handler.
121  */
122 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
123 {
124     if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
125         (DEBUG_SendExceptionEvent( rec, FALSE ) == DBG_CONTINUE))
126         return;  /* continue execution */
127
128     if (debug_hook( rec, context, FALSE ) == DBG_CONTINUE)
129         return;  /* continue execution */
130
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");
135     else
136         ERR("Unhandled exception code %lx flags %lx addr %p\n",
137             rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
138     TerminateProcess( GetCurrentProcess(), 1 );
139 }
140
141
142 /***********************************************************************
143  *            RtlRaiseException  (NTDLL.464)
144  */
145 void WINAPI REGS_FUNC(RtlRaiseException)( EXCEPTION_RECORD *rec, CONTEXT *context )
146 {
147     PEXCEPTION_FRAME frame, dispatch, nested_frame;
148     EXCEPTION_RECORD newrec;
149     DWORD res;
150
151     TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
152
153     if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
154         (DEBUG_SendExceptionEvent( rec, TRUE ) == DBG_CONTINUE))
155         return;  /* continue execution */
156
157     if (debug_hook( rec, context, TRUE ) == DBG_CONTINUE)
158         return;  /* continue execution */
159
160     frame = NtCurrentTeb()->except;
161     nested_frame = NULL;
162     while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
163     {
164         /* Check frame address */
165         if (((void*)frame < NtCurrentTeb()->stack_low) ||
166             ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
167             (int)frame & 3)
168         {
169             rec->ExceptionFlags |= EH_STACK_INVALID;
170             break;
171         }
172
173         /* Call handler */
174         res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
175         if (frame == nested_frame)
176         {
177             /* no longer nested */
178             nested_frame = NULL;
179             rec->ExceptionFlags &= ~EH_NESTED_CALL;
180         }
181
182         switch(res)
183         {
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 */
191             break;
192         case ExceptionContinueSearch:
193             break;
194         case ExceptionNestedException:
195             if (nested_frame < dispatch) nested_frame = dispatch;
196             rec->ExceptionFlags |= EH_NESTED_CALL;
197             break;
198         default:
199             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
200             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
201             newrec.ExceptionRecord  = rec;
202             newrec.NumberParameters = 0;
203             RtlRaiseException( &newrec );  /* never returns */
204             break;
205         }
206         frame = frame->Prev;
207     }
208     EXC_DefaultHandling( rec, context );
209 }
210
211
212 /*******************************************************************
213  *         RtlUnwind  (KERNEL32.590) (NTDLL.518)
214  */
215 void WINAPI REGS_FUNC(RtlUnwind)( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip, 
216                                   PEXCEPTION_RECORD pRecord, DWORD returnEax,
217                                   CONTEXT *context )
218 {
219     EXCEPTION_RECORD record, newrec;
220     PEXCEPTION_FRAME frame, dispatch;
221
222 #ifdef __i386__
223     context->Eax = returnEax;
224 #endif
225
226     /* build an exception record, if we do not have one */
227     if (!pRecord)
228     {
229         record.ExceptionCode    = STATUS_UNWIND;
230         record.ExceptionFlags   = 0;
231         record.ExceptionRecord  = NULL;
232         record.ExceptionAddress = GET_IP(context); 
233         record.NumberParameters = 0;
234         pRecord = &record;
235     }
236
237     pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
238
239     TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
240
241     /* get chain of exception frames */
242     frame = NtCurrentTeb()->except;
243     while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
244     {
245         /* Check frame address */
246         if (pEndFrame && (frame > pEndFrame))
247         {
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 */
253         }
254         if (((void*)frame < NtCurrentTeb()->stack_low) ||
255             ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
256             (int)frame & 3)
257         {
258             newrec.ExceptionCode    = STATUS_BAD_STACK;
259             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
260             newrec.ExceptionRecord  = pRecord;
261             newrec.NumberParameters = 0;
262             RtlRaiseException( &newrec );  /* never returns */
263         }
264
265         /* Call handler */
266         switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
267                                 frame->Handler, EXC_UnwindHandler ))
268         {
269         case ExceptionContinueSearch:
270             break;
271         case ExceptionCollidedUnwind:
272             frame = dispatch;
273             break;
274         default:
275             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
276             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
277             newrec.ExceptionRecord  = pRecord;
278             newrec.NumberParameters = 0;
279             RtlRaiseException( &newrec );  /* never returns */
280             break;
281         }
282         frame = EXC_pop_frame( frame );
283     }
284 }
285
286
287 /*******************************************************************
288  *         NtRaiseException    (NTDLL.175)
289  *
290  * Real prototype:
291  *    DWORD WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first );
292  */
293 void WINAPI REGS_FUNC(NtRaiseException)( EXCEPTION_RECORD *rec, CONTEXT *ctx,
294                                          BOOL first, CONTEXT *context )
295 {
296     REGS_FUNC(RtlRaiseException)( rec, ctx );
297     *context = *ctx;
298 }
299
300
301 /***********************************************************************
302  *            RtlRaiseStatus  (NTDLL.465)
303  *
304  * Raise an exception with ExceptionCode = status
305  */
306 void WINAPI RtlRaiseStatus( NTSTATUS status )
307 {
308     EXCEPTION_RECORD ExceptionRec;
309
310     ExceptionRec.ExceptionCode    = status;
311     ExceptionRec.ExceptionFlags   = EH_NONCONTINUABLE;
312     ExceptionRec.ExceptionRecord  = NULL;
313     ExceptionRec.NumberParameters = 0;
314     RtlRaiseException( &ExceptionRec );
315 }
316
317
318 /***********************************************************************
319  *           DebugBreak   (KERNEL32.181)
320  */
321 void WINAPI REGS_FUNC(DebugBreak)( CONTEXT *context )
322 {
323     EXCEPTION_RECORD rec;
324
325     rec.ExceptionCode    = EXCEPTION_BREAKPOINT;
326     rec.ExceptionFlags   = 0;
327     rec.ExceptionRecord  = NULL;
328     rec.NumberParameters = 0;
329     REGS_FUNC(RtlRaiseException)( &rec, context );
330 }
331
332
333 /***********************************************************************
334  *           DebugBreak16   (KERNEL.203)
335  */
336 void WINAPI DebugBreak16( CONTEXT86 *context )
337 {
338 #ifdef __i386__
339     REGS_FUNC(DebugBreak)( context );
340 #endif  /* defined(__i386__) */
341 }