setupapi/tests: Use FIELD_OFFSET instead of offsetof.
[wine] / include / wine / exception.h
1 /*
2  * Wine exception handling
3  *
4  * Copyright (c) 1999 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #ifndef __WINE_WINE_EXCEPTION_H
22 #define __WINE_WINE_EXCEPTION_H
23
24 #include <setjmp.h>
25 #include <windef.h>
26 #include <excpt.h>
27
28 /* The following definitions allow using exceptions in Wine and Winelib code
29  *
30  * They should be used like this:
31  *
32  * __TRY
33  * {
34  *     do some stuff that can raise an exception
35  * }
36  * __EXCEPT(filter_func,param)
37  * {
38  *     handle the exception here
39  * }
40  * __ENDTRY
41  *
42  * or
43  *
44  * __TRY
45  * {
46  *     do some stuff that can raise an exception
47  * }
48  * __FINALLY(finally_func,param)
49  *
50  * The filter_func must be defined with the WINE_EXCEPTION_FILTER
51  * macro, and return one of the EXCEPTION_* code; it can use
52  * GetExceptionInformation and GetExceptionCode to retrieve the
53  * exception info.
54  *
55  * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
56  *
57  * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
58  *          break out of the current block. You cannot use 'return', 'goto'
59  *          or 'longjmp' to leave a __TRY block, as this will surely crash.
60  *          You can use them to leave a __EXCEPT block though.
61  *
62  * -- AJ
63  */
64
65 /* Define this if you want to use your compiler built-in __try/__except support.
66  * This is only useful when compiling to a native Windows binary, as the built-in
67  * compiler exceptions will most certainly not work under Winelib.
68  */
69 #ifdef USE_COMPILER_EXCEPTIONS
70
71 #define __TRY __try
72 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
73 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
74 #define __ENDTRY /*nothing*/
75 #define __EXCEPT_PAGE_FAULT __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
76 #define __EXCEPT_ALL __except(EXCEPTION_EXECUTE_HANDLER)
77
78 #else  /* USE_COMPILER_EXCEPTIONS */
79
80 #ifndef __GNUC__
81 #define __attribute__(x) /* nothing */
82 #endif
83
84 #define __TRY \
85     do { __WINE_FRAME __f; \
86          int __first = 1; \
87          for (;;) if (!__first) \
88          { \
89              do {
90
91 #define __EXCEPT(func) \
92              } while(0); \
93              __wine_pop_frame( &__f.frame ); \
94              break; \
95          } else { \
96              __f.frame.Handler = __wine_exception_handler; \
97              __f.u.filter = (func); \
98              __wine_push_frame( &__f.frame ); \
99              if (sigsetjmp( __f.jmp, 0 )) { \
100                  const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
101                  do {
102
103 #define __ENDTRY \
104                  } while (0); \
105                  break; \
106              } \
107              __first = 0; \
108          } \
109     } while (0);
110
111 #define __FINALLY(func) \
112              } while(0); \
113              __wine_pop_frame( &__f.frame ); \
114              (func)(1); \
115              break; \
116          } else { \
117              __f.frame.Handler = __wine_finally_handler; \
118              __f.u.finally_func = (func); \
119              __wine_push_frame( &__f.frame ); \
120              __first = 0; \
121          } \
122     } while (0);
123
124
125 typedef LONG (CALLBACK *__WINE_FILTER)(PEXCEPTION_POINTERS);
126 typedef void (CALLBACK *__WINE_FINALLY)(BOOL);
127
128 /* convenience handler for page fault exceptions */
129 #define __EXCEPT_PAGE_FAULT __EXCEPT( (__WINE_FILTER)1 )
130 /* convenience handler for all exception */
131 #define __EXCEPT_ALL __EXCEPT( NULL )
132
133 #define GetExceptionInformation() (__eptr)
134 #define GetExceptionCode()        (__eptr->ExceptionRecord->ExceptionCode)
135 #define AbnormalTermination()     (!__normal)
136
137 typedef struct __tagWINE_FRAME
138 {
139     EXCEPTION_REGISTRATION_RECORD frame;
140     union
141     {
142         /* exception data */
143         __WINE_FILTER filter;
144         /* finally data */
145         __WINE_FINALLY finally_func;
146     } u;
147     sigjmp_buf jmp;
148     /* hack to make GetExceptionCode() work in handler */
149     DWORD ExceptionCode;
150     const struct __tagWINE_FRAME *ExceptionRecord;
151 } __WINE_FRAME;
152
153 extern DWORD __wine_exception_handler( PEXCEPTION_RECORD record, EXCEPTION_REGISTRATION_RECORD *frame,
154                                        CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher );
155 extern DWORD __wine_finally_handler( PEXCEPTION_RECORD record, EXCEPTION_REGISTRATION_RECORD *frame,
156                                      CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher );
157
158 #endif /* USE_COMPILER_EXCEPTIONS */
159
160 static inline EXCEPTION_REGISTRATION_RECORD *__wine_push_frame( EXCEPTION_REGISTRATION_RECORD *frame )
161 {
162 #if defined(__GNUC__) && defined(__i386__)
163     EXCEPTION_REGISTRATION_RECORD *prev;
164     __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
165                          "\n\tmovl %0,(%1)"
166                          "\n\t.byte 0x64\n\tmovl %1,(0)"
167                          : "=&r" (prev) : "r" (frame) : "memory" );
168     return prev;
169 #else
170     NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
171     frame->Prev = (void *)teb->ExceptionList;
172     teb->ExceptionList = (void *)frame;
173     return frame->Prev;
174 #endif
175 }
176
177 static inline EXCEPTION_REGISTRATION_RECORD *__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD *frame )
178 {
179 #if defined(__GNUC__) && defined(__i386__)
180     __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
181                          : : "r" (frame->Prev) : "memory" );
182     return frame->Prev;
183
184 #else
185     NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
186     teb->ExceptionList = (void *)frame->Prev;
187     return frame->Prev;
188 #endif
189 }
190
191 /* Exception handling flags - from OS/2 2.0 exception handling */
192
193 /* Win32 seems to use the same flags as ExceptionFlags in an EXCEPTION_RECORD */
194 #define EH_NONCONTINUABLE   0x01
195 #define EH_UNWINDING        0x02
196 #define EH_EXIT_UNWIND      0x04
197 #define EH_STACK_INVALID    0x08
198 #define EH_NESTED_CALL      0x10
199
200 /* Wine-specific exceptions codes */
201
202 #define EXCEPTION_WINE_STUB       0x80000100  /* stub entry point called */
203 #define EXCEPTION_WINE_ASSERTION  0x80000101  /* assertion failed */
204
205 /* unhandled return status from vm86 mode */
206 #define EXCEPTION_VM86_INTx       0x80000110
207 #define EXCEPTION_VM86_STI        0x80000111
208 #define EXCEPTION_VM86_PICRETURN  0x80000112
209
210 extern void __wine_enter_vm86( CONTEXT *context );
211
212 #endif  /* __WINE_WINE_EXCEPTION_H */