-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.c
More file actions
267 lines (233 loc) · 8.66 KB
/
Copy patherror.c
File metadata and controls
267 lines (233 loc) · 8.66 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
/* Copyright 2012-2018 Dustin M. DeWeese
This file is part of the Startle library.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdbool.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "startle/types.h"
#include "startle/macros.h"
#include "startle/error.h"
#include "startle/log.h"
static bool breakpoint_disabled = false;
#if INTERFACE
#include <setjmp.h>
/** @file
* @brief Error handling
*/
typedef enum error_type_e {
ERROR_TYPE_NONE = 0,
ERROR_TYPE_UNEXPECTED = 1, /**< An assumption was violated, indicating incorrect operation. */
ERROR_TYPE_LIMITATION = 2 /**< A known limitation has been reached. */
} error_type_t;
/** Used to throw and catch errors. */
typedef struct {
jmp_buf env;
error_type_t type;
bool quiet;
} error_t;
#define assert_msg(...) DISPATCH(assert_msg, ##__VA_ARGS__)
#define assert_msg_1(cond, ...) "Assertion `" #cond "' failed."
#define assert_msg_2(cond, fmt, ...) "Assertion `" #cond "' failed: " fmt
#define assert_msg_3(cond, fmt, ...) assert_msg_2(cond, fmt)
#define assert_msg_4(cond, fmt, ...) assert_msg_2(cond, fmt)
#define assert_msg_5(cond, fmt, ...) assert_msg_2(cond, fmt)
#define assert_msg_6(cond, fmt, ...) assert_msg_2(cond, fmt)
#define assert_msg_7(cond, fmt, ...) assert_msg_2(cond, fmt)
#define assert_msg_8(cond, fmt, ...) assert_msg_2(cond, fmt)
#define assert_msg_9(cond, fmt, ...) assert_msg_2(cond, fmt)
#define assert_msg_10(cond, fmt, ...) assert_msg_2(cond, fmt)
/** Assert a limitation.
* Throw a limitation error if the condition is violated, logging the following arguments.
*/
#define assert_throw(cond, ...) \
do { \
if(!(cond)) { \
throw_error(ERROR_TYPE_LIMITATION, \
assert_msg(cond, ##__VA_ARGS__), \
##__VA_ARGS__); \
} \
} while(0)
/** Assert an assumption.
* Throw an unexpected error if the condition is violated, logging the following arguments.
*/
#ifdef NDEBUG
#define assert_error(...) ((void)0)
#else
#define assert_error(...) _assert_error(__VA_ARGS__)
#endif
#define _assert_error(cond, ...) \
do { \
if(!(cond)) { \
throw_error(ERROR_TYPE_UNEXPECTED, \
assert_msg(cond, ##__VA_ARGS__), \
##__VA_ARGS__); \
} \
} while(0)
/** Run following code and then throw if the condition is violated. */
#ifdef NDEBUG
#define on_assert_error(...) if(0)
#else
#define on_assert_error(...) _on_assert_error(__VA_ARGS__)
#endif
/* Some explanation:
*
* This:
* for(; !cond; throw_error()) {
* ...
* }
*
* Is equivalent to this:
* if(!cond) {
* ...
* throw_error();
* }
*
* There will be no looping since throw_error() will longjmp out at the end.
*/
#define _on_assert_error(cond, ...) \
for(;!(cond); \
({throw_error(ERROR_TYPE_UNEXPECTED, \
assert_msg(cond, ##__VA_ARGS__), \
##__VA_ARGS__);}))
/** Warn when an assumption doesn't hold.
* If the condition is violated, log the following arguments and print them.
*/
#ifdef NDEBUG
#define assert_warn(...) ((void)0)
#else
#define assert_warn(...) _assert_warn(__VA_ARGS__)
#endif
#define _assert_warn(cond, ...) \
do { \
if(!(cond)) { \
LOG(MARK("WARN") " " \
assert_msg(cond, ##__VA_ARGS__) \
DROP(__VA_ARGS__)); \
breakpoint(); \
} \
} while(0)
/** Throw an unexpected error after being called `n` times.
* A quick way to prevent unexpected infinite/long loops.
* `n` should be some really large number, because this counter
* cannot be reset.
*/
#ifdef NDEBUG
#define assert_counter(n) ((void)0)
#else
#define assert_counter(n) _assert_counter(n)
#endif
#define _assert_counter(n) \
do { \
static unsigned int *counter = NULL; \
if(!counter) counter = alloc_counter(); \
if(*counter > (n)) { \
throw_error(ERROR_TYPE_UNEXPECTED, \
"Assertion counter exhausted: %d > " #n, X, *counter); \
} \
(*counter)++; \
} while(0)
#ifdef NDEBUG
#define assert_op(op, x, y) ((void)0)
#else
#define assert_op(op, x, y) \
do { \
__typeof__(x) _x = (x); \
__typeof__(y) _y = (y); \
if(!(_x op _y)) { \
throw_error(ERROR_TYPE_UNEXPECTED, \
"Assertion `" #x " " #op " " #y \
"' failed: %d, %d", X, (int)_x, (int)_y); \
} \
} while(0)
#endif
#define assert_eq(x, y) assert_op(==, x, y)
#define assert_neq(x, y) assert_op(!=, x, y)
#define assert_lt(x, y) assert_op(<, x, y)
#define assert_le(x, y) assert_op(<=, x, y)
#define assert_gt(x, y) assert_op(>, x, y)
#define assert_ge(x, y) assert_op(>=, x, y)
/** Catch errors.
* `e` is a pointer to an `error_t` that will be set after an error.
* @snippet error.c error
*/
#define catch_error_1(e) catch_error_2(e, false)
#define catch_error_2(e, q) (current_error = (e), current_error->quiet = (q), !!setjmp((e)->env))
#define catch_error(...) DISPATCH(catch_error, __VA_ARGS__)
/** Throw an error of a particular type.
* Returns the error type and logs the following arguments.
* @snippet error.c error
*/
#define throw_error(type, fmt, ...) \
do { \
LOG_NO_POS(MARK("!!!") " " __FILE__ ":" STRINGIFY(__LINE__) \
": %s: " fmt, __func__ DROP(__VA_ARGS__)); \
breakpoint(); \
return_error(type); \
} while(0)
#endif
error_t *current_error = NULL;
/** Return the error type to `catch_error`. */
void return_error(error_type_t type) {
if(current_error) {
current_error->type = type;
longjmp(current_error->env, type);
} else {
exit(-1);
}
}
TEST(error) {
error_t *prev_error = current_error;
/** [error] */
error_t test_error;
if(catch_error(&test_error)) {
printf(NOTE("TEST") " ");
print_last_log_msg();
} else {
COUNTUP(i, 5) {
assert_throw(i < 3, "Don't worry, it's okay.");
printf("i = %d\n", (int)i);
}
}
/** [error] */
current_error = prev_error;
return 0;
}
/** A hook that can will be called in breakpoint() */
void __attribute__((weak)) breakpoint_hook();
void breakpoint_hook() {}
/** Convenient place to set a breakpoint for debugger integration. */
void breakpoint() {
if(breakpoint_disabled) return;
SHADOW(breakpoint_disabled, true) { // avoid error loops
if(!maybe_get(current_error, quiet, false)) {
print_context(5);
printf(NOTE("BREAKPOINT") " ");
print_last_log_msg();
}
breakpoint_hook();
}
}
static unsigned int counters[8] = {0};
static unsigned int counters_n = 0;
unsigned int *alloc_counter() {
assert_error(counters_n < LENGTH(counters));
return &counters[counters_n++];
}
void reset_counters() {
zero(counters);
}