UNIVERS  15.3
UNIVERS base processing software API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ExSim.h
1 /* ExSim.h 1.13 1999/04/05 12:37:01 */
2 
3 #ifndef __ExSim_h
4 
5 #define __ExSim_h
6 
7 
8 /*************************************************************
9  Exception simulation by means of long jump.
10  May be used with or without debugging information.
11  Debug information is fat stderr during program execution.
12  Programmer has next possibilities:
13 
14  * no exception simulation at all (in C++): do not define
15  SimulateExceptions;
16 
17  * exception simulation without possible run-time debugging:
18  do define NDEBUG;
19 
20  * exception simulation with possibility of run-time
21  debugging but without turning it on: do not define NDEBUG
22  and POOR_DEBUG (rich debug info is default mode with file
23  name and line number if compiler has predefined __FILE__ and
24  __LINE__ macro);
25 
26  * exception simulation with possibility of run-time
27  debugging but without turning it on: do not define NDEBUG
28  but define POOR_DEBUG (poor debug info mode place only codes
29  of thrown exceptions to stderr -- for odd compilers);
30 
31  * exception simulation with turned on run-time debugging:
32  place non-empty variable ESDEBUG in process environment
33  of your program.
34 
35  * Level of usage:
36  * 1) #define SimulateExceptions - force exception simulation
37  * for C++;
38  * 2) #define NDEBUG - disable run-time debug information;
39  * 3) #define POOR_DEBUG - disable run-time information about
40  * source file name and line number;
41  * 4) Env.name ESDEBUG != "" - turns on debugging of
42  * exceptions' processing;
43  * 5) Env.name ESDEBUG == "full" - turns on debugging of
44  * library for exceptions' simulation;
45  * 6) Env.name ESDEBUG == "error" - abort() on runtime error;
46  * 7) Env.name ESDEBUG == "throw" - abort() on Throw();
47  * 8) Env.name ESDEBUG == "errno" - perror() on Throw().
48  *************************************************************/
49 
50 #if defined(SimulateExceptions) || !defined(__cplusplus)
51 /*------------------------------------------------------------*
52  * Exception sumulation is turned on.
53  * <<< C and old C++ mode >>>
54  *------------------------------------------------------------*/
55 
56 /* Turned On */
57 #define ExceptionSimulation 1
58 
59 #include <setjmp.h>
60 #include <mix/mutex.h>
61 
62 #ifdef NDEBUG
63 /* Without run-time debug options */
64 # define Try if(!(esThrownCode = setjmp(*esNewControlLevel())))
65 # define Catch if(esEndControlLevel())
66 # define Throw(x) esThrowCode(x)
67 
68 #else
69 /* With run-time debug options */
70 
71 # if defined(__LINE__) && defined(__FILE__) && !defined(POOR_DEBUG)
72 /* Rich run-time debug information */
73 # define Try if(!(esThrownCode = setjmp(*esNewControlLevelDebug(__FILE__, __LINE__))))
74 # define Catch if(esEndControlLevelDebug(__FILE__, __LINE__))
75 # define Throw(x) esThrowCodeDebug(x, __FILE__, __LINE__)
76 # else
77 /* Poor run-time debug information */
78 # define Try if(!(esThrownCode = setjmp(*esNewControlLevelDebug(NULL, 0))))
79 # define Catch if(esEndControlLevelDebug(NULL, 0))
80 # define Throw(x) esThrowCodeDebug(x, NULL, 0)
81 # endif /* rich/poor debug information */
82 
83 #endif /* NDEBUG */
84 
85 #define exCode esLastCode
86 
87 
88 /* Stores last exception code */
89 
91 extern TLStorage int esLastCode;
92 
95 extern TLStorage int esThrownCode;
96 
97 /* Stores three state of run-time debugging:
98  ES_UNKNOWN_STATE - it's needed to be find out;
99  ES_NO_DEBUG - it's not needed to perform run-time debugging;
100  ES_DEBUG - it's needed to perform run-time debugging;
101  ES_DETAILED_DEBUG - it's needed to perform detailed run-time debugging;
102  ES_ABORT_ON_ERROR - make coredump if a runtime error take place;
103  ES_ABORT_ON_THROW - make coredump if an exception is thrown;
104  */
105 enum es_debug_state {
106  ES_UNKNOWN_STATE=0,
107  ES_NO_DEBUG=1,
108  ES_DEBUG=2,
109  ES_DETAILED_DEBUG=3,
110  ES_ABORT_ON_ERROR=4,
111  ES_ABORT_ON_THROW=5,
112  ES_PERROR_ON_THROW=6
113 };
114 extern volatile enum es_debug_state esDebugState;
115 
116 
117 #ifdef __cplusplus
118 extern "C"
119 {
120 #endif
121 
122 /* General functions */
123 extern void esCoredumpHere ();
124 extern void esAllocCrash ();
125 extern void esExtraCatchCrash ();
126 extern int esIsRunTimeDebugging ();
127 extern jmp_buf* esAllocListItem ();
128 extern void esFreeListItem ();
129 extern jmp_buf* esCurrentHandler ();
130 extern int esNoTrySection ();
131 
132 
133 /* Without run-time debug information */
134 extern jmp_buf* esNewControlLevel ();
135 extern int esEndControlLevel ();
136 extern void esThrowCode (int code);
137 extern void esNoCatchCrash (int code);
138 
139 /* With run-time debug information */
140 extern jmp_buf* esNewControlLevelDebug (const char* szSourceName,
141  int iLineNumber);
142 extern int esEndControlLevelDebug (const char* szSourceName,
143  int iLineNumber);
144 extern void esThrowCodeDebug (int code,
145  const char* szSourceName,
146  int iLineNumber);
147 extern void esNoCatchCrashDebug (int code,
148  const char* szSourceName,
149  int iLineNumber);
150 
151 /* Check for external catcher in variable "ESCATCH".
152  Format of this var is:
153  <escatch-value> ::= <list>
154  <list> ::= <item> | <item> "," <list>
155  <item> ::= <code> | <range> | <domain>
156  <code> ::= <number> [ "t" ]
157  <range> ::= <number> "-" <number> [ "t" ]
158  <domain> ::= <number> "-" [ "t" ] means range number..number+99
159  Up to 100 items are supported.
160  Return:
161  ES_NO_DEBUG - do nothing
162  ES_DEBUG - trace events
163  ES_ABORT_ON_THROW - abort on event */
164 extern enum es_debug_state esRunTimeCatcher (int code);
165 
166 
167 #ifdef __cplusplus
168 };
169 #endif
170 
171 #else
172 /*------------------------------------------------------------*
173  * Exception sumulation is turned off.
174  * <<< Only C++ mode >>>
175  *------------------------------------------------------------*/
176 
177 /* Turned Off */
178 #define ExceptionSimulation 0
179 
180 #define Try try
181 #define Catch catch(int esCurrentCode)
182 #define Throw(x) throw (x)
183 #define exCode esCurrentCode
184 
185 #endif /* Which exception model is worked */
186 
187 /* Safe block of code execution */
188 #define ExSafe(x) Try{x;}Catch{}
189 
190 /* Sinonimous definitions */
191 #define TRY Try
192 #define CATCH Catch
193 #define THROW(x) Throw(x)
194 
195 
196 /* Crash messages and exit */
197 #define ES_CRASH_CODE 99
198 
199 /* Environment name for run-time debugging */
200 #define ES_ENV_NAME "ESDEBUG"
201 
202 /* Turn on full run-time debug information by assigning the value
203  to ES_ENV_NAME */
204 #define ES_FULL_DEBUG "full"
205 
206 /* Make abort() (with coredump) if a runtime error take place by
207  assigning the value to ES_ENV_NAME */
208 #define ES_CORE_ERROR "error"
209 
210 /* Make abort() (with coredump) if an exception take place by
211  assigning the value to ES_ENV_NAME */
212 #define ES_CORE_THROW "throw"
213 
214 /* Make perror("errno=%d") if an exception take place by
215  assigning the value to ES_ENV_NAME */
216 #define ES_ERRNO_THROW "errno"
217 
218 
219 #endif /* ExSim.h */