| 1 |
|
|---|
| 2 | /*
|
|---|
| 3 | *@@sourcefile except.h:
|
|---|
| 4 | * header file for except.c. See remarks there.
|
|---|
| 5 | *
|
|---|
| 6 | * Note: Version numbering in this file relates to XWorkplace version
|
|---|
| 7 | * numbering.
|
|---|
| 8 | *
|
|---|
| 9 | *@@include #define INCL_DOSEXCEPTIONS
|
|---|
| 10 | *@@include #define INCL_DOSPROCESS
|
|---|
| 11 | *@@include #include <os2.h>
|
|---|
| 12 | *@@include #include <stdio.h>
|
|---|
| 13 | *@@include #include <setjmp.h>
|
|---|
| 14 | *@@include #include "except.h"
|
|---|
| 15 | */
|
|---|
| 16 |
|
|---|
| 17 | /*
|
|---|
| 18 | * Copyright (C) 1999-2000 Ulrich Mller.
|
|---|
| 19 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 20 | * it under the terms of the GNU General Public License as published by
|
|---|
| 21 | * the Free Software Foundation, in version 2 as it comes in the COPYING
|
|---|
| 22 | * file of the XFolder main distribution.
|
|---|
| 23 | * This program is distributed in the hope that it will be useful,
|
|---|
| 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 26 | * GNU General Public License for more details.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #if __cplusplus
|
|---|
| 30 | extern "C" {
|
|---|
| 31 | #endif
|
|---|
| 32 |
|
|---|
| 33 | #ifndef EXCEPT_HEADER_INCLUDED
|
|---|
| 34 | #define EXCEPT_HEADER_INCLUDED
|
|---|
| 35 |
|
|---|
| 36 | #if defined __IBMCPP__ || defined __IBMC__
|
|---|
| 37 | #ifndef INCL_DOSEXCEPTIONS
|
|---|
| 38 | #error except.h requires INCL_DOSEXCEPTIONS to be defined.
|
|---|
| 39 | #endif
|
|---|
| 40 | #ifndef INCL_DOSPROCESS
|
|---|
| 41 | #error except.h requires INCL_DOSPROCESS to be defined.
|
|---|
| 42 | #endif
|
|---|
| 43 |
|
|---|
| 44 | #ifndef __stdio_h
|
|---|
| 45 | #error except.h requires stdio.h to be included.
|
|---|
| 46 | #endif
|
|---|
| 47 | #ifndef __setjmp_h
|
|---|
| 48 | #error except.h requires setjmp.h to be included.
|
|---|
| 49 | #endif
|
|---|
| 50 | #endif
|
|---|
| 51 |
|
|---|
| 52 | /********************************************************************
|
|---|
| 53 | *
|
|---|
| 54 | * Declarations
|
|---|
| 55 | *
|
|---|
| 56 | ********************************************************************/
|
|---|
| 57 |
|
|---|
| 58 | // forward declaration
|
|---|
| 59 | typedef struct _EXCEPTIONREGISTRATIONRECORD2 *PEXCEPTIONREGISTRATIONRECORD2;
|
|---|
| 60 |
|
|---|
| 61 | // "OnKill" function prototype for EXCEPTIONREGISTRATIONRECORD2
|
|---|
| 62 | // added V0.9.0 (99-10-22) [umoeller]
|
|---|
| 63 | // removed V0.9.7 (2000-12-08) [umoeller]
|
|---|
| 64 | // typedef VOID APIENTRY FNEXCONKILL(PEXCEPTIONREGISTRATIONRECORD2);
|
|---|
| 65 | // typedef FNEXCONKILL *PFNEXCONKILL;
|
|---|
| 66 |
|
|---|
| 67 | /*
|
|---|
| 68 | *@@ EXCEPTIONREGISTRATIONRECORD2:
|
|---|
| 69 | * replacement EXCEPTIONREGISTRATIONRECORD
|
|---|
| 70 | * struct for thread exception handling.
|
|---|
| 71 | *
|
|---|
| 72 | *@@changed V0.9.0 (99-10-22) [umoeller]: pfnOnKill added
|
|---|
| 73 | *@@changed V0.9.0 (99-10-22) [umoeller]: renamed from REGREC2
|
|---|
| 74 | */
|
|---|
| 75 |
|
|---|
| 76 | typedef struct _EXCEPTIONREGISTRATIONRECORD2
|
|---|
| 77 | {
|
|---|
| 78 | PVOID pNext; // as in EXCEPTIONREGISTRATIONRECORD
|
|---|
| 79 | PFN pfnHandler; // as in EXCEPTIONREGISTRATIONRECORD
|
|---|
| 80 | jmp_buf jmpThread; // additional buffer for setjmp
|
|---|
| 81 | // PFNEXCONKILL pfnOnKill; // subroutine to call upon process/thread termination
|
|---|
| 82 | PVOID pvUser; // user ptr
|
|---|
| 83 | } EXCEPTIONREGISTRATIONRECORD2;
|
|---|
| 84 |
|
|---|
| 85 | /*
|
|---|
| 86 | *@@ EXCEPTSTRUCT:
|
|---|
| 87 | * structure used with TRY_xxx macros.
|
|---|
| 88 | */
|
|---|
| 89 |
|
|---|
| 90 | typedef struct _EXCEPTSTRUCT
|
|---|
| 91 | {
|
|---|
| 92 | EXCEPTIONREGISTRATIONRECORD2 RegRec2;
|
|---|
| 93 | ULONG ulExcpt; // != NULL if exception caught
|
|---|
| 94 | APIRET arc; // rc of DosSetExceptionHandler
|
|---|
| 95 | } EXCEPTSTRUCT, *PEXCEPTSTRUCT;
|
|---|
| 96 |
|
|---|
| 97 | // function prototypes for exception hooks (V0.9.0)
|
|---|
| 98 |
|
|---|
| 99 | // "open traplog file" hook
|
|---|
| 100 | typedef FILE* APIENTRY FNEXCOPENFILE(VOID);
|
|---|
| 101 | typedef FNEXCOPENFILE *PFNEXCOPENFILE;
|
|---|
| 102 |
|
|---|
| 103 | // "exception" hook
|
|---|
| 104 | typedef VOID APIENTRY FNEXCHOOK(FILE*, PTIB);
|
|---|
| 105 | typedef FNEXCHOOK *PFNEXCHOOK;
|
|---|
| 106 |
|
|---|
| 107 | // "error" hook
|
|---|
| 108 | typedef VOID APIENTRY FNEXCHOOKERROR(const char *pcszFile,
|
|---|
| 109 | ULONG ulLine,
|
|---|
| 110 | const char *pcszFunction,
|
|---|
| 111 | APIRET arc);
|
|---|
| 112 | typedef FNEXCHOOKERROR *PFNEXCHOOKERROR;
|
|---|
| 113 |
|
|---|
| 114 | /********************************************************************
|
|---|
| 115 | *
|
|---|
| 116 | * Prototypes
|
|---|
| 117 | *
|
|---|
| 118 | ********************************************************************/
|
|---|
| 119 |
|
|---|
| 120 | VOID _Optlink excExplainException(FILE *file,
|
|---|
| 121 | PSZ pszHandlerName,
|
|---|
| 122 | PEXCEPTIONREPORTRECORD pReportRec,
|
|---|
| 123 | PCONTEXTRECORD pContextRec);
|
|---|
| 124 |
|
|---|
| 125 | VOID excRegisterHooks(PFNEXCOPENFILE pfnExcOpenFileNew,
|
|---|
| 126 | PFNEXCHOOK pfnExcHookNew,
|
|---|
| 127 | PFNEXCHOOKERROR pfnExcHookError,
|
|---|
| 128 | BOOL fBeepOnExceptionNew);
|
|---|
| 129 |
|
|---|
| 130 | ULONG _System excHandlerLoud(PEXCEPTIONREPORTRECORD pReportRec,
|
|---|
| 131 | PEXCEPTIONREGISTRATIONRECORD2 pRegRec2,
|
|---|
| 132 | PCONTEXTRECORD pContextRec,
|
|---|
| 133 | PVOID pv);
|
|---|
| 134 |
|
|---|
| 135 | ULONG _System excHandlerQuiet(PEXCEPTIONREPORTRECORD pReportRec,
|
|---|
| 136 | PEXCEPTIONREGISTRATIONRECORD2 pRegRec2,
|
|---|
| 137 | PCONTEXTRECORD pContextRec,
|
|---|
| 138 | PVOID pv);
|
|---|
| 139 |
|
|---|
| 140 | extern PFNEXCHOOKERROR G_pfnExcHookError;
|
|---|
| 141 |
|
|---|
| 142 | extern ULONG G_ulExplainExceptionRunning;
|
|---|
| 143 |
|
|---|
| 144 | /********************************************************************
|
|---|
| 145 | *
|
|---|
| 146 | * Macros
|
|---|
| 147 | *
|
|---|
| 148 | ********************************************************************/
|
|---|
| 149 |
|
|---|
| 150 | /* See except.c for explanations how to use these. */
|
|---|
| 151 |
|
|---|
| 152 | #ifdef __NO_EXCEPTION_HANDLERS__
|
|---|
| 153 | // exception handlers can completely be disabled
|
|---|
| 154 | #define TRY_LOUD(excptstruct)
|
|---|
| 155 | #else
|
|---|
| 156 | #define TRY_LOUD(excptstruct) \
|
|---|
| 157 | { \
|
|---|
| 158 | EXCEPTSTRUCT excptstruct = {0}; \
|
|---|
| 159 | excptstruct.RegRec2.pfnHandler = (PFN)excHandlerLoud; \
|
|---|
| 160 | excptstruct.arc = DosSetExceptionHandler( \
|
|---|
| 161 | (PEXCEPTIONREGISTRATIONRECORD)&(excptstruct.RegRec2)); \
|
|---|
| 162 | if (excptstruct.arc) \
|
|---|
| 163 | if (G_pfnExcHookError) \
|
|---|
| 164 | G_pfnExcHookError(__FILE__, __LINE__, __FUNCTION__, excptstruct.arc); \
|
|---|
| 165 | else \
|
|---|
| 166 | DosBeep(1000, 1000); \
|
|---|
| 167 | excptstruct.ulExcpt = setjmp(excptstruct.RegRec2.jmpThread); \
|
|---|
| 168 | if (excptstruct.ulExcpt == 0) \
|
|---|
| 169 | {
|
|---|
| 170 |
|
|---|
| 171 | #endif
|
|---|
| 172 |
|
|---|
| 173 | #ifdef __NO_EXCEPTION_HANDLERS__
|
|---|
| 174 | // exception handlers can completely be disabled
|
|---|
| 175 | #define TRY_QUIET(excptstruct)
|
|---|
| 176 | #else
|
|---|
| 177 | #define TRY_QUIET(excptstruct) \
|
|---|
| 178 | { \
|
|---|
| 179 | EXCEPTSTRUCT excptstruct = {0}; \
|
|---|
| 180 | excptstruct.RegRec2.pfnHandler = (PFN)excHandlerQuiet; \
|
|---|
| 181 | excptstruct.arc = DosSetExceptionHandler( \
|
|---|
| 182 | (PEXCEPTIONREGISTRATIONRECORD)&(excptstruct.RegRec2)); \
|
|---|
| 183 | if (excptstruct.arc) \
|
|---|
| 184 | if (G_pfnExcHookError) \
|
|---|
| 185 | G_pfnExcHookError(__FILE__, __LINE__, __FUNCTION__, excptstruct.arc); \
|
|---|
| 186 | else \
|
|---|
| 187 | DosBeep(1000, 1000); \
|
|---|
| 188 | excptstruct.ulExcpt = setjmp(excptstruct.RegRec2.jmpThread); \
|
|---|
| 189 | if (excptstruct.ulExcpt == 0) \
|
|---|
| 190 | {
|
|---|
| 191 |
|
|---|
| 192 | #endif
|
|---|
| 193 |
|
|---|
| 194 | #ifdef __NO_EXCEPTION_HANDLERS__
|
|---|
| 195 | // exception handlers can completely be disabled
|
|---|
| 196 | #define CATCH(excptstruct) if (FALSE) {
|
|---|
| 197 | #else
|
|---|
| 198 | #define CATCH(excptstruct) \
|
|---|
| 199 | DosUnsetExceptionHandler( \
|
|---|
| 200 | (PEXCEPTIONREGISTRATIONRECORD)&(excptstruct.RegRec2)); \
|
|---|
| 201 | } /* end of TRY block */ \
|
|---|
| 202 | else \
|
|---|
| 203 | { /* exception occured: */ \
|
|---|
| 204 | DosUnsetExceptionHandler((PEXCEPTIONREGISTRATIONRECORD)&(excptstruct.RegRec2));
|
|---|
| 205 | #endif
|
|---|
| 206 |
|
|---|
| 207 | #ifdef __NO_EXCEPTION_HANDLERS__
|
|---|
| 208 | // exception handlers can completely be disabled
|
|---|
| 209 | #define END_CATCH() }
|
|---|
| 210 | #else
|
|---|
| 211 | #define END_CATCH() \
|
|---|
| 212 | } /* end of exception-occured block */ \
|
|---|
| 213 | }
|
|---|
| 214 | #endif
|
|---|
| 215 |
|
|---|
| 216 | /*
|
|---|
| 217 | * CRASH:
|
|---|
| 218 | * this macro is helpful for testing
|
|---|
| 219 | * the exception handlers.
|
|---|
| 220 | * This is not for general use. ;-)
|
|---|
| 221 | */
|
|---|
| 222 |
|
|---|
| 223 | #define CRASH {PSZ p = NULL; *p = 'a'; }
|
|---|
| 224 |
|
|---|
| 225 | #endif // EXCEPT_HEADER_INCLUDED
|
|---|
| 226 |
|
|---|
| 227 | #if __cplusplus
|
|---|
| 228 | }
|
|---|
| 229 | #endif
|
|---|
| 230 |
|
|---|