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
27 #include <wine/port.h>
29 /* The following definitions allow using exceptions in Wine and Winelib code
31 * They should be used like this:
35 * do some stuff that can raise an exception
37 * __EXCEPT(filter_func,param)
39 * handle the exception here
47 * do some stuff that can raise an exception
49 * __FINALLY(finally_func,param)
51 * The filter_func must be defined with the WINE_EXCEPTION_FILTER
52 * macro, and return one of the EXCEPTION_* code; it can use
53 * GetExceptionInformation and GetExceptionCode to retrieve the
56 * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
58 * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
59 * break out of the current block. You cannot use 'return', 'goto'
60 * or 'longjmp' to leave a __TRY block, as this will surely crash.
61 * You can use them to leave a __EXCEPT block though.
66 /* Define this if you want to use your compiler built-in __try/__except support.
67 * This is only useful when compiling to a native Windows binary, as the built-in
68 * compiler exceptions will most certainly not work under Winelib.
70 #ifdef USE_COMPILER_EXCEPTIONS
73 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
74 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
75 #define __ENDTRY /*nothing*/
77 #else /* USE_COMPILER_EXCEPTIONS */
80 #define __attribute__(x) /* nothing */
84 do { __WINE_FRAME __f; \
86 for (;;) if (!__first) \
90 #define __EXCEPT(func) \
92 __wine_pop_frame( &__f.frame ); \
95 __f.frame.Handler = __wine_exception_handler; \
96 __f.u.filter = (func); \
97 __wine_push_frame( &__f.frame ); \
98 if (sigsetjmp( __f.jmp, 1 )) { \
99 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
110 #define __FINALLY(func) \
112 __wine_pop_frame( &__f.frame ); \
116 __f.frame.Handler = __wine_finally_handler; \
117 __f.u.finally_func = (func); \
118 __wine_push_frame( &__f.frame ); \
124 typedef DWORD (CALLBACK
*__WINE_FILTER
)(PEXCEPTION_POINTERS
);
125 typedef void (CALLBACK
*__WINE_FINALLY
)(BOOL
);
127 #define WINE_EXCEPTION_FILTER(func) DWORD WINAPI func( EXCEPTION_POINTERS *__eptr )
128 #define WINE_FINALLY_FUNC(func) void WINAPI func( BOOL __normal )
130 #define GetExceptionInformation() (__eptr)
131 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
133 #undef AbnormalTermination
134 #define AbnormalTermination() (!__normal)
136 typedef struct __tagWINE_FRAME
138 EXCEPTION_REGISTRATION_RECORD frame
;
142 __WINE_FILTER filter
;
144 __WINE_FINALLY finally_func
;
147 /* hack to make GetExceptionCode() work in handler */
149 const struct __tagWINE_FRAME
*ExceptionRecord
;
152 extern DWORD
__wine_exception_handler( PEXCEPTION_RECORD record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
153 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**pdispatcher
);
154 extern DWORD
__wine_finally_handler( PEXCEPTION_RECORD record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
155 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**pdispatcher
);
157 #endif /* USE_COMPILER_EXCEPTIONS */
159 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_push_frame( EXCEPTION_REGISTRATION_RECORD
*frame
)
161 #if defined(__GNUC__) && defined(__i386__)
162 EXCEPTION_REGISTRATION_RECORD
*prev
;
163 __asm__
__volatile__(".byte 0x64\n\tmovl (0),%0"
165 "\n\t.byte 0x64\n\tmovl %1,(0)"
166 : "=&r" (prev
) : "r" (frame
) : "memory" );
169 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
170 frame
->Prev
= (void *)teb
->ExceptionList
;
171 teb
->ExceptionList
= (void *)frame
;
176 static inline EXCEPTION_REGISTRATION_RECORD
*__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD
*frame
)
178 #if defined(__GNUC__) && defined(__i386__)
179 __asm__
__volatile__(".byte 0x64\n\tmovl %0,(0)"
180 : : "r" (frame
->prev
) : "memory" );
184 NT_TIB
*teb
= (NT_TIB
*)NtCurrentTeb();
185 teb
->ExceptionList
= (void *)frame
->Prev
;
191 /* Wine-specific exceptions codes */
193 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
194 #define EXCEPTION_WINE_ASSERTION 0x80000101 /* assertion failed */
196 /* unhandled return status from vm86 mode */
197 #define EXCEPTION_VM86_INTx 0x80000110
198 #define EXCEPTION_VM86_STI 0x80000111
199 #define EXCEPTION_VM86_PICRETURN 0x80000112
201 extern void __wine_enter_vm86( CONTEXT
*context
);
203 #endif /* __WINE_WINE_EXCEPTION_H */