2 This file is part of the NoBug debugging library.
5 2007, 2008, 2009 Christian Thaeter <ct@pipapo.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, contact Christian Thaeter <ct@pipapo.org>.
23 #ifndef NOBUG_LIBNOBUG_C /* not when building libnobug */
26 #error NDEBUG incompatible with -DEBUG_ALPHA
29 #error NDEBUG incompatible with -DEBUG_BETA
33 #if defined(EBUG_ALPHA)
34 # define NOBUG_MODE_ALPHA 1
35 # define NOBUG_MODE_BETA 0
36 # define NOBUG_MODE_RELEASE 0
37 #elif defined(EBUG_BETA)
38 # define NOBUG_MODE_ALPHA 0
39 # define NOBUG_MODE_BETA 1
40 # define NOBUG_MODE_RELEASE 0
42 # define NOBUG_MODE_ALPHA 0
43 # define NOBUG_MODE_BETA 0
44 # define NOBUG_MODE_RELEASE 1
46 #error no debug level and no NDEBUG defined
48 #endif /* NOBUG_LIBNOBUG_C */
60 #ifdef HAVE_EXECINFO_H
61 # ifndef NOBUG_USE_EXECINFO
62 # define NOBUG_USE_EXECINFO 1
65 # undef NOBUG_USE_EXECINFO
66 # define NOBUG_USE_EXECINFO 0
69 #if NOBUG_USE_EXECINFO
73 #if defined(HAVE_VALGRIND_H) && !defined(NVALGRIND)
74 # ifndef NOBUG_USE_VALGRIND
75 # define NOBUG_USE_VALGRIND 1
78 # undef NOBUG_USE_VALGRIND
79 # define NOBUG_USE_VALGRIND 0
82 #if NOBUG_USE_VALGRIND
87 # ifndef NOBUG_USE_PTHREAD
88 # define NOBUG_USE_PTHREAD 1
91 # ifdef NOBUG_USE_PTHREAD
92 # undef NOBUG_USE_PTHREAD
94 # define NOBUG_USE_PTHREAD 0
101 #if NOBUG_MODE_RELEASE==0
103 //index.assert xref:assert[assert]:: C standard assertion
104 //assertions [[assert]]
106 //assertions assert(expr)
108 //assertions NoBug overrides the standard assert macro in *ALPHA* and *BETA* builds.
109 //assertions This is just a compatibility feature, its use is not suggested.
113 #define assert(expr) NOBUG_ASSERT(expr)
122 //index.require xref:REQUIRE[REQUIRE]:: preconditions (incoming parameters)
123 //assertions [[REQUIRE]]
124 //assertions .REQUIRE
125 //assertions REQUIRE(expr, ...)
126 //assertions REQUIRE_DBG(expr, ...)
127 //assertions REQUIRE_IF(when, expr, ...)
128 //assertions REQUIRE_IF_DBG(when, expr, ...)
130 //assertions Precondition (input) check. Use these macros to validate input a
131 //assertions function receives. The checks are enabled in *ALPHA* and *BETA* builds and
132 //assertions optimized out in *RELEASE* builds.
135 #define NOBUG_REQUIRE(expr, ...) \
136 NOBUG_IF_NOT_RELEASE(NOBUG_ASSERT_(expr, "PRECONDITION", \
137 NOBUG_LOCATION_INFO, ## __VA_ARGS__))
140 #define NOBUG_REQUIRE_DBG(expr, ...) \
141 NOBUG_IF_NOT_RELEASE(NOBUG_ASSERT_DBG_(expr, "PRECONDITION", \
142 NOBUG_LOCATION_INFO, ## __VA_ARGS__))
145 #define NOBUG_REQUIRE_IF(when, expr, ...) \
146 NOBUG_IF_NOT_RELEASE (NOBUG_WHEN(when, NOBUG_REQUIRE(expr, ## __VA_ARGS__)))
148 #define NOBUG_REQUIRE_IF_DBG(when, expr, ...) \
149 NOBUG_IF_NOT_RELEASE (NOBUG_WHEN(when, NOBUG_REQUIRE_DBG(expr, ## __VA_ARGS__)))
153 //index.ensure xref:ENSURE[ENSURE]:: postconditions (computation outcomes)
154 //assertions [[ENSURE]]
156 //assertions ENSURE(expr, ...)
157 //assertions ENSURE_DBG(expr, ...)
158 //assertions ENSURE_IF(when, expr, ...)
159 //assertions ENSURE_IF_DBG(when, expr, ...)
161 //assertions Postcondition (progress/output) check. Use these macros to validate the
162 //assertions data a function produces (example: return value). The checks enabled
163 //assertions unconditionally in *ALPHA* builds and optimized out in *BETA* builds for
164 //assertions scopes which are tagged as `CHECKED`. In *RELEASE* builds this checks are
165 //assertions always optimized out, but scopes tagged as `UNCHECKED` are not permitted.
168 #define NOBUG_ENSURE(expr, ...) \
169 NOBUG_IF_ALPHA (NOBUG_ASSERT_(expr, "POSTCONDITION", \
170 NOBUG_LOCATION_INFO, ## __VA_ARGS__)) \
172 NOBUG_WHEN (NOBUG_SCOPE_UNCHECKED, \
173 NOBUG_ASSERT_(expr, "POSTCONDITION", \
174 NOBUG_LOCATION_INFO, ## __VA_ARGS__))\
177 #define NOBUG_ENSURE_DBG(expr, ...) \
178 NOBUG_IF_ALPHA (NOBUG_ASSERT_DBG_ (expr, "POSTCONDITION", \
179 NOBUG_LOCATION_INFO, ## __VA_ARGS__)) \
181 NOBUG_WHEN (NOBUG_SCOPE_UNCHECKED, \
182 NOBUG_ASSERT_DBG_(expr, "POSTCONDITION", \
183 NOBUG_LOCATION_INFO, ## __VA_ARGS__)) \
187 #define NOBUG_ENSURE_IF(when, expr, ...) \
188 NOBUG_IF_NOT_RELEASE (NOBUG_WHEN(when, NOBUG_ENSURE(expr, ## __VA_ARGS__)))
190 #define NOBUG_ENSURE_IF_DBG(when, expr, ...) \
191 NOBUG_IF_NOT_RELEASE (NOBUG_WHEN(when, NOBUG_ENSURE_DBG(expr, ## __VA_ARGS__)))
197 //index.assert xref:ASSERT[ASSERT]:: generic assertion
198 //assertions [[ASSERT]]
200 //assertions ASSERT(expr, ...)
201 //assertions ASSERT_DBG(expr, ...)
202 //assertions ASSERT_IF(when, expr, ...)
203 //assertions ASSERT_IF_DBG(when, expr, ...)
205 //assertions Generic check. Use these macros when you want to validate something
206 //assertions which doesn't fall into one of the above categories. A example is when
207 //assertions a library function can return a unexpected result (scanf with syntax
208 //assertions error in the formatstring, when a constant/literal formatstring is
209 //assertions expected). The checks are enabled in *ALPHA* and *BETA* builds and
210 //assertions optimized out in *RELEASE* builds.
213 #define NOBUG_ASSERT(expr, ...) \
214 NOBUG_IF_NOT_RELEASE( \
215 NOBUG_ASSERT_(expr, "ASSERTION", \
216 NOBUG_LOCATION_INFO, ## __VA_ARGS__) \
219 #define NOBUG_ASSERT_DBG(expr, ...) \
220 NOBUG_IF_NOT_RELEASE( \
221 NOBUG_ASSERT_DBG_(expr, "ASSERTION", \
222 NOBUG_LOCATION_INFO, ## __VA_ARGS__) \
225 #define NOBUG_ASSERT_IF(when, expr, ...) \
226 NOBUG_IF_NOT_RELEASE (NOBUG_WHEN(when, NOBUG_ASSERT(expr, ## __VA_ARGS__)))
228 #define NOBUG_ASSERT_IF_DBG(when, expr, ...) \
229 NOBUG_IF_NOT_RELEASE (NOBUG_WHEN(when, NOBUG_ASSERT_DBG(expr, ## __VA_ARGS__)))
234 //index.check xref:CHECK[CHECK]:: assertion for testsuites
235 //assertions [[CHECK]]
237 //assertions CHECK(expr, ...)
238 //assertions CHECK_IF(when, expr, ...)
240 //assertions This assertion is never optimized out. Its main purpose is for implementing
241 //assertions testsuites where one want to assert tests independent of the build level
244 #define NOBUG_CHECK(expr, ...) \
245 NOBUG_ASSERT_(expr, "CHECK", NOBUG_LOCATION_INFO, ## __VA_ARGS__)
247 #define NOBUG_CHECK_IF(when, expr, ...) \
248 NOBUG_WHEN(when, NOBUG_CHECK(expr, ## __VA_ARGS__))
255 #define NOBUG_ASSERT_(expr, what, location, ...) \
256 NOBUG_WHEN (!(expr), \
257 NOBUG_LOG_( &nobug_flag_NOBUG_ON, LOG_EMERG, \
258 location, what, "(%s) " \
259 NOBUG_HEAD(__VA_ARGS__), \
260 #expr NOBUG_TAIL(__VA_ARGS__)); \
265 #define NOBUG_ASSERT_DBG_(expr, what, location, ...) \
266 NOBUG_WHEN (NOBUG_ACTIVE_DBG && !(expr), \
267 NOBUG_LOG_( &nobug_flag_NOBUG_ON, LOG_EMERG, \
268 location, what, "(%s) " \
269 NOBUG_HEAD(__VA_ARGS__), \
270 #expr NOBUG_TAIL(__VA_ARGS__)); \
275 #define NOBUG_ASSERTN_(expr, what, location, ...) \
276 NOBUG_WHEN (!(expr), \
277 NOBUG_LOG_( &nobug_flag_NOBUG_ON, LOG_EMERG, \
286 //index.invariant xref:INVARIANT[INVARIANT]:: validate the state
287 //assertions [[INVARIANT]]
288 //assertions .INVARIANT
289 //assertions INVARIANT(type, pointer, depth)
290 //assertions INVARIANT_DBG(type, pointer, depth)
291 //assertions INVARIANT_IF(when,type, pointer, depth)
292 //assertions INVARIANT_IF_DBG(when, type, pointer, depth)
294 //assertions Checking invariants. You can provide more complex checking functions
295 //assertions which test the validity of datastructures. Invariants are only enabled
296 //assertions in *ALPHA* builds for scopes which are not tagged as `CHECKED` and
297 //assertions otherwise optimized out.
299 //assertions TODO: describe how to create invariant checks
302 #define NOBUG_INVARIANT(type, pointer, depth) \
304 NOBUG_WHEN (NOBUG_SCOPE_UNCHECKED, \
305 NOBUG_CAT(type,_invariant)(pointer, depth, \
306 NOBUG_LOCATION_ARGS) \
310 #define NOBUG_INVARIANT_DBG(type, pointer, ...) \
312 NOBUG_WHEN (NOBUG_SCOPE_UNCHECKED && NOBUG_ACTIVE_DBG, \
313 NOBUG_CAT(type,_invariant)(pointer, depth, \
314 NOBUG_LOCATION_ARGS) \
318 #define NOBUG_INVARIANT_IF(when, type, pointer, ...) \
319 NOBUG_IF_ALPHA (NOBUG_WHEN (when, \
320 NOBUG_INVARIANT(type, pointer, ## __VA_ARGS__)) \
323 #define NOBUG_INVARIANT_IF_DBG(when, type, pointer, ...) \
324 NOBUG_IF_ALPHA (NOBUG_WHEN (when, \
325 NOBUG_INVARIANT_DBG(type, pointer, ## __VA_ARGS__))\
328 #define NOBUG_INVARIANT_ASSERT(expr, ...) \
329 NOBUG_ASSERT_(expr, "INVARIANT", (file, line, func), ## __VA_ARGS__)
333 checked/unchecked tagged scopes
335 #define NOBUG_SCOPE_UNCHECKED NOBUG_CHECKED_VALUE == 0
337 #define NOBUG_CHECKED NOBUG_IF_NOT_RELEASE(enum {NOBUG_CHECKED_VALUE=1})
339 #define NOBUG_UNCHECKED \
340 NOBUG_IF_NOT_RELEASE(enum {NOBUG_CHECKED_VALUE=0}) \
341 NOBUG_IF_RELEASE(NOBUG_UNCHECKED_NOT_ALLOWED_IN_RELEASE_BUILD)
346 dumping datastructures
348 /*TODO dump-level for flags instead limits[0]*/
350 //index.dump xref:DUMP[DUMP]:: dumping datastructures
351 //dumpmacros [[DUMP]]
352 //dumpmacros * DUMP(flag, type, pointer, depth)
353 //dumpmacros + Dump a datastructure
354 //dumpmacros DUMP_IF(expr, flag, type, pointer, depth)
355 //dumpmacros + Dump datastructure if expr is true
357 //dumpmacros This macros call a datastructure dump of the object (`pointer`) in question.
358 //dumpmacros `DUMP_IF` is the only enabled dumping macro for the RELEASE build level.
361 #define NOBUG_DUMP(flag, type, pointer, depth) \
362 NOBUG_IF_NOT_RELEASE( \
363 NOBUG_WHEN (NOBUG_DUMP_LEVEL <= nobug_flag_##flag.limits[0], \
364 NOBUG_CAT3 (nobug_, type, _dump)(pointer, depth, \
365 NOBUG_LOCATION_ARGS) \
369 #define NOBUG_DUMP_DBG(flag, type, pointer, depth) \
370 NOBUG_IF_NOT_RELEASE( \
371 NOBUG_WHEN (NOBUG_ACTIVE_DBG && NOBUG_DUMP_LEVEL <= nobug_flag_##flag.limits[0], \
372 NOBUG_CAT3 (nobug_, type, _dump)(pointer, depth, \
373 NOBUG_LOCATION_ARGS) \
377 #define NOBUG_DUMP_IF(when, flag, type, pointer, depth) \
378 NOBUG_WHEN (when && NOBUG_DUMP_LEVEL <= nobug_flag_##flag.limits[0], \
379 NOBUG_CAT3 (nobug_, type, _dump)(pointer, depth, \
380 NOBUG_LOCATION_ARGS) \
383 #define NOBUG_DUMP_IF_DBG(when, flag, type, pointer, depth) \
384 NOBUG_IF_NOT_RELEASE( \
385 NOBUG_WHEN (NOBUG_ACTIVE_DBG && when && NOBUG_DUMP_LEVEL <= nobug_flag_##flag.limits[0],\
386 NOBUG_CAT3 (nobug_, type, _dump)(pointer, depth, \
387 NOBUG_LOCATION_ARGS) \
393 //index.dump_log xref:DUMP_LOG[DUMP_LOG]:: Logging functions for dumping
394 //dumpmacros [[DUMP_LOG]]
395 //dumpmacros .DUMP_LOG
396 //dumpmacros DUMP_LOG(fmt, ...)
397 //dumpmacros DUMP_LOG_DBG(fmt, ...)
398 //dumpmacros DUMP_LOG_IF(when, fmt, ...)
399 //dumpmacros DUMP_LOG_IF_DBG(when, fmt, ...)
401 //dumpmacros Any output from `DUMP` handlers should be done by these macros.
403 //dumpmacros Dumping is by default done on level `LOG_DEBUG`, this can be overridden by
404 //dumpmacros defining `NOBUG_DUMP_LEVEL` to some other level.
407 #define NOBUG_DUMP_LOG(fmt, ...) \
408 NOBUG_LOG_( &nobug_flag_NOBUG_ON, NOBUG_DUMP_LEVEL, \
409 (file, line, func), "DUMP", \
413 #define NOBUG_DUMP_LOG_DBG(fmt, ...) \
414 NOBUG_WHEN (NOBUG_ACTIVE_DBG, \
415 NOBUG_LOG_( &nobug_flag_NOBUG_ON, NOBUG_DUMP_LEVEL, \
416 (file, line, func), "DUMP", \
417 fmt, ## __VA_ARGS__) \
420 #define NOBUG_DUMP_LOG_IF(expr, fmt, ...) \
422 NOBUG_LOG_( &nobug_flag_NOBUG_ON, NOBUG_DUMP_LEVEL, \
423 (file, line, func), "DUMP", \
424 fmt, ## __VA_ARGS__) \
427 #define NOBUG_DUMP_LOG_IF_DBG(expr, fmt, ...) \
428 NOBUG_WHEN (NOBUG_ACTIVE_DBG && expr, \
429 NOBUG_LOG_( &nobug_flag_NOBUG_ON, NOBUG_DUMP_LEVEL, \
430 (file, line, func), "DUMP", \
431 fmt, ## __VA_ARGS__) \
435 #ifndef NOBUG_DUMP_LEVEL
436 #define NOBUG_DUMP_LEVEL LOG_DEBUG
444 //index.echo xref:ECHO[ECHO]:: logging for tests
447 //logmacros ECHO(...)
449 //logmacros Never optimized out, logs at LOG_NOTICE level. Its main purpose is for implementing
450 //logmacros testsuites where one want to print and log messages independent of the build level
453 #define NOBUG_ECHO(...) \
454 NOBUG_LOG_(&nobug_flag_NOBUG_ON, LOG_NOTICE, NOBUG_LOCATION_INFO, "ECHO", ## __VA_ARGS__)
457 //index.alert xref:ALERT[ALERT]:: about to die
458 //logmacros [[ALERT]]
460 //logmacros ALERT(flag, ...)
461 //logmacros ALERT_DBG(flag, ...)
462 //logmacros ALERT_IF(when, flag, ...)
463 //logmacros ALERT_IF_DBG(when, flag, ...)
465 //logmacros This is the most critical condition an application might log. This might be used
466 //logmacros if an error occurs which can not be handled except a safe shutdown for example.
469 #define NOBUG_ALERT(flag, ...) \
470 NOBUG_LOG(flag, LOG_ALERT, ## __VA_ARGS__)
471 #define NOBUG_ALERT_IF(expr, flag, ...) \
472 NOBUG_LOG_IF(expr, flag, LOG_ALERT, ## __VA_ARGS__)
473 #define NOBUG_ALERT_DBG(flag, ...) \
474 NOBUG_LOG_DBG(flag, LOG_ALERT, ## __VA_ARGS__)
475 #define NOBUG_ALERT_IF_DBG(expr, flag, ...) \
476 NOBUG_LOG_IF_DBG(expr, flag, LOG_ALERT, ## __VA_ARGS__)
479 //index.critical xref:CRITICAL[CRITICAL]:: can not continue
480 //logmacros [[CRITICAL]]
481 //logmacros .CRITICAL
482 //logmacros CRITICAL(flag, ...)
483 //logmacros CRITICAL_DBG(flag, ...)
484 //logmacros CRITICAL_IF(when, flag, ...)
485 //logmacros CRITICAL_IF_DBG(when, flag, ...)
487 //logmacros An error which can not be handled occured but the application does not need to be
488 //logmacros shutdowen, perhaps waiting for an operator to fix the cause.
491 #define NOBUG_CRITICAL(flag, ...) \
492 NOBUG_LOG(flag, LOG_CRIT, ## __VA_ARGS__)
493 #define NOBUG_CRITICAL_IF(expr, flag, ...) \
494 NOBUG_LOG_IF(expr, flag, LOG_CRIT, ## __VA_ARGS__)
495 #define NOBUG_CRITICAL_DBG(flag, ...) \
496 NOBUG_LOG_DBG(flag, LOG_CRIT, ## __VA_ARGS__)
497 #define NOBUG_CRITICAL_IF_DBG(expr, flag, ...) \
498 NOBUG_LOG_IF_DBG(expr, flag, LOG_CRIT, ## __VA_ARGS__)
501 //index.error xref:ERROR[ERROR]:: something gone wrong
502 //logmacros [[ERROR]]
504 //logmacros ERROR(flag, fmt, ...)
505 //logmacros ERROR_DBG(flag, fmt, ...)
506 //logmacros ERROR_IF(when, flag, fmt, ...)
507 //logmacros ERROR_IF_DBG(when, flag, fmt, ...)
509 //logmacros Application takes a error handling brach
512 #define NOBUG_ERROR(flag, ...) \
513 NOBUG_LOG(flag, LOG_ERR, ## __VA_ARGS__)
514 #define NOBUG_ERROR_IF(expr, flag, ...) \
515 NOBUG_LOG_IF(expr, flag, LOG_ERR, ## __VA_ARGS__)
516 #define NOBUG_ERROR_DBG(flag, ...) \
517 NOBUG_LOG_DBG(flag, LOG_ERR, ## __VA_ARGS__)
518 #define NOBUG_ERROR_IF_DBG(expr, flag, ...) \
519 NOBUG_LOG_IF_DBG(expr, flag, LOG_ERR, ## __VA_ARGS__)
522 //index.warn xref:WARN[WARN]:: unexpected fixable error
525 //logmacros WARN(flag, fmt, ...)
526 //logmacros WARN_DBG(flag, fmt, ...)
527 //logmacros WARN_IF(when, flag, fmt, ...)
528 //logmacros WARN_IF_DBG(when, flag, fmt, ...)
530 //logmacros Rare, handled but unexpected branch
533 #define NOBUG_WARN(flag, ...) \
534 NOBUG_LOG(flag, LOG_WARNING, ## __VA_ARGS__)
535 #define NOBUG_WARN_IF(expr, flag, ...) \
536 NOBUG_LOG_IF(expr, flag, LOG_WARNING, ## __VA_ARGS__)
537 #define NOBUG_WARN_DBG(flag, ...) \
538 NOBUG_LOG_DBG(flag, LOG_WARNING, ## __VA_ARGS__)
539 #define NOBUG_WARN_IF_DBG(expr, flag, ...) \
540 NOBUG_LOG_IF_DBG(expr, flag, LOG_WARNING, ## __VA_ARGS__)
543 //index.info xref:INFO[INFO]:: progress message
546 //logmacros INFO(flag, fmt, ...)
547 //logmacros INFO_DBG(flag, fmt, ...)
548 //logmacros INFO_IF(when, flag, fmt, ...)
549 //logmacros INFO_IF_DBG(when, flag, fmt, ...)
551 //logmacros Message about program progress
554 #define NOBUG_INFO(flag, ...) \
555 NOBUG_LOG(flag, LOG_INFO, ## __VA_ARGS__)
556 #define NOBUG_INFO_IF(expr, flag, ...) \
557 NOBUG_LOG_IF(expr, flag, LOG_INFO, ## __VA_ARGS__)
558 #define NOBUG_INFO_DBG(flag, ...) \
559 NOBUG_LOG_DBG(flag, LOG_INFO, ## __VA_ARGS__)
560 #define NOBUG_INFO_IF_DBG(expr, flag, ...) \
561 NOBUG_LOG_IF_DBG(expr, flag, LOG_INFO, ## __VA_ARGS__)
564 //index.notice xref:NOTICE[NOTICE]:: detailed progress message
565 //logmacros [[NOTICE]]
567 //logmacros NOTICE(flag, fmt, ...)
568 //logmacros NOTICE_DBG(flag, fmt, ...)
569 //logmacros NOTICE_IF(when, flag, fmt, ...)
570 //logmacros NOTICE_IF_DBG(when, flag, fmt, ...)
572 //logmacros More detailed progress message
575 #define NOBUG_NOTICE(flag, ...) \
576 NOBUG_LOG(flag, LOG_NOTICE, ## __VA_ARGS__)
577 #define NOBUG_NOTICE_IF(expr, flag, ...) \
578 NOBUG_LOG_IF(expr, flag, LOG_NOTICE, ## __VA_ARGS__)
579 #define NOBUG_NOTICE_DBG(flag, ...) \
580 NOBUG_LOG_DBG(flag, LOG_NOTICE, ## __VA_ARGS__)
581 #define NOBUG_NOTICE_IF_DBG(expr, flag, ...) \
582 NOBUG_LOG_IF_DBG(expr, flag, LOG_NOTICE, ## __VA_ARGS__)
585 //index.trace xref:TRACE[TRACE]:: debugging level message
586 //logmacros [[TRACE]]
588 //logmacros TRACE(flag, fmt, ...)
589 //logmacros TRACE_DBG(flag, fmt, ...)
590 //logmacros TRACE_IF(when, flag, fmt, ...)
591 //logmacros TRACEIF_DBG(when, flag, fmt, ...)
593 //logmacros Very fine grained messages
595 //logmacros NOTE: that `TRACE` corresponds to `LOG_DEBUG`, because using `DEBUG` could be ambiguous.
598 #define NOBUG_TRACE(flag, ...) \
599 NOBUG_LOG(flag, LOG_DEBUG, ## __VA_ARGS__)
600 #define NOBUG_TRACE_IF(expr, flag, ...) \
601 NOBUG_LOG_IF(expr, flag, LOG_DEBUG, ## __VA_ARGS__)
602 #define NOBUG_TRACE_DBG(flag, ...) \
603 NOBUG_LOG_DBG(flag, LOG_DEBUG, ## __VA_ARGS__)
604 #define NOBUG_TRACE_IF_DBG(expr, flag, ...) \
605 NOBUG_LOG_IF_DBG(expr, flag, LOG_DEBUG, ## __VA_ARGS__)
609 //index.log xref:LOG[LOG]:: generic logging
612 //logmacros LOG(flag, lvl, fmt, ...)
613 //logmacros LOG_DBG(flag, lvl, fmt, ...)
614 //logmacros LOG_IF(when, flag, lvl, fmt, ...)
615 //logmacros LOG_IF_DBG(when, flag, lvl, fmt, ...)
617 //logmacros Generic logging macro which takes the level explicitly,
618 //logmacros avoid this, unless you implement your own logging facilities.
621 #define NOBUG_LOG(flag, lvl, ...) \
622 NOBUG_LOG_(&NOBUG_FLAG(flag), lvl, NOBUG_LOCATION_INFO, \
623 NOBUG_LVL(lvl), ## __VA_ARGS__)
625 #define NOBUG_LOG_DBG(flag, lvl, ...) \
626 NOBUG_WHEN (NOBUG_ACTIVE_DBG, \
627 NOBUG_LOG_(&NOBUG_FLAG(flag), lvl, NOBUG_LOCATION_INFO, \
628 NOBUG_LVL(lvl), ## __VA_ARGS__) \
631 #define NOBUG_LOG_IF(expr, flag, lvl, ...) \
633 NOBUG_LOG_(&NOBUG_FLAG(flag), lvl, NOBUG_LOCATION_INFO, \
634 NOBUG_LVL(lvl), ## __VA_ARGS__) \
637 #define NOBUG_LOG_IF_DBG(expr, flag, lvl, ...) \
638 NOBUG_WHEN (NOBUG_ACTIVE_DBG && expr, \
639 NOBUG_LOG_(&NOBUG_FLAG(flag), lvl, NOBUG_LOCATION_INFO, \
640 NOBUG_LVL(lvl), ## __VA_ARGS__) \
645 low level logging handler
647 #define NOBUG_LOG_(flag, lvl, location, what, ...) \
648 NOBUG_WHEN (lvl <= NOBUG_LOG_BASELIMIT && lvl <= (flag)->limits[NOBUG_TARGET_RINGBUFFER], \
649 nobug_log (flag, lvl, \
650 "%0.10llu: %s: %s:%d:"NOBUG_THREAD_ID_FMT(" ",":")" %s: " \
651 NOBUG_HEAD(__VA_ARGS__), ++nobug_counter, what, \
652 NOBUG_LOCATION_FILE location, \
653 NOBUG_LOCATION_LINE location NOBUG_THREAD_ID_COMMA, \
654 NOBUG_LOCATION_FUNC location NOBUG_TAIL(__VA_ARGS__)) \
657 #define NOBUG_LVL(lvl) NOBUG_LVL_##lvl
658 #define NOBUG_LVL_0 "EMERG"
659 #define NOBUG_LVL_1 "ALERT"
660 #define NOBUG_LVL_2 "CRIT"
661 #define NOBUG_LVL_3 "ERR"
662 #define NOBUG_LVL_4 "WARNING"
663 #define NOBUG_LVL_5 "NOTICE"
664 #define NOBUG_LVL_6 "INFO"
665 #define NOBUG_LVL_7 "TRACE"
668 //index.log_baselimit xref:LOG_BASELIMIT[LOG_BASELIMIT]
669 //logmacros [[LOG_BASELIMIT]]
670 //logmacros .LOG_BASELIMIT
671 //logmacros NOBUG_LOG_BASELIMIT_ALPHA
672 //logmacros NOBUG_LOG_BASELIMIT_BETA
673 //logmacros NOBUG_LOG_BASELIMIT_RELEASE
674 //logmacros NOBUG_LOG_BASELIMIT
676 //logmacros NoBug defines some logging base limits, anything more detailed
677 //logmacros than this base limits will be optimized out. This is used to
678 //logmacros reduce the logging overhead for *RELEASE* builds.
679 //logmacros By default the limit is set to `LOG_DEBUG` for *ALPHA* and *BETA*
680 //logmacros builds, so all logging is retained and `LOG_NOTICE` in *RELEASE*
681 //logmacros builds to log the application progress only coarsely then.
683 //logmacros This macros can be defined before including 'nobug.h' to some other
684 //logmacros log level (as defined in 'syslog.h').
687 #ifndef NOBUG_LOG_BASELIMIT_ALPHA
688 #define NOBUG_LOG_BASELIMIT_ALPHA LOG_DEBUG
691 #ifndef NOBUG_LOG_BASELIMIT_BETA
692 #define NOBUG_LOG_BASELIMIT_BETA LOG_DEBUG
695 #ifndef NOBUG_LOG_BASELIMIT_RELEASE
696 #define NOBUG_LOG_BASELIMIT_RELEASE LOG_NOTICE
699 #ifndef NOBUG_LOG_BASELIMIT
700 #define NOBUG_LOG_BASELIMIT \
701 NOBUG_IF_ALPHA(NOBUG_LOG_BASELIMIT_ALPHA) \
702 NOBUG_IF_BETA(NOBUG_LOG_BASELIMIT_BETA) \
703 NOBUG_IF_RELEASE(NOBUG_LOG_BASELIMIT_RELEASE)
712 DEPRECATED log nothing wont compile
714 //index.deprecated xref:DEPRECATED[DEPRECATED]:: to be discarded in future
715 //annotations [[DEPRECATED]]
716 //annotations * DEPRECATED(...)
717 //annotations + Something which shouldn't be used in future
720 #define NOBUG_DEPRECATED(msg) \
721 NOBUG_IF_ALPHA(NOBUG_LOG_(&nobug_flag_NOBUG_ANN, LOG_WARN, \
722 NOBUG_LOCATION_INFO, "DEPRECATED", msg) \
724 NOBUG_IF_RELEASE(NOBUG_DEPRECATED_NOT_ALLOWED_IN_RELEASE_BUILD(msg))
729 UNIMPLEMENTED abort abort wont compile
731 //index.unimplemented xref:UNIMPLEMENTED[UNIMPLEMENTED]:: not yet implemented
732 //annotations [[UNIMPLEMENTED]]
733 //annotations * UNIMPLEMENTED(...)
734 //annotations + not yet finished feature
737 #define NOBUG_UNIMPLEMENTED(msg) \
738 NOBUG_IF_NOT_RELEASE ( do { \
739 NOBUG_LOG_ (&nobug_flag_NOBUG_ANN, LOG_EMERG, NOBUG_LOCATION_INFO, \
740 "UNIMPLEMENTED", msg); \
743 NOBUG_IF_RELEASE( NOBUG_UNIMPLEMENTED_NOT_ALLOWED_IN_RELEASE_BUILD(msg))
748 FIXME log wont compile wont compile
750 //index.fixme xref:FIXME[FIXME]:: known Bug
751 //annotations [[FIXME]]
752 //annotations * FIXME(...)
753 //annotations + known bug to be fixed later
756 #define NOBUG_FIXME(msg) \
757 NOBUG_IF_ALPHA( NOBUG_ONCE( NOBUG_LOG_(&nobug_flag_NOBUG_ANN, LOG_ALERT, \
758 NOBUG_LOCATION_INFO, "FIXME", msg))) \
759 NOBUG_IF_BETA( NOBUG_FIXME_NOT_ALLOWED_IN_BETA_BUILD(msg)) \
760 NOBUG_IF_RELEASE( NOBUG_FIXME_NOT_ALLOWED_IN_RELEASE_BUILD(msg))
765 TODO log log wont compile
767 //index.todo xref:TODO[TODO]:: things to be done
768 //annotations [[TODO]]
769 //annotations * TODO(...)
770 //annotations + enhancement to be done soon
773 #define NOBUG_TODO(msg) \
774 NOBUG_IF_NOT_RELEASE ( \
776 NOBUG_LOG_( &nobug_flag_NOBUG_ANN, LOG_NOTICE, NOBUG_LOCATION_INFO, \
779 NOBUG_IF_RELEASE(NOBUG_TODO_NOT_ALLOWED_IN_RELEASE_BUILD(msg))
784 PLANNED log nothing nothing
786 //index.planned xref:PLANNED[PLANNED]:: ideas for future
787 //annotations [[PLANNED]]
788 //annotations * PLANNED(...)
789 //annotations + future enhancement
792 #define NOBUG_PLANNED(msg) \
793 NOBUG_IF_ALPHA( NOBUG_ONCE(NOBUG_LOG_ (&nobug_flag_NOBUG_ANN, LOG_INFO, \
794 NOBUG_LOCATION_INFO, "PLANNED", msg)))
799 NOTREACHED abort abort nothing
801 //index.notreached xref:NOTREACHED[NOTREACHED]:: code path never taken
802 //annotations [[NOTREACHED]]
803 //annotations * NOTREACHED
804 //annotations + used to tag code-path which shall be never executed (defaults in switch
805 //annotations statements, `else NOTREACHED;` after series of if's, etc.)
808 #define NOBUG_NOTREACHED \
809 NOBUG_IF_NOT_RELEASE( do { \
810 NOBUG_LOG_( &nobug_flag_NOBUG_ANN, LOG_EMERG, \
811 NOBUG_LOCATION_INFO, "NOTREACHED"); \
819 //faultinjection NoBug has some macros which can be used to simulate errorneous behaviour:
821 //index.inject_goodbad xref:INJECT_GOODBAD[INJECT_GOODBAD]:: fault injection expression
822 //faultinjection [[INJECT_GOODBAD]]
823 //faultinjection * INJECT_GOODBAD(expr, good, bad) substitutes to an expression and returns
824 //faultinjection good when expr is false and bad when expr is true. In BETA and RELEASE
825 //faultinjection builds 'good' is always returned.
828 #define NOBUG_INJECT_GOODBAD(expr, good, bad) \
829 NOBUG_IF_ALPHA((expr)?({NOBUG_INJECT_LOG(#expr": "#bad);}),bad:good) \
830 NOBUG_IF_NOT_ALPHA(good)
834 //index.inject_fault xref:INJECT_FAULT[INJECT_FAULT]:: fault injection statement
835 //faultinjection [[INJECT_FAULT]]
836 //faultinjection * INJECT_FAULT(expr, bad) substitutes to a statement which executes 'bad'
837 //faultinjection when expr is true. Optimitzed out in BETA and RELEASE builds.
840 #define NOBUG_INJECT_FAULT(expr, bad) \
841 NOBUG_IF_ALPHA(NOBUG_WHEN(expr,NOBUG_INJECT_LOG(#expr": "#bad); bad))
845 //faultinjection In both cases, when a fault is injected it will be logged at
846 //faultinjection NOBUG_INJECT_LEVEL (LOG_NOTICE).
849 #define NOBUG_INJECT_LOG(msg) \
850 NOBUG_LOG_( &nobug_flag_NOBUG_ON, NOBUG_INJECT_LEVEL, \
851 NOBUG_LOCATION_INFO, "INJECT_FAULT", \
855 #ifndef NOBUG_INJECT_LEVEL
856 #define NOBUG_INJECT_LEVEL LOG_NOTICE
863 #define NOBUG_FLAG(name) NOBUG_CAT(nobug_flag_,name)
865 #define NOBUG_DEFINE_FLAG(name) struct nobug_flag NOBUG_FLAG(name) = \
866 NOBUG_IF(NOBUG_MODE_ALPHA, {#name, NULL, 0, \
867 {LOG_DEBUG, LOG_INFO, LOG_DEBUG, -1, LOG_INFO}, &nobug_default_ringbuffer, NULL,NULL}) \
868 NOBUG_IF(NOBUG_MODE_BETA, {#name, NULL, 0, \
869 {LOG_INFO, LOG_NOTICE, LOG_NOTICE, LOG_NOTICE, LOG_WARNING}, &nobug_default_ringbuffer, NULL,NULL}) \
870 NOBUG_IF(NOBUG_MODE_RELEASE, {#name, NULL, 0, \
871 {LOG_NOTICE, -1, LOG_WARNING, LOG_WARNING, LOG_ERR}, &nobug_default_ringbuffer, NULL,NULL})
873 #define NOBUG_DEFINE_FLAG_PARENT(name, parent) \
874 NOBUG_DECLARE_FLAG(parent); \
875 struct nobug_flag NOBUG_FLAG(name) = \
876 NOBUG_IF(NOBUG_MODE_ALPHA, {#name, &NOBUG_FLAG(parent), 0, \
877 {LOG_DEBUG, LOG_INFO, LOG_DEBUG, -1, LOG_INFO}, &nobug_default_ringbuffer, NULL,NULL}) \
878 NOBUG_IF(NOBUG_MODE_BETA, {#name, &NOBUG_FLAG(parent), 0, \
879 {LOG_INFO, LOG_NOTICE, LOG_NOTICE, LOG_NOTICE, LOG_WARNING}, &nobug_default_ringbuffer, NULL,NULL}) \
880 NOBUG_IF(NOBUG_MODE_RELEASE, {#name, &NOBUG_FLAG(parent), 0, \
881 {LOG_NOTICE, -1, LOG_WARNING, LOG_WARNING, LOG_ERR}, &nobug_default_ringbuffer, NULL,NULL})
883 #define NOBUG_DEFINE_FLAG_LIMIT(name, limit) struct nobug_flag NOBUG_FLAG(name) = \
884 NOBUG_IF(NOBUG_MODE_ALPHA, {#name, NULL, 0, \
885 {LOG_DEBUG, limit, LOG_DEBUG, -1, LOG_INFO}, &nobug_default_ringbuffer, NULL,NULL}) \
886 NOBUG_IF(NOBUG_MODE_BETA, {#name, NULL, 0, \
887 {LOG_INFO, LOG_NOTICE, limit, LOG_NOTICE, LOG_WARNING}, &nobug_default_ringbuffer, NULL,NULL}) \
888 NOBUG_IF(NOBUG_MODE_RELEASE, {#name, NULL, 0, \
889 {LOG_NOTICE, -1, LOG_WARNING, limit, LOG_ERR}, &nobug_default_ringbuffer, NULL,NULL})
891 #define NOBUG_DEFINE_FLAG_PARENT_LIMIT(name, parent, limit) \
892 NOBUG_DECLARE_FLAG(parent); \
893 struct nobug_flag NOBUG_FLAG(name) = \
894 NOBUG_IF(NOBUG_MODE_ALPHA, {#name, &NOBUG_FLAG(parent), 0, \
895 {LOG_DEBUG, limit, LOG_DEBUG, -1, LOG_INFO}, &nobug_default_ringbuffer, NULL,NULL}) \
896 NOBUG_IF(NOBUG_MODE_BETA, {#name, &NOBUG_FLAG(parent), 0, \
897 {LOG_INFO, LOG_NOTICE, limit, LOG_NOTICE, LOG_WARNING}, &nobug_default_ringbuffer, NULL,NULL}) \
898 NOBUG_IF(NOBUG_MODE_RELEASE, {#name, &NOBUG_FLAG(parent), 0, \
899 {LOG_NOTICE, -1, LOG_WARNING, limit, LOG_ERR}, &nobug_default_ringbuffer, NULL,NULL})
902 /*TODO better FLAG_LIMIT handling, should respect LOG_TARGET */
905 #define NOBUG_DECLARE_FLAG(name) extern struct nobug_flag NOBUG_FLAG(name)
907 #define NOBUG_INIT_FLAG(name) \
908 nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, NOBUG_LOG_LIMIT)
910 #define NOBUG_INIT_FLAG_LIMIT(name, default) \
911 nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, default)
914 #define NOBUG_CPP_DEFINE_FLAG(name) \
915 NOBUG_DEFINE_FLAG(name); \
916 static int nobug_##name##_cppinit##__LINE__ = NOBUG_INIT_FLAG(name)
918 #define NOBUG_CPP_DEFINE_FLAG_PARENT(name, parent) \
919 NOBUG_DEFINE_FLAG_PARENT(name, parent); \
920 static int nobug_##name##_cppinit##__LINE__ = NOBUG_INIT_FLAG(name)
922 #define NOBUG_CPP_DEFINE_FLAG_LIMIT(name, default) \
923 NOBUG_DEFINE_FLAG_LIMIT(name, default); \
924 static int nobug_##name##_cppinit##__LINE__ = NOBUG_INIT_FLAG_LIMIT(name, default)
926 #define NOBUG_CPP_DEFINE_FLAG_PARENT_LIMIT(name, parent, default) \
927 NOBUG_DEFINE_FLAG_PARENT_LIMIT(name, parent, default); \
928 static int nobug_##name##_cppinit##__LINE__ = NOBUG_INIT_FLAG_LIMIT(name, default)
931 #ifndef NOBUG_LOG_LIMIT_ALPHA
932 # define NOBUG_LOG_LIMIT_ALPHA LOG_INFO
934 #ifndef NOBUG_LOG_LIMIT_BETA
935 # define NOBUG_LOG_LIMIT_BETA LOG_WARNING
937 #ifndef NOBUG_LOG_LIMIT_RELEASE
938 # define NOBUG_LOG_LIMIT_RELEASE LOG_CRIT
941 #ifndef NOBUG_LOG_LIMIT
942 # define NOBUG_LOG_LIMIT \
943 NOBUG_IF_ALPHA( NOBUG_LOG_LIMIT_ALPHA) \
944 NOBUG_IF_BETA( NOBUG_LOG_LIMIT_BETA) \
945 NOBUG_IF_RELEASE( NOBUG_LOG_LIMIT_RELEASE)
948 #ifndef NOBUG_LOG_TARGET_ALPHA
949 # define NOBUG_LOG_TARGET_ALPHA NOBUG_TARGET_CONSOLE
951 #ifndef NOBUG_LOG_TARGET_BETA
952 # define NOBUG_LOG_TARGET_BETA NOBUG_TARGET_FILE
954 #ifndef NOBUG_LOG_TARGET_RELEASE
955 # define NOBUG_LOG_TARGET_RELEASE NOBUG_TARGET_SYSLOG
958 #ifndef NOBUG_LOG_TARGET
959 # define NOBUG_LOG_TARGET \
960 NOBUG_IF_ALPHA( NOBUG_LOG_TARGET_ALPHA) \
961 NOBUG_IF_BETA( NOBUG_LOG_TARGET_BETA) \
962 NOBUG_IF_RELEASE( NOBUG_LOG_TARGET_RELEASE)
965 #define NOBUG_SET_LIMIT(flag, min) \
966 NOBUG_IF_NOT_RELEASE( NOBUG_FLAG(flag) = (min))
970 Resource registry macros
974 //index.resource_logging xref:RESOURCE_LOGGING[RESOURCE_LOGGING]:: switch resource logging on and off
975 //index.resource_log_level xref:RESOURCE_LOGGING[RESOURCE_LOG_LEVEL]:: select the log level for resource logging
976 //resourcemacros [[RESOURCE_LOGGING]]
978 //resourcemacros Unless the user defines `NOBUG_RESOURCE_LOGGING` to 0 each of the above macros
979 //resourcemacros will emit a log message at `NOBUG_RESOURCE_LOG_LEVEL` which defaults to
980 //resourcemacros `LOG_DEBUG`.
983 #ifndef NOBUG_RESOURCE_LOGGING
984 #define NOBUG_RESOURCE_LOGGING 1
987 #ifndef NOBUG_RESOURCE_LOG_LEVEL
988 #define NOBUG_RESOURCE_LOG_LEVEL LOG_DEBUG
993 //index.resource_handle xref:RESOURCE_HANDLE[RESOURCE_HANDLE]:: define an resource handle
994 //resourcemacros [[RESOURCE_HANDLE]]
995 //resourcemacros Resources are accessed through handles.
997 //resourcemacros .RESOURCE_HANDLE
998 //resourcemacros RESOURCE_HANDLE(name)
1000 //resourcemacros Defines handles to access resources.
1002 //resourcemacros `name`::
1003 //resourcemacros identifer to be used for the handle
1005 //resourcemacros This macro takes care that the declaration is optimized out in the same manner
1006 //resourcemacros the rest of the resource tracker would be disabled. You can still instantiate
1007 //resourcemacros handles as `struct nobug_resource_record*` in structures which must have a
1008 //resourcemacros constant size unconditional of the build level.
1011 #define NOBUG_RESOURCE_HANDLE(handle) \
1012 NOBUG_IF_ALPHA(struct nobug_resource_record* handle)
1014 #define NOBUG_RESOURCE_HANDLE_INIT(handle) NOBUG_IF_ALPHA(handle = NULL)
1018 //index.resource_announce xref:RESOURCE_ANNOUNCE[RESOURCE_ANNOUNCE]:: publish new resources
1019 //resourcemacros [[RESOURCE_ANNOUNCE]]
1021 //resourcemacros .RESOURCE_ANNOUNCE
1022 //resourcemacros RESOURCE_ANNOUNCE(flag, type, name, identifier, handle)
1024 //resourcemacros Publishes resources.
1026 //resourcemacros `flag`::
1027 //resourcemacros the NoBug flag which turns logging on for this macro
1028 //resourcemacros `type`::
1029 //resourcemacros a string which should denote the domain of the resource,
1030 //resourcemacros examples are "file", "mutex", "lock", "database" and so on
1031 //resourcemacros `name`::
1032 //resourcemacros the actual name of a named resource this as string which
1033 //resourcemacros together with type forms a unique identifier of the resource. `type` and
1034 //resourcemacros `name` must be available through the entire lifetime of the resource, using
1035 //resourcemacros literal strings is recommended
1036 //resourcemacros `identifier`::
1037 //resourcemacros a pointer which should be unique for this resource, any
1038 //resourcemacros kind of pointer will suffice, it is only used for identification. In
1039 //resourcemacros multithreaded applications the thread identifier becomes an additional
1040 //resourcemacros identifier
1041 //resourcemacros `handle`::
1042 //resourcemacros a `nobug_resource_record*` which will be initialized to point to
1043 //resourcemacros the newly created resource.
1045 //resourcemacros Resources must be unique, it is a fatal error when a resource it tried to be
1046 //resourcemacros announced more than one time.
1049 #define NOBUG_RESOURCE_ANNOUNCE(flag, type, name, ptr, handle) \
1050 NOBUG_RESOURCE_ANNOUNCE_RAW(&NOBUG_FLAG(flag), type, name, ptr, handle)
1052 #define NOBUG_RESOURCE_ANNOUNCE_RAW(flag, type, name, ptr, handle) \
1053 NOBUG_IF_ALPHA( do { \
1054 NOBUG_REQUIRE(!handle, "Announced resource handle not initialized"); \
1055 NOBUG_IF(NOBUG_RESOURCE_LOGGING, \
1056 NOBUG_LOG_(flag, NOBUG_RESOURCE_LOG_LEVEL, \
1057 NOBUG_LOCATION_INFO, "RESOURCE_ANNOUNCE", \
1058 "%s: %s@%p", type, name, ptr);) \
1059 NOBUG_ASSERTN_(handle = nobug_resource_announce (type, name, ptr, \
1060 NOBUG_BASENAME(__FILE__ ":" NOBUG_STRINGIZE(__LINE__))), \
1061 "RESOURCE_ANNOUNCE", NOBUG_LOCATION_INFO, \
1062 "%s: %s@%p %s", type, name, ptr, nobug_resource_error); \
1067 //index.resource_forget xref:RESOURCE_FORGET[RESOURCE_FORGET]:: remove resources
1068 //resourcemacros [[RESOURCE_FORGET]]
1070 //resourcemacros .RESOURCE_FORGET
1071 //resourcemacros RESOURCE_FORGET(flag, handle)
1073 //resourcemacros Removes resources that have become unavailable from the registry.
1075 //resourcemacros `flag`::
1076 //resourcemacros the NoBug flag which turns logging on for this macro
1077 //resourcemacros `handle`::
1078 //resourcemacros the `nobug_resource_record*` used to track this resource
1080 //resourcemacros The resource must still exist and no users must be attached to it, else a fatal
1081 //resourcemacros error is raised.
1084 #define NOBUG_RESOURCE_FORGET(flag, handle) \
1085 NOBUG_RESOURCE_FORGET_RAW(&NOBUG_FLAG(flag), handle)
1087 #define NOBUG_RESOURCE_FORGET_RAW(flag, handle) \
1088 NOBUG_IF_ALPHA( do { \
1089 NOBUG_IF(NOBUG_RESOURCE_LOGGING, \
1090 NOBUG_LOG_(flag, NOBUG_RESOURCE_LOG_LEVEL, NOBUG_LOCATION_INFO, \
1091 "RESOURCE_FORGET", "%s: %s@%p", handle?handle->type:"", \
1092 handle?handle->name:"", handle?handle->object_id:NULL);) \
1093 NOBUG_ASSERTN_(nobug_resource_forget (handle), \
1094 "RESOURCE_FORGET", NOBUG_LOCATION_INFO, "%s: %s@%p: %s", \
1095 handle?handle->type:"", handle?handle->name:"", \
1096 handle?handle->object_id:NULL, nobug_resource_error); \
1102 //index.resource_enter xref:RESOURCE_ENTER[RESOURCE_ENTER]:: claim a resource
1103 //resourcemacros [[RESOURCE_ENTER]]
1105 //resourcemacros .RESOURCE_ENTER
1106 //resourcemacros RESOURCE_ENTER(flag, announced, name, identifier, state, handle)
1108 //resourcemacros Attach a resource to code.
1110 //resourcemacros `flag`::
1111 //resourcemacros nobug flag which turns logging on for this macro
1112 //resourcemacros `announced`::
1113 //resourcemacros the handle set by `RESOURCE_ANNOUNCE`
1114 //resourcemacros `name`::
1115 //resourcemacros a free-form identifier
1116 //resourcemacros `identifier`::
1117 //resourcemacros some pointer which must be unique for the user of the
1118 //resourcemacros resource (see above)
1119 //resourcemacros `state`::
1120 //resourcemacros the initial state, one of `NOBUG_RESOURCE_WAITING`,
1121 //resourcemacros `NOBUG_RESOURCE_EXCLUSIVE` or `NOBUG_RESOURCE_RECURSIVE`
1122 //resourcemacros `handle`::
1123 //resourcemacros a `nobug_resource_record*` which will be initialized to the
1124 //resourcemacros entering node
1127 #define NOBUG_RESOURCE_ENTER(flag, resource, user, ptr, state, handle) \
1128 NOBUG_IF_ALPHA( do { \
1129 NOBUG_REQUIRE(resource, "Announced resource handle not initialized"); \
1130 NOBUG_IF(NOBUG_RESOURCE_LOGGING, \
1131 NOBUG_LOG_(&NOBUG_FLAG(flag), NOBUG_RESOURCE_LOG_LEVEL, \
1132 NOBUG_LOCATION_INFO, "RESOURCE_ENTER", \
1133 "%s: %s@%p: %s@%p: %s", resource->type, resource->name, \
1134 resource->object_id, user, ptr, \
1135 nobug_resource_states[state]);) \
1136 NOBUG_ASSERTN_(handle = nobug_resource_enter (resource, user, ptr, state, \
1137 NOBUG_BASENAME(__FILE__ ":" NOBUG_STRINGIZE(__LINE__))), \
1138 "RESOURCE_ENTER", NOBUG_LOCATION_INFO, \
1139 "%s: %s@%p: %s@%p: %s: %s", \
1140 resource?resource->type:"", resource?resource->name:"", \
1141 resource?resource->object_id:NULL, \
1142 user, ptr, nobug_resource_states[state], \
1143 nobug_resource_error); \
1148 //index.resource_state xref:RESOURCE_STATE[RESOURCE_STATE]:: change the state of a resource
1149 //resourcemacros [[RESOURCE_STATE]]
1151 //resourcemacros .NOBUG_RESOURCE_STATE
1152 //resourcemacros NOBUG_RESOURCE_STATE(flag, entered, state)
1154 //resourcemacros Changes resource's state.
1156 //resourcemacros `flag`::
1157 //resourcemacros is nobug flag which turns logging on for this macro
1158 //resourcemacros `entered`::
1159 //resourcemacros the handle set by `RESOURCE_ENTER`
1160 //resourcemacros `state`::
1161 //resourcemacros the new state Note that only certain state transitions are
1162 //resourcemacros allowed, see discussion/diagram above
1165 #define NOBUG_RESOURCE_STATE(flag, resource, state) \
1166 NOBUG_RESOURCE_STATE_RAW(&NOBUG_FLAG(flag), resource, state)
1168 #define NOBUG_RESOURCE_STATE_RAW(flag, resource, state) \
1169 NOBUG_IF_ALPHA( do { \
1170 NOBUG_IF(NOBUG_RESOURCE_LOGGING, \
1171 NOBUG_LOG_(flag, NOBUG_RESOURCE_LOG_LEVEL, NOBUG_LOCATION_INFO, \
1172 "RESOURCE_STATE", "%s: %s@%p: %s@%p: %s->%s", \
1173 resource?((struct nobug_resource_record*)resource->data)->type:"", \
1174 resource?((struct nobug_resource_record*)resource->data)->name:"", \
1175 resource?((struct nobug_resource_record*)resource->data)->object_id:"", \
1176 resource?resource->name:NULL, \
1177 resource?resource->object_id:NULL, \
1178 resource?resource->type:"", nobug_resource_states[state]); \
1180 NOBUG_ASSERTN_(nobug_resource_state (resource, state), \
1181 "RESOURCE_STATE", NOBUG_LOCATION_INFO, "%s: %s@%p: %s@%p: %s->%s: %s", \
1182 resource?((struct nobug_resource_record*)resource->data)->type:"", \
1183 resource?((struct nobug_resource_record*)resource->data)->name:"", \
1184 resource?((struct nobug_resource_record*)resource->data)->object_id:"", \
1185 resource?resource->name:NULL, \
1186 resource?resource->object_id:NULL, \
1187 resource?resource->type:"", nobug_resource_states[state], \
1188 nobug_resource_error); \
1193 //index.resource_leave xref:RESOURCE_LEAVE[RESOURCE_LEAVE]:: relinquish a claimed resource
1194 //resourcemacros [[RESOURCE_LEAVE]]
1196 //resourcemacros .RESOURCE_LEAVE
1197 //resourcemacros RESOURCE_LEAVE(flag, handle)
1199 //resourcemacros Disconnect from a resource identified with its handle.
1201 //resourcemacros `flag`::
1202 //resourcemacros nobug flag which turns logging on for this macro
1203 //resourcemacros `handle`::
1204 //resourcemacros the handle you got while entering the resource
1207 #define NOBUG_RESOURCE_LEAVE(flag, handle) \
1208 NOBUG_RESOURCE_LEAVE_RAW(&NOBUG_FLAG(flag), handle)
1210 #define NOBUG_RESOURCE_LEAVE_RAW(flag, handle) \
1211 NOBUG_IF_ALPHA( do { \
1212 NOBUG_IF(NOBUG_RESOURCE_LOGGING, \
1213 NOBUG_LOG_(flag, NOBUG_RESOURCE_LOG_LEVEL, NOBUG_LOCATION_INFO, \
1214 "RESOURCE_LEAVE", "%s: %s@%p: %s@%p: %s", \
1215 handle?((struct nobug_resource_record*)handle->data)->type:"", \
1216 handle?((struct nobug_resource_record*)handle->data)->name:"", \
1217 handle?((struct nobug_resource_record*)handle->data)->object_id:"", \
1218 handle?handle->name:"", \
1219 handle?handle->object_id:NULL, \
1220 handle?handle->type:""); \
1222 NOBUG_ASSERTN_(nobug_resource_leave (handle), \
1223 "RESOURCE_LEAVE", NOBUG_LOCATION_INFO, "%s: %s@%p: %s@%p: %s: %s", \
1224 handle?((struct nobug_resource_record*)handle->data)->type:"", \
1225 handle?((struct nobug_resource_record*)handle->data)->name:"", \
1226 handle?((struct nobug_resource_record*)handle->data)->object_id:"", \
1227 handle?handle->name:NULL, \
1228 handle?handle->object_id:NULL, \
1229 handle?handle->type:"", \
1230 nobug_resource_error); \
1236 //index.resource_leave_lookup xref:RESOURCE_LEAVE_LOOKUP[RESOURCE_LEAVE_LOOKUP]:: relinquish a claimed resource w/o handle
1237 //resourcemacros [[RESOURCE_LEAVE_LOOKUP]]
1239 //resourcemacros .RESOURCE_LEAVE_LOOKUP
1240 //resourcemacros RESOURCE_LEAVE_LOOKUP(flag, resource, identifier)
1242 //resourcemacros Disconnect from a resource identified by its identifier.
1244 //resourcemacros `flag`::
1245 //resourcemacros nobug flag which turns logging on for this macro
1246 //resourcemacros `resource`::
1247 //resourcemacros the handle you got from announcing the resource
1248 //resourcemacros `identifier`::
1249 //resourcemacros the pointer you gave it when creating it
1251 //resourcemacros Resources keep track of the thread which used them, you cant ANNOUNCE/FORGET
1252 //resourcemacros ENTER/LEAVE the same handle from different threads.
1255 #define NOBUG_RESOURCE_LEAVE_LOOKUP(flag, handle, ptr) \
1256 NOBUG_IF_ALPHA( do { \
1257 NOBUG_IF(NOBUG_RESOURCE_LOGGING, \
1258 NOBUG_LOG_(&NOBUG_FLAG(flag), NOBUG_RESOURCE_LOG_LEVEL, NOBUG_LOCATION_INFO, \
1259 "RESOURCE_LEAVE", "%s: %s@%p: @%p", \
1260 handle?handle->type:"", \
1261 handle?handle->name:"", \
1262 handle?handle->object_id:"", \
1265 NOBUG_ASSERTN_(nobug_resource_leave (nobug_resource_find(handle, ptr)), \
1266 "RESOURCE_LEAVE", NOBUG_LOCATION_INFO, "%s: %s@%p: @%p: %s", \
1267 handle?handle->type:"", \
1268 handle?handle->name:"", \
1269 handle?handle->object_id:"", \
1270 ptr, nobug_resource_error); \
1275 //index.resource_dump xref:NOBUG_RESOURCE_DUMP[NOBUG_RESOURCE_DUMP]:: dump the state of a single resource
1276 //resourcemacros [[NOBUG_RESOURCE_DUMP]]
1278 //resourcemacros .NOBUG_RESOURCE_DUMP
1279 //resourcemacros NOBUG_RESOURCE_DUMP(flag, handle)
1281 //resourcemacros Dump the state of a single resource.
1283 //resourcemacros `flag`::
1284 //resourcemacros Nobug flag for the log channel
1285 //resourcemacros `handle`::
1286 //resourcemacros handle of the resource to be dumped
1289 #define NOBUG_RESOURCE_DUMP(flag, handle) \
1290 do { NOBUG_RESOURCE_LOCK; \
1291 nobug_resource_dump (handle, &(struct nobug_resource_dump_context) \
1292 {&NOBUG_FLAG(flag), \
1293 NOBUG_RESOURCE_LOG_LEVEL, \
1294 __FILE__, __LINE__, __func__}); \
1295 NOBUG_RESOURCE_UNLOCK; } while (0)
1297 #define NOBUG_RESOURCE_DUMP_IF(when, flag, handle) \
1298 NOBUG_WHEN(when, NOBUG_RESOURCE_LOCK; \
1299 nobug_resource_dump (handle, &(struct nobug_resource_dump_context) \
1300 {&NOBUG_FLAG(flag), \
1301 NOBUG_RESOURCE_LOG_LEVEL, \
1302 __FILE__, __LINE__, __func__}); \
1303 NOBUG_RESOURCE_UNLOCK)
1307 //index.resource_dumpall xref:NOBUG_RESOURCE_DUMPALL[NOBUG_RESOURCE_DUMPALL]:: dump the state of all resources
1308 //resourcemacros [[NOBUG_RESOURCE_DUMPALL]]
1310 //resourcemacros .NOBUG_RESOURCE_DUMPALL
1311 //resourcemacros NOBUG_RESOURCE_DUMPALL(flag)
1313 //resourcemacros Dump the state of all resources.
1315 //resourcemacros `flag`::
1316 //resourcemacros Nobug flag for the log channel
1319 #define NOBUG_RESOURCE_DUMPALL(flag) \
1320 do { NOBUG_RESOURCE_LOCK; \
1321 nobug_resource_dump_all (&(struct nobug_resource_dump_context) \
1322 {&NOBUG_FLAG(flag), \
1323 NOBUG_RESOURCE_LOG_LEVEL, \
1324 __FILE__, __LINE__, __func__}); \
1325 NOBUG_RESOURCE_UNLOCK; } while (0)
1327 #define NOBUG_RESOURCE_DUMPALL_IF(when, flag) \
1328 NOBUG_WHEN(when, NOBUG_RESOURCE_LOCK; \
1329 nobug_resource_dump_all (&(struct nobug_resource_dump_context) \
1330 {&NOBUG_FLAG(flag), \
1331 NOBUG_RESOURCE_LOG_LEVEL, \
1332 __FILE__, __LINE__, __func__}); \
1333 NOBUG_RESOURCE_UNLOCK)
1337 //index.resource_list xref:NOBUG_RESOURCE_LIST[NOBUG_RESOURCE_LIST]:: enumerate all registered resources
1338 //resourcemacros [[NOBUG_RESOURCE_LIST]]
1340 //resourcemacros .NOBUG_RESOURCE_LIST
1341 //resourcemacros NOBUG_RESOURCE_LIST(flag)
1343 //resourcemacros List all registered resources.
1345 //resourcemacros `flag`::
1346 //resourcemacros Nobug flag for the log channel
1349 #define NOBUG_RESOURCE_LIST(flag) \
1350 do { NOBUG_RESOURCE_LOCK; \
1351 nobug_resource_list (&(struct nobug_resource_dump_context) \
1352 {&NOBUG_FLAG(flag), \
1353 NOBUG_RESOURCE_LOG_LEVEL, \
1354 __FILE__, __LINE__, __func__}); \
1355 NOBUG_RESOURCE_UNLOCK; } while (0)
1357 #define NOBUG_RESOURCE_LIST_IF(when, flag) \
1358 NOBUG_WHEN(when, NOBUG_RESOURCE_LOCK; \
1359 nobug_resource_list (&(struct nobug_resource_dump_context) \
1360 {&NOBUG_FLAG(flag), \
1361 NOBUG_RESOURCE_LOG_LEVEL, \
1362 __FILE__, __LINE__, __func__}); \
1363 NOBUG_RESOURCE_UNLOCK)
1369 #if NOBUG_USE_PTHREAD
1370 #define NOBUG_THREAD_ID_SET(name) nobug_thread_id_set(name)
1371 #define NOBUG_THREAD_ID_FMT(pre,post) pre "%s" post
1372 #define NOBUG_THREAD_ID_COMMA , NOBUG_THREAD_ID_GET
1373 #define NOBUG_THREAD_ID_GET nobug_thread_id_get()
1376 #define NOBUG_THREAD_ID_SET(name)
1377 #define NOBUG_THREAD_ID_FMT(pre,post) ""
1378 #define NOBUG_THREAD_ID_COMMA
1379 #define NOBUG_THREAD_ID_GET ""
1386 #define NOBUG_LOCATION_ARGS NOBUG_BASENAME(__FILE__), __LINE__, __func__
1387 #define NOBUG_LOCATION_INFO (NOBUG_LOCATION_ARGS)
1389 #define NOBUG_LOCATION_FILE(file, _1, _2) file
1390 #define NOBUG_LOCATION_LINE(_1, line, _2) line
1391 #define NOBUG_LOCATION_FUNC(_1, _2, func) func
1398 #define NOBUG_DBG_NONE 0
1399 #define NOBUG_DBG_GDB 1
1400 #define NOBUG_DBG_VALGRIND 2
1402 #define NOBUG_ACTIVE_DBG \
1403 NOBUG_IF(NOBUG_USE_VALGRIND, (RUNNING_ON_VALGRIND?2:0)) \
1404 NOBUG_IFNOT(NOBUG_USE_VALGRIND, 0)
1407 //index.backtrace xref:BACKTRACE[BACKTRACE]:: generate a backtrace
1408 //toolmacros [[BACKTRACE]]
1409 //toolmacros .BACKTRACE
1410 //toolmacros BACKTRACE
1412 //toolmacros The backtrace macro logs a stacktrace using the NoBug facilities.
1413 //toolmacros This is automatically called when NoBug finds an error and is due
1414 //toolmacros to abort. But one might call it manually too.
1417 #define NOBUG_BACKTRACE \
1419 switch (NOBUG_ACTIVE_DBG) { \
1420 case NOBUG_DBG_VALGRIND: \
1421 NOBUG_BACKTRACE_VALGRIND; \
1424 NOBUG_BACKTRACE_GLIBC; \
1426 NOBUG_IF_NOT_ALPHA (NOBUG_BACKTRACE_GLIBC)
1428 #define NOBUG_BACKTRACE_DBG \
1429 NOBUG_IF_NOT_RELEASE( \
1430 switch (NOBUG_ACTIVE_DBG) { \
1431 case NOBUG_DBG_VALGRIND: \
1432 NOBUG_BACKTRACE_VALGRIND; \
1436 #define NOBUG_BACKTRACE_GDB fprintf(stderr, "UNIMPLEMENTED : GDB Backtraces\n")
1438 #define NOBUG_BACKTRACE_VALGRIND \
1439 NOBUG_IF(NOBUG_USE_VALGRIND, \
1440 VALGRIND_PRINTF_BACKTRACE("BACKTRACE: %s@%d %s", \
1441 NOBUG_LOCATION_ARGS))
1444 #ifndef NOBUG_BACKTRACE_DEPTH
1445 #define NOBUG_BACKTRACE_DEPTH 256
1448 #define NOBUG_BACKTRACE_GLIBC \
1449 NOBUG_IF_NOT_RELEASE( \
1450 NOBUG_IF(NOBUG_USE_EXECINFO, do { \
1451 NOBUG_LOG_(&nobug_flag_NOBUG_ON, LOG_NOTICE, NOBUG_LOCATION_INFO, "BACKTRACE"); \
1452 nobug_backtrace_glibc (); \
1457 init and other function wrapers
1459 #define NOBUG_INIT nobug_init()
1460 #define NOBUG_BASENAME(name) (strrchr((name), '/')?strrchr((name), '/') + 1 : (name))
1461 #define NOBUG_ABORT abort()
1465 short macros without NOBUG_
1467 #ifndef NOBUG_DISABLE_SHORTNAMES
1469 #define REQUIRE NOBUG_REQUIRE
1472 #define REQUIRE_DBG NOBUG_REQUIRE_DBG
1475 #define REQUIRE_IF NOBUG_REQUIRE_IF
1477 #ifndef REQUIRE_IF_DBG
1478 #define REQUIRE_IF_DBG NOBUG_REQUIRE_IF_DBG
1481 #define ENSURE NOBUG_ENSURE
1484 #define ENSURE_DBG NOBUG_ENSURE_DBG
1487 #define ENSURE_IF NOBUG_ENSURE_IF
1489 #ifndef ENSURE_IF_DBG
1490 #define ENSURE_IF_DBG NOBUG_ENSURE_IF_DBG
1493 #define ASSERT NOBUG_ASSERT
1496 #define ASSERT_DBG NOBUG_ASSERT_DBG
1499 #define ASSERT_IF NOBUG_ASSERT_IF
1501 #ifndef ASSERT_IF_DBG
1502 #define ASSERT_IF_DBG NOBUG_ASSERT_IF_DBG
1505 #define CHECK NOBUG_CHECK
1508 #define CHECK NOBUG_CHECK
1511 #define INVARIANT NOBUG_INVARIANT
1513 #ifndef INVARIANT_DBG
1514 #define INVARIANT_DBG NOBUG_INVARIANT_DBG
1516 #ifndef INVARIANT_IF
1517 #define INVARIANT_IF NOBUG_INVARIANT_IF
1519 #ifndef INVARIANT_IF_DBG
1520 #define INVARIANT_IF_DBG NOBUG_INVARIANT_IF_DBG
1522 #ifndef INVARIANT_ASSERT
1523 #define INVARIANT_ASSERT NOBUG_INVARIANT_ASSERT
1526 #define DUMP NOBUG_DUMP
1529 #define DUMP_DBG NOBUG_DUMP_DBG
1532 #define DUMP_IF NOBUG_DUMP_IF
1535 #define DUMP_IF_DBG NOBUG_DUMP_IF_DBG
1538 #define DUMP_LOG NOBUG_DUMP_LOG
1540 #ifndef DUMP_LOG_DBG
1541 #define DUMP_LOG_DBG NOBUG_DUMP_LOG_DBG
1544 #define DUMP_LOG_IF NOBUG_DUMP_LOG_IF
1546 #ifndef DUMP_LOG_IF_DBG
1547 #define DUMP_LOG_IF_DBG NOBUG_DUMP_LOG_IF_DBG
1550 #define LOG NOBUG_LOG
1553 #define LOG_DBG NOBUG_LOG_DBG
1556 #define LOG_IF NOBUG_LOG_IF
1559 #define LOG_IF_DBG NOBUG_LOG_IF_DBG
1562 #define ECHO NOBUG_ECHO
1565 #define ALERT NOBUG_ALERT
1568 #define ALERT_DBG NOBUG_ALERT_DBG
1571 #define ALERT_IF NOBUG_ALERT_IF
1573 #ifndef ALERT_IF_DBG
1574 #define ALERT_IF_DBG NOBUG_ALERT_IF_DBG
1577 #define CRITICAL NOBUG_CRITICAL
1579 #ifndef CRITICAL_DBG
1580 #define CRITICAL_DBG NOBUG_CRITICAL_DBG
1583 #define CRITICAL_IF NOBUG_CRITICAL_IF
1585 #ifndef CRITICAL_IF_DBG
1586 #define CRITICAL_IF_DBG NOBUG_CRITICAL_IF_DBG
1589 #define ERROR NOBUG_ERROR
1592 #define ERROR_DBG NOBUG_ERROR_DBG
1595 #define ERROR_IF NOBUG_ERROR_IF
1597 #ifndef ERROR_IF_DBG
1598 #define ERROR_IF_DBG NOBUG_ERROR_IF_DBG
1601 #define WARN NOBUG_WARN
1604 #define WARN_DBG NOBUG_WARN_DBG
1607 #define WARN_IF NOBUG_WARN_IF
1610 #define WARN_IF_DBG NOBUG_WARN_IF_DBG
1613 #define INFO NOBUG_INFO
1616 #define INFO_DBG NOBUG_INFO_DBG
1619 #define INFO_IF NOBUG_INFO_IF
1622 #define INFO_IF_DBG NOBUG_INFO_IF_DBG
1625 #define NOTICE NOBUG_NOTICE
1628 #define NOTICE_DBG NOBUG_NOTICE_DBG
1631 #define NOTICE_IF NOBUG_NOTICE_IF
1633 #ifndef NOTICE_IF_DBG
1634 #define NOTICE_IF_DBG NOBUG_NOTICE_IF_DBG
1637 #define TRACE NOBUG_TRACE
1640 #define TRACE_DBG NOBUG_TRACE_DBG
1643 #define TRACE_IF NOBUG_TRACE_IF
1645 #ifndef TRACE_IF_DBG
1646 #define TRACE_IF_DBG NOBUG_TRACE_IF_DBG
1649 #define BACKTRACE NOBUG_BACKTRACE
1651 #ifndef BACKTRACE_DBG
1652 #define BACKTRACE_DBG NOBUG_BACKTRACE_DBG
1655 #define DEPRECATED NOBUG_DEPRECATED
1657 #ifndef UNIMPLEMENTED
1658 #define UNIMPLEMENTED NOBUG_UNIMPLEMENTED
1661 #define FIXME NOBUG_FIXME
1664 #define TODO NOBUG_TODO
1667 #define PLANNED NOBUG_PLANNED
1670 #define NOTREACHED NOBUG_NOTREACHED
1672 #ifndef INJECT_GOODBAD
1673 #define INJECT_GOODBAD NOBUG_INJECT_GOODBAD
1675 #ifndef INJECT_FAULT
1676 #define INJECT_FAULT NOBUG_INJECT_FAULT
1679 #define CLEANUP NOBUG_CLEANUP
1682 #define CHECKED NOBUG_CHECKED
1685 #define UNCHECKED NOBUG_UNCHECKED
1687 #ifndef RESOURCE_ANNOUNCE
1688 #define RESOURCE_ANNOUNCE NOBUG_RESOURCE_ANNOUNCE
1690 #ifndef RESOURCE_FORGET
1691 #define RESOURCE_FORGET NOBUG_RESOURCE_FORGET
1693 #ifndef RESOURCE_ENTER
1694 #define RESOURCE_ENTER NOBUG_RESOURCE_ENTER
1696 #ifndef RESOURCE_STATE
1697 #define RESOURCE_STATE NOBUG_RESOURCE_STATE
1699 #ifndef RESOURCE_LEAVE
1700 #define RESOURCE_LEAVE NOBUG_RESOURCE_LEAVE
1702 #ifndef RESOURCE_LEAVE_LOOKUP
1703 #define RESOURCE_LEAVE_LOOKUP NOBUG_RESOURCE_LEAVE_LOOKUP
1705 #ifndef RESOURCE_HANDLE
1706 #define RESOURCE_HANDLE NOBUG_RESOURCE_HANDLE
1708 #ifndef RESOURCE_HANDLE_INIT
1709 #define RESOURCE_HANDLE_INIT NOBUG_RESOURCE_HANDLE_INIT
1711 #ifndef RESOURCE_DUMP
1712 #define RESOURCE_DUMP NOBUG_RESOURCE_DUMP
1714 #ifndef RESOURCE_DUMP_IF
1715 #define RESOURCE_DUMP_IF NOBUG_RESOURCE_DUMP_IF
1717 #ifndef RESOURCE_DUMPALL
1718 #define RESOURCE_DUMPALL NOBUG_RESOURCE_DUMPALL
1720 #ifndef RESOURCE_DUMPALL_IF
1721 #define RESOURCE_DUMPALL_IF NOBUG_RESOURCE_DUMPALL_IF
1723 #ifndef RESOURCE_LIST
1724 #define RESOURCE_LIST NOBUG_RESOURCE_LIST
1726 #ifndef RESOURCE_LIST_IF
1727 #define RESOURCE_LIST_IF NOBUG_RESOURCE_LIST_IF
1729 #endif /* NOBUG_DISABLE_SHORTNAMES */
1736 #define NOBUG_CLEANUP(fn) NOBUG_IF_ALPHA(__attribute__((cleanup(fn))))
1738 #define NOBUG_CLEANUP(fn)
1742 //index.comma xref:NOBUG_ALPHA_COMMA[NOBUG_ALPHA_COMMA]:: append something after a comma in *ALPHA* builds
1743 //toolmacros [[NOBUG_ALPHA_COMMA]]
1744 //toolmacros .NOBUG_ALPHA_COMMA
1745 //toolmacros NOBUG_ALPHA_COMMA(something)
1746 //toolmacros NOBUG_ALPHA_COMMA_NULL
1748 //toolmacros Sometimes it is useful to have initializer code only in *ALPHA* builds, for example when you
1749 //toolmacros conditionally include resource handles only in *ALPHA* versions. An initializer can then
1750 //toolmacros use this macros to append a comman and something else only in *ALPHA* builds as in:
1751 //toolmacros struct foo = {"foo", "bar" NOBUG_ALPHA_COMMA_NULL };
1754 #define NOBUG_COMMA ,
1755 #define NOBUG_ALPHA_COMMA(something) NOBUG_IF_ALPHA(NOBUG_COMMA something)
1756 #define NOBUG_ALPHA_COMMA_NULL NOBUG_ALPHA_COMMA(NULL)
1758 #define NOBUG_ONCE(code) \
1760 static volatile int NOBUG_CAT(nobug_once_,__LINE__) = 0; \
1761 if (NOBUG_EXPECT_FALSE(!NOBUG_CAT(nobug_once_,__LINE__))) \
1763 NOBUG_CAT(nobug_once_,__LINE__) = 1; \
1769 #define NOBUG_EXPECT_FALSE(x) __builtin_expect(!!(x),0)
1771 #define NOBUG_EXPECT_FALSE(x) x
1774 #define NOBUG_WHEN(when, ...) \
1775 do{ if (NOBUG_EXPECT_FALSE(when)){ __VA_ARGS__;}} while(0)
1778 //index.if xref:NOBUG_IF[NOBUG_IF_*]:: include code conditionally on build level
1779 //toolmacros [[NOBUG_IF]]
1780 //toolmacros .NOBUG_IF_*
1781 //toolmacros NOBUG_IF_ALPHA(...)
1782 //toolmacros NOBUG_IF_NOT_ALPHA(...)
1783 //toolmacros NOBUG_IF_BETA(...)
1784 //toolmacros NOBUG_IF_NOT_BETA(...)
1785 //toolmacros NOBUG_IF_RELEASE(...)
1786 //toolmacros NOBUG_IF_NOT_RELEASE(...)
1788 //toolmacros This macros allow one to conditionally include the code in '(...)' only if the
1789 //toolmacros criteria on the build level is met. If not, nothing gets substituted. Mostly used
1790 //toolmacros internally, but can also be used for custom things.
1793 #define NOBUG_IF_ALPHA(...) \
1794 NOBUG_IF(NOBUG_MODE_ALPHA, __VA_ARGS__) \
1796 #define NOBUG_IF_NOT_ALPHA(...) \
1797 NOBUG_IFNOT(NOBUG_MODE_ALPHA, __VA_ARGS__) \
1799 #define NOBUG_IF_BETA(...) \
1800 NOBUG_IF(NOBUG_MODE_BETA, __VA_ARGS__) \
1802 #define NOBUG_IF_NOT_BETA(...) \
1803 NOBUG_IFNOT(NOBUG_MODE_BETA, __VA_ARGS__) \
1805 #define NOBUG_IF_RELEASE(...) \
1806 NOBUG_IF(NOBUG_MODE_RELEASE, __VA_ARGS__) \
1808 #define NOBUG_IF_NOT_RELEASE(...) \
1809 NOBUG_IFNOT(NOBUG_MODE_RELEASE, __VA_ARGS__) \
1812 preprocessor hacks/metaprogramming
1815 #define NOBUG_IF(bool, ...) NOBUG_CAT(NOBUG_IF_,bool)(__VA_ARGS__)
1816 #define NOBUG_IF_1(...) __VA_ARGS__
1817 #define NOBUG_IF_0(...)
1819 #define NOBUG_IFNOT(bool, ...) NOBUG_CAT(NOBUG_IF_, NOBUG_NOT(bool))(__VA_ARGS__)
1821 #define NOBUG_NOT(bool) NOBUG_CAT(NOBUG_NOT_, bool)
1822 #define NOBUG_NOT_1 0
1823 #define NOBUG_NOT_0 1
1825 #define NOBUG_AND(a,b) NOBUG_CAT3(NOBUG_AND_, a, b)
1826 #define NOBUG_AND_00 0
1827 #define NOBUG_AND_01 0
1828 #define NOBUG_AND_10 0
1829 #define NOBUG_AND_11 1
1831 #define NOBUG_OR(a,b) NOBUG_CAT3(NOBUG_OR_, a, b)
1832 #define NOBUG_OR_00 0
1833 #define NOBUG_OR_01 1
1834 #define NOBUG_OR_10 1
1835 #define NOBUG_OR_11 1
1837 #define NOBUG_XOR(a,b) NOBUG_CAT( NOBUG_XOR_, NOBUG_CAT(a,b))
1838 #define NOBUG_XOR_00 0
1839 #define NOBUG_XOR_01 1
1840 #define NOBUG_XOR_10 1
1841 #define NOBUG_XOR_11 0
1843 #define NOBUG_CAT(a,b) NOBUG_CAT_(a,b)
1844 #define NOBUG_CAT_(a,b) a##b
1846 #define NOBUG_CAT3(a,b,c) NOBUG_CAT3_(a,b,c)
1847 #define NOBUG_CAT3_(a,b,c) a##b##c
1849 #define NOBUG_HEAD(head, ...) head
1850 #define NOBUG_TAIL(_, ...) , ## __VA_ARGS__
1852 #define NOBUG_STRINGIZE(s) NOBUG_STRINGIZE_(s)
1853 #define NOBUG_STRINGIZE_(s) #s
1857 LIBNOBUG DECLARATIONS
1866 enum nobug_log_targets
1868 NOBUG_TARGET_RINGBUFFER
,
1869 NOBUG_TARGET_CONSOLE
,
1871 NOBUG_TARGET_SYSLOG
,
1872 NOBUG_TARGET_APPLICATION
1878 struct nobug_flag
* parent
;
1881 struct nobug_ringbuffer
* ringbuffer_target
;
1882 FILE* console_target
;
1887 nobug_env_parse_flag (const char* env
, struct nobug_flag
* flag
, int default_target
, int default_limit
);
1890 nobug_env_init_flag (struct nobug_flag
* flag
, int default_target
, int default_limit
);
1896 struct nobug_ringbuffer
1905 enum nobug_ringbuffer_flags
1907 NOBUG_RINGBUFFER_DEFAULT
, /* Default is to overwrite file and delete it on nobug_ringbuffer_destroy */
1908 NOBUG_RINGBUFFER_APPEND
= 1, /* use existing backing file, append if possible */
1909 NOBUG_RINGBUFFER_TEMP
= 2, /* unlink file instantly */
1910 NOBUG_RINGBUFFER_KEEP
= 4 /* dont unlink the file at destroy */
1914 Note: some flags conflict (TEMP with KEEP) nobug_ringbuffer will not error on these but continue gracefully
1915 with sane (but undefined) semantics.
1917 struct nobug_ringbuffer
*
1918 nobug_ringbuffer_init (struct nobug_ringbuffer
* self
, size_t size
,
1919 const char * name
, int flags
);
1921 struct nobug_ringbuffer
*
1922 nobug_ringbuffer_new (size_t size
, const char * name
, int flags
);
1924 struct nobug_ringbuffer
*
1925 nobug_ringbuffer_destroy (struct nobug_ringbuffer
* self
);
1928 nobug_ringbuffer_delete (struct nobug_ringbuffer
* self
);
1931 nobug_ringbuffer_vprintf (struct nobug_ringbuffer
* self
, const char* fmt
, va_list ap
);
1934 nobug_ringbuffer_printf (struct nobug_ringbuffer
* self
, const char* fmt
, ...);
1937 nobug_ringbuffer_prev (struct nobug_ringbuffer
* self
, char* pos
);
1940 nobug_ringbuffer_next (struct nobug_ringbuffer
* self
, char* pos
);
1943 nobug_ringbuffer_save (struct nobug_ringbuffer
* self
, FILE* out
);
1946 nobug_ringbuffer_load (struct nobug_ringbuffer
* self
, FILE* in
);
1949 nobug_ringbuffer_pos (struct nobug_ringbuffer
* self
);
1952 nobug_ringbuffer_pop (struct nobug_ringbuffer
* self
);
1956 multithreading extras
1958 #if NOBUG_USE_PTHREAD
1960 struct nobug_tls_data
1962 const char * thread_id
;
1963 struct nobug_resource_record
* lock_stack
;
1966 extern pthread_key_t nobug_tls_key
;
1969 nobug_thread_id_set (const char* name
);
1972 nobug_thread_id_get (void);
1974 extern pthread_mutex_t nobug_logging_mutex
;
1975 extern pthread_mutex_t nobug_resource_mutex
;
1983 #if NOBUG_USE_PTHREAD
1984 #define NOBUG_RESOURCE_LOCK pthread_mutex_lock (&nobug_resource_mutex)
1985 #define NOBUG_RESOURCE_UNLOCK pthread_mutex_unlock (&nobug_resource_mutex)
1987 Note: errors are all fatal, we dont unlock the mutex when returning an error,
1988 this should be handled better in future but does not hurt
1991 #define NOBUG_RESOURCE_LOCK
1992 #define NOBUG_RESOURCE_UNLOCK
1997 struct nobug_resource_record
1999 const char* type
; /* type or state */
2000 const char* name
; /* name */
2001 void* object_id
; /* unique identifer, usually a this pointer or similar */
2002 void* data
; /* tree of holders in resouces, backpointer to resource in holders */
2003 const char* extra
; /* extra information, file/line usually */
2004 unsigned long counter
; /* users or recursive enters */
2005 #if NOBUG_USE_PTHREAD
2006 const char* thread_id
;
2007 struct nobug_resource_record
* link
; /* used by the deadlock detector */
2011 enum nobug_resource_state
2013 NOBUG_RESOURCE_WAITING
,
2014 //NOBUG_RESOURCE_TRYING,
2015 NOBUG_RESOURCE_EXCLUSIVE
,
2016 NOBUG_RESOURCE_RECURSIVE
2019 extern void* nobug_resource_registry
;
2021 extern const char* nobug_resource_error
;
2023 extern const char* nobug_resource_states
[];
2025 struct nobug_resource_record
*
2026 nobug_resource_announce (const char* type
, const char* name
, const void* object_id
, const char* extra
);
2029 nobug_resource_forget (struct nobug_resource_record
* node
);
2031 struct nobug_resource_record
*
2032 nobug_resource_find (const struct nobug_resource_record
* resource
, const void* object_id
);
2034 struct nobug_resource_record
*
2035 nobug_resource_enter (struct nobug_resource_record
* resource
,
2037 const void* object_id
,
2038 enum nobug_resource_state state
,
2042 nobug_resource_state (struct nobug_resource_record
* resource
,
2043 enum nobug_resource_state state
);
2046 nobug_resource_leave (struct nobug_resource_record
* handle
);
2049 struct nobug_resource_dump_context
2051 struct nobug_flag
* flag
;
2059 nobug_resource_dump (struct nobug_resource_record
* resource
, struct nobug_resource_dump_context
* context
);
2062 nobug_resource_dump_all (struct nobug_resource_dump_context
* context
);
2065 nobug_resource_list (struct nobug_resource_dump_context
* context
);
2069 global config, data and defaults
2071 extern const char* nobug_resource_acquired
;
2073 void nobug_init (void);
2076 nobug_backtrace_glibc (void);
2079 nobug_log (struct nobug_flag
* flag
, int lvl
, const char* fmt
, ...);
2081 typedef void (*nobug_application_cb
)(struct nobug_flag
* flag
, int priority
, const char *log
, void* data
);
2083 extern struct nobug_ringbuffer nobug_default_ringbuffer
;
2084 extern FILE* nobug_default_file
;
2085 extern struct nobug_flag nobug_flag_NOBUG_ON
;
2086 extern struct nobug_flag nobug_flag_NOBUG_ANN
;
2087 extern struct nobug_flag nobug_flag_nobug
;
2088 extern unsigned long long nobug_counter
;
2089 extern nobug_application_cb nobug_callback
;
2090 extern void* nobug_callback_data
;
2096 #ifndef NOBUG_LIBNOBUG_C
2099 tag this translation unit as unchecked in ALPHA and BETA builds
2101 NOBUG_IF_NOT_RELEASE(NOBUG_UNCHECKED
);
2103 #endif /* NOBUG_LIBNOBUG_C */