push 94e35525417ba0671ead00a6c6dfaaed30f46234
[wine/hacks.git] / include / wine / exception.h
blob7e0d5bc990848f1a3551676d822d729fab1f796d
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,param)
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,param)
54 * The filter_func must be defined with the WINE_EXCEPTION_FILTER
55 * macro, and return one of the EXCEPTION_* code; it can use
56 * GetExceptionInformation and GetExceptionCode to retrieve the
57 * exception info.
59 * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
61 * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
62 * break out of the current block. You cannot use 'return', 'goto'
63 * or 'longjmp' to leave a __TRY block, as this will surely crash.
64 * You can use them to leave a __EXCEPT block though.
66 * -- AJ
69 /* Define this if you want to use your compiler built-in __try/__except support.
70 * This is only useful when compiling to a native Windows binary, as the built-in
71 * compiler exceptions will most certainly not work under Winelib.
73 #ifdef USE_COMPILER_EXCEPTIONS
75 #define __TRY __try
76 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
77 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
78 #define __ENDTRY /*nothing*/
79 #define __EXCEPT_PAGE_FAULT __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
80 #define __EXCEPT_ALL __except(EXCEPTION_EXECUTE_HANDLER)
82 #else /* USE_COMPILER_EXCEPTIONS */
84 #ifndef __GNUC__
85 #define __attribute__(x) /* nothing */
86 #endif
88 #if defined(__MINGW32__) || defined(__CYGWIN__)
89 #define sigjmp_buf jmp_buf
90 #define sigsetjmp(buf,sigs) setjmp(buf)
91 #define siglongjmp(buf,val) longjmp(buf,val)
92 #endif
94 #define __TRY \
95 do { __WINE_FRAME __f; \
96 int __first = 1; \
97 for (;;) if (!__first) \
98 { \
99 do {
101 #define __EXCEPT(func) \
102 } while(0); \
103 __wine_pop_frame( &__f.frame ); \
104 break; \
105 } else { \
106 __f.frame.Handler = __wine_exception_handler; \
107 __f.u.filter = (func); \
108 if (sigsetjmp( __f.jmp, 0 )) { \
109 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
110 do {
112 /* convenience handler for page fault exceptions */
113 #define __EXCEPT_PAGE_FAULT \
114 } while(0); \
115 __wine_pop_frame( &__f.frame ); \
116 break; \
117 } else { \
118 __f.frame.Handler = __wine_exception_handler_page_fault; \
119 if (sigsetjmp( __f.jmp, 0 )) { \
120 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
121 do {
123 /* convenience handler for all exception */
124 #define __EXCEPT_ALL \
125 } while(0); \
126 __wine_pop_frame( &__f.frame ); \
127 break; \
128 } else { \
129 __f.frame.Handler = __wine_exception_handler_all; \
130 if (sigsetjmp( __f.jmp, 0 )) { \
131 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
132 do {
134 #define __ENDTRY \
135 } while (0); \
136 break; \
138 __wine_push_frame( &__f.frame ); \
139 __first = 0; \
141 } while (0);
143 #define __FINALLY(func) \
144 } while(0); \
145 __wine_pop_frame( &__f.frame ); \
146 (func)(1); \
147 break; \
148 } else { \
149 __f.frame.Handler = __wine_finally_handler; \
150 __f.u.finally_func = (func); \
151 __wine_push_frame( &__f.frame ); \
152 __first = 0; \
154 } while (0);
157 typedef LONG (CALLBACK *__WINE_FILTER)(PEXCEPTION_POINTERS);
158 typedef void (CALLBACK *__WINE_FINALLY)(BOOL);
160 #define GetExceptionInformation() (__eptr)
161 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
162 #define AbnormalTermination() (!__normal)
164 typedef struct __tagWINE_FRAME
166 EXCEPTION_REGISTRATION_RECORD frame;
167 union
169 /* exception data */
170 __WINE_FILTER filter;
171 /* finally data */
172 __WINE_FINALLY finally_func;
173 } u;
174 sigjmp_buf jmp;
175 /* hack to make GetExceptionCode() work in handler */
176 DWORD ExceptionCode;
177 const struct __tagWINE_FRAME *ExceptionRecord;
178 } __WINE_FRAME;
180 #endif /* USE_COMPILER_EXCEPTIONS */
182 static inline EXCEPTION_REGISTRATION_RECORD *__wine_push_frame( EXCEPTION_REGISTRATION_RECORD *frame )
184 #if defined(__GNUC__) && defined(__i386__)
185 EXCEPTION_REGISTRATION_RECORD *prev;
186 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
187 "\n\tmovl %0,(%1)"
188 "\n\t.byte 0x64\n\tmovl %1,(0)"
189 : "=&r" (prev) : "r" (frame) : "memory" );
190 return prev;
191 #else
192 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
193 frame->Prev = teb->ExceptionList;
194 teb->ExceptionList = frame;
195 return frame->Prev;
196 #endif
199 static inline EXCEPTION_REGISTRATION_RECORD *__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD *frame )
201 #if defined(__GNUC__) && defined(__i386__)
202 __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
203 : : "r" (frame->Prev) : "memory" );
204 return frame->Prev;
206 #else
207 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
208 teb->ExceptionList = frame->Prev;
209 return frame->Prev;
210 #endif
213 /* Exception handling flags - from OS/2 2.0 exception handling */
215 /* Win32 seems to use the same flags as ExceptionFlags in an EXCEPTION_RECORD */
216 #define EH_NONCONTINUABLE 0x01
217 #define EH_UNWINDING 0x02
218 #define EH_EXIT_UNWIND 0x04
219 #define EH_STACK_INVALID 0x08
220 #define EH_NESTED_CALL 0x10
222 /* Wine-specific exceptions codes */
224 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
225 #define EXCEPTION_WINE_ASSERTION 0x80000101 /* assertion failed */
227 /* unhandled return status from vm86 mode */
228 #define EXCEPTION_VM86_INTx 0x80000110
229 #define EXCEPTION_VM86_STI 0x80000111
230 #define EXCEPTION_VM86_PICRETURN 0x80000112
232 extern void __wine_enter_vm86( CONTEXT *context );
234 #ifndef USE_COMPILER_EXCEPTIONS
236 NTSYSAPI void WINAPI RtlUnwind(PVOID,PVOID,PEXCEPTION_RECORD,PVOID);
238 /* wrapper for RtlUnwind since it clobbers registers on Windows */
239 static inline void __wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD* frame, EXCEPTION_RECORD *record )
241 #if defined(__GNUC__) && defined(__i386__)
242 int dummy1, dummy2, dummy3;
243 __asm__ __volatile__("pushl %%ebp\n\t"
244 "pushl %%ebx\n\t"
245 "pushl $0\n\t"
246 "pushl %2\n\t"
247 "pushl $0\n\t"
248 "pushl %1\n\t"
249 "call *%0\n\t"
250 "popl %%ebx\n\t"
251 "popl %%ebp"
252 : "=a" (dummy1), "=S" (dummy2), "=D" (dummy3)
253 : "0" (RtlUnwind), "1" (frame), "2" (record)
254 : "ecx", "edx", "memory" );
255 #else
256 RtlUnwind( frame, 0, record, 0 );
257 #endif
260 static inline void DECLSPEC_NORETURN __wine_unwind_frame( EXCEPTION_RECORD *record,
261 EXCEPTION_REGISTRATION_RECORD *frame )
263 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
265 /* hack to make GetExceptionCode() work in handler */
266 wine_frame->ExceptionCode = record->ExceptionCode;
267 wine_frame->ExceptionRecord = wine_frame;
269 __wine_rtl_unwind( frame, record );
270 __wine_pop_frame( frame );
271 siglongjmp( wine_frame->jmp, 1 );
274 static inline DWORD __wine_exception_handler( EXCEPTION_RECORD *record,
275 EXCEPTION_REGISTRATION_RECORD *frame,
276 CONTEXT *context,
277 EXCEPTION_REGISTRATION_RECORD **pdispatcher )
279 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
280 EXCEPTION_POINTERS ptrs;
282 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
283 return ExceptionContinueSearch;
285 ptrs.ExceptionRecord = record;
286 ptrs.ContextRecord = context;
287 switch(wine_frame->u.filter( &ptrs ))
289 case EXCEPTION_CONTINUE_SEARCH:
290 return ExceptionContinueSearch;
291 case EXCEPTION_CONTINUE_EXECUTION:
292 return ExceptionContinueExecution;
293 case EXCEPTION_EXECUTE_HANDLER:
294 break;
296 __wine_unwind_frame( record, frame );
299 static inline DWORD __wine_exception_handler_page_fault( EXCEPTION_RECORD *record,
300 EXCEPTION_REGISTRATION_RECORD *frame,
301 CONTEXT *context,
302 EXCEPTION_REGISTRATION_RECORD **pdispatcher )
304 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
305 return ExceptionContinueSearch;
306 if (record->ExceptionCode != STATUS_ACCESS_VIOLATION)
307 return ExceptionContinueSearch;
308 __wine_unwind_frame( record, frame );
311 static inline DWORD __wine_exception_handler_all( EXCEPTION_RECORD *record,
312 EXCEPTION_REGISTRATION_RECORD *frame,
313 CONTEXT *context,
314 EXCEPTION_REGISTRATION_RECORD **pdispatcher )
316 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
317 return ExceptionContinueSearch;
318 __wine_unwind_frame( record, frame );
321 static inline DWORD __wine_finally_handler( EXCEPTION_RECORD *record,
322 EXCEPTION_REGISTRATION_RECORD *frame,
323 CONTEXT *context,
324 EXCEPTION_REGISTRATION_RECORD **pdispatcher )
326 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
328 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
329 wine_frame->u.finally_func( FALSE );
331 return ExceptionContinueSearch;
334 #endif /* USE_COMPILER_EXCEPTIONS */
336 #ifdef __cplusplus
338 #endif
340 #endif /* __WINE_WINE_EXCEPTION_H */