| 1 | /* $Id: exception.h,v 1.1 1999-08-22 22:52:05 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 |  | 
|---|
| 58 | #define __TRY __try | 
|---|
| 59 | #define __EXCEPT(func) __except((func)(GetExceptionInformation())) | 
|---|
| 60 | #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); } | 
|---|
| 61 | #define __ENDTRY /*nothing*/ | 
|---|
| 62 |  | 
|---|
| 63 | #else  /* USE_COMPILER_EXCEPTIONS */ | 
|---|
| 64 |  | 
|---|
| 65 | #define __TRY \ | 
|---|
| 66 | do { __WINE_FRAME __f; \ | 
|---|
| 67 | int __first = 1; \ | 
|---|
| 68 | for (;;) if (!__first) \ | 
|---|
| 69 | { \ | 
|---|
| 70 | do { | 
|---|
| 71 |  | 
|---|
| 72 | #define __EXCEPT(func) \ | 
|---|
| 73 | } while(0); \ | 
|---|
| 74 | EXC_pop_frame( &__f.frame ); \ | 
|---|
| 75 | break; \ | 
|---|
| 76 | } else { \ | 
|---|
| 77 | __f.frame.Handler = (PEXCEPTION_HANDLER)WINE_exception_handler; \ | 
|---|
| 78 | __f.u.filter = (func); \ | 
|---|
| 79 | EXC_push_frame( &__f.frame ); \ | 
|---|
| 80 | if (setjmp( __f.jmp)) { \ | 
|---|
| 81 | const __WINE_FRAME * const __eptr WINE_UNUSED = &__f; \ | 
|---|
| 82 | do { | 
|---|
| 83 |  | 
|---|
| 84 | #define __ENDTRY \ | 
|---|
| 85 | } while (0); \ | 
|---|
| 86 | break; \ | 
|---|
| 87 | } \ | 
|---|
| 88 | __first = 0; \ | 
|---|
| 89 | } \ | 
|---|
| 90 | } while (0); | 
|---|
| 91 |  | 
|---|
| 92 | #define __FINALLY(func) \ | 
|---|
| 93 | } while(0); \ | 
|---|
| 94 | EXC_pop_frame( &__f.frame ); \ | 
|---|
| 95 | (func)(1); \ | 
|---|
| 96 | break; \ | 
|---|
| 97 | } else { \ | 
|---|
| 98 | __f.frame.Handler = (PEXCEPTION_HANDLER)WINE_finally_handler; \ | 
|---|
| 99 | __f.u.finally_func = (func); \ | 
|---|
| 100 | EXC_push_frame( &__f.frame ); \ | 
|---|
| 101 | __first = 0; \ | 
|---|
| 102 | } \ | 
|---|
| 103 | } while (0); | 
|---|
| 104 |  | 
|---|
| 105 |  | 
|---|
| 106 | typedef DWORD (*CALLBACK __WINE_FILTER)(PEXCEPTION_POINTERS); | 
|---|
| 107 | typedef void (*CALLBACK __WINE_FINALLY)(BOOL); | 
|---|
| 108 |  | 
|---|
| 109 | #define WINE_EXCEPTION_FILTER(func) DWORD WINAPI func( EXCEPTION_POINTERS *__eptr ) | 
|---|
| 110 | #define WINE_FINALLY_FUNC(func) void WINAPI func( BOOL __normal ) | 
|---|
| 111 |  | 
|---|
| 112 | #define GetExceptionInformation() (__eptr) | 
|---|
| 113 | #define GetExceptionCode()        (__eptr->ExceptionRecord->ExceptionCode) | 
|---|
| 114 | #define AbnormalTermination()     (!__normal) | 
|---|
| 115 |  | 
|---|
| 116 | typedef struct __tagWINE_FRAME | 
|---|
| 117 | { | 
|---|
| 118 | EXCEPTION_FRAME frame; | 
|---|
| 119 | union | 
|---|
| 120 | { | 
|---|
| 121 | /* exception data */ | 
|---|
| 122 | __WINE_FILTER filter; | 
|---|
| 123 | /* finally data */ | 
|---|
| 124 | __WINE_FINALLY finally_func; | 
|---|
| 125 | } u; | 
|---|
| 126 | jmp_buf jmp; | 
|---|
| 127 | /* hack to make GetExceptionCode() work in handler */ | 
|---|
| 128 | DWORD ExceptionCode; | 
|---|
| 129 | const struct __tagWINE_FRAME *ExceptionRecord; | 
|---|
| 130 | } __WINE_FRAME; | 
|---|
| 131 |  | 
|---|
| 132 | extern DWORD WINAPI WINE_exception_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame, | 
|---|
| 133 | CONTEXT *context, LPVOID pdispatcher ); | 
|---|
| 134 | extern DWORD WINAPI WINE_finally_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame, | 
|---|
| 135 | CONTEXT *context, LPVOID pdispatcher ); | 
|---|
| 136 |  | 
|---|
| 137 | #endif /* USE_COMPILER_EXCEPTIONS */ | 
|---|
| 138 |  | 
|---|
| 139 | static inline EXCEPTION_FRAME * WINE_UNUSED EXC_push_frame( EXCEPTION_FRAME *frame ) | 
|---|
| 140 | { | 
|---|
| 141 | #if defined(__GNUC__) && defined(__i386__) | 
|---|
| 142 | EXCEPTION_FRAME *prev; | 
|---|
| 143 | __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0" | 
|---|
| 144 | "\n\tmovl %0,(%1)" | 
|---|
| 145 | "\n\t.byte 0x64\n\tmovl %1,(0)" | 
|---|
| 146 | : "=&r" (prev) : "r" (frame) : "memory" ); | 
|---|
| 147 | return prev; | 
|---|
| 148 | #else | 
|---|
| 149 | TEB *teb = NtCurrentTeb(); | 
|---|
| 150 | frame->Prev = teb->except; | 
|---|
| 151 | teb->except = frame; | 
|---|
| 152 | return frame->Prev; | 
|---|
| 153 | #endif | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | static inline EXCEPTION_FRAME * WINE_UNUSED EXC_pop_frame( EXCEPTION_FRAME *frame ) | 
|---|
| 157 | { | 
|---|
| 158 | #if defined(__GNUC__) && defined(__i386__) | 
|---|
| 159 | __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)" | 
|---|
| 160 | : : "r" (frame->Prev) : "memory" ); | 
|---|
| 161 | return frame->Prev; | 
|---|
| 162 |  | 
|---|
| 163 | #else | 
|---|
| 164 | NtCurrentTeb()->except = frame->Prev; | 
|---|
| 165 | return frame->Prev; | 
|---|
| 166 | #endif | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | #ifdef __WINE__ | 
|---|
| 170 | typedef DWORD (*DEBUGHOOK)( EXCEPTION_RECORD *, CONTEXT *, BOOL ); | 
|---|
| 171 | extern void EXC_SetDebugEventHook( DEBUGHOOK hook ); | 
|---|
| 172 | extern DEBUGHOOK EXC_GetDebugEventHook(void); | 
|---|
| 173 | extern void EXC_InitHandlers(void); | 
|---|
| 174 | #endif | 
|---|
| 175 |  | 
|---|
| 176 | #endif  /* __WINE_WINE_EXCEPTION_H */ | 
|---|