/
error.c
448 lines (378 loc) · 18.1 KB
/
error.c
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/***********************************************************************************************************************************
Error Handler
***********************************************************************************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common/error.h"
#include "common/stackTrace.h"
/***********************************************************************************************************************************
Represents an error type
***********************************************************************************************************************************/
struct ErrorType
{
const int code;
const char *name;
const struct ErrorType *parentType;
};
// Macro for defining new error types
#define ERROR_DEFINE(code, name, parentType) \
const ErrorType name = {code, #name, &parentType}
// Include error type definitions
#include "common/error.auto.c"
/***********************************************************************************************************************************
Maximum allowed number of nested try blocks
***********************************************************************************************************************************/
#define ERROR_TRY_MAX 32
/***********************************************************************************************************************************
States for each try
***********************************************************************************************************************************/
typedef enum {errorStateBegin, errorStateTry, errorStateCatch, errorStateFinal, errorStateEnd} ErrorState;
/***********************************************************************************************************************************
Track error handling
***********************************************************************************************************************************/
static struct
{
// Array of jump buffers
jmp_buf jumpList[ERROR_TRY_MAX];
// State of each try
int tryTotal;
struct
{
ErrorState state;
bool uncaught;
} tryList[ERROR_TRY_MAX + 1];
// Last error
struct
{
const ErrorType *errorType; // Error type
const char *fileName; // Source file where the error occurred
const char *functionName; // Function where the error occurred
int fileLine; // Source file line where the error occurred
const char *message; // Description of the error
const char *stackTrace; // Stack trace
} error;
} errorContext;
/***********************************************************************************************************************************
Message buffer and buffer size
The message buffer is statically allocated so there is some space to store error messages. Not being able to allocate such a small
amount of memory seems pretty unlikely so just keep the code simple and let the loader deal with massively constrained memory
situations.
The temp buffer is required because the error message being passed might be the error already stored in the message buffer.
***********************************************************************************************************************************/
#define ERROR_MESSAGE_BUFFER_SIZE 8192
static char messageBuffer[ERROR_MESSAGE_BUFFER_SIZE];
static char messageBufferTemp[ERROR_MESSAGE_BUFFER_SIZE];
static char stackTraceBuffer[ERROR_MESSAGE_BUFFER_SIZE];
/***********************************************************************************************************************************
Error type code
***********************************************************************************************************************************/
int
errorTypeCode(const ErrorType *errorType)
{
return errorType->code;
}
/***********************************************************************************************************************************
Get error type using a code
***********************************************************************************************************************************/
const ErrorType *
errorTypeFromCode(int code)
{
// Search for error type by code
int errorTypeIdx = 0;
const ErrorType *result = errorTypeList[errorTypeIdx];
while (result != NULL)
{
if (result->code == code)
break;
result = errorTypeList[++errorTypeIdx];
}
// Error if type was not found
if (result == NULL)
THROW_FMT(AssertError, "could not find error type for code '%d'", code);
return result;
}
/***********************************************************************************************************************************
Error type name
***********************************************************************************************************************************/
const char *
errorTypeName(const ErrorType *errorType)
{
return errorType->name;
}
/***********************************************************************************************************************************
Error type parent
***********************************************************************************************************************************/
const ErrorType *
errorTypeParent(const ErrorType *errorType)
{
return errorType->parentType;
}
/***********************************************************************************************************************************
Get the depth of the current try statement (0 if none)
***********************************************************************************************************************************/
unsigned int errorTryDepth()
{
return (unsigned int)errorContext.tryTotal;
}
/***********************************************************************************************************************************
Does the child error type extend the parent error type?
***********************************************************************************************************************************/
bool
errorTypeExtends(const ErrorType *child, const ErrorType *parent)
{
const ErrorType *find = child;
do
{
find = errorTypeParent(find);
// Parent was found
if (find == parent)
return true;
}
while (find != errorTypeParent(find));
// Parent was not found
return false;
}
/***********************************************************************************************************************************
Error type
***********************************************************************************************************************************/
const ErrorType *
errorType()
{
return errorContext.error.errorType;
}
/***********************************************************************************************************************************
Error code (pulled from error type)
***********************************************************************************************************************************/
int
errorCode()
{
return errorTypeCode(errorType());
}
/***********************************************************************************************************************************
Error filename
***********************************************************************************************************************************/
const char *
errorFileName()
{
return errorContext.error.fileName;
}
/***********************************************************************************************************************************
Error function name
***********************************************************************************************************************************/
const char *
errorFunctionName()
{
return errorContext.error.functionName;
}
/***********************************************************************************************************************************
Error file line number
***********************************************************************************************************************************/
int
errorFileLine()
{
return errorContext.error.fileLine;
}
/***********************************************************************************************************************************
Error message
***********************************************************************************************************************************/
const char *
errorMessage()
{
return errorContext.error.message;
}
/***********************************************************************************************************************************
Error name (pulled from error type)
***********************************************************************************************************************************/
const char *
errorName()
{
return errorTypeName(errorType());
}
/***********************************************************************************************************************************
Error stack trace
***********************************************************************************************************************************/
const char *
errorStackTrace()
{
return errorContext.error.stackTrace;
}
/***********************************************************************************************************************************
Is this error an instance of the error type?
***********************************************************************************************************************************/
bool
errorInstanceOf(const ErrorType *errorTypeTest)
{
return errorType() == errorTypeTest || errorTypeExtends(errorType(), errorTypeTest);
}
/***********************************************************************************************************************************
Return current error context state
***********************************************************************************************************************************/
static ErrorState
errorInternalState()
{
return errorContext.tryList[errorContext.tryTotal].state;
}
/***********************************************************************************************************************************
True when in try state
***********************************************************************************************************************************/
bool
errorInternalStateTry()
{
return errorInternalState() == errorStateTry;
}
/***********************************************************************************************************************************
True when in catch state and the expected error matches
***********************************************************************************************************************************/
bool
errorInternalStateCatch(const ErrorType *errorTypeCatch)
{
if (errorInternalState() == errorStateCatch && errorInstanceOf(errorTypeCatch))
return errorInternalProcess(true);
return false;
}
/***********************************************************************************************************************************
True when in final state
***********************************************************************************************************************************/
bool
errorInternalStateFinal()
{
return errorInternalState() == errorStateFinal;
}
/***********************************************************************************************************************************
Return jump buffer for current try
***********************************************************************************************************************************/
jmp_buf *
errorInternalJump()
{
return &errorContext.jumpList[errorContext.tryTotal - 1];
}
/***********************************************************************************************************************************
Begin the try block
***********************************************************************************************************************************/
bool
errorInternalTry(const char *fileName, const char *functionName, int fileLine)
{
// If try total has been exceeded then throw an error
if (errorContext.tryTotal >= ERROR_TRY_MAX)
errorInternalThrowFmt(&AssertError, fileName, functionName, fileLine, "too many nested try blocks");
// Increment try total
errorContext.tryTotal++;
// Setup try
errorContext.tryList[errorContext.tryTotal].state = errorStateBegin;
errorContext.tryList[errorContext.tryTotal].uncaught = false;
// Try setup was successful
return true;
}
/***********************************************************************************************************************************
Propogate the error up so it can be caught
***********************************************************************************************************************************/
void
errorInternalPropagate()
{
// Mark the error as uncaught
errorContext.tryList[errorContext.tryTotal].uncaught = true;
// If there is a parent try then jump to it
if (errorContext.tryTotal > 0)
longjmp(errorContext.jumpList[errorContext.tryTotal - 1], 1);
// If there was no try to catch this error then output to stderr
fprintf(stderr, "\nUncaught %s: %s\n thrown at %s:%d\n\n", errorName(), errorMessage(), errorFileName(), errorFileLine());
fflush(stderr);
// Exit with failure
exit(UnhandledError.code);
}
/***********************************************************************************************************************************
Process the error through each try and state
***********************************************************************************************************************************/
bool
errorInternalProcess(bool catch)
{
// If a catch statement then return
if (catch)
{
errorContext.tryList[errorContext.tryTotal].uncaught = false;
return true;
}
// Else if just entering error state clean up the stack
else if (errorContext.tryList[errorContext.tryTotal].state == errorStateTry)
stackTraceClean(errorTryDepth());
// Increment the state
errorContext.tryList[errorContext.tryTotal].state++;
// If the error has been caught then increment the state
if (errorContext.tryList[errorContext.tryTotal].state == errorStateCatch &&
!errorContext.tryList[errorContext.tryTotal].uncaught)
{
errorContext.tryList[errorContext.tryTotal].state++;
}
// Return if not done
if (errorContext.tryList[errorContext.tryTotal].state < errorStateEnd)
return true;
// Remove the try
errorContext.tryTotal--;
// If not caught in the last try then propogate
if (errorContext.tryList[errorContext.tryTotal + 1].uncaught)
errorInternalPropagate();
// Nothing left to process
return false;
}
/***********************************************************************************************************************************
Throw an error
***********************************************************************************************************************************/
void
errorInternalThrow(const ErrorType *errorType, const char *fileName, const char *functionName, int fileLine, const char *message)
{
// Setup error data
errorContext.error.errorType = errorType;
errorContext.error.fileName = fileName;
errorContext.error.functionName = functionName;
errorContext.error.fileLine = fileLine;
// Assign message to the error
strncpy(messageBuffer, message, sizeof(messageBuffer));
messageBuffer[sizeof(messageBuffer) - 1] = 0;
errorContext.error.message = (const char *)messageBuffer;
// Generate the stack trace for the error
if (stackTraceToZ(
stackTraceBuffer, sizeof(stackTraceBuffer), fileName, functionName, (unsigned int)fileLine) >= sizeof(stackTraceBuffer))
{
// Indicate that the stack trace was truncated
}
errorContext.error.stackTrace = (const char *)stackTraceBuffer;
// Propogate the error
errorInternalPropagate();
}
void
errorInternalThrowFmt(
const ErrorType *errorType, const char *fileName, const char *functionName, int fileLine, const char *format, ...)
{
// Format message
va_list argument;
va_start(argument, format);
vsnprintf(messageBufferTemp, ERROR_MESSAGE_BUFFER_SIZE - 1, format, argument);
va_end(argument);
errorInternalThrow(errorType, fileName, functionName, fileLine, messageBufferTemp);
}
/***********************************************************************************************************************************
Throw a system error
***********************************************************************************************************************************/
void
errorInternalThrowSys(
int errNo, const ErrorType *errorType, const char *fileName, const char *functionName, int fileLine, const char *message)
{
// Format message with system message appended
snprintf(messageBufferTemp, ERROR_MESSAGE_BUFFER_SIZE - 1, "%s: [%d] %s", message, errNo, strerror(errNo));
errorInternalThrow(errorType, fileName, functionName, fileLine, messageBufferTemp);
}
void
errorInternalThrowSysFmt(
int errNo, const ErrorType *errorType, const char *fileName, const char *functionName, int fileLine, const char *format, ...)
{
// Format message
va_list argument;
va_start(argument, format);
size_t messageSize = (size_t)vsnprintf(messageBufferTemp, ERROR_MESSAGE_BUFFER_SIZE - 1, format, argument);
va_end(argument);
// Append the system message
snprintf(messageBufferTemp + messageSize, ERROR_MESSAGE_BUFFER_SIZE - 1 - messageSize, ": [%d] %s", errNo, strerror(errNo));
errorInternalThrow(errorType, fileName, functionName, fileLine, messageBufferTemp);
}