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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifndef __WINE_WINE_EXCEPTION_H
22 #define __WINE_WINE_EXCEPTION_H
32 /* The following definitions allow using exceptions in Wine and Winelib code
34 * They should be used like this:
38 * do some stuff that can raise an exception
40 * __EXCEPT(filter_func)
42 * handle the exception here
50 * do some stuff that can raise an exception
52 * __FINALLY(finally_func)
54 * The filter_func and finally_func functions must be defined like this:
56 * LONG CALLBACK filter_func( PEXCEPTION_POINTERS __eptr ) { ... }
58 * void CALLBACK finally_func( BOOL __normal ) { ... }
60 * The filter function must return one of the EXCEPTION_* code; it can
61 * use GetExceptionInformation() and GetExceptionCode() to retrieve the
64 * Warning: Inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
65 * break out of the current block, but avoid using them because they
66 * won't work when compiling with native exceptions. You cannot use
67 * 'return', 'goto', or 'longjmp' to leave a __TRY block either, as
68 * this will surely crash. You can use 'return', 'goto', or 'longjmp'
69 * to leave an __EXCEPT block though.
75 #define __attribute__(x) /* nothing */
78 /* Define this if you want to use your compiler built-in __try/__except support.
79 * This is only useful when compiling to a native Windows binary, as the built-in
80 * compiler exceptions will most certainly not work under Winelib.
82 #ifdef USE_COMPILER_EXCEPTIONS
85 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
86 #define __EXCEPT_CTX(func, ctx) __except((func)(GetExceptionInformation(), ctx))
87 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
88 #define __FINALLY_CTX(func, ctx) __finally { (func)(!AbnormalTermination(), ctx); }
89 #define __ENDTRY /*nothing*/
90 #define __EXCEPT_PAGE_FAULT __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
91 #define __EXCEPT_ALL __except(EXCEPTION_EXECUTE_HANDLER)
93 #else /* USE_COMPILER_EXCEPTIONS */
95 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__WINE_SETJMP_H)
96 #define sigjmp_buf jmp_buf
97 #define sigsetjmp(buf,sigs) setjmp(buf)
98 #define siglongjmp(buf,val) longjmp(buf,val)
101 extern void __wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD
* frame
, EXCEPTION_RECORD
*record
,
102 void (*target
)(void) ) DECLSPEC_HIDDEN DECLSPEC_NORETURN
;
103 extern DWORD
__wine_exception_handler( EXCEPTION_RECORD
*record
,
104 EXCEPTION_REGISTRATION_RECORD
*frame
,
106 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
107 extern DWORD
__wine_exception_ctx_handler( EXCEPTION_RECORD
*record
,
108 EXCEPTION_REGISTRATION_RECORD
*frame
,
110 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
111 extern DWORD
__wine_exception_handler_page_fault( EXCEPTION_RECORD
*record
,
112 EXCEPTION_REGISTRATION_RECORD
*frame
,
114 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
115 extern DWORD
__wine_exception_handler_all( EXCEPTION_RECORD
*record
,
116 EXCEPTION_REGISTRATION_RECORD
*frame
,
118 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
119 extern DWORD
__wine_finally_handler( EXCEPTION_RECORD
*record
,
120 EXCEPTION_REGISTRATION_RECORD
*frame
,
122 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
123 extern DWORD
__wine_finally_ctx_handler( EXCEPTION_RECORD
*record
,
124 EXCEPTION_REGISTRATION_RECORD
*frame
,
126 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
) DECLSPEC_HIDDEN
;
129 do { __WINE_FRAME __f; \
131 for (;;) if (!__first) \
135 #define __EXCEPT(func) \
137 __wine_pop_frame( &__f.frame ); \
140 __f.frame.Handler = __wine_exception_handler; \
141 __f.u.filter = (func); \
142 if (sigsetjmp( __f.jmp, 0 )) { \
143 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
146 #define __EXCEPT_CTX(func, context) \
148 __wine_pop_frame( &__f.frame ); \
151 __f.frame.Handler = __wine_exception_ctx_handler; \
152 __f.u.filter_ctx = (func); \
154 if (sigsetjmp( __f.jmp, 0 )) { \
155 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
158 /* convenience handler for page fault exceptions */
159 #define __EXCEPT_PAGE_FAULT \
161 __wine_pop_frame( &__f.frame ); \
164 __f.frame.Handler = __wine_exception_handler_page_fault; \
165 if (sigsetjmp( __f.jmp, 0 )) { \
166 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
169 /* convenience handler for all exception */
170 #define __EXCEPT_ALL \
172 __wine_pop_frame( &__f.frame ); \
175 __f.frame.Handler = __wine_exception_handler_all; \
176 if (sigsetjmp( __f.jmp, 0 )) { \
177 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
184 __wine_push_frame( &__f.frame ); \
189 #define __FINALLY(func) \
191 __wine_pop_frame( &__f.frame ); \
195 __f.frame.Handler = __wine_finally_handler; \
196 __f.u.finally_func = (func); \
197 __wine_push_frame( &__f.frame ); \
202 #define __FINALLY_CTX(func, context) \
204 __wine_pop_frame( &__f.frame ); \
205 (func)(1, context); \
208 __f.frame.Handler = __wine_finally_ctx_handler; \
209 __f.u.finally_func_ctx = (func); \
211 __wine_push_frame( &__f.frame ); \
217 typedef LONG (CALLBACK
*__WINE_FILTER
)(PEXCEPTION_POINTERS
);
218 typedef LONG (CALLBACK
*__WINE_FILTER_CTX
)(PEXCEPTION_POINTERS
, void*);
219 typedef void (CALLBACK
*__WINE_FINALLY
)(BOOL
);
220 typedef void (CALLBACK
*__WINE_FINALLY_CTX
)(BOOL
, void*);
222 #define GetExceptionInformation() (__eptr)
223 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
224 #define AbnormalTermination() (!__normal)
226 typedef struct __tagWINE_FRAME
228 EXCEPTION_REGISTRATION_RECORD frame
;
232 __WINE_FILTER filter
;
233 __WINE_FILTER_CTX filter_ctx
;
235 __WINE_FINALLY finally_func
;
236 __WINE_FINALLY_CTX finally_func_ctx
;
240 /* hack to make GetExceptionCode() work in handler */
242 const struct __tagWINE_FRAME
*ExceptionRecord
;
245 #endif /* USE_COMPILER_EXCEPTIONS */
247 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_push_frame( EXCEPTION_REGISTRATION_RECORD
*frame
)
249 #if defined(__GNUC__) && defined(__i386__)
250 EXCEPTION_REGISTRATION_RECORD
*prev
;
251 __asm__
__volatile__(".byte 0x64\n\tmovl (0),%0"
253 "\n\t.byte 0x64\n\tmovl %1,(0)"
254 : "=&r" (prev
) : "r" (frame
) : "memory" );
257 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
258 frame
->Prev
= teb
->ExceptionList
;
259 teb
->ExceptionList
= frame
;
264 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD
*frame
)
266 #if defined(__GNUC__) && defined(__i386__)
267 __asm__
__volatile__(".byte 0x64\n\tmovl %0,(0)"
268 : : "r" (frame
->Prev
) : "memory" );
272 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
273 teb
->ExceptionList
= frame
->Prev
;
278 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_get_frame(void)
280 #if defined(__GNUC__) && defined(__i386__)
281 EXCEPTION_REGISTRATION_RECORD
*ret
;
282 __asm__
__volatile__(".byte 0x64\n\tmovl (0),%0" : "=r" (ret
) );
285 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
286 return teb
->ExceptionList
;
290 /* Exception handling flags - from OS/2 2.0 exception handling */
292 /* Win32 seems to use the same flags as ExceptionFlags in an EXCEPTION_RECORD */
293 #define EH_NONCONTINUABLE 0x01
294 #define EH_UNWINDING 0x02
295 #define EH_EXIT_UNWIND 0x04
296 #define EH_STACK_INVALID 0x08
297 #define EH_NESTED_CALL 0x10
298 #define EH_TARGET_UNWIND 0x20
299 #define EH_COLLIDED_UNWIND 0x40
301 /* Wine-specific exceptions codes */
303 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
304 #define EXCEPTION_WINE_ASSERTION 0x80000101 /* assertion failed */
310 #endif /* __WINE_WINE_EXCEPTION_H */