summaryrefslogtreecommitdiffstats
path: root/clib/exception.h
blob: 2874447549db6d672c66a61260aad55fecd52f5e (plain)
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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: clib/exception.h $                                            */
/*                                                                        */
/* OpenPOWER FFS Project                                                  */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2015                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* 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.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

/*!
 * @file exception.h
 * @brief Exceptions for C
 * @details This file implements setjump / longjump based exceptions
 * @note Using these macros will create an exception context in each thread
 * @author Shaun Wetzstein <shaun@us.ibm.com>
 * @date 2008-2011
 */

#ifndef __EXCEPTION_H__
#define __EXCEPTION_H__

#include <setjmp.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>

#define EX_PAGE_SIZE	4096	//!< Max. size of an exception payload
#define EXC_MAGIC	0x45584350	//!< Magic number, i.e. "EXCP"

#define ASSERTION	1	//!< Assertion exception class
//#define UNEXPECTED    2                       //!< Unexpected result exception class
//#define ERRNO         3                       //!< @em errno error exception class
#define EXC_LAST	4

typedef struct exception_frame exception_frame_t;	//!< Alias for the @em exception_frame class

/*!
 * @brief Exception class
 */
typedef struct {
	int ex;			//!< Exception class

	const char *file;	//!< Source file exception was thrown
	int line;		//!< Source line exception was thrown

	void *data;		//!< User-defined data associated with the exception
	int size;		//!< Size (in bytes) of the user-defined data
} exception_t;

/*!
 * @brief Exception frame class
 */
struct exception_frame {
	unsigned long magic;	//!< @private
	exception_frame_t *prev;	//!< @private
	jmp_buf jmp;		//!< @private
	exception_t exc;	//!< @private
};

/*!
 * @def try
 * @brief Simulate a try {...} catch {...} else {...} end_try block
 * @hideinitializer
 * @details For example,
 * @code
 * ...
 * #define ERROR 4
 * ...
 * exception_t * ex;
 * ...
 * try {
 *    ...
 * } catch (ERROR, ex) {
 *     fprintf(stderr, "caught ERROR exception at: %s(%d)\n",
 *             ex->file, ex->line);
 *     struct ErrorStruct * err = (struct ErrorStruct *)ex->data
 *     ...format and log err...
 * } end_try
 * @endcode
 */
#define try								\
do {									\
    __exc_init();							\
    exception_frame_t __frame;						\
    memset(&__frame, 0, sizeof(__frame));				\
    __frame.magic = EXC_MAGIC, __frame.prev = __exc_get_frame();	\
    __exc_set_frame(&__frame);						\
    volatile int __flag = setjmp(__frame.jmp);				\
    if (__flag == 0) {

/*!
 * @def catch
 * @brief Simulate a try {...} catch {...} else {...} end_try block
 * @hideinitializer
 * @details For example,
 * @code
 * ...
 * exception_t ex;
 * ...
 * try {
 *     ....
 * } catch (TYPE1, ex) {
 *     ...
 * } catch (TYPE2, ex) {
 *     ...
 * } catch (TYPE3, ex) {
 *     ...
 * } else (ex) {
 *     ...
 * } end_try
 * @endcode
 */
#define catch(x, e)							\
    } else if (__flag == (x)) {						\
        exception_frame_t * __tmp = __exc_get_frame();			\
        (e) = __tmp->exc, (e).ex = (__flag);				\

/*!
 * @def else
 * @brief Simulate a try {...} catch {...} else {...} end_try block
 * @hideinitializer
 * @details For example,
 * @code
 * ...
 * exception_t ex;
 * ...
 * try {
 *     ....
 * } catch (TYPE1, ex) {
 *     ...
 * } catch (TYPE2, ex) {
 *     ...
 * } catch (TYPE3, ex) {
 *     ...
 * } else (ex) {
 *     ...
 * } end_try
 * @endcode
 */
#define else(e) catch(__flag, (e))

/*!
 * @def end_try
 * @brief Simulate a try {...} catch {...} else {...} end_try block
 * @hideinitializer
 */
#define end_try								\
    } else {								\
        __exc_set_frame(__frame.prev);					\
        throw_bytes(__flag, __frame.exc.data, __frame.exc.size);	\
    }									\
    __exc_set_frame(__frame.prev);					\
} while (0);

/*!
 * @def throw(x, f, ...)
 * @brief Throw a C exception with printf-like formatting
 * @hideinitializer
 * @param x [in] Exception class
 * @param d [in] Pointer to user-defined data
 * @param s [in] Size of user-defined data (in bytes)
 * @details For example,
 * @code
 * ...
 * #define ERRNO_STRING
 * ...
 * #define throw_errno_string(x) \
 *         throw(ERRNO_STRING, "errno=%d : %s", (x),
 *               (void*)strerror((x)), __FILE__, __LINE__)
 * ...
 * if (rc < 0)
 *     throw_errno_string(errno);
 * ...
 * exception_t ex;
 * ...
 * catch (ERRNO_STRING ex) {
 *     fprintf(strerr, "EXCEPTION: errno: %s in file: %s on line: %d\n",
 *            (char *)ex.data, ex.file, ex.line);
 *     exit(1);
 * }
 * @endcode
 */
#define throw(x, f, ...)	({					\
    char __d[EX_PAGE_SIZE];						\
    __exc_throw((x), __d, snprintf(__d, sizeof __d, (f), ##__VA_ARGS__),\
                __FILE__, __LINE__);					\
})

/*!
 * @def rethrow(x, f, ...)
 * @brief Rethrow a C exception from within a try ... catch ... end_try block
 * @hideinitializer
 * @param e [in] Exception object
 * @details For example,
 * @code
 * ...
 * exception_t ex;
 * ...
 * catch (ERRNO_STRING ex) {
 *     fprintf(strerr, "EXCEPTION: errno: %s in file: %s on line: %d\n",
 *            (char *)ex.data, ex.file, ex.line);
 *     rethrow(ex);
 * }
 * @endcode
 */
#define rethrow(e)	({						\
    __exc_rethrow((e).ex, (e).data, (e).size, (e).file, (e).line);	\
})

/*!
 * @def throw_bytes(x, d, s)
 * @brief Throw a C exception with user-defined data
 * @hideinitializer
 * @param x [in] Exception class
 * @param d [in] Pointer to user-defined data
 * @param s [in] Size of user-defined data (in bytes)
 * @details For example,
 * @code
 * ...
 * #define ERRNO_STRING
 * ...
 * #define throw_errno_string(x) \
 *         throw_bytes(ERRNO_STRING, (void*)strerror((x)),
 *                     strlen(strerror((x)), __FILE__, __LINE__)
 * ...
 * if (rc < 0)
 *     throw_errno_string(errno);
 * ...
 * exception_t ex;
 * ...
 * catch (ERRNO_STRING ex) {
 *     fprintf(strerr, "EXCEPTION: errno: %s in file: %s on line: %d\n",
 *            (char *)ex.data, ex.file, ex.line);
 *     exit(1);
 * }
 * @endcode
 */
#define throw_bytes(x, d, s)						\
    __exc_throw((x), (d), (s), __FILE__, __LINE__)

/*!
 * @def throw_unexpected(x)
 * @brief Throw a message (NULL terminated C string), due to an @em
 * unexpected error
 * @hideinitializer
 * @param x [in] Unexpected error message
 * @details For example,
 * @code
 * ...
 * rc = foo_function();
 * if (rc < 0)
 *     throw_unexpected("Invalid return code from foo_function()");
 * ...
 * @endcode
 */
#define throw_unexpected(x) ({						\
    __exc_throw(UNEXPECTED,((void*)x), strlen((x)),			\
                __FILE__, __LINE__);					\
			     })

/*!
 * @def throw_errno(x)
 * @brief Throw an errno number, due to an invalid function result.
 * @hideinitializer
 * @param x [in] Unexpected errno number
 * @details For example,
 * @code
 * ...
 * rc = open(...);
 * if (rc == -1)
 *     throw_errno(errno);
 * ...
 * @endcode
 */
#define throw_errno(x) ({						\
    __exc_throw(ERRNO,(void*)(x), 0,					\
                __FILE__, __LINE__);					\
			})

/*! @cond */
extern void __exc_init(void);
extern exception_frame_t *__exc_get_frame(void);
extern void __exc_set_frame(exception_frame_t *);
extern int __exc_throw(int, void *, int, const char *, int);
extern int __exc_rethrow(int, void *, int, const char *, int);
extern void __exc_backtrace(const char *, ...);
extern const char *__exc_name(int exc);
/*! @endcond */

#endif				/* __EXCEPTION_H__ */
OpenPOWER on IntegriCloud