Add check for <sys/ioctl.h>.
[wine/multimedia.git] / dlls / ntdll / exception.c
blobf936b77a2582c58e377a173062c94e5a03907f17
1 /*
2 * NT exception handling routines
3 *
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <signal.h>
28 #include "winnt.h"
29 #include "ntddk.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 "msvcrt/excpt.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(seh);
40 /* Exception record for handling exceptions happening inside exception handlers */
41 typedef struct
43 EXCEPTION_FRAME frame;
44 EXCEPTION_FRAME *prevFrame;
45 } EXC_NESTED_FRAME;
47 #ifdef __i386__
48 # define GET_IP(context) ((LPVOID)(context)->Eip)
49 #endif
50 #ifdef __sparc__
51 # define GET_IP(context) ((LPVOID)(context)->pc)
52 #endif
53 #ifndef GET_IP
54 # error You must define GET_IP for this CPU
55 #endif
57 extern void SIGNAL_Unblock(void);
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 );
65 /*******************************************************************
66 * EXC_RaiseHandler
68 * Handler for exceptions happening inside a handler.
70 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
71 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
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;
81 /*******************************************************************
82 * EXC_UnwindHandler
84 * Handler for exceptions happening inside an unwind handler.
86 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
87 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
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;
97 /*******************************************************************
98 * EXC_CallHandler
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
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)
109 EXC_NESTED_FRAME newframe;
110 DWORD ret;
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;
124 /**********************************************************************
125 * send_debug_event
127 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
129 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
131 int ret;
132 HANDLE handle = 0;
134 SERVER_START_REQ( queue_exception_event )
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;
141 SERVER_END_REQ;
142 if (!handle) return 0; /* no debugger present or other error */
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.
148 SERVER_START_REQ( get_exception_status )
150 req->handle = handle;
151 wine_server_set_reply( req, context, sizeof(*context) );
152 wine_server_call( req );
153 ret = reply->status;
155 SERVER_END_REQ;
156 NtClose( handle );
157 return ret;
161 /*******************************************************************
162 * EXC_DefaultHandling
164 * Default handling for exceptions. Called when we didn't find a suitable handler.
166 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
168 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
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 );
181 /***********************************************************************
182 * RtlRaiseException (NTDLL.@)
184 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * );
185 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
187 PEXCEPTION_FRAME frame, dispatch, nested_frame;
188 EXCEPTION_RECORD newrec;
189 DWORD res, c;
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) TRACE(" stub=%s\n", (char*)rec->ExceptionInformation[1]);
195 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
197 SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
199 frame = NtCurrentTeb()->except;
200 nested_frame = NULL;
201 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
203 /* Check frame address */
204 if (((void*)frame < NtCurrentTeb()->stack_low) ||
205 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
206 (int)frame & 3)
208 rec->ExceptionFlags |= EH_STACK_INVALID;
209 break;
212 /* Call handler */
213 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
214 if (frame == nested_frame)
216 /* no longer nested */
217 nested_frame = NULL;
218 rec->ExceptionFlags &= ~EH_NESTED_CALL;
221 switch(res)
223 case ExceptionContinueExecution:
224 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
225 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
226 newrec.ExceptionFlags = EH_NONCONTINUABLE;
227 newrec.ExceptionRecord = rec;
228 newrec.NumberParameters = 0;
229 RtlRaiseException( &newrec ); /* never returns */
230 break;
231 case ExceptionContinueSearch:
232 break;
233 case ExceptionNestedException:
234 if (nested_frame < dispatch) nested_frame = dispatch;
235 rec->ExceptionFlags |= EH_NESTED_CALL;
236 break;
237 default:
238 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
239 newrec.ExceptionFlags = EH_NONCONTINUABLE;
240 newrec.ExceptionRecord = rec;
241 newrec.NumberParameters = 0;
242 RtlRaiseException( &newrec ); /* never returns */
243 break;
245 frame = frame->Prev;
247 EXC_DefaultHandling( rec, context );
251 /*******************************************************************
252 * RtlUnwind (NTDLL.@)
254 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
255 PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD );
256 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
257 PEXCEPTION_RECORD pRecord, DWORD returnEax,
258 CONTEXT *context )
260 EXCEPTION_RECORD record, newrec;
261 PEXCEPTION_FRAME frame, dispatch;
263 #ifdef __i386__
264 context->Eax = returnEax;
265 #endif
267 /* build an exception record, if we do not have one */
268 if (!pRecord)
270 record.ExceptionCode = STATUS_UNWIND;
271 record.ExceptionFlags = 0;
272 record.ExceptionRecord = NULL;
273 record.ExceptionAddress = GET_IP(context);
274 record.NumberParameters = 0;
275 pRecord = &record;
278 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
280 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
282 /* get chain of exception frames */
283 frame = NtCurrentTeb()->except;
284 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
286 /* Check frame address */
287 if (pEndFrame && (frame > pEndFrame))
289 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
290 newrec.ExceptionFlags = EH_NONCONTINUABLE;
291 newrec.ExceptionRecord = pRecord;
292 newrec.NumberParameters = 0;
293 RtlRaiseException( &newrec ); /* never returns */
295 if (((void*)frame < NtCurrentTeb()->stack_low) ||
296 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
297 (int)frame & 3)
299 newrec.ExceptionCode = STATUS_BAD_STACK;
300 newrec.ExceptionFlags = EH_NONCONTINUABLE;
301 newrec.ExceptionRecord = pRecord;
302 newrec.NumberParameters = 0;
303 RtlRaiseException( &newrec ); /* never returns */
306 /* Call handler */
307 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
308 frame->Handler, EXC_UnwindHandler ))
310 case ExceptionContinueSearch:
311 break;
312 case ExceptionCollidedUnwind:
313 frame = dispatch;
314 break;
315 default:
316 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
317 newrec.ExceptionFlags = EH_NONCONTINUABLE;
318 newrec.ExceptionRecord = pRecord;
319 newrec.NumberParameters = 0;
320 RtlRaiseException( &newrec ); /* never returns */
321 break;
323 frame = __wine_pop_frame( frame );
328 /*******************************************************************
329 * NtRaiseException (NTDLL.@)
331 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
332 EXCEPTION_RECORD *, CONTEXT *, BOOL );
333 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
334 BOOL first, CONTEXT *context )
336 EXC_RtlRaiseException( rec, ctx );
337 *context = *ctx;
341 /***********************************************************************
342 * RtlRaiseStatus (NTDLL.@)
344 * Raise an exception with ExceptionCode = status
346 void WINAPI RtlRaiseStatus( NTSTATUS status )
348 EXCEPTION_RECORD ExceptionRec;
350 ExceptionRec.ExceptionCode = status;
351 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
352 ExceptionRec.ExceptionRecord = NULL;
353 ExceptionRec.NumberParameters = 0;
354 RtlRaiseException( &ExceptionRec );
358 /*************************************************************
359 * __wine_exception_handler (NTDLL.@)
361 * Exception handler for exception blocks declared in Wine code.
363 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
364 CONTEXT *context, LPVOID pdispatcher )
366 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
368 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
369 return ExceptionContinueSearch;
370 if (wine_frame->u.filter)
372 EXCEPTION_POINTERS ptrs;
373 ptrs.ExceptionRecord = record;
374 ptrs.ContextRecord = context;
375 switch(wine_frame->u.filter( &ptrs ))
377 case EXCEPTION_CONTINUE_SEARCH:
378 return ExceptionContinueSearch;
379 case EXCEPTION_CONTINUE_EXECUTION:
380 return ExceptionContinueExecution;
381 case EXCEPTION_EXECUTE_HANDLER:
382 break;
383 default:
384 MESSAGE( "Invalid return value from exception filter\n" );
385 assert( FALSE );
388 /* hack to make GetExceptionCode() work in handler */
389 wine_frame->ExceptionCode = record->ExceptionCode;
390 wine_frame->ExceptionRecord = wine_frame;
392 RtlUnwind( frame, 0, record, 0 );
393 __wine_pop_frame( frame );
394 longjmp( wine_frame->jmp, 1 );
398 /*************************************************************
399 * __wine_finally_handler (NTDLL.@)
401 * Exception handler for try/finally blocks declared in Wine code.
403 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
404 CONTEXT *context, LPVOID pdispatcher )
406 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
408 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
409 return ExceptionContinueSearch;
410 wine_frame->u.finally_func( FALSE );
411 return ExceptionContinueSearch;