summaryrefslogtreecommitdiff
path: root/cesar/lib/dbg.h
blob: f3462a92e278d2e70bfb2f7a964cd7cf54f37e9a (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#ifndef lib_dbg_h
#define lib_dbg_h
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    lib/dbg.h
 * \brief   Debug functions.
 * \ingroup lib
 */

#include "config/debug.h"

/** Shortcut for CONFIG_DEBUG */
#define DEBUG CONFIG_DEBUG

/** Shortcut for CONFIG_DEBUG_MORE.
 * If activated, costly debugs are activated. */
#define DEBUG_MORE CONFIG_DEBUG_MORE

#if DEBUG && CONFIG_DEBUG_FATAL_CATCH
#  include "lib/try.h"
#endif
#include <stdarg.h>

/**
 * Check that the provided expression is true.
 * \param  expr  the assumed truth
 *
 * \warning \a expr is not evaluated when asserts are disabled.
 */
#define dbg_assert(expr) \
    dbg_assert_ (expr, #expr)

/**
 * Check that the provided pointer is fine.
 * \param  ptr  the pointer to check
 *
 * \warning \a ptr is not evaluated when asserts are disabled.
 */
#define dbg_assert_ptr(ptr) \
    dbg_assert_ptr_ (ptr, #ptr)

/**
 * Check that the provided expression is true and provide more information on
 * failure.
 * \param  expr  the assumed truth
 * \param  fmt  printf-like format string
 * \param  rest  printf-like arguments
 *
 * \warning \a expr is not evaluated when asserts are disabled.
 */
#define dbg_assert_print(expr, fmt, rest...) \
    dbg_assert_print_ (expr, fmt, ## rest)

/**
 * Check that the provided expression is true and print additional errno
 * information on failure.
 * \param  expr  the assumed truth
 *
 * \warning \a expr is not evaluated when asserts are disabled.
 */
#define dbg_assert_perror(expr) \
    dbg_assert_perror_ (expr, #expr)

/**
 * Use this macro in a forbidden default clause in switches.
 */
#define dbg_assert_default() \
    dbg_assert_default_ ()

/**
 * Check that the provided expression is true, evaluate the expression even
 * when asserts are disabled.
 * \param  expr  the assumed truth
 *
 * \warning \a expr is always evaluated.
 */
#define dbg_check(expr) \
    dbg_check_ (expr, #expr)

/**
 * Check that the provided expression is true, evaluate the expression even
 * when asserts are disabled and provide more information on failure.
 * \param  expr  the assumed truth
 * \param  fmt  printf-like format string
 * \param  rest  printf-like arguments
 *
 * \warning \a expr is always evaluated.
 */
#define dbg_check_print(expr, fmt, rest...) \
    dbg_check_print_ (expr, fmt, ## rest)

/**
 * Check that the provided expression is true and print additional errno
 * information on failure, evaluate the expression even when asserts are
 * disabled.
 * \param  expr  the assumed truth
 *
 * \warning \a expr is always evaluated.
 */
#define dbg_check_perror(expr) \
    dbg_check_perror_ (expr, #expr)

/* Asserts definitions. */
#if DEBUG

#  define DBG_ASSERT_FMT_ "%s:%d: assertion failure in %s: "

#  define dbg_assert_ dbg_assert__
#  define dbg_assert_ptr_ dbg_assert_ptr__
#  define dbg_assert_print_ dbg_assert_print__
#  define dbg_assert_perror_ dbg_assert_perror__
#  define dbg_assert_default_ dbg_assert_default__
#  define dbg_check_ dbg_assert__
#  define dbg_check_print_ dbg_assert_print__
#  define dbg_check_perror_ dbg_assert_perror__
#  define dbg_assert__(expr, exprs) \
    ((void) ((expr) ? 0 : (dbg_assert_fail (exprs, __FILE__, __LINE__, \
                                            __PRETTY_FUNCTION__), 0)))
#  define dbg_assert_ptr__(ptr, ptrs) \
    ((void) ((ptr && ptr != INVALID_PTR) ? 0 : \
             (dbg_assert_fail ("@" ptrs, __FILE__, __LINE__, \
                               __PRETTY_FUNCTION__), 0)))
#  define dbg_assert_print__(expr, fmt, rest...) \
    ((void) ((expr) ? 0 : (dbg_assert_print_fail (DBG_ASSERT_FMT_ fmt, \
                                                  __FILE__, __LINE__, \
                                                  __PRETTY_FUNCTION__, \
                                                  ##rest), 0)))
#  define dbg_assert_perror__(expr, exprs) \
    ((void) ((expr) ? 0 : (dbg_assert_perror_fail (exprs, __FILE__, __LINE__, \
                                                   __PRETTY_FUNCTION__), 0)))
#  define dbg_assert_default__() \
    dbg_assert_fail ("unexpected default case", __FILE__, __LINE__, \
                     __PRETTY_FUNCTION__)

#else /* !DEBUG */

#  define dbg_assert_ dbg_void__
#  define dbg_assert_ptr_ dbg_void__
#  define dbg_assert_print_ dbg_void_print__
#  define dbg_assert_perror_ dbg_void__
#  define dbg_assert_default_ dbg_void__
#  define dbg_check_ dbg_eval__
#  define dbg_check_print_ dbg_eval_print__
#  define dbg_check_perror_ dbg_eval__
#  define dbg_void__(args...) ((void) 0)
#  define dbg_eval__(expr, exprs) ((void) (expr, 0))
#  define dbg_void_print__(expr, fmt, rest...) ((void) 0)
#  define dbg_eval_print__(expr, fmt, rest...) ((void) (expr, 0))

#endif /* !DEBUG */

/**
 * Invalidate a pointer.
 * \param  ptr  pointer to invalidate
 *
 * The pointer is set to an invalid value.  This is better than set to NULL,
 * because one could test a pointer for this special NULL value and be very
 * disappointed when compiling without debug.
 */
#define dbg_invalid_ptr(ptr) dbg_invalid_ptr_ (ptr)

/**
 * Compile something only when debug is activated.
 * \param  smth  the thing conditionally compiled
 */
#define dbg_do(smth) dbg_do_ (smth)

#if DEBUG
#  define dbg_invalid_ptr_(ptr) (ptr) = INVALID_PTR
#  define dbg_do_(smth) smth
#else /* !DEBUG */
#  define dbg_invalid_ptr_(ptr) ((void) 0)
#  define dbg_do_(smth)
#endif /* !DEBUG */

/**
 * Begin a try block to catch fatal errors.
 *
 * dbg_fatal_try_begin, dbg_fatal_try_catch and dbg_fatal_try_end define a
 * try-catch sequence to catch fatal errors:
 *
 * \code
 * dbg_fatal_try_begin
 * {
 *     ...code which could fail...
 * }
 * dbg_fatal_try_catch (const char *fatal_message)
 * {
 *     ...code to handle failure...
 * }
 * dbg_fatal_try_end;
 * \endcode
 *
 * This is implemented using setjmp, please see setjmp documentation for
 * quirks about this.
 */
#define dbg_fatal_try_begin dbg_fatal_try_begin_
/** End a try block to catch fatal errors. */
#define dbg_fatal_try_end dbg_fatal_try_end_
/** Catch fatal errors. */
#define dbg_fatal_try_catch(vardecl) dbg_fatal_try_catch_(vardecl)
/** Use in place of dbg_fatal_try_catch when not interested by the message. */
#define dbg_fatal_try_catch_void() dbg_fatal_try_catch_void_

#if DEBUG && CONFIG_DEBUG_FATAL_CATCH

extern int dbg_fatal_try_level_;

/** Fatal error message buffer. */
extern char dbg_fatal_text_[];

#  define dbg_fatal_try_begin_ \
{ \
    dbg_fatal_try_level_++; \
    try_begin \
    {

#define dbg_fatal_try_catch_(vardecl) \
    } \
    try_catch (TRY_CODE_FATAL) \
    { \
        vardecl = dbg_fatal_text_; \
        {

#define dbg_fatal_try_catch_void_ \
    } \
    try_catch (TRY_CODE_FATAL) \
    { \
        {

#  define dbg_fatal_try_end_ \
        } \
    } \
    try_always \
    { \
        dbg_fatal_try_level_--; \
    } \
    try_end; \
}

#else /* !(DEBUG && CONFIG_DEBUG_FATAL_CATCH) */

#  define dbg_fatal_try_begin_ \
{ \
    if (1) \
    {

#define dbg_fatal_try_catch_(vardecl) \
    } \
    else \
    { \
        vardecl = NULL; \
        {

#define dbg_fatal_try_catch_void_ \
    } \
    else \
    { \
        {

#  define dbg_fatal_try_end_ \
        } \
    } \
}

#endif /* !(DEBUG && CONFIG_DEBUG_FATAL_CATCH) */

BEGIN_DECLS

/**
 * Called on assertion failure.
 * \param  assertion  assertion text
 * \param  file  source file name
 * \param  line  source file line
 * \param  function  source function name
 */
void
dbg_assert_fail (const char *assertion, const char *file, uint line,
                 const char *function) __attribute__ ((__noreturn__));

/**
 * Called on assertion failure with more information on failure.
 * \param  fmt  printf-like format string
 */
void
dbg_assert_print_fail (const char *fmt, ...)
    __attribute__ ((__noreturn__, format (printf, 1, 2)));

/**
 * Called on errno assertion failure.
 * \param  assertion  assertion text
 * \param  file  source file name
 * \param  line  source file line
 * \param  function  source function name
 *
 * \todo implement
 */
void
dbg_assert_perror_fail (const char *assertion, const char *file, uint line,
                        const char *function) __attribute__ ((__noreturn__));

/**
 * Stop the program with a fatal error.
 * \param  fmt  printf-like format string
 */
void
dbg_fatal (const char *fmt, ...)
    __attribute__ ((__noreturn__, format (printf, 1, 2)));

/**
 * Stop the program with a fatal error, variable arguments version.
 * \param  fmt  printf-like format string
 * \param  ap  argument list pointer
 */
void
dbg_vfatal (const char *fmt, va_list ap)
    __attribute__ ((__noreturn__));

END_DECLS

#endif /* lib_dbg_h */