2 * Wine exception handling
4 * Copyright (c) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef __WINE_WINE_EXCEPTION_H
22 #define __WINE_WINE_EXCEPTION_H
28 /* The following definitions allow using exceptions in Wine and Winelib code
30 * They should be used like this:
34 * do some stuff that can raise an exception
36 * __EXCEPT(filter_func,param)
38 * handle the exception here
46 * do some stuff that can raise an exception
48 * __FINALLY(finally_func,param)
50 * The filter_func must be defined with the WINE_EXCEPTION_FILTER
51 * macro, and return one of the EXCEPTION_* code; it can use
52 * GetExceptionInformation and GetExceptionCode to retrieve the
55 * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
57 * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
58 * break out of the current block. You cannot use 'return', 'goto'
59 * or 'longjmp' to leave a __TRY block, as this will surely crash.
60 * You can use them to leave a __EXCEPT block though.
65 typedef struct _EXCEPTION_REGISTRATION_RECORD
67 struct _EXCEPTION_REGISTRATION_RECORD
*Prev
;
68 PEXCEPTION_HANDLER Handler
;
69 } EXCEPTION_REGISTRATION_RECORD
, *PEXCEPTION_REGISTRATION_RECORD
;
71 /* Define this if you want to use your compiler built-in __try/__except support.
72 * This is only useful when compiling to a native Windows binary, as the built-in
73 * compiler exceptions will most certainly not work under Winelib.
75 #ifdef USE_COMPILER_EXCEPTIONS
78 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
79 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
80 #define __ENDTRY /*nothing*/
81 #define __EXCEPT_PAGE_FAULT __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
83 #else /* USE_COMPILER_EXCEPTIONS */
86 #define __attribute__(x) /* nothing */
90 do { __WINE_FRAME __f; \
92 for (;;) if (!__first) \
96 #define __EXCEPT(func) \
98 __wine_pop_frame( &__f.frame ); \
101 __f.frame.Handler = __wine_exception_handler; \
102 __f.u.filter = (func); \
103 __wine_push_frame( &__f.frame ); \
104 if (setjmp( __f.jmp )) { \
105 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
116 #define __FINALLY(func) \
118 __wine_pop_frame( &__f.frame ); \
122 __f.frame.Handler = __wine_finally_handler; \
123 __f.u.finally_func = (func); \
124 __wine_push_frame( &__f.frame ); \
130 typedef DWORD (CALLBACK
*__WINE_FILTER
)(PEXCEPTION_POINTERS
);
131 typedef void (CALLBACK
*__WINE_FINALLY
)(BOOL
);
133 /* convenience handler for page fault exceptions */
134 #define __EXCEPT_PAGE_FAULT __EXCEPT( (__WINE_FILTER)1 )
136 #define WINE_EXCEPTION_FILTER(func) DWORD CALLBACK func( PEXCEPTION_POINTERS __eptr )
137 #define WINE_FINALLY_FUNC(func) void CALLBACK func( BOOL __normal )
139 #define GetExceptionInformation() (__eptr)
140 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
142 #undef AbnormalTermination
143 #define AbnormalTermination() (!__normal)
145 typedef struct __tagWINE_FRAME
147 EXCEPTION_REGISTRATION_RECORD frame
;
151 __WINE_FILTER filter
;
153 __WINE_FINALLY finally_func
;
156 /* hack to make GetExceptionCode() work in handler */
158 const struct __tagWINE_FRAME
*ExceptionRecord
;
161 #endif /* USE_COMPILER_EXCEPTIONS */
163 static __inline EXCEPTION_REGISTRATION_RECORD
*__wine_push_frame( EXCEPTION_REGISTRATION_RECORD
*frame
)
165 #if defined(__GNUC__) && defined(__i386__)
166 EXCEPTION_REGISTRATION_RECORD
*prev
;
167 __asm__
__volatile__(".byte 0x64\n\tmovl (0),%0"
169 "\n\t.byte 0x64\n\tmovl %1,(0)"
170 : "=&r" (prev
) : "r" (frame
) : "memory" );
173 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
174 frame
->Prev
= (void *)teb
->ExceptionList
;
175 teb
->ExceptionList
= (void *)frame
;
180 static __inline EXCEPTION_REGISTRATION_RECORD
*__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD
*frame
)
182 #if defined(__GNUC__) && defined(__i386__)
183 __asm__
__volatile__(".byte 0x64\n\tmovl %0,(0)"
184 : : "r" (frame
->Prev
) : "memory" );
188 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
189 teb
->ExceptionList
= (void *)frame
->Prev
;
194 #ifndef USE_COMPILER_EXCEPTIONS
196 extern VOID NTAPI
RtlUnwind(PVOID
,PVOID
,PEXCEPTION_RECORD
,PVOID
);
198 static __inline EXCEPTION_DISPOSITION
199 __wine_exception_handler( struct _EXCEPTION_RECORD
*record
, void *frame
,
200 struct _CONTEXT
*context
, void *pdispatcher
)
202 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
204 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
| EH_NESTED_CALL
))
205 return ExceptionContinueSearch
;
207 if (wine_frame
->u
.filter
== (void *)1) /* special hack for page faults */
209 if (record
->ExceptionCode
!= EXCEPTION_ACCESS_VIOLATION
)
210 return ExceptionContinueSearch
;
212 else if (wine_frame
->u
.filter
)
214 EXCEPTION_POINTERS ptrs
;
215 ptrs
.ExceptionRecord
= record
;
216 ptrs
.ContextRecord
= context
;
217 switch(wine_frame
->u
.filter( &ptrs
))
219 case EXCEPTION_CONTINUE_SEARCH
:
220 return ExceptionContinueSearch
;
221 case EXCEPTION_CONTINUE_EXECUTION
:
222 return ExceptionContinueExecution
;
223 case EXCEPTION_EXECUTE_HANDLER
:
229 /* hack to make GetExceptionCode() work in handler */
230 wine_frame
->ExceptionCode
= record
->ExceptionCode
;
231 wine_frame
->ExceptionRecord
= wine_frame
;
233 RtlUnwind( frame
, 0, record
, 0 );
234 __wine_pop_frame( frame
);
235 longjmp( wine_frame
->jmp
, 1 );
239 static __inline EXCEPTION_DISPOSITION
240 __wine_finally_handler( struct _EXCEPTION_RECORD
*record
, void *frame
,
241 struct _CONTEXT
*context
, void *pdispatcher
)
243 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
245 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
246 wine_frame
->u
.finally_func( FALSE
);
248 return ExceptionContinueSearch
;
251 #endif /* USE_COMPILER_EXCEPTIONS */
254 /* Wine-specific exceptions codes */
256 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
257 #define EXCEPTION_WINE_ASSERTION 0x80000101 /* assertion failed */
259 /* unhandled return status from vm86 mode */
260 #define EXCEPTION_VM86_INTx 0x80000110
261 #define EXCEPTION_VM86_STI 0x80000111
262 #define EXCEPTION_VM86_PICRETURN 0x80000112
264 extern void __wine_enter_vm86( CONTEXT
*context
);
266 static __inline
WINE_EXCEPTION_FILTER(__wine_pagefault_filter
)
268 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
269 return EXCEPTION_CONTINUE_SEARCH
;
270 return EXCEPTION_EXECUTE_HANDLER
;
273 #endif /* __WINE_WINE_EXCEPTION_H */