2 * Wine exception handling
4 * Copyright (c) 1999 Alexandre Julliard
7 #ifndef __WINE_WINE_EXCEPTION_H
8 #define __WINE_WINE_EXCEPTION_H
14 /* The following definitions allow using exceptions in Wine and Winelib code
16 * They should be used like this:
20 * do some stuff that can raise an exception
22 * __EXCEPT(filter_func,param)
24 * handle the exception here
32 * do some stuff that can raise an exception
34 * __FINALLY(finally_func,param)
36 * The filter_func must be defined with the WINE_EXCEPTION_FILTER
37 * macro, and return one of the EXCEPTION_* code; it can use
38 * GetExceptionInformation and GetExceptionCode to retrieve the
41 * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
43 * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
44 * break out of the current block. You cannot use 'return', 'goto'
45 * or 'longjmp' to leave a __TRY block, as this will surely crash.
46 * You can use them to leave a __EXCEPT block though.
51 /* Define this if you want to use your compiler built-in __try/__except support.
52 * This is only useful when compiling to a native Windows binary, as the built-in
53 * compiler exceptions will most certainly not work under Winelib.
55 #ifdef USE_COMPILER_EXCEPTIONS
58 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
59 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
60 #define __ENDTRY /*nothing*/
62 #else /* USE_COMPILER_EXCEPTIONS */
65 do { __WINE_FRAME __f; \
67 for (;;) if (!__first) \
71 #define __EXCEPT(func) \
73 EXC_pop_frame( &__f.frame ); \
76 __f.frame.Handler = (PEXCEPTION_HANDLER)WINE_exception_handler; \
77 __f.u.filter = (func); \
78 EXC_push_frame( &__f.frame ); \
79 if (setjmp( __f.jmp)) { \
80 const __WINE_FRAME * const __eptr WINE_UNUSED = &__f; \
91 #define __FINALLY(func) \
93 EXC_pop_frame( &__f.frame ); \
97 __f.frame.Handler = (PEXCEPTION_HANDLER)WINE_finally_handler; \
98 __f.u.finally_func = (func); \
99 EXC_push_frame( &__f.frame ); \
105 typedef DWORD (*CALLBACK __WINE_FILTER
)(PEXCEPTION_POINTERS
);
106 typedef void (*CALLBACK __WINE_FINALLY
)(BOOL
);
108 #define WINE_EXCEPTION_FILTER(func) DWORD WINAPI func( EXCEPTION_POINTERS *__eptr )
109 #define WINE_FINALLY_FUNC(func) void WINAPI func( BOOL __normal )
111 #define GetExceptionInformation() (__eptr)
112 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
113 #define AbnormalTermination() (!__normal)
115 typedef struct __tagWINE_FRAME
117 EXCEPTION_FRAME frame
;
121 __WINE_FILTER filter
;
123 __WINE_FINALLY finally_func
;
126 /* hack to make GetExceptionCode() work in handler */
128 const struct __tagWINE_FRAME
*ExceptionRecord
;
131 extern DWORD
WINE_exception_handler( PEXCEPTION_RECORD record
, EXCEPTION_FRAME
*frame
,
132 CONTEXT
*context
, LPVOID pdispatcher
);
133 extern DWORD
WINE_finally_handler( PEXCEPTION_RECORD record
, EXCEPTION_FRAME
*frame
,
134 CONTEXT
*context
, LPVOID pdispatcher
);
136 #endif /* USE_COMPILER_EXCEPTIONS */
138 static inline EXCEPTION_FRAME
* WINE_UNUSED
EXC_push_frame( EXCEPTION_FRAME
*frame
)
140 #if defined(__GNUC__) && defined(__i386__)
141 EXCEPTION_FRAME
*prev
;
142 __asm__
__volatile__(".byte 0x64\n\tmovl (0),%0"
144 "\n\t.byte 0x64\n\tmovl %1,(0)"
145 : "=&r" (prev
) : "r" (frame
) : "memory" );
148 TEB
*teb
= NtCurrentTeb();
149 frame
->Prev
= teb
->except
;
155 static inline EXCEPTION_FRAME
* WINE_UNUSED
EXC_pop_frame( EXCEPTION_FRAME
*frame
)
157 #if defined(__GNUC__) && defined(__i386__)
158 __asm__
__volatile__(".byte 0x64\n\tmovl %0,(0)"
159 : : "r" (frame
->Prev
) : "memory" );
163 NtCurrentTeb()->except
= frame
->Prev
;
169 extern void WINAPI
EXC_NtRaiseException( PEXCEPTION_RECORD
, PCONTEXT
, BOOL
, PCONTEXT
);
170 extern void WINAPI
EXC_RtlRaiseException( PEXCEPTION_RECORD
, PCONTEXT
);
171 extern void WINAPI
EXC_RtlUnwind( PEXCEPTION_FRAME
, LPVOID
,
172 PEXCEPTION_RECORD
, DWORD
, CONTEXT
* );
173 extern void WINAPI
EXC_DebugBreak( CONTEXT
*context
);
175 typedef DWORD (*DEBUGHOOK
)( EXCEPTION_RECORD
*, CONTEXT
*, BOOL
);
176 extern void EXC_SetDebugEventHook( DEBUGHOOK hook
);
177 extern DEBUGHOOK
EXC_GetDebugEventHook(void);
178 extern BOOL
SIGNAL_Init(void);
181 #endif /* __WINE_WINE_EXCEPTION_H */