~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/include/wine/exception.h

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  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 #ifdef __cplusplus
 29 extern "C" {
 30 #endif
 31 
 32 /* The following definitions allow using exceptions in Wine and Winelib code
 33  *
 34  * They should be used like this:
 35  *
 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
 45  *
 46  * or
 47  *
 48  * __TRY
 49  * {
 50  *     do some stuff that can raise an exception
 51  * }
 52  * __FINALLY(finally_func)
 53  *
 54  * The filter_func and finally_func functions must be defined like this:
 55  *
 56  * LONG CALLBACK filter_func( PEXCEPTION_POINTERS __eptr ) { ... }
 57  *
 58  * void CALLBACK finally_func( BOOL __normal ) { ... }
 59  *
 60  * The filter function must return one of the EXCEPTION_* code; it can
 61  * use GetExceptionInformation() and GetExceptionCode() to retrieve the
 62  * exception info.
 63  *
 64  * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
 65  *          break out of the current block. You cannot use 'return', 'goto'
 66  *          or 'longjmp' to leave a __TRY block, as this will surely crash.
 67  *          You can use them to leave a __EXCEPT block though.
 68  *
 69  * -- AJ
 70  */
 71 
 72 /* Define this if you want to use your compiler built-in __try/__except support.
 73  * This is only useful when compiling to a native Windows binary, as the built-in
 74  * compiler exceptions will most certainly not work under Winelib.
 75  */
 76 #ifdef USE_COMPILER_EXCEPTIONS
 77 
 78 #define __TRY __try
 79 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
 80 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
 81 #define __ENDTRY /*nothing*/
 82 #define __EXCEPT_PAGE_FAULT __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
 83 #define __EXCEPT_ALL __except(EXCEPTION_EXECUTE_HANDLER)
 84 
 85 #else  /* USE_COMPILER_EXCEPTIONS */
 86 
 87 #ifndef __GNUC__
 88 #define __attribute__(x) /* nothing */
 89 #endif
 90 
 91 #if defined(__MINGW32__) || defined(__CYGWIN__)
 92 #define sigjmp_buf jmp_buf
 93 #define sigsetjmp(buf,sigs) setjmp(buf)
 94 #define siglongjmp(buf,val) longjmp(buf,val)
 95 #endif
 96 
 97 #define __TRY \
 98     do { __WINE_FRAME __f; \
 99          int __first = 1; \
100          for (;;) if (!__first) \
101          { \
102              do {
103 
104 #define __EXCEPT(func) \
105              } while(0); \
106              __wine_pop_frame( &__f.frame ); \
107              break; \
108          } else { \
109              __f.frame.Handler = __wine_exception_handler; \
110              __f.u.filter = (func); \
111              if (sigsetjmp( __f.jmp, 0 )) { \
112                  const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
113                  do {
114 
115 /* convenience handler for page fault exceptions */
116 #define __EXCEPT_PAGE_FAULT \
117              } while(0); \
118              __wine_pop_frame( &__f.frame ); \
119              break; \
120          } else { \
121              __f.frame.Handler = __wine_exception_handler_page_fault; \
122              if (sigsetjmp( __f.jmp, 0 )) { \
123                  const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
124                  do {
125 
126 /* convenience handler for all exception */
127 #define __EXCEPT_ALL \
128              } while(0); \
129              __wine_pop_frame( &__f.frame ); \
130              break; \
131          } else { \
132              __f.frame.Handler = __wine_exception_handler_all; \
133              if (sigsetjmp( __f.jmp, 0 )) { \
134                  const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
135                  do {
136 
137 #define __ENDTRY \
138                  } while (0); \
139                  break; \
140              } \
141              __wine_push_frame( &__f.frame ); \
142              __first = 0; \
143          } \
144     } while (0);
145 
146 #define __FINALLY(func) \
147              } while(0); \
148              __wine_pop_frame( &__f.frame ); \
149              (func)(1); \
150              break; \
151          } else { \
152              __f.frame.Handler = __wine_finally_handler; \
153              __f.u.finally_func = (func); \
154              __wine_push_frame( &__f.frame ); \
155              __first = 0; \
156          } \
157     } while (0);
158 
159 
160 typedef LONG (CALLBACK *__WINE_FILTER)(PEXCEPTION_POINTERS);
161 typedef void (CALLBACK *__WINE_FINALLY)(BOOL);
162 
163 #define GetExceptionInformation() (__eptr)
164 #define GetExceptionCode()        (__eptr->ExceptionRecord->ExceptionCode)
165 #define AbnormalTermination()     (!__normal)
166 
167 typedef struct __tagWINE_FRAME
168 {
169     EXCEPTION_REGISTRATION_RECORD frame;
170     union
171     {
172         /* exception data */
173         __WINE_FILTER filter;
174         /* finally data */
175         __WINE_FINALLY finally_func;
176     } u;
177     sigjmp_buf jmp;
178     /* hack to make GetExceptionCode() work in handler */
179     DWORD ExceptionCode;
180     const struct __tagWINE_FRAME *ExceptionRecord;
181 } __WINE_FRAME;
182 
183 #endif /* USE_COMPILER_EXCEPTIONS */
184 
185 static inline EXCEPTION_REGISTRATION_RECORD *__wine_push_frame( EXCEPTION_REGISTRATION_RECORD *frame )
186 {
187 #if defined(__GNUC__) && defined(__i386__)
188     EXCEPTION_REGISTRATION_RECORD *prev;
189     __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
190                          "\n\tmovl %0,(%1)"
191                          "\n\t.byte 0x64\n\tmovl %1,(0)"
192                          : "=&r" (prev) : "r" (frame) : "memory" );
193     return prev;
194 #else
195     NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
196     frame->Prev = teb->ExceptionList;
197     teb->ExceptionList = frame;
198     return frame->Prev;
199 #endif
200 }
201 
202 static inline EXCEPTION_REGISTRATION_RECORD *__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD *frame )
203 {
204 #if defined(__GNUC__) && defined(__i386__)
205     __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
206                          : : "r" (frame->Prev) : "memory" );
207     return frame->Prev;
208 
209 #else
210     NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
211     teb->ExceptionList = frame->Prev;
212     return frame->Prev;
213 #endif
214 }
215 
216 static inline EXCEPTION_REGISTRATION_RECORD *__wine_get_frame(void)
217 {
218 #if defined(__GNUC__) && defined(__i386__)
219     EXCEPTION_REGISTRATION_RECORD *ret;
220     __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0" : "=r" (ret) );
221     return ret;
222 #else
223     NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
224     return teb->ExceptionList;
225 #endif
226 }
227 
228 /* Exception handling flags - from OS/2 2.0 exception handling */
229 
230 /* Win32 seems to use the same flags as ExceptionFlags in an EXCEPTION_RECORD */
231 #define EH_NONCONTINUABLE   0x01
232 #define EH_UNWINDING        0x02
233 #define EH_EXIT_UNWIND      0x04
234 #define EH_STACK_INVALID    0x08
235 #define EH_NESTED_CALL      0x10
236 
237 /* Wine-specific exceptions codes */
238 
239 #define EXCEPTION_WINE_STUB       0x80000100  /* stub entry point called */
240 #define EXCEPTION_WINE_ASSERTION  0x80000101  /* assertion failed */
241 
242 /* unhandled return status from vm86 mode */
243 #define EXCEPTION_VM86_INTx       0x80000110
244 #define EXCEPTION_VM86_STI        0x80000111
245 #define EXCEPTION_VM86_PICRETURN  0x80000112
246 
247 extern void __wine_enter_vm86( CONTEXT *context );
248 
249 #ifndef USE_COMPILER_EXCEPTIONS
250 
251 NTSYSAPI void WINAPI RtlUnwind(PVOID,PVOID,PEXCEPTION_RECORD,PVOID);
252 
253 static inline void DECLSPEC_NORETURN __wine_unwind_target(void)
254 {
255     __WINE_FRAME *wine_frame = (__WINE_FRAME *)__wine_get_frame();
256     __wine_pop_frame( &wine_frame->frame );
257     siglongjmp( wine_frame->jmp, 1 );
258 }
259 
260 /* wrapper for RtlUnwind since it clobbers registers on Windows */
261 static inline void DECLSPEC_NORETURN __wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD* frame,
262                                                         EXCEPTION_RECORD *record,
263                                                         void (*target)(void) )
264 {
265 #if defined(__GNUC__) && defined(__i386__)
266     int dummy1, dummy2, dummy3, dummy4;
267     __asm__ __volatile__("pushl %%ebp\n\t"
268                          "pushl %%ebx\n\t"
269                          "pushl $0\n\t"
270                          "pushl %3\n\t"
271                          "pushl %2\n\t"
272                          "pushl %1\n\t"
273                          "call *%0\n\t"
274                          "popl %%ebx\n\t"
275                          "popl %%ebp"
276                          : "=a" (dummy1), "=S" (dummy2), "=D" (dummy3), "=c" (dummy4)
277                          : "" (RtlUnwind), "1" (frame), "2" (target), "3" (record)
278                          : "edx", "memory" );
279 #else
280     RtlUnwind( frame, target, record, 0 );
281 #endif
282     for (;;) target();
283 }
284 
285 static inline void DECLSPEC_NORETURN __wine_unwind_frame( EXCEPTION_RECORD *record,
286                                                           EXCEPTION_REGISTRATION_RECORD *frame )
287 {
288     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
289 
290     /* hack to make GetExceptionCode() work in handler */
291     wine_frame->ExceptionCode   = record->ExceptionCode;
292     wine_frame->ExceptionRecord = wine_frame;
293 
294     __wine_rtl_unwind( frame, record, __wine_unwind_target );
295 }
296 
297 static inline DWORD __wine_exception_handler( EXCEPTION_RECORD *record,
298                                               EXCEPTION_REGISTRATION_RECORD *frame,
299                                               CONTEXT *context,
300                                               EXCEPTION_REGISTRATION_RECORD **pdispatcher )
301 {
302     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
303     EXCEPTION_POINTERS ptrs;
304 
305     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
306         return ExceptionContinueSearch;
307 
308     ptrs.ExceptionRecord = record;
309     ptrs.ContextRecord = context;
310     switch(wine_frame->u.filter( &ptrs ))
311     {
312     case EXCEPTION_CONTINUE_SEARCH:
313         return ExceptionContinueSearch;
314     case EXCEPTION_CONTINUE_EXECUTION:
315         return ExceptionContinueExecution;
316     case EXCEPTION_EXECUTE_HANDLER:
317         break;
318     }
319     __wine_unwind_frame( record, frame );
320 }
321 
322 static inline DWORD __wine_exception_handler_page_fault( EXCEPTION_RECORD *record,
323                                                          EXCEPTION_REGISTRATION_RECORD *frame,
324                                                          CONTEXT *context,
325                                                          EXCEPTION_REGISTRATION_RECORD **pdispatcher )
326 {
327     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
328         return ExceptionContinueSearch;
329     if (record->ExceptionCode != STATUS_ACCESS_VIOLATION)
330         return ExceptionContinueSearch;
331     __wine_unwind_frame( record, frame );
332 }
333 
334 static inline DWORD __wine_exception_handler_all( EXCEPTION_RECORD *record,
335                                                   EXCEPTION_REGISTRATION_RECORD *frame,
336                                                   CONTEXT *context,
337                                                   EXCEPTION_REGISTRATION_RECORD **pdispatcher )
338 {
339     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
340         return ExceptionContinueSearch;
341     __wine_unwind_frame( record, frame );
342 }
343 
344 static inline DWORD __wine_finally_handler( EXCEPTION_RECORD *record,
345                                             EXCEPTION_REGISTRATION_RECORD *frame,
346                                             CONTEXT *context,
347                                             EXCEPTION_REGISTRATION_RECORD **pdispatcher )
348 {
349     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
350     {
351         __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
352         wine_frame->u.finally_func( FALSE );
353     }
354     return ExceptionContinueSearch;
355 }
356 
357 #endif /* USE_COMPILER_EXCEPTIONS */
358 
359 #ifdef __cplusplus
360 }
361 #endif
362 
363 #endif  /* __WINE_WINE_EXCEPTION_H */
364 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.