summaryrefslogtreecommitdiff
path: root/cesar/lib/dbg.h
blob: 81ee7535f883f00d376efdaf265ff07008641a7c (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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#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"
#include "lib/blame.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

/** Blaming cannot be terse. */
#if CONFIG_DEBUG_TERSE && CONFIG_BLAME
#  error "CONFIG_DEBUG_TERSE and CONFIG_BLAME are not compatibles"
#endif

#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)

/**
 * Claim that the provided expression is true, check is conditioned by
 * CONFIG_DEBUG_CLAIM.
 * \param  expr  the assumed truth
 *
 * \warning \a expr is not evaluated when asserts are disabled.
 */
#define dbg_claim(expr) \
    dbg_claim_ (expr, #expr)

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

/**
 * Check that the provided expression is true, blame the caller.
 * \param  expr  the assumed truth
 *
 * Faulty function is taken from _fl_ (see blame.h).
 *
 * \warning \a expr is not evaluated when asserts are disabled.
 */
#define dbg_blame(expr) \
    dbg_blame_ (expr, #expr)

/**
 * Check that the provided pointer is fine, blame the caller.
 * \param  ptr  the pointer to check
 *
 * Faulty function is taken from _fl_ (see blame.h).
 *
 * \warning \a ptr is not evaluated when asserts are disabled.
 */
#define dbg_blame_ptr(ptr) \
    dbg_blame_ptr_ (ptr, #ptr)

/**
 * Check that the provided expression is true and provide more information on
 * failure, blame the caller.
 * \param  expr  the assumed truth
 * \param  fmt  printf-like format string
 * \param  rest  printf-like arguments
 *
 * Faulty function is taken from _fl_ (see blame.h).
 *
 * \warning \a expr is not evaluated when asserts are disabled.
 */
#define dbg_blame_print(expr, fmt, rest...) \
    dbg_blame_print_ (expr, fmt, ## rest)

/**
 * Use this macro in a forbidden default clause in switches, blame the caller.
 *
 * Faulty function is taken from _fl_ (see blame.h).
 */
#define dbg_blame_default() \
    dbg_blame_default_ ()

/* 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__
#  if CONFIG_DEBUG_CLAIM
#    define dbg_claim_ dbg_assert__
#    define dbg_claim_ptr_ dbg_assert_ptr__
#  else
#    define dbg_claim_ dbg_void__
#    define dbg_claim_ptr_ dbg_void__
#  endif
#  if CONFIG_BLAME
#    define dbg_blame_ dbg_blame__
#    define dbg_blame_ptr_ dbg_blame_ptr__
#    define dbg_blame_print_ dbg_blame_print__
#    define dbg_blame_default_ dbg_blame_default__
#  else
#    define dbg_blame_ dbg_assert__
#    define dbg_blame_ptr_ dbg_assert_ptr__
#    define dbg_blame_print_ dbg_assert_print__
#    define dbg_blame_default_ dbg_assert_default__
#  endif

#  if !CONFIG_DEBUG_TERSE
#    define dbg_assert_fail_ dbg_assert_fail
#    define dbg_assert_fail_const_ dbg_assert_fail_const
#    define dbg_assert_print_fail_(fmt, file, line, function, rest...) \
        dbg_assert_print_fail (DBG_ASSERT_FMT_ fmt, file, line, \
                               function, ## rest)
#  else
#    define dbg_assert_fail_(message, file, line, function) \
        dbg_assert_fail_terse (file ":" #line ": assertion failure: " message)
#    define dbg_assert_fail_const_ dbg_assert_fail_
#    define dbg_assert_print_fail_(fmt, file, line, function, rest...) \
        dbg_assert_fail_terse (file ":" #line ": assertion failure: " fmt)
#  endif

#  define dbg_void__(args...) ((void) 0)
#  define dbg_assert__(expr, exprs) \
    ((void) ((expr) ? 0 : (dbg_assert_fail_const_ (exprs, __FILE__, __LINE__, \
                                                   __PRETTY_FUNCTION__), 0)))
#  define dbg_assert_ptr__(ptr, ptrs) \
    ((void) ((ptr && ptr != INVALID_PTR) ? 0 : \
             (dbg_assert_fail_const_ ("@" ptrs, __FILE__, __LINE__, \
                                      __PRETTY_FUNCTION__), 0)))
#  define dbg_assert_print__(expr, fmt, rest...) \
    ((void) ((expr) ? 0 : (dbg_assert_print_fail_ (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_const_ ("unexpected default case", __FILE__, __LINE__, \
                            __PRETTY_FUNCTION__)

#  define dbg_blame__(expr, exprs) \
    ((void) ((expr) ? 0 : (dbg_assert_fail_ (exprs, _fl_, \
                                             __PRETTY_FUNCTION__), 0)))
#  define dbg_blame_ptr__(ptr, ptrs) \
    ((void) ((ptr && ptr != INVALID_PTR) ? 0 : \
             (dbg_assert_fail_ ("@" ptrs, _fl_, __PRETTY_FUNCTION__), 0)))
#  define dbg_blame_print__(expr, fmt, rest...) \
    ((void) ((expr) ? 0 : (dbg_assert_print_fail_ (fmt, _fl_, \
                                                   __PRETTY_FUNCTION__ \
                                                   , ##rest), 0)))
#  define dbg_blame_default__() \
    dbg_assert_fail_ ("unexpected default case", _fl_, \
                      __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_fatal_blind
#  define dbg_check_ dbg_eval__
#  define dbg_check_print_ dbg_eval_print__
#  define dbg_check_perror_ dbg_eval__
#  define dbg_claim_ dbg_void__
#  define dbg_claim_ptr_ dbg_void__
#  define dbg_blame_ dbg_void__
#  define dbg_blame_ptr_ dbg_void__
#  define dbg_blame_print_ dbg_void_print__
#  define dbg_blame_default_ dbg_fatal_blind
#  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 */

/** Structure to optimize call to dbg_assert_fail in code size. */
struct dbg_assert_desc_t
{
    /** Assertion text. */
    const char *assertion;
    /** Source file name. */
    const char *file;
    /** Source file line. */
    uint line;
    /** Source function name. */
    const char *function;
};
typedef struct dbg_assert_desc_t dbg_assert_desc_t;

/**
 * Call dbg_assert_fail, optimize in case parameters are constant.
 * \param  assertion  assertion text
 * \param  file  source file name
 * \param  line  source file line
 * \param  function  source function name
 */
#define dbg_assert_fail_const(assertion, file, line, function) \
    ({ \
       static const dbg_assert_desc_t assert_desc = \
           { assertion, file, line, function }; \
       dbg_assert_fail_desc (&assert_desc); \
     })

/**
 * 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) */

/**
 * Callback for debug dump.
 * \param  user  user parameter
 * \param  text  text buffer with text to write
 * \param  text_size  size of text to write, i.e. number of characters, or 0
 * for end of dump
 * \return  should return size, any other value will stop dump
 */
typedef int (*dbg_dump_callback_t) (void *user, const char *text,
                                    uint text_size);

/**
 * Callback for fatal error.
 */
typedef void (*dbg_fatal_callback_t) (void);

/**
 * Callback for fatal error dump.
 * \param  dump_cb  dump callback
 * \param  dump_cb_user  dump callback user parameter
 */
typedef void (*dbg_fatal_dump_callback_t) (dbg_dump_callback_t dump_cb,
                                           void *dump_cb_user);

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, descriptor version.
 * \param  assert_desc  assertion descriptor
 */
void
dbg_assert_fail_desc (const dbg_assert_desc_t *assert_desc)
    __attribute__ ((__noreturn__));

/**
 * Called on assertion failure, terse version.
 * \param  message  message text
 */
void
dbg_assert_fail_terse (const char *message) __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, but no other indication.
 */
void
dbg_fatal_blind (void) __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__));

/**
 * Register a new dump callback.
 * \param  callback  new dump callback
 * \param  user  callback user parameter
 */
void
dbg_register_dump_callback (dbg_dump_callback_t callback, void *user);

/**
 * Register a callback called on fatal error, before dump.
 * \param  priority  define order in which callbacks are called, the lesser,
 * the sooner
 * \param  callback  callback to register
 */
void
dbg_register_fatal_callback (uint priority, dbg_fatal_callback_t callback);

/**
 * Register a callback called on fatal error dump.
 * \param  priority  define order in which callbacks are called, the lesser,
 * the sooner
 * \param  callback  callback to register
 */
void
dbg_register_fatal_dump_callback (uint priority,
                                  dbg_fatal_dump_callback_t callback);

/**
 * Formated output to debug dump.
 * \param  dump_cb  dump callback
 * \param  dump_cb_user  dump callback user parameter
 * \param  fmt  printf-like format string
 * \return  output size
 *
 * \warning output size is limited to internal buffer size.
 */
int
dbg_dump_printf (dbg_dump_callback_t dump_cb, void *dump_cb_user,
                 const char *fmt, ...)
    __attribute__ ((format (printf, 3, 4)));

END_DECLS

#endif /* lib_dbg_h */