-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrycatchc.c
More file actions
389 lines (298 loc) · 10.3 KB
/
Copy pathtrycatchc.c
File metadata and controls
389 lines (298 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// ------------------ trycatchc.c ------------------
// Include the header
#include "trycatchc.h"
// Size of the stack of TryCatch blocks, define how many recursive incursion
// of TryCatch blocks can be done, overflow is checked at the beginning of
// each TryCatch blocks with TryCatchGuardOverflow()
// (Guard with ifndef to be able to set it delibarately low here and
// be able to test in the example main.c)
#ifndef TryCatchMaxExcLvl
#define TryCatchMaxExcLvl 256
#endif
// Stack of jmp_buf to memorise the TryCatch blocks
// To avoid exposing this variable to the user, implement any code using
// it as functions here instead of in the #define-s of trycatch.h
static _Thread_local jmp_buf tryCatchExcJmp[TryCatchMaxExcLvl];
// Index of the next TryCatch block in the stack of jmp_buf
// To avoid exposing this variable to the user, implement any code using
// it as functions here instead of in the #define-s of trycatch.h
static _Thread_local int tryCatchExcLvl = 0;
// ID of the last raised exception
// To avoid exposing this variable to the user, implement any code using
// it as functions here instead of in the #define-s of trycatch.h
// Do not use the type enum TryCatchException to allow the user to extend
// the list of exceptions with user-defined exceptions outside of enum
// TryCatchException.
static _Thread_local int tryCatchExc = 0;
// Flag to memorise if we are inside a catch block at a given exception level
static _Thread_local bool flagInCatchBlock[TryCatchMaxExcLvl] = {false};
// Label for the TryCatchExceptions
static char* exceptionStr[TryCatchExc_LastID] = {
"",
"TryCatchExc_Segv",
"TryCatchExc_MallocFailed",
"TryCatchExc_IOError",
"TryCatchExc_TooManyExcToStrFun",
"TryCatchException_NaN",
"TryCatchExc_IntOverflow",
"TryCatchExc_OutOfRange",
"TryCatchExc_NotYetImplemented",
"TryCatchExc_UnitTestFailed",
"TryCatchExc_InfiniteLoop",
};
// Buffer to build default label for user defined exceptions
// Size of the buffer is calculated as length of "User-defined exception
// ()" plus enough space to hold the representation of an int
static char userDefinedExceptionDefaultLabel[50];
// Max number of user-defined functions used to convert user-defined
// exception ID to strings
#define nbMaxUserDefinedExcToStr 256
// Current number of user-defined functions used to convert user-defined
// exception ID to strings
static int nbUserDefinedExcToStr = 0;
// Pointers to user-defined functions used to convert user-defined
// exception ID to strings
static char const* (*userDefinedExcToStr[nbMaxUserDefinedExcToStr])(int);
// Stream to print out a message each time Raise is called
static FILE* streamRaise = NULL;
// Function called at the beginning of a TryCatch block to guard against
// overflow of the stack of jump_buf
void TryCatchGuardOverflow(
void) {
// If the max level of incursion is reached
if (tryCatchExcLvl == TryCatchMaxExcLvl) {
// Print a message on the standard error output and exit
fprintf(
stderr,
"TryCatch blocks recursive incursion overflow, exiting. "
"(You can try to raise the value of TryCatchMaxExcLvl in trycatch.c, "
"it was: %d)\n",
TryCatchMaxExcLvl);
exit(EXIT_FAILURE);
}
}
// Function called to get the jmp_buf on the top of the stack when
// starting a new TryCatch block
// Output:
// Remove the jmp_buf on the top of the stack and return it
jmp_buf* TryCatchGetJmpBufOnStackTop(
void) {
// Reset the last raised exception
tryCatchExc = 0;
// Memorise the current jmp_buf at the top of the stack
jmp_buf* topStack = tryCatchExcJmp + tryCatchExcLvl;
// Move the index of the top of the stack of jmp_buf to the upper level
tryCatchExcLvl++;
// Return the jmp_buf previously at the top of the stack
return topStack;
}
// Function called to raise the TryCatchException 'exc'
// Inputs:
// exc: The TryCatchException to raise. Do not use the type enum
// TryCatchException to allow the user to extend the list of
// exceptions with user-defined exception outside of enum
// TryCatchException.
// filename: File where the exception has been raised
// line: Line where the exception has been raised
void Raise_(
int exc,
char const* const filename,
int const line) {
// If the stream to record exception raising is set and the raised
// exception do not come from trycatch.c (to avoid unnecessary
// repeatition in the trace), print the exception
// on the stream
bool retStrCmp =
strcmp(
filename,
__FILE__);
if (streamRaise != NULL && retStrCmp != 0)
fprintf(
streamRaise,
"Exception (%s) raised in %s, line %d.\n",
TryCatchExcToStr(exc),
filename,
line);
if (tryCatchExcLvl > 0) {
// Memorise the last raised exception to be able to handle it if
// it reaches the default case in the swith statement of the TryCatch
// block
tryCatchExc = exc;
// Get the level in the stack where to jump back
int jumpTo = (tryCatchExcLvl > 0 ? tryCatchExcLvl - 1 : 0);
// Call longjmp with the appropriate jmp_buf in the stack and the
// raised TryCatchException.
longjmp(
tryCatchExcJmp[jumpTo],
exc);
}
}
// Function called when entering a catch block
void TryCatchEnterCatchBlock(
void) {
// Update the flag
flagInCatchBlock[tryCatchExcLvl - 1] = true;
}
// Function called when exiting a catch block
void TryCatchExitCatchBlock(
void) {
// Update the flag
flagInCatchBlock[tryCatchExcLvl] = false;
}
// Function called at the end of a TryCatch block
void TryCatchEnd(
void) {
// The execution has reached the end of the current TryCatch block,
// move back to the lower level in the stack of jmp_buf
if (tryCatchExcLvl > 0) tryCatchExcLvl--;
}
// The struct siginfo_t used to handle the SIGSEV is not defined in
// ANSI C, guard against this.
#ifndef __STRICT_ANSI__
// Handler function to raise the exception TryCatchExc_Segv when
// receiving the signal SIGSEV.
// Inputs:
// signal: Received signal, will always be SIGSEV, unused
// si: Info about the signal, unused
// arg: Optional arguments, unused
void TryCatchSigSegvHandler(
int signal,
siginfo_t* si,
void* arg) {
// Unused parameters
(void)signal; (void)si; (void)arg;
// Raise the exception
Raise(TryCatchExc_Segv);
}
// Function to set the handler function of the signal SIGSEV and raise
// TryCatchExc_Segv upon reception of this signal. Must have been
// called before using Catch(TryCatchExc_Segv)
void TryCatchInitHandlerSigSegv(
void) {
// Create a struct sigaction to set the handler
struct sigaction sigActionSegv;
memset(
&sigActionSegv,
0,
sizeof(struct sigaction));
sigemptyset(&(sigActionSegv.sa_mask));
sigActionSegv.sa_sigaction = TryCatchSigSegvHandler;
sigActionSegv.sa_flags = SA_SIGINFO;
// Set the handler
sigaction(
SIGSEGV,
&sigActionSegv,
NULL);
}
#endif
// Function to get the ID of the last raised exception
// Output:
// Return the id of the last raised exception
int TryCatchGetLastExc(
void) {
// Return the ID
return tryCatchExc;
}
// Function to convert an exception ID to char*
// Input:
// exc: The exception ID
// Output:
// Return the stringified exception
char const* TryCatchExcToStr(
int exc) {
// Declare the pointer to the result string
char const* excStr = NULL;
// If the exception ID is one of TryCatchException
if (exc < TryCatchExc_LastID) excStr = exceptionStr[exc];
// Loop on user-defined conversion functions
for (
int iFun = 0;
iFun < nbUserDefinedExcToStr;
++iFun) {
// Get the conversion using this function
char const* str = (*userDefinedExcToStr[iFun])(exc);
// If the exception ID could be converted
if (str != NULL) {
// If there was already another result of conversion,
// it means there is a ID conflict
if (excStr != NULL) {
fprintf(
stderr,
"!!! TryCatch: Exception ID conflict, between %s and %s !!!\n",
str,
excStr);
}
// Update the pointer the result
excStr = str;
}
}
// If we haven't find a conversion yet
if (excStr == NULL) {
// Create a default string
sprintf(
userDefinedExceptionDefaultLabel,
"User-defined exception (%d)",
exc);
excStr = userDefinedExceptionDefaultLabel;
}
// Return the converted exception to string
return excStr;
}
// Function to add a function used by TryCatch to convert user-defined
// function to a string. The function in argument must return NULL if its
// argument is not an exception ID it is handling, else a pointer to a
// statically allocated string.
// It is highly recommended to provide conversion functions to cover
// all the user defined exceptions as it also allows TryCatch to detect
// conflict between exception IDs.
// Input:
// fun: The conversion function to add
void TryCatchAddExcToStrFun(
char const* (*fun)(int)) {
// If the buffer of pointer to conversion function is full, raise
// the exception TooManyExcToStrFun
if (nbUserDefinedExcToStr >= nbMaxUserDefinedExcToStr)
Raise(TryCatchExc_TooManyExcToStrFun);
// Loop on the pointer to conversion functions
for (
int iFun = 0;
iFun < nbUserDefinedExcToStr;
++iFun) {
// If this is the function in argument
if (userDefinedExcToStr[iFun] == fun) {
// Avoid adding it several times
return;
}
}
// Add the pointer
userDefinedExcToStr[nbUserDefinedExcToStr] = fun;
// Increment the number of conversion functions
++nbUserDefinedExcToStr;
}
// Set the stream on which to print exception raising, set it to NULL to
// turn off messages
// Input:
// stream: The stream to used
void TryCatchSetRaiseStream(
FILE* const stream) {
// Set the stream
streamRaise = stream;
}
// Function to get the commit id of the library
// Output:
// Return a string containing the result of `git rev-parse HEAD` at
// compilation time
char const* TryCatchGetCommitId(
void) {
// Return the commit id
#define STRINGIFY(x) #x
#define STRINGIFY_VALUE_OF(x) STRINGIFY(x)
return STRINGIFY_VALUE_OF(COMMIT);
}
// Function to forward the current exception if any
void ForwardExc(
void) {
// If there is a currently raised exception, reraise it
if (tryCatchExc != 0) Raise(tryCatchExc);
}
// ------------------ trycatch.c ------------------