Release 4.20.
[wine.git] / include / wine / exception.h
blob90ce30d6a215eb002b8355c31d87987402761be0
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 <windef.h>
25 #include <excpt.h>
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
31 /* The following definitions allow using exceptions in Wine and Winelib code
33 * They should be used like this:
35 * __TRY
36 * {
37 * do some stuff that can raise an exception
38 * }
39 * __EXCEPT(filter_func)
40 * {
41 * handle the exception here
42 * }
43 * __ENDTRY
45 * or
47 * __TRY
48 * {
49 * do some stuff that can raise an exception
50 * }
51 * __FINALLY(finally_func)
53 * The filter_func and finally_func functions must be defined like this:
55 * LONG CALLBACK filter_func( PEXCEPTION_POINTERS __eptr ) { ... }
57 * void CALLBACK finally_func( BOOL __normal ) { ... }
59 * The filter function must return one of the EXCEPTION_* code; it can
60 * use GetExceptionInformation() and GetExceptionCode() to retrieve the
61 * exception info.
63 * Warning: Inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
64 * break out of the current block, but avoid using them because they
65 * won't work when compiling with native exceptions. You cannot use
66 * 'return', 'goto', or 'longjmp' to leave a __TRY block either, as
67 * this will surely crash. You can use 'return', 'goto', or 'longjmp'
68 * to leave an __EXCEPT block though.
70 * -- AJ
73 #ifndef __GNUC__
74 #define __attribute__(x) /* nothing */
75 #endif
77 /* Define this if you want to use your compiler built-in __try/__except support.
78 * This is only useful when compiling to a native Windows binary, as the built-in
79 * compiler exceptions will most certainly not work under Winelib.
81 #ifdef USE_COMPILER_EXCEPTIONS
83 #define __TRY __try
84 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
85 #define __EXCEPT_CTX(func, ctx) __except((func)(GetExceptionInformation(), ctx))
86 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
87 #define __FINALLY_CTX(func, ctx) __finally { (func)(!AbnormalTermination(), ctx); }
88 #define __ENDTRY /*nothing*/
89 #define __EXCEPT_PAGE_FAULT __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
90 #define __EXCEPT_ALL __except(EXCEPTION_EXECUTE_HANDLER)
92 #else /* USE_COMPILER_EXCEPTIONS */
94 #ifdef __i386__
95 typedef struct { int reg[16]; } __wine_jmp_buf;
96 #elif defined(__x86_64__)
97 typedef struct { DECLSPEC_ALIGN(16) struct { unsigned __int64 Part[2]; } reg[16]; } __wine_jmp_buf;
98 #elif defined(__arm__)
99 typedef struct { int reg[28]; } __wine_jmp_buf;
100 #elif defined(__aarch64__)
101 typedef struct { __int64 reg[24]; } __wine_jmp_buf;
102 #else
103 typedef struct { int reg; } __wine_jmp_buf;
104 #endif
106 extern int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) __wine_setjmpex( __wine_jmp_buf *buf,
107 EXCEPTION_REGISTRATION_RECORD *frame ) DECLSPEC_HIDDEN;
108 extern void __cdecl __wine_longjmp( __wine_jmp_buf *buf, int retval ) DECLSPEC_HIDDEN DECLSPEC_NORETURN;
109 extern void __cdecl __wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD* frame, EXCEPTION_RECORD *record,
110 void (*target)(void) ) DECLSPEC_HIDDEN DECLSPEC_NORETURN;
111 extern DWORD __cdecl __wine_exception_handler( EXCEPTION_RECORD *record,
112 EXCEPTION_REGISTRATION_RECORD *frame,
113 CONTEXT *context,
114 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
115 extern DWORD __cdecl __wine_exception_ctx_handler( EXCEPTION_RECORD *record,
116 EXCEPTION_REGISTRATION_RECORD *frame,
117 CONTEXT *context,
118 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
119 extern DWORD __cdecl __wine_exception_handler_page_fault( EXCEPTION_RECORD *record,
120 EXCEPTION_REGISTRATION_RECORD *frame,
121 CONTEXT *context,
122 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
123 extern DWORD __cdecl __wine_exception_handler_all( EXCEPTION_RECORD *record,
124 EXCEPTION_REGISTRATION_RECORD *frame,
125 CONTEXT *context,
126 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
127 extern DWORD __cdecl __wine_finally_handler( EXCEPTION_RECORD *record,
128 EXCEPTION_REGISTRATION_RECORD *frame,
129 CONTEXT *context,
130 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
131 extern DWORD __cdecl __wine_finally_ctx_handler( EXCEPTION_RECORD *record,
132 EXCEPTION_REGISTRATION_RECORD *frame,
133 CONTEXT *context,
134 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
136 #define __TRY \
137 do { __WINE_FRAME __f; \
138 int __first = 1; \
139 for (;;) if (!__first) \
141 do {
143 #define __EXCEPT(func) \
144 } while(0); \
145 __wine_pop_frame( &__f.frame ); \
146 break; \
147 } else { \
148 __f.frame.Handler = __wine_exception_handler; \
149 __f.u.filter = (func); \
150 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
151 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
152 do {
154 #define __EXCEPT_CTX(func, context) \
155 } while(0); \
156 __wine_pop_frame( &__f.frame ); \
157 break; \
158 } else { \
159 __f.frame.Handler = __wine_exception_ctx_handler; \
160 __f.u.filter_ctx = (func); \
161 __f.ctx = context; \
162 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
163 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
164 do {
166 /* convenience handler for page fault exceptions */
167 #define __EXCEPT_PAGE_FAULT \
168 } while(0); \
169 __wine_pop_frame( &__f.frame ); \
170 break; \
171 } else { \
172 __f.frame.Handler = __wine_exception_handler_page_fault; \
173 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
174 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
175 do {
177 /* convenience handler for all exceptions */
178 #define __EXCEPT_ALL \
179 } while(0); \
180 __wine_pop_frame( &__f.frame ); \
181 break; \
182 } else { \
183 __f.frame.Handler = __wine_exception_handler_all; \
184 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
185 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
186 do {
188 #define __ENDTRY \
189 } while (0); \
190 break; \
192 __wine_push_frame( &__f.frame ); \
193 __first = 0; \
195 } while (0);
197 #define __FINALLY(func) \
198 } while(0); \
199 __wine_pop_frame( &__f.frame ); \
200 (func)(1); \
201 break; \
202 } else { \
203 __f.frame.Handler = __wine_finally_handler; \
204 __f.u.finally_func = (func); \
205 __wine_push_frame( &__f.frame ); \
206 __first = 0; \
208 } while (0);
210 #define __FINALLY_CTX(func, context) \
211 } while(0); \
212 __wine_pop_frame( &__f.frame ); \
213 (func)(1, context); \
214 break; \
215 } else { \
216 __f.frame.Handler = __wine_finally_ctx_handler; \
217 __f.u.finally_func_ctx = (func); \
218 __f.ctx = context; \
219 __wine_push_frame( &__f.frame ); \
220 __first = 0; \
222 } while (0);
225 typedef LONG (CALLBACK *__WINE_FILTER)(PEXCEPTION_POINTERS);
226 typedef LONG (CALLBACK *__WINE_FILTER_CTX)(PEXCEPTION_POINTERS, void*);
227 typedef void (CALLBACK *__WINE_FINALLY)(BOOL);
228 typedef void (CALLBACK *__WINE_FINALLY_CTX)(BOOL, void*);
230 #define GetExceptionInformation() (__eptr)
231 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
232 #define AbnormalTermination() (!__normal)
234 typedef struct __tagWINE_FRAME
236 EXCEPTION_REGISTRATION_RECORD frame;
237 union
239 /* exception data */
240 __WINE_FILTER filter;
241 __WINE_FILTER_CTX filter_ctx;
242 /* finally data */
243 __WINE_FINALLY finally_func;
244 __WINE_FINALLY_CTX finally_func_ctx;
245 } u;
246 void *ctx;
247 __wine_jmp_buf jmp;
248 /* hack to make GetExceptionCode() work in handler */
249 DWORD ExceptionCode;
250 const struct __tagWINE_FRAME *ExceptionRecord;
251 } __WINE_FRAME;
253 #endif /* USE_COMPILER_EXCEPTIONS */
255 static inline EXCEPTION_REGISTRATION_RECORD *__wine_push_frame( EXCEPTION_REGISTRATION_RECORD *frame )
257 #if defined(__GNUC__) && defined(__i386__)
258 EXCEPTION_REGISTRATION_RECORD *prev;
259 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
260 "\n\tmovl %0,(%1)"
261 "\n\t.byte 0x64\n\tmovl %1,(0)"
262 : "=&r" (prev) : "r" (frame) : "memory" );
263 return prev;
264 #else
265 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
266 frame->Prev = teb->ExceptionList;
267 teb->ExceptionList = frame;
268 return frame->Prev;
269 #endif
272 static inline EXCEPTION_REGISTRATION_RECORD *__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD *frame )
274 #if defined(__GNUC__) && defined(__i386__)
275 __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
276 : : "r" (frame->Prev) : "memory" );
277 return frame->Prev;
279 #else
280 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
281 teb->ExceptionList = frame->Prev;
282 return frame->Prev;
283 #endif
286 static inline EXCEPTION_REGISTRATION_RECORD *__wine_get_frame(void)
288 #if defined(__GNUC__) && defined(__i386__)
289 EXCEPTION_REGISTRATION_RECORD *ret;
290 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0" : "=r" (ret) );
291 return ret;
292 #else
293 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
294 return teb->ExceptionList;
295 #endif
298 /* Exception handling flags - from OS/2 2.0 exception handling */
300 /* Win32 seems to use the same flags as ExceptionFlags in an EXCEPTION_RECORD */
301 #define EH_NONCONTINUABLE 0x01
302 #define EH_UNWINDING 0x02
303 #define EH_EXIT_UNWIND 0x04
304 #define EH_STACK_INVALID 0x08
305 #define EH_NESTED_CALL 0x10
306 #define EH_TARGET_UNWIND 0x20
307 #define EH_COLLIDED_UNWIND 0x40
309 /* Wine-specific exceptions codes */
311 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
312 #define EXCEPTION_WINE_ASSERTION 0x80000101 /* assertion failed */
314 #ifdef __cplusplus
316 #endif
318 #endif /* __WINE_WINE_EXCEPTION_H */