Added current context to the exception debug event sent to the server.
[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, context ) == 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  *            EXC_RtlRaiseException / RtlRaiseException (NTDLL.464)
144  */
145 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * )
146 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
147 {
148     PEXCEPTION_FRAME frame, dispatch, nested_frame;
149     EXCEPTION_RECORD newrec;
150     DWORD res;
151
152     TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
153
154     if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
155         (DEBUG_SendExceptionEvent( rec, TRUE, context ) == DBG_CONTINUE))
156         return;  /* continue execution */
157
158     if (debug_hook( rec, context, TRUE ) == DBG_CONTINUE)
159         return;  /* continue execution */
160
161     frame = NtCurrentTeb()->except;
162     nested_frame = NULL;
163     while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
164     {
165         /* Check frame address */
166         if (((void*)frame < NtCurrentTeb()->stack_low) ||
167             ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
168             (int)frame & 3)
169         {
170             rec->ExceptionFlags |= EH_STACK_INVALID;
171             break;
172         }
173
174         /* Call handler */
175         res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
176         if (frame == nested_frame)
177         {
178             /* no longer nested */
179             nested_frame = NULL;
180             rec->ExceptionFlags &= ~EH_NESTED_CALL;
181         }
182
183         switch(res)
184         {
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 */
192             break;
193         case ExceptionContinueSearch:
194             break;
195         case ExceptionNestedException:
196             if (nested_frame < dispatch) nested_frame = dispatch;
197             rec->ExceptionFlags |= EH_NESTED_CALL;
198             break;
199         default:
200             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
201             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
202             newrec.ExceptionRecord  = rec;
203             newrec.NumberParameters = 0;
204             RtlRaiseException( &newrec );  /* never returns */
205             break;
206         }
207         frame = frame->Prev;
208     }
209     EXC_DefaultHandling( rec, context );
210 }
211
212
213 /*******************************************************************
214  *         EXC_RtlUnwind / RtlUnwind  (KERNEL32.590) (NTDLL.518)
215  */
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,
220                            CONTEXT *context )
221 {
222     EXCEPTION_RECORD record, newrec;
223     PEXCEPTION_FRAME frame, dispatch;
224
225 #ifdef __i386__
226     context->Eax = returnEax;
227 #endif
228
229     /* build an exception record, if we do not have one */
230     if (!pRecord)
231     {
232         record.ExceptionCode    = STATUS_UNWIND;
233         record.ExceptionFlags   = 0;
234         record.ExceptionRecord  = NULL;
235         record.ExceptionAddress = GET_IP(context); 
236         record.NumberParameters = 0;
237         pRecord = &record;
238     }
239
240     pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
241
242     TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
243
244     /* get chain of exception frames */
245     frame = NtCurrentTeb()->except;
246     while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
247     {
248         /* Check frame address */
249         if (pEndFrame && (frame > pEndFrame))
250         {
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 */
256         }
257         if (((void*)frame < NtCurrentTeb()->stack_low) ||
258             ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
259             (int)frame & 3)
260         {
261             newrec.ExceptionCode    = STATUS_BAD_STACK;
262             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
263             newrec.ExceptionRecord  = pRecord;
264             newrec.NumberParameters = 0;
265             RtlRaiseException( &newrec );  /* never returns */
266         }
267
268         /* Call handler */
269         switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
270                                 frame->Handler, EXC_UnwindHandler ))
271         {
272         case ExceptionContinueSearch:
273             break;
274         case ExceptionCollidedUnwind:
275             frame = dispatch;
276             break;
277         default:
278             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
279             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
280             newrec.ExceptionRecord  = pRecord;
281             newrec.NumberParameters = 0;
282             RtlRaiseException( &newrec );  /* never returns */
283             break;
284         }
285         frame = EXC_pop_frame( frame );
286     }
287 }
288
289
290 /*******************************************************************
291  *            EXC_NtRaiseException / NtRaiseException  (NTDLL.175)
292  */
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 )
297 {
298     EXC_RtlRaiseException( rec, ctx );
299     *context = *ctx;
300 }
301
302
303 /***********************************************************************
304  *            RtlRaiseStatus  (NTDLL.465)
305  *
306  * Raise an exception with ExceptionCode = status
307  */
308 void WINAPI RtlRaiseStatus( NTSTATUS status )
309 {
310     EXCEPTION_RECORD ExceptionRec;
311
312     ExceptionRec.ExceptionCode    = status;
313     ExceptionRec.ExceptionFlags   = EH_NONCONTINUABLE;
314     ExceptionRec.ExceptionRecord  = NULL;
315     ExceptionRec.NumberParameters = 0;
316     RtlRaiseException( &ExceptionRec );
317 }
318
319
320 /***********************************************************************
321  *           EXC_DebugBreak / DebugBreak (KERNEL32.181)
322  */
323 DEFINE_REGS_ENTRYPOINT_0( DebugBreak, EXC_DebugBreak )
324 void WINAPI EXC_DebugBreak( CONTEXT *context )
325 {
326     EXCEPTION_RECORD rec;
327
328     rec.ExceptionCode    = EXCEPTION_BREAKPOINT;
329     rec.ExceptionFlags   = 0;
330     rec.ExceptionRecord  = NULL;
331     rec.NumberParameters = 0;
332     EXC_RtlRaiseException( &rec, context );
333 }
334
335
336 /***********************************************************************
337  *           DebugBreak16   (KERNEL.203)
338  */
339 void WINAPI DebugBreak16( CONTEXT86 *context )
340 {
341 #ifdef __i386__
342     EXC_DebugBreak( context );
343 #endif  /* defined(__i386__) */
344 }