2 * Wine exception handling
4 * Copyright (c) 1999 Alexandre Julliard
7 #ifndef __WINE_WINE_EXCEPTION_H
8 #define __WINE_WINE_EXCEPTION_H
13 /* The following definitions allow using exceptions in Wine and Winelib code
15 * They should be used like this:
19 * do some stuff that can raise an exception
21 * __EXCEPT(filter_func,param)
23 * handle the exception here
31 * do some stuff that can raise an exception
33 * __FINALLY(finally_func,param)
35 * The filter_func must be defined with the WINE_EXCEPTION_FILTER
36 * macro, and return one of the EXCEPTION_* code; it can use
37 * GetExceptionInformation and GetExceptionCode to retrieve the
40 * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
42 * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
43 * break out of the current block. You cannot use 'return', 'goto'
44 * or 'longjmp' to leave a __TRY block, as this will surely crash.
45 * You can use them to leave a __EXCEPT block though.
50 /* Define this if you want to use your compiler built-in __try/__except support.
51 * This is only useful when compiling to a native Windows binary, as the built-in
52 * compiler exceptions will most certainly not work under Winelib.
54 #ifdef USE_COMPILER_EXCEPTIONS
57 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
58 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
59 #define __ENDTRY /*nothing*/
61 #else /* USE_COMPILER_EXCEPTIONS */
64 do { __WINE_FRAME __f; \
66 for (;;) if (!__first) \
70 #define __EXCEPT(func) \
72 __wine_pop_frame( &__f.frame ); \
75 __f.frame.Handler = (PEXCEPTION_HANDLER)__wine_exception_handler; \
76 __f.u.filter = (func); \
77 __wine_push_frame( &__f.frame ); \
78 if (setjmp( __f.jmp)) { \
79 const __WINE_FRAME * const __eptr WINE_UNUSED = &__f; \
90 #define __FINALLY(func) \
92 __wine_pop_frame( &__f.frame ); \
96 __f.frame.Handler = (PEXCEPTION_HANDLER)__wine_finally_handler; \
97 __f.u.finally_func = (func); \
98 __wine_push_frame( &__f.frame ); \
104 typedef DWORD CALLBACK (*__WINE_FILTER)(PEXCEPTION_POINTERS);
105 typedef void CALLBACK (*__WINE_FINALLY)(BOOL);
107 #define WINE_EXCEPTION_FILTER(func) DWORD WINAPI func( EXCEPTION_POINTERS *__eptr )
108 #define WINE_FINALLY_FUNC(func) void WINAPI func( BOOL __normal )
110 #define GetExceptionInformation() (__eptr)
111 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
112 #define AbnormalTermination() (!__normal)
114 typedef struct __tagWINE_FRAME
116 EXCEPTION_FRAME frame;
120 __WINE_FILTER filter;
122 __WINE_FINALLY finally_func;
125 /* hack to make GetExceptionCode() work in handler */
127 const struct __tagWINE_FRAME *ExceptionRecord;
130 extern DWORD __wine_exception_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame,
131 CONTEXT *context, LPVOID pdispatcher );
132 extern DWORD __wine_finally_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame,
133 CONTEXT *context, LPVOID pdispatcher );
135 #endif /* USE_COMPILER_EXCEPTIONS */
137 static inline EXCEPTION_FRAME * WINE_UNUSED __wine_push_frame( EXCEPTION_FRAME *frame )
139 #if defined(__GNUC__) && defined(__i386__)
140 EXCEPTION_FRAME *prev;
141 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
143 "\n\t.byte 0x64\n\tmovl %1,(0)"
144 : "=&r" (prev) : "r" (frame) : "memory" );
147 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
148 frame->Prev = (void *)teb->ExceptionList;
149 teb->ExceptionList = (void *)frame;
154 static inline EXCEPTION_FRAME * WINE_UNUSED __wine_pop_frame( EXCEPTION_FRAME *frame )
156 #if defined(__GNUC__) && defined(__i386__)
157 __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
158 : : "r" (frame->Prev) : "memory" );
162 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
163 teb->ExceptionList = (void *)frame->Prev;
169 /* Wine-specific exceptions codes */
171 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
173 /* unhandled return status from vm86 mode */
174 #define EXCEPTION_VM86_INTx 0x80000101
175 #define EXCEPTION_VM86_STI 0x80000102
176 #define EXCEPTION_VM86_PICRETURN 0x80000103
178 extern void __wine_enter_vm86( CONTEXT *context );
181 extern void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
182 extern BOOL SIGNAL_Init(void);
185 #endif /* __WINE_WINE_EXCEPTION_H */