| 1 | /* $Id: exception.h,v 1.3 2003-01-15 10:41:13 sandervl Exp $ */
 | 
|---|
| 2 | /*
 | 
|---|
| 3 |  * Wine exception handling
 | 
|---|
| 4 |  *
 | 
|---|
| 5 |  * Copyright (c) 1999 Alexandre Julliard
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef __WINE_WINE_EXCEPTION_H
 | 
|---|
| 9 | #define __WINE_WINE_EXCEPTION_H
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #include <setjmp.h>
 | 
|---|
| 12 | #include "winnt.h"
 | 
|---|
| 13 | #include "thread.h"
 | 
|---|
| 14 | 
 | 
|---|
| 15 | /* The following definitions allow using exceptions in Wine and Winelib code
 | 
|---|
| 16 |  *
 | 
|---|
| 17 |  * They should be used like this:
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  * __TRY
 | 
|---|
| 20 |  * {
 | 
|---|
| 21 |  *     do some stuff that can raise an exception
 | 
|---|
| 22 |  * }
 | 
|---|
| 23 |  * __EXCEPT(filter_func,param)
 | 
|---|
| 24 |  * {
 | 
|---|
| 25 |  *     handle the exception here
 | 
|---|
| 26 |  * }
 | 
|---|
| 27 |  * __ENDTRY
 | 
|---|
| 28 |  *
 | 
|---|
| 29 |  * or
 | 
|---|
| 30 |  *
 | 
|---|
| 31 |  * __TRY
 | 
|---|
| 32 |  * {
 | 
|---|
| 33 |  *     do some stuff that can raise an exception
 | 
|---|
| 34 |  * }
 | 
|---|
| 35 |  * __FINALLY(finally_func,param)
 | 
|---|
| 36 |  *
 | 
|---|
| 37 |  * The filter_func must be defined with the WINE_EXCEPTION_FILTER
 | 
|---|
| 38 |  * macro, and return one of the EXCEPTION_* code; it can use
 | 
|---|
| 39 |  * GetExceptionInformation and GetExceptionCode to retrieve the
 | 
|---|
| 40 |  * exception info.
 | 
|---|
| 41 |  *
 | 
|---|
| 42 |  * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
 | 
|---|
| 43 |  *
 | 
|---|
| 44 |  * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
 | 
|---|
| 45 |  *          break out of the current block. You cannot use 'return', 'goto'
 | 
|---|
| 46 |  *          or 'longjmp' to leave a __TRY block, as this will surely crash.
 | 
|---|
| 47 |  *          You can use them to leave a __EXCEPT block though.
 | 
|---|
| 48 |  *
 | 
|---|
| 49 |  * -- AJ
 | 
|---|
| 50 |  */
 | 
|---|
| 51 | 
 | 
|---|
| 52 | /* Define this if you want to use your compiler built-in __try/__except support.
 | 
|---|
| 53 |  * This is only useful when compiling to a native Windows binary, as the built-in
 | 
|---|
| 54 |  * compiler exceptions will most certainly not work under Winelib.
 | 
|---|
| 55 |  */
 | 
|---|
| 56 | //#ifdef USE_COMPILER_EXCEPTIONS
 | 
|---|
| 57 | #if 1
 | 
|---|
| 58 | #define __TRY if(1)
 | 
|---|
| 59 | #define __EXCEPT_PAGE_FAULT else
 | 
|---|
| 60 | #define __EXCEPT(func) else
 | 
|---|
| 61 | //#define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
 | 
|---|
| 62 | #define __ENDTRY /*nothing*/
 | 
|---|
| 63 | 
 | 
|---|
| 64 | #else  /* USE_COMPILER_EXCEPTIONS */
 | 
|---|
| 65 | 
 | 
|---|
| 66 | #define __TRY \
 | 
|---|
| 67 |     do { __WINE_FRAME __f; \
 | 
|---|
| 68 |          int __first = 1; \
 | 
|---|
| 69 |          for (;;) if (!__first) \
 | 
|---|
| 70 |          { \
 | 
|---|
| 71 |              do {
 | 
|---|
| 72 | 
 | 
|---|
| 73 | #define __EXCEPT(func) \
 | 
|---|
| 74 |              } while(0); \
 | 
|---|
| 75 |              EXC_pop_frame( &__f.frame ); \
 | 
|---|
| 76 |              break; \
 | 
|---|
| 77 |          } else { \
 | 
|---|
| 78 |              __f.frame.Handler = (PEXCEPTION_HANDLER)WINE_exception_handler; \
 | 
|---|
| 79 |              __f.u.filter = (func); \
 | 
|---|
| 80 |              __wine_push_frame( &__f.frame ); \
 | 
|---|
| 81 |              if (setjmp( __f.jmp)) { \
 | 
|---|
| 82 |                  const __WINE_FRAME * const __eptr WINE_UNUSED = &__f; \
 | 
|---|
| 83 |                  do {
 | 
|---|
| 84 | 
 | 
|---|
| 85 | #define __ENDTRY \
 | 
|---|
| 86 |                  } while (0); \
 | 
|---|
| 87 |                  break; \
 | 
|---|
| 88 |              } \
 | 
|---|
| 89 |              __first = 0; \
 | 
|---|
| 90 |          } \
 | 
|---|
| 91 |     } while (0);
 | 
|---|
| 92 | 
 | 
|---|
| 93 | #define __FINALLY(func) \
 | 
|---|
| 94 |              } while(0); \
 | 
|---|
| 95 |              EXC_pop_frame( &__f.frame ); \
 | 
|---|
| 96 |              (func)(1); \
 | 
|---|
| 97 |              break; \
 | 
|---|
| 98 |          } else { \
 | 
|---|
| 99 |              __f.frame.Handler = (PEXCEPTION_HANDLER)WINE_finally_handler; \
 | 
|---|
| 100 |              __f.u.finally_func = (func); \
 | 
|---|
| 101 |              __wine_push_frame( &__f.frame ); \
 | 
|---|
| 102 |              __first = 0; \
 | 
|---|
| 103 |          } \
 | 
|---|
| 104 |     } while (0);
 | 
|---|
| 105 | 
 | 
|---|
| 106 | 
 | 
|---|
| 107 | typedef DWORD (*CALLBACK __WINE_FILTER)(PEXCEPTION_POINTERS);
 | 
|---|
| 108 | typedef void (*CALLBACK __WINE_FINALLY)(BOOL);
 | 
|---|
| 109 | 
 | 
|---|
| 110 | #define WINE_EXCEPTION_FILTER(func) DWORD WINAPI func( EXCEPTION_POINTERS *__eptr )
 | 
|---|
| 111 | #define WINE_FINALLY_FUNC(func) void WINAPI func( BOOL __normal )
 | 
|---|
| 112 | 
 | 
|---|
| 113 | #define GetExceptionInformation() (__eptr)
 | 
|---|
| 114 | #define GetExceptionCode()        (__eptr->ExceptionRecord->ExceptionCode)
 | 
|---|
| 115 | #define AbnormalTermination()     (!__normal)
 | 
|---|
| 116 | 
 | 
|---|
| 117 | typedef struct __tagWINE_FRAME
 | 
|---|
| 118 | {
 | 
|---|
| 119 |     EXCEPTION_FRAME frame;
 | 
|---|
| 120 |     union
 | 
|---|
| 121 |     {
 | 
|---|
| 122 |         /* exception data */
 | 
|---|
| 123 |         __WINE_FILTER filter;
 | 
|---|
| 124 |         /* finally data */
 | 
|---|
| 125 |         __WINE_FINALLY finally_func;
 | 
|---|
| 126 |     } u;
 | 
|---|
| 127 |     jmp_buf jmp;
 | 
|---|
| 128 |     /* hack to make GetExceptionCode() work in handler */
 | 
|---|
| 129 |     DWORD ExceptionCode;
 | 
|---|
| 130 |     const struct __tagWINE_FRAME *ExceptionRecord;
 | 
|---|
| 131 | } __WINE_FRAME;
 | 
|---|
| 132 | 
 | 
|---|
| 133 | extern DWORD WINAPI WINE_exception_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame,
 | 
|---|
| 134 |                                             CONTEXT *context, LPVOID pdispatcher );
 | 
|---|
| 135 | extern DWORD WINAPI WINE_finally_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame,
 | 
|---|
| 136 |                                           CONTEXT *context, LPVOID pdispatcher );
 | 
|---|
| 137 | 
 | 
|---|
| 138 | #endif /* USE_COMPILER_EXCEPTIONS */
 | 
|---|
| 139 | 
 | 
|---|
| 140 | static inline EXCEPTION_FRAME * WINE_UNUSED __wine_push_frame( EXCEPTION_FRAME *frame )
 | 
|---|
| 141 | {
 | 
|---|
| 142 | #if defined(__GNUC__) && defined(__i386__)
 | 
|---|
| 143 |     EXCEPTION_FRAME *prev;
 | 
|---|
| 144 |     __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
 | 
|---|
| 145 |                          "\n\tmovl %0,(%1)"
 | 
|---|
| 146 |                          "\n\t.byte 0x64\n\tmovl %1,(0)"
 | 
|---|
| 147 |                          : "=&r" (prev) : "r" (frame) : "memory" );
 | 
|---|
| 148 |     return prev;
 | 
|---|
| 149 | #else
 | 
|---|
| 150 |     TEB *teb = NtCurrentTeb();
 | 
|---|
| 151 |     frame->Prev = teb->except;
 | 
|---|
| 152 |     teb->except = frame;
 | 
|---|
| 153 |     return frame->Prev;
 | 
|---|
| 154 | #endif
 | 
|---|
| 155 | }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | static inline EXCEPTION_FRAME * WINE_UNUSED __wine_pop_frame( EXCEPTION_FRAME *frame )
 | 
|---|
| 158 | {
 | 
|---|
| 159 | #if defined(__GNUC__) && defined(__i386__)
 | 
|---|
| 160 |     __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
 | 
|---|
| 161 |                          : : "r" (frame->Prev) : "memory" );
 | 
|---|
| 162 |     return frame->Prev;
 | 
|---|
| 163 | 
 | 
|---|
| 164 | #else
 | 
|---|
| 165 |     NtCurrentTeb()->except = frame->Prev;
 | 
|---|
| 166 |     return frame->Prev;
 | 
|---|
| 167 | #endif
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | #ifdef __WINE__
 | 
|---|
| 171 | typedef DWORD (*DEBUGHOOK)( EXCEPTION_RECORD *, CONTEXT *, BOOL );
 | 
|---|
| 172 | extern void EXC_SetDebugEventHook( DEBUGHOOK hook );
 | 
|---|
| 173 | extern DEBUGHOOK EXC_GetDebugEventHook(void);
 | 
|---|
| 174 | extern void EXC_InitHandlers(void);
 | 
|---|
| 175 | #endif
 | 
|---|
| 176 | 
 | 
|---|
| 177 | #endif  /* __WINE_WINE_EXCEPTION_H */
 | 
|---|