Replaced GetDC by CreateDC.
[wine.git] / dlls / ntdll / exception.c
blobb36fff9c08d51dd36840f9462ffcf1765a7e05d5
1 /*
2 * NT exception handling routines
3 *
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
6 */
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 "server.h"
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(seh);
21 /* Exception record for handling exceptions happening inside exception handlers */
22 typedef struct
24 EXCEPTION_FRAME frame;
25 EXCEPTION_FRAME *prevFrame;
26 } EXC_NESTED_FRAME;
29 /*******************************************************************
30 * EXC_RaiseHandler
32 * Handler for exceptions happening inside a handler.
34 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
35 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
37 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
38 return ExceptionContinueSearch;
39 /* We shouldn't get here so we store faulty frame in dispatcher */
40 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
41 return ExceptionNestedException;
45 /*******************************************************************
46 * EXC_UnwindHandler
48 * Handler for exceptions happening inside an unwind handler.
50 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
51 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
53 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
54 return ExceptionContinueSearch;
55 /* We shouldn't get here so we store faulty frame in dispatcher */
56 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
57 return ExceptionCollidedUnwind;
61 /*******************************************************************
62 * EXC_CallHandler
64 * Call an exception handler, setting up an exception frame to catch exceptions
65 * happening during the handler execution.
66 * Please do not change the first 4 parameters order in any way - some exceptions handlers
67 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
69 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
70 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
71 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
73 EXC_NESTED_FRAME newframe;
74 DWORD ret;
76 newframe.frame.Handler = nested_handler;
77 newframe.prevFrame = frame;
78 EXC_push_frame( &newframe.frame );
79 TRACE( "calling handler at %p code=%lx flags=%lx\n",
80 handler, record->ExceptionCode, record->ExceptionFlags );
81 ret = handler( record, frame, context, dispatcher );
82 TRACE( "handler returned %lx\n", ret );
83 EXC_pop_frame( &newframe.frame );
84 return ret;
88 /**********************************************************************
89 * EXC_SendEvent
91 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
93 static inline int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
95 struct exception_event_request *req = get_req_buffer();
96 req->record = *rec;
97 req->first = first_chance;
98 req->context = *context;
99 if (!server_call_noerr( REQ_EXCEPTION_EVENT )) *context = req->context;
100 return req->status;
104 /*******************************************************************
105 * EXC_DefaultHandling
107 * Default handling for exceptions. Called when we didn't find a suitable handler.
109 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
111 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
113 if (rec->ExceptionFlags & EH_STACK_INVALID)
114 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
115 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
116 ERR("Process attempted to continue execution after noncontinuable exception.\n");
117 else
118 ERR("Unhandled exception code %lx flags %lx addr %p\n",
119 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
120 TerminateProcess( GetCurrentProcess(), 1 );
124 /***********************************************************************
125 * EXC_RtlRaiseException / RtlRaiseException (NTDLL.464)
127 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * )
128 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
130 PEXCEPTION_FRAME frame, dispatch, nested_frame;
131 EXCEPTION_RECORD newrec;
132 DWORD res;
134 TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
136 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
138 frame = NtCurrentTeb()->except;
139 nested_frame = NULL;
140 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
142 /* Check frame address */
143 if (((void*)frame < NtCurrentTeb()->stack_low) ||
144 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
145 (int)frame & 3)
147 rec->ExceptionFlags |= EH_STACK_INVALID;
148 break;
151 /* Call handler */
152 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
153 if (frame == nested_frame)
155 /* no longer nested */
156 nested_frame = NULL;
157 rec->ExceptionFlags &= ~EH_NESTED_CALL;
160 switch(res)
162 case ExceptionContinueExecution:
163 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
164 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
165 newrec.ExceptionFlags = EH_NONCONTINUABLE;
166 newrec.ExceptionRecord = rec;
167 newrec.NumberParameters = 0;
168 RtlRaiseException( &newrec ); /* never returns */
169 break;
170 case ExceptionContinueSearch:
171 break;
172 case ExceptionNestedException:
173 if (nested_frame < dispatch) nested_frame = dispatch;
174 rec->ExceptionFlags |= EH_NESTED_CALL;
175 break;
176 default:
177 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
178 newrec.ExceptionFlags = EH_NONCONTINUABLE;
179 newrec.ExceptionRecord = rec;
180 newrec.NumberParameters = 0;
181 RtlRaiseException( &newrec ); /* never returns */
182 break;
184 frame = frame->Prev;
186 EXC_DefaultHandling( rec, context );
190 /*******************************************************************
191 * EXC_RtlUnwind / RtlUnwind (KERNEL32.590) (NTDLL.518)
193 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
194 PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD )
195 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
196 PEXCEPTION_RECORD pRecord, DWORD returnEax,
197 CONTEXT *context )
199 EXCEPTION_RECORD record, newrec;
200 PEXCEPTION_FRAME frame, dispatch;
202 #ifdef __i386__
203 context->Eax = returnEax;
204 #endif
206 /* build an exception record, if we do not have one */
207 if (!pRecord)
209 record.ExceptionCode = STATUS_UNWIND;
210 record.ExceptionFlags = 0;
211 record.ExceptionRecord = NULL;
212 record.ExceptionAddress = GET_IP(context);
213 record.NumberParameters = 0;
214 pRecord = &record;
217 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
219 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
221 /* get chain of exception frames */
222 frame = NtCurrentTeb()->except;
223 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
225 /* Check frame address */
226 if (pEndFrame && (frame > pEndFrame))
228 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
229 newrec.ExceptionFlags = EH_NONCONTINUABLE;
230 newrec.ExceptionRecord = pRecord;
231 newrec.NumberParameters = 0;
232 RtlRaiseException( &newrec ); /* never returns */
234 if (((void*)frame < NtCurrentTeb()->stack_low) ||
235 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
236 (int)frame & 3)
238 newrec.ExceptionCode = STATUS_BAD_STACK;
239 newrec.ExceptionFlags = EH_NONCONTINUABLE;
240 newrec.ExceptionRecord = pRecord;
241 newrec.NumberParameters = 0;
242 RtlRaiseException( &newrec ); /* never returns */
245 /* Call handler */
246 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
247 frame->Handler, EXC_UnwindHandler ))
249 case ExceptionContinueSearch:
250 break;
251 case ExceptionCollidedUnwind:
252 frame = dispatch;
253 break;
254 default:
255 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
256 newrec.ExceptionFlags = EH_NONCONTINUABLE;
257 newrec.ExceptionRecord = pRecord;
258 newrec.NumberParameters = 0;
259 RtlRaiseException( &newrec ); /* never returns */
260 break;
262 frame = EXC_pop_frame( frame );
267 /*******************************************************************
268 * EXC_NtRaiseException / NtRaiseException (NTDLL.175)
270 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
271 EXCEPTION_RECORD *, CONTEXT *, BOOL )
272 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
273 BOOL first, CONTEXT *context )
275 EXC_RtlRaiseException( rec, ctx );
276 *context = *ctx;
280 /***********************************************************************
281 * RtlRaiseStatus (NTDLL.465)
283 * Raise an exception with ExceptionCode = status
285 void WINAPI RtlRaiseStatus( NTSTATUS status )
287 EXCEPTION_RECORD ExceptionRec;
289 ExceptionRec.ExceptionCode = status;
290 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
291 ExceptionRec.ExceptionRecord = NULL;
292 ExceptionRec.NumberParameters = 0;
293 RtlRaiseException( &ExceptionRec );