ddraw/tests: Sync test_clear() with d3d8/9.
[wine.git] / include / wine / exception.h
blob8e3f48182ffb85d359a89239676d01fb955cb3af
1 /*
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
24 #include <setjmp.h>
25 #include <windef.h>
26 #include <excpt.h>
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
32 /* The following definitions allow using exceptions in Wine and Winelib code
34 * They should be used like this:
36 * __TRY
37 * {
38 * do some stuff that can raise an exception
39 * }
40 * __EXCEPT(filter_func)
41 * {
42 * handle the exception here
43 * }
44 * __ENDTRY
46 * or
48 * __TRY
49 * {
50 * do some stuff that can raise an exception
51 * }
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
62 * exception info.
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.
71 * -- AJ
74 #ifndef __GNUC__
75 #define __attribute__(x) /* nothing */
76 #endif
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
84 #define __TRY __try
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__)
96 #define sigjmp_buf jmp_buf
97 #define sigsetjmp(buf,sigs) setjmp(buf)
98 #define siglongjmp(buf,val) longjmp(buf,val)
99 #endif
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,
105 CONTEXT *context,
106 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
107 extern DWORD __wine_exception_ctx_handler( EXCEPTION_RECORD *record,
108 EXCEPTION_REGISTRATION_RECORD *frame,
109 CONTEXT *context,
110 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
111 extern DWORD __wine_exception_handler_page_fault( EXCEPTION_RECORD *record,
112 EXCEPTION_REGISTRATION_RECORD *frame,
113 CONTEXT *context,
114 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
115 extern DWORD __wine_exception_handler_all( EXCEPTION_RECORD *record,
116 EXCEPTION_REGISTRATION_RECORD *frame,
117 CONTEXT *context,
118 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
119 extern DWORD __wine_finally_handler( EXCEPTION_RECORD *record,
120 EXCEPTION_REGISTRATION_RECORD *frame,
121 CONTEXT *context,
122 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
123 extern DWORD __wine_finally_ctx_handler( EXCEPTION_RECORD *record,
124 EXCEPTION_REGISTRATION_RECORD *frame,
125 CONTEXT *context,
126 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
128 #define __TRY \
129 do { __WINE_FRAME __f; \
130 int __first = 1; \
131 for (;;) if (!__first) \
133 do {
135 #define __EXCEPT(func) \
136 } while(0); \
137 __wine_pop_frame( &__f.frame ); \
138 break; \
139 } else { \
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; \
144 do {
146 #define __EXCEPT_CTX(func, context) \
147 } while(0); \
148 __wine_pop_frame( &__f.frame ); \
149 break; \
150 } else { \
151 __f.frame.Handler = __wine_exception_ctx_handler; \
152 __f.u.filter_ctx = (func); \
153 __f.ctx = context; \
154 if (sigsetjmp( __f.jmp, 0 )) { \
155 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
156 do {
158 /* convenience handler for page fault exceptions */
159 #define __EXCEPT_PAGE_FAULT \
160 } while(0); \
161 __wine_pop_frame( &__f.frame ); \
162 break; \
163 } else { \
164 __f.frame.Handler = __wine_exception_handler_page_fault; \
165 if (sigsetjmp( __f.jmp, 0 )) { \
166 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
167 do {
169 /* convenience handler for all exception */
170 #define __EXCEPT_ALL \
171 } while(0); \
172 __wine_pop_frame( &__f.frame ); \
173 break; \
174 } else { \
175 __f.frame.Handler = __wine_exception_handler_all; \
176 if (sigsetjmp( __f.jmp, 0 )) { \
177 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
178 do {
180 #define __ENDTRY \
181 } while (0); \
182 break; \
184 __wine_push_frame( &__f.frame ); \
185 __first = 0; \
187 } while (0);
189 #define __FINALLY(func) \
190 } while(0); \
191 __wine_pop_frame( &__f.frame ); \
192 (func)(1); \
193 break; \
194 } else { \
195 __f.frame.Handler = __wine_finally_handler; \
196 __f.u.finally_func = (func); \
197 __wine_push_frame( &__f.frame ); \
198 __first = 0; \
200 } while (0);
202 #define __FINALLY_CTX(func, context) \
203 } while(0); \
204 __wine_pop_frame( &__f.frame ); \
205 (func)(1, context); \
206 break; \
207 } else { \
208 __f.frame.Handler = __wine_finally_ctx_handler; \
209 __f.u.finally_func_ctx = (func); \
210 __f.ctx = context; \
211 __wine_push_frame( &__f.frame ); \
212 __first = 0; \
214 } while (0);
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;
229 union
231 /* exception data */
232 __WINE_FILTER filter;
233 __WINE_FILTER_CTX filter_ctx;
234 /* finally data */
235 __WINE_FINALLY finally_func;
236 __WINE_FINALLY_CTX finally_func_ctx;
237 } u;
238 void *ctx;
239 sigjmp_buf jmp;
240 /* hack to make GetExceptionCode() work in handler */
241 DWORD ExceptionCode;
242 const struct __tagWINE_FRAME *ExceptionRecord;
243 } __WINE_FRAME;
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"
252 "\n\tmovl %0,(%1)"
253 "\n\t.byte 0x64\n\tmovl %1,(0)"
254 : "=&r" (prev) : "r" (frame) : "memory" );
255 return prev;
256 #else
257 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
258 frame->Prev = teb->ExceptionList;
259 teb->ExceptionList = frame;
260 return frame->Prev;
261 #endif
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" );
269 return frame->Prev;
271 #else
272 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
273 teb->ExceptionList = frame->Prev;
274 return frame->Prev;
275 #endif
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) );
283 return ret;
284 #else
285 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
286 return teb->ExceptionList;
287 #endif
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 */
306 /* unhandled return status from vm86 mode */
307 #define EXCEPTION_VM86_INTx 0x80000110
308 #define EXCEPTION_VM86_STI 0x80000111
309 #define EXCEPTION_VM86_PICRETURN 0x80000112
311 extern void __wine_enter_vm86( CONTEXT *context );
313 #ifdef __cplusplus
315 #endif
317 #endif /* __WINE_WINE_EXCEPTION_H */