Print dll name too for stub exceptions.
[wine] / dlls / ntdll / exception.c
1 /*
2  * NT exception handling routines
3  *
4  * Copyright 1999 Turchanov Sergey
5  * Copyright 1999 Alexandre Julliard
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <signal.h>
27
28 #include "windef.h"
29 #include "winternl.h"
30 #include "global.h"
31 #include "wine/exception.h"
32 #include "stackframe.h"
33 #include "miscemu.h"
34 #include "wine/server.h"
35 #include "wine/debug.h"
36 #include "excpt.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(seh);
39
40 /* Exception record for handling exceptions happening inside exception handlers */
41 typedef struct
42 {
43     EXCEPTION_FRAME frame;
44     EXCEPTION_FRAME *prevFrame;
45 } EXC_NESTED_FRAME;
46
47 #ifdef __i386__
48 # define GET_IP(context) ((LPVOID)(context)->Eip)
49 #elif defined(__sparc__)
50 # define GET_IP(context) ((LPVOID)(context)->pc)
51 #elif defined(__powerpc__)
52 # define GET_IP(context) ((LPVOID)(context)->Iar)
53 #else
54 # error You must define GET_IP for this CPU
55 #endif
56
57 extern void SIGNAL_Unblock(void);
58
59 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
60 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME, LPVOID,
61                            PEXCEPTION_RECORD, DWORD, PCONTEXT );
62 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
63                                   BOOL, PCONTEXT );
64
65 /*******************************************************************
66  *         EXC_RaiseHandler
67  *
68  * Handler for exceptions happening inside a handler.
69  */
70 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
71                                CONTEXT *context, EXCEPTION_FRAME **dispatcher )
72 {
73     if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
74         return ExceptionContinueSearch;
75     /* We shouldn't get here so we store faulty frame in dispatcher */
76     *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
77     return ExceptionNestedException;
78 }
79
80
81 /*******************************************************************
82  *         EXC_UnwindHandler
83  *
84  * Handler for exceptions happening inside an unwind handler.
85  */
86 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
87                                 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
88 {
89     if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
90         return ExceptionContinueSearch;
91     /* We shouldn't get here so we store faulty frame in dispatcher */
92     *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
93     return ExceptionCollidedUnwind;
94 }
95
96
97 /*******************************************************************
98  *         EXC_CallHandler
99  *
100  * Call an exception handler, setting up an exception frame to catch exceptions
101  * happening during the handler execution.
102  * Please do not change the first 4 parameters order in any way - some exceptions handlers
103  * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
104  */
105 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
106                               CONTEXT *context, EXCEPTION_FRAME **dispatcher,
107                               PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
108 {
109     EXC_NESTED_FRAME newframe;
110     DWORD ret;
111
112     newframe.frame.Handler = nested_handler;
113     newframe.prevFrame     = frame;
114     __wine_push_frame( &newframe.frame );
115     TRACE( "calling handler at %p code=%lx flags=%lx\n",
116            handler, record->ExceptionCode, record->ExceptionFlags );
117     ret = handler( record, frame, context, dispatcher );
118     TRACE( "handler returned %lx\n", ret );
119     __wine_pop_frame( &newframe.frame );
120     return ret;
121 }
122
123
124 /**********************************************************************
125  *           send_debug_event
126  *
127  * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
128  */
129 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
130 {
131     int ret;
132     HANDLE handle = 0;
133
134     SERVER_START_REQ( queue_exception_event )
135     {
136         req->first   = first_chance;
137         wine_server_add_data( req, context, sizeof(*context) );
138         wine_server_add_data( req, rec, sizeof(*rec) );
139         if (!wine_server_call( req )) handle = reply->handle;
140     }
141     SERVER_END_REQ;
142     if (!handle) return 0;  /* no debugger present or other error */
143
144     /* No need to wait on the handle since the process gets suspended
145      * once the event is passed to the debugger, so when we get back
146      * here the event has been continued already.
147      */
148     SERVER_START_REQ( get_exception_status )
149     {
150         req->handle = handle;
151         wine_server_set_reply( req, context, sizeof(*context) );
152         wine_server_call( req );
153         ret = reply->status;
154     }
155     SERVER_END_REQ;
156     NtClose( handle );
157     return ret;
158 }
159
160
161 /*******************************************************************
162  *         EXC_DefaultHandling
163  *
164  * Default handling for exceptions. Called when we didn't find a suitable handler.
165  */
166 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
167 {
168     if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return;  /* continue execution */
169
170     if (rec->ExceptionFlags & EH_STACK_INVALID)
171         ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
172     else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
173         ERR("Process attempted to continue execution after noncontinuable exception.\n");
174     else
175         ERR("Unhandled exception code %lx flags %lx addr %p\n",
176             rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
177     NtTerminateProcess( NtCurrentProcess(), 1 );
178 }
179
180
181 /***********************************************************************
182  *              RtlRaiseException (NTDLL.@)
183  */
184 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * );
185 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
186 {
187     PEXCEPTION_FRAME frame, dispatch, nested_frame;
188     EXCEPTION_RECORD newrec;
189     DWORD res, c;
190
191     TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
192     for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
193     if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
194         FIXME( "call to unimplemented function %s.%s\n",
195                (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
196
197     if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return;  /* continue execution */
198
199     SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
200
201     frame = NtCurrentTeb()->except;
202     nested_frame = NULL;
203     while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
204     {
205         /* Check frame address */
206         if (((void*)frame < NtCurrentTeb()->stack_low) ||
207             ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
208             (int)frame & 3)
209         {
210             rec->ExceptionFlags |= EH_STACK_INVALID;
211             break;
212         }
213
214         /* Call handler */
215         res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
216         if (frame == nested_frame)
217         {
218             /* no longer nested */
219             nested_frame = NULL;
220             rec->ExceptionFlags &= ~EH_NESTED_CALL;
221         }
222
223         switch(res)
224         {
225         case ExceptionContinueExecution:
226             if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
227             newrec.ExceptionCode    = STATUS_NONCONTINUABLE_EXCEPTION;
228             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
229             newrec.ExceptionRecord  = rec;
230             newrec.NumberParameters = 0;
231             RtlRaiseException( &newrec );  /* never returns */
232             break;
233         case ExceptionContinueSearch:
234             break;
235         case ExceptionNestedException:
236             if (nested_frame < dispatch) nested_frame = dispatch;
237             rec->ExceptionFlags |= EH_NESTED_CALL;
238             break;
239         default:
240             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
241             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
242             newrec.ExceptionRecord  = rec;
243             newrec.NumberParameters = 0;
244             RtlRaiseException( &newrec );  /* never returns */
245             break;
246         }
247         frame = frame->Prev;
248     }
249     EXC_DefaultHandling( rec, context );
250 }
251
252
253 /*******************************************************************
254  *              RtlUnwind (NTDLL.@)
255  */
256 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
257                           PVOID, PVOID, PEXCEPTION_RECORD, PVOID );
258 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
259                            PEXCEPTION_RECORD pRecord, DWORD returnEax,
260                            CONTEXT *context )
261 {
262     EXCEPTION_RECORD record, newrec;
263     PEXCEPTION_FRAME frame, dispatch;
264
265 #ifdef __i386__
266     context->Eax = returnEax;
267 #endif
268
269     /* build an exception record, if we do not have one */
270     if (!pRecord)
271     {
272         record.ExceptionCode    = STATUS_UNWIND;
273         record.ExceptionFlags   = 0;
274         record.ExceptionRecord  = NULL;
275         record.ExceptionAddress = GET_IP(context);
276         record.NumberParameters = 0;
277         pRecord = &record;
278     }
279
280     pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
281
282     TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
283
284     /* get chain of exception frames */
285     frame = NtCurrentTeb()->except;
286     while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
287     {
288         /* Check frame address */
289         if (pEndFrame && (frame > pEndFrame))
290         {
291             newrec.ExceptionCode    = STATUS_INVALID_UNWIND_TARGET;
292             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
293             newrec.ExceptionRecord  = pRecord;
294             newrec.NumberParameters = 0;
295             RtlRaiseException( &newrec );  /* never returns */
296         }
297         if (((void*)frame < NtCurrentTeb()->stack_low) ||
298             ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
299             (int)frame & 3)
300         {
301             newrec.ExceptionCode    = STATUS_BAD_STACK;
302             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
303             newrec.ExceptionRecord  = pRecord;
304             newrec.NumberParameters = 0;
305             RtlRaiseException( &newrec );  /* never returns */
306         }
307
308         /* Call handler */
309         switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
310                                 frame->Handler, EXC_UnwindHandler ))
311         {
312         case ExceptionContinueSearch:
313             break;
314         case ExceptionCollidedUnwind:
315             frame = dispatch;
316             break;
317         default:
318             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
319             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
320             newrec.ExceptionRecord  = pRecord;
321             newrec.NumberParameters = 0;
322             RtlRaiseException( &newrec );  /* never returns */
323             break;
324         }
325         frame = __wine_pop_frame( frame );
326     }
327 }
328
329
330 /*******************************************************************
331  *              NtRaiseException (NTDLL.@)
332  */
333 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
334                           EXCEPTION_RECORD *, CONTEXT *, BOOL );
335 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
336                                   BOOL first, CONTEXT *context )
337 {
338     EXC_RtlRaiseException( rec, ctx );
339     *context = *ctx;
340 }
341
342
343 /***********************************************************************
344  *            RtlRaiseStatus  (NTDLL.@)
345  *
346  * Raise an exception with ExceptionCode = status
347  */
348 void WINAPI RtlRaiseStatus( NTSTATUS status )
349 {
350     EXCEPTION_RECORD ExceptionRec;
351
352     ExceptionRec.ExceptionCode    = status;
353     ExceptionRec.ExceptionFlags   = EH_NONCONTINUABLE;
354     ExceptionRec.ExceptionRecord  = NULL;
355     ExceptionRec.NumberParameters = 0;
356     RtlRaiseException( &ExceptionRec );
357 }
358
359
360 /*************************************************************
361  *            __wine_exception_handler (NTDLL.@)
362  *
363  * Exception handler for exception blocks declared in Wine code.
364  */
365 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
366                                 CONTEXT *context, LPVOID pdispatcher )
367 {
368     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
369
370     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
371         return ExceptionContinueSearch;
372     if (wine_frame->u.filter)
373     {
374         EXCEPTION_POINTERS ptrs;
375         ptrs.ExceptionRecord = record;
376         ptrs.ContextRecord = context;
377         switch(wine_frame->u.filter( &ptrs ))
378         {
379         case EXCEPTION_CONTINUE_SEARCH:
380             return ExceptionContinueSearch;
381         case EXCEPTION_CONTINUE_EXECUTION:
382             return ExceptionContinueExecution;
383         case EXCEPTION_EXECUTE_HANDLER:
384             break;
385         default:
386             MESSAGE( "Invalid return value from exception filter\n" );
387             assert( FALSE );
388         }
389     }
390     /* hack to make GetExceptionCode() work in handler */
391     wine_frame->ExceptionCode   = record->ExceptionCode;
392     wine_frame->ExceptionRecord = wine_frame;
393
394     RtlUnwind( frame, 0, record, 0 );
395     __wine_pop_frame( frame );
396     longjmp( wine_frame->jmp, 1 );
397 }
398
399
400 /*************************************************************
401  *            __wine_finally_handler (NTDLL.@)
402  *
403  * Exception handler for try/finally blocks declared in Wine code.
404  */
405 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
406                               CONTEXT *context, LPVOID pdispatcher )
407 {
408     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
409     {
410         __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
411         wine_frame->u.finally_func( FALSE );
412     }
413     return ExceptionContinueSearch;
414 }
415
416
417 /*************************************************************
418  *            __wine_callto16_handler
419  *
420  * Handler for exceptions occuring in 16-bit code.
421  */
422 DWORD __wine_callto16_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
423                                CONTEXT *context, LPVOID pdispatcher )
424 {
425     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
426     {
427         /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
428         STACK32FRAME *frame32 = (STACK32FRAME *)((char *)frame - offsetof(STACK32FRAME,frame));
429         NtCurrentTeb()->cur_stack = frame32->frame16;
430         _LeaveWin16Lock();
431     }
432     return ExceptionContinueSearch;
433 }