FIX: the glibc logger using nobug_log internally, no locking needed
[nobug.git] / README
blob43ae853741d2d05aec0d8e7d4c125ae1243c4bf0
1 NoBug
2 =====
3 Christian_Thäter,_Benny_Lyons
5 // doc/overview.txt:1 //
6 ____
7 Everyone makes mistakes, but with NoBug you won't make them twice!
8 ____
10 Nobug is a debugging library for instrumenting C and C++ programs
11 inspired by ideas originating from Design-by-Contract.
13 Overview
14 --------
16 The following features are provided by NoBug:
18   * Three different check levels: from detailed to final no-overhead
19   * Scope tags: tell whenever a function or loop is considered to be bug free
20   * Precondition, Postcondition and Invariant checks and generic assertions
21   * Debugger support (actions are only executed while running under a
22     debugger), currently only valgrind is supported
23   * Datastructures can be dumped
24   * Application activities can be logged
25   * Runtime customizable logging via an environment variable
26   * Different logging targets to stderr, syslog, debugger, ...
27   * Annotation of your sourcecode about known bugs, things to do, etc.
28   * Tracking resources (files, locks, etc.) used by your program; help in
29     detecting misuse
30   * Detecting potential deadlocks
31   * Simulate errors by injecting faults
32   * Additionally, the NoBug project is used to maintain a script and
33     some tools to setup testsuites
35 In contrast to traditional debuggers, NoBug is a non-interactive debugger which
36 is linked to your application doing hard-coded tests in an efficient,
37 low-overhead way.
39 .What NoBug can not do
41 NoBug is a (macro-)library, it is not a C/C++ language extension. This
42 means that code must be called at runtime to benefit from the set up
43 contracts. Whats not tested is likely slipping through the net. Being
44 part of the program itself it is affected by memory corruption,
45 certain kinds of misuse may introduce new bugs (test expressions with
46 side effectsfor example).
48 // doc/buildinstall.txt:1 //
49 Building and Installing
50 -----------------------
52 Supported Platforms
53 ~~~~~~~~~~~~~~~~~~~
55 NoBug is developed on linux, using gcc. It should be possible to port
56 it to any other POSIX conforming operating system. Most platform
57 specific things are kept optional, but some things need to be
58 rewritten for the target platform. Currently Linux with a C99
59 conforming gcc is supported, for both, 32 and 64 bit architectures.
60 One gcc extentsion is used (token pasting of varadic macros) which
61 prevents portability to other compilers which don't support this. When
62 this is a problem, contact the NoBug author for implementing a workaround.
64 A Solaris and a Free BSD port is planned at the time of this writing.
66 NoBug has no mandatory dependencies on other software and libraries,
67 some things like valgrind support are optional and automatially
68 detected.
71 Release Tarballs
72 ~~~~~~~~~~~~~~~~
74 Releases are available on:
75   http://www.pipapo.org/nobug-releases/
77 Gpg signed tarballs are being used for distribution. The first step involves
78 checking the signature:
80  $ gpg nobug-VERSION.tar.gz.gpg
82 This will produce a nobug-VERSION.tar.gz and report if the signature could be
83 validated.
85 Since they are built with gnu autotools, the usual build and install procedure
86 will work:
88  $ tar xzvf nobug-VERSION.tar.gz
89  $ cd nobug-VERSION
90  $ ./configure
91  $ make
92  $ make check           # optional, run the testsuite
93  $ make install
96 Development Version via git
97 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
99 The development version is available via git from
100 `git://git.pipapo.org/nobug` or mirrored at repo.or.cz
101 `git://repo.or.cz/nobug.git`.
103 After you have cloned the repository, you'll then have to bootstrap the
104 autotools first:
106  $ autoreconf -i
108 Then the usual `./configure && make && make install` (as above) will work.
109 Careful users may run `make check` to run a testsuite before installing.
112 What Is Installed
113 ~~~~~~~~~~~~~~~~~
115 Currently, NoBug installs the following:
117   * A single nobug.h headerfile which your code will use
118   * Libraries that are used by statically linking:
119     - `libnobug.a` for singlethreaded programs.
120     - `libnobugmt.a` for multithreaded programs.
121   * Libraries for dynamic linking:
122     - `libnobug.so` for singlethreaded programs.
123     - `libnobugmt.so` for multithreaded programs.
124     - associated libtool descriptors (`libnobug*.la`)
125   * Pkgconfig control files:
126     - `nobug.pc` for singlethreaded programs.
127     - `nobugmt.pc` for multithreaded programs.
128   * The `nobug_rbdump` utility to inspect NoBug ringbuffers
130 // doc/using.txt:1 //
131 Using NoBug
132 -----------
134 To use NoBug, one needs to include the 'nobug.h' header and link with
135 its approbiate libararies. Compiling must select a
136 xref:buildlevel[build level], else a error will be reported.
137 Many aspects of NoBug can be configured by overriding macros before
138 'nobug.h' gets included.
140 A project using NoBug may use autoconf to check for execinfo and
141 valgrind.
143  AC_CHECK_HEADER([execinfo.h], AC_DEFINE(HAVE_EXECINFO_H))
144  PKG_HAVE_DEFINE_WITH_MODULES(VALGRIND, [valgrind])
146 For Multithreaded programs, you should also check for pthreads
147 availability and flavor
149  ACX_PTHREAD
151 When the resulting `HAVE_PTHREAD`, `HAVE_EXECINFO_H` and
152 `HAVE_VALGRIND_H` are defined by the configure script, the
153 corresponding features become available.
155 NoBug then defines `NOBUG_USE_PTHREAD`, `NOBUG_USE_VALGRIND` and
156 `NOBUG_USE_EXECINFO` to 1. If you do not want to use any of these features in
157 NoBug, you can override these macros to 0 before including nobug.h.
159 If `NVALGRIND` is defined, valgrind support will not be available.
161 There are many other macros which can be set and overridden by the user to
162 control behavior. Please help completing this documentation if you
163 find them useful.
165 To use NoBug with single threaded programmes, link 'libnobug' to your project;
166 multi-threaded programmes should link with 'libnobugmt'. The both libraries must
167 be initialized with `NOBUG_INIT` before any features can be used or a thread is
168 created. This is discussed in more detail at the
169 xref:multithreading[multithreading] chapter.
171 .Using Nobug from a Project using autoconf
172 [source,sh]
173 ----------------
174 PKG_CHECK_MODULES(NOBUGMT_LUMIERA, [nobugmt >= 0.3rc1],
175                                  AC_DEFINE(HAVE_NOBUGMT_H),
176                                  AC_MSG_ERROR([NoBug pkg-config metadata missing])
178 ----------------
180 // doc/additional.txt:1 //
181 Checking for Additional Tools
182 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184 NoBug uses a number of optional tools depending on the application
185 and the debugging information required.  Such tools as valgrind, gdb and support
186 for multi-threaded applications can be tested whether they are available on a
187 particular system. This can be done, for example, by using autoconf.
189 Depending on the value of the following defines, the relevant tool or feature
190 is activated:
192 `*NOBUG_USE_VALGRIND*`::
193    `1`:: Use valgrind
194    `0`:: Do not use valgrind
196 `*NOBUG_USE_PTHREAD*`::
197    `1`:: Support for multi-thread applications
198    `0`:: Single-threaded applications
200 `*NOBUG_USE_EXECINFO*`::
201    `1`:: Backtrace information
202    `0`:: No backtrace information
204 This macros get automatically defined when the configuration system
205 provides associated `HAVE_*` macros, but might be overridden on the
206 users choice.
208 // doc/whichlibrary.txt:1 //
209 Link Appropriate Library
210 ~~~~~~~~~~~~~~~~~~~~~~~~
212 Finally, the appropriate library (for either single or multi-threaded
213 applications) is linked to the project.
215    *libnobug*:: Link-in this library for single threaded applications.
216    *libnobugmt*:: Link with this library for multi-threaded applications.
218 NoBug installed static and dynamic libraries. When your application
219 uses multiple dynamic libraries which use NoBug or you build a dynamic
220 library, then you have to link against the dynamic library.
222 You can use the `pkg-config` tool to gather information about NoBug in
223 your build system.
225 Release builds remove all assertions, but logging is still kept. We
226 make the assumption that bugs which were not covered in alpha and beta
227 builds will not easily show up in releases because the assertions
228 there were not sufficient. Furthermore, end users are not test bunnies
229 and will not provide good bug reports anyway. If there is a problem in
230 a release build, try to track down the cause using a beta build from
231 the same source.
233 // doc/initialization.txt:1 //
234 Initialization
235 --------------
237 Global init
238 ~~~~~~~~~~~
240 You have to call
242   NOBUG_INIT()
244 before using any other NoBug feature. Probably in main or any other library
245 initialization routine. Calling `NOBUG_INIT` more than once is supported and each
246 subsequent call will be a no-op, thus initialization in main and in libraries
247 won't interfere.
249 .Destroying NoBug
250 Since NoBug is intended to be available through the whole lifetime,
251 destroying it is not advised. Nevertheless there is a desstoy function
252  void nobug_destroy (void)
254 for shutting down NoBug, and free all resources associated with it.
255 This is mostly used in the NoBug testsuite itself to check for leaks
256 and it might be useful for other programs which employ some kind of
257 leak checker.
259 Init logging Flags
260 ~~~~~~~~~~~~~~~~~~
262 If you want to use environment variable controlled debuging, then you have to
263 initialize each defined flag with
265   NOBUG_INIT_FLAG(flagname)
269   NOBUG_INIT_FLAG_LIMIT(flagname, default)
271 or one of the C++ compatibility macros.
273 This is documented later in the xref:logconfig[logging configuration] chapter.
275 Threads
276 ~~~~~~~
278 In Multithreaded programs you should assign an identifier to each
279 thread. A thread identifier is a string which will be automatically
280 appended with an underscore and a incrementing integer. It is is created with:
282   NOBUG_THREAD_ID_SET(name)
284 Calling `NOBUG_THREAD_ID_SET("worker")` will yield in a thread
285 identifier 'worker_1' for example.
287 If you don't set an identifier, then NoBug will assign an automatic one.
288 This is further documented in the xref:multithreading[multi threading]
289 section of this manual.
291 [[initexample]]
292 .Initialization
293 [source,c]
294 -------------------------------------------------------
295 #include "nobug.h"
296 NOBUG_DEFINE_FLAG(example);
300 int main()
302     NOBUG_INIT();
303     NOBUG_THREAD_ID_SET("main");
304     NOBUG_INIT_FLAG(example);
306     ...
308 -------------------------------------------------------
310 // doc/buildlevels.txt:1 //
311 [[buildlevel]]
312 Debugging Information Granuality: The Build Levels
313 --------------------------------------------------
315 A project can be compiled with debugging information at three
316 different levels: alpha, beta and release:
318   *ALPHA*::
319         This debugging level is envisaged for the development phase of a project
320         where exhaustive testing and logging are required.
321   *BETA*::
322         This debugging level is more appropriate for projects beyond the
323         development phase and ready for trials in the field and users willing to
324         test the software.
325   *RELEASE*::
326         This level is for final, end-users.
328 .Select a Build Level
329 One of the above debugging levels is selected by defining one of the
330 following compiling options:
332     *ALPHA*::   -DEBUG_ALPHA (`#define EBUG_ALPHA`)
334     *BETA*::    -DEBUG_BETA (`#define EBUG_BETA`)
336     *RELEASE*:: -DNDEBUG (`#define NDEBUG`)
338 If none of the above switches has been set, NoBug will abort the
339 compilation with an error.
341 // doc/logging.txt:1 //
342 Logging
343 -------
345 Nearly all NoBug Macros emit some log message. NoBug gives the user fine
346 grained control over these log messages to display only interesting information
347 without loosing details.
349 Log messages are routed to different destinations which are:
351   *RINGBUFFER*::
352         The underlying storage backend. Messages are appended to the
353         end of the buffer, overwriting older messages at the front of
354         the buffer. NoBug comes with a highly efficient ringbuffer
355         implementation. This ringbuffer is temporary by default but
356         can be made persistent on disk which can be inspected with the
357         'nobug_rbdump' tool.
359   *CONSOLE*::
360         This is either just stderr or if running under a supported
361         debugger then its facilities to print messages will be used.
363   *FILE*::
364         The user can open files for log messages.
366   *SYSLOG*::
367         Messages are send to the standard system logging daemon.
369   *APPLICATION*::
370         There is are hooks which allows the programmer to catch logmessages and
371         display them in a application defined way.
373 Each logmessage has a priority describing its severity in the same way as
374 syslog messages do.
376 All non-fatal messages are associated with a programmer defined flag describing
377 the source of the message (subsystem, module, ...).
379 Putting it all together: A user can define which source/flag shall be logged at
380 what priority level and to which destination. To make this all easier NoBug
381 tries to give reasonable defaults.
383 // doc/logconfiguration.txt:1 //
384 [[logconfig]]
385 Configuration
386 ~~~~~~~~~~~~~
388 .Log Levels
390 Each Log macro has a explicit or implicit Log-Level which
391 correspondends to syslog levels. Logging is only emitted when the
392 messages is more severe or same than a defined limit.
394 [[logdefaults]]
395 .Defaults levels for logging
396 [grid="all"]
397 `````~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
398              , ALPHA, BETA   , RELEASE,
399 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
400 *ringbuffer* , TRACE, INFO   , NOTICE , ringbuffer must always be most verbose
401 *console*    , INFO , NOTICE , -1     , no log to console in release
402 *file*       , TRACE, NOTICE , WARNING,
403 *syslog*     , -1   , NOTICE , WARNING, no syslog for test runs
404 *application*, INFO , WARNING, ERROR  ,
405 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
407 Depending on the build level there is a default logging target and a default
408 limit which is choosen when the user doesn't specify one.
410 The default limits are:
412   * In *ALPHA* builds, `NOBUG_LOG_LIMIT_ALPHA` is used which defaults to `LOG_INFO`
413   * In *BETA* builds, `NOBUG_LOG_LIMIT_BETA` is used and defaults to `LOG_WARNING`
414   * In *RELEASE* builds, `NOBUG_LOG_LIMIT_RELEASE` is used and defaults to `LOG_CRIT`
416 The default targets are:
418   * In *ALPHA* builds, `NOBUG_LOG_TARGET_ALPHA` is used which defaults to
419     `NOBUG_TARGET_CONSOLE`
420   * In *BETA* builds, `NOBUG_LOG_TARGET_BETA` is used and defaults to
421     `NOBUG_TARGET_FILE`
422   * In *RELEASE* builds, `NOBUG_LOG_TARGET_RELEASE` is used and defaults to
423     `NOBUG_TARGET_SYSLOG`
425 You can override all of those values with your own preference. Alternatively
426 `NOBUG_LOG_LIMIT` and `NOBUG_LOG_TARGET` can be defined before
427 including "nobug.h" to override all defaults.
429 // doc/logflags.txt:1 //
430 [[logflags]]
431 Log Flags
432 ~~~~~~~~~
434 Flags are used to tell NoBug about subsystems/modules or even finer
435 grained parts of the code. Other logging libraries call this
436 'channels'
438 A Flag should be declared in a headerfile with
440 [[DECLARE_FLAG]]
441  NOBUG_DECLARE_FLAG(flagname)
443 preferably in one of your headers
445 Further it must be defined  in some implementation file with
447 [[DEFINE_FLAG]]
448  NOBUG_DEFINE_FLAG(flagname)
452 [[DEFINE_FLAG_LIMIT]]
453  NOBUG_DEFINE_FLAG_LIMIT(flagname, limit)
455 in one of your source files
457 There are also macros which take a 'parent' flag as parameter which is then
458 used to initialize the defaults from another flag
460 [[DEFINE_FLAG_PARENT]]
461  NOBUG_DEFINE_FLAG_PARENT(flagname, parent)
465 [[DEFINE_FLAG_PARENT_LIMIT]]
466  NOBUG_DEFINE_FLAG_PARENT_LIMIT(flagname, parent, limit)
468 This can be used to create hierachies of flags
470 PARA C++ support, c++_logflags; C++ support for log flags
472 When used in C++ programms the following additional marocs are available:
474  NOBUG_CPP_DEFINE_FLAG(name)
475  NOBUG_CPP_DEFINE_FLAG_PARENT(name, parent)
476  NOBUG_CPP_DEFINE_FLAG_LIMIT(name, default)
477  NOBUG_CPP_DEFINE_FLAG_PARENT_LIMIT(name, parent, default)
479 This macros statically initialize the flags at definition time, there is no
480 need to call `NOBUG_INIT_FLAG()` (see below).
483 .Force declarations only
485 When the preprocessor constant
487 [[DECLARE_ONLY]]
488  NOBUG_DECLARE_ONLY
490 is defined to be `1` then *all* definitions here (`NOBUG_DEFINE_*`)
491 become declarations only, when it is defined to be `0` (which is the
492 default) then all definitions behave as described.
493 This can be used to construct a headerfile which only contains
494 definitions but by default yield only declarations. This gives a
495 single point for maintaining flag configurations.
497 .Maintaining flags in a single header 'flags.h'
498 [source,c]
499 ----
500 #include <nobug.h>
503  if not included from flags.c then declare the flags,
504  else define them
505  */
506 #ifndef FLAGS_C
507 #define NOBUG_DECLARE_ONLY 1
508 #endif
510 /* use only DEFINE_FLAG here */
511 NOBUG_DEFINE_FLAG(example);
514  Reset it to 0 to cause no trouble
515  */
516 #ifndef FLAGS_C
517 #undef NOBUG_DECLARE_ONLY
518 #define NOBUG_DECLARE_ONLY 0
519 #endif
520 ----
522 .flags.c
523 [source,c]
524 ----
525 #define FLAGS_C
526 #include "flags.h"
528 ----
531 .Logging Flag Initialization
533 Next you should call
535  NOBUG_INIT_FLAG(flagname)
539  NOBUG_INIT_FLAG_LIMIT(flagname, default)
541 once at the start of your program for every flag.
543 For flags defined with `NOBUG_DEFINE_FLAG(flagname)` the defaults are initialized
544 as in the xref:logdefaults[table above], while
545 `NOBUG_DEFINE_FLAG_LIMIT(flagname, level)` is used to initialize the
546 default target (depending on build level) to `level`.
548 // doc/logflagsenv.txt:1 //
549 [[NOBUG_ENV]]
550 Control what gets logged
551 ~~~~~~~~~~~~~~~~~~~~~~~~
553 The `NOBUG_INIT_FLAG...` calls parsing the environment variable
554 'NOBUG_LOG' to configure what gets logged at runtime. The syntax is as
555 following:
557 .Formal Syntax for log control
558 [source,prolog]
559 ----
560  logdecl_list --> logdecl, any( ',' logdecl_list).
562  logdecl --> flag, opt(limitdecl, any(targetdecl)).
564  flag --> "identifier of a flag".
566  limitdecl --> ':', "LIMITNAME".
568  targetdecl --> '@', "targetname", opt(targetopts).
570  targetopts --> '(', "options for target", ')', opt(targetopts).
571 ----
573 Roughly speaking, 'NOBUG_LOG' contains a comma separated list of declarations for
574 flags which are the name of the flag followed by a limit which is written in
575 all uppercase letters and preceeded by a colon, followed by target declarations
576 which are names of the targets, introduced by a at sign. Target declarations
577 can have option, described in the next section. Limit and target
578 declarations are optional and then choosen from the defaults table above. These
579 defaults are currently just an guess what should be useable and might be
580 redefined in future.
582 .Targets and Options
584 The Following options are available:
586  `@ringbuffer`::
587    `(file=_filename_)`:: set filename backing the ringbuffer
588    `(size=_nnn_)`::      set size of the ringbuffer
589    `(append)`::          don't erase existing ringbuffer
590    `(keep)`::            keep file after application end
591    `(temp)`::            unlink file instantly at creation
593  `@console`::
594    `(fd=n)`::            redirect console output to fd n
596  `@file`::
597    `(name=_filename_)`:: log to filename
598    `(append)`::          append to (existing) log
600  `@syslog`::
601    `(ident=_name_)`::    global prefix for syslog
602    `(cons)`::            log to system console if syslog is down
603    `(pid)`::             include pid in log
604    `(perror)`::          log to stderr as well
607 .How the NOBUG_LOG is used
608 [source,sh]
609 ----
610 # set the limit of the default target a default limit (see table above)
611 NOBUG_LOG='flag,other'
613 # set the limit of the default target to DEBUG
614 NOBUG_LOG='flag:DEBUG'
616 # set console and syslog limits for flag to DEBUG
617 NOBUG_LOG='flag:DEBUG@console@syslog'
619 # trace 'other' to a persistent ringbuffer
620 NOBUG_LOG='other:TRACE@ringbuffer(file=log.rb)(size=8192)(keep)'
621 ----
623 .Using log flags (example.c)
624 [source,c]
625 ----
626 #include "nobug.h"
628 NOBUG_DEFINE_FLAG (test);
630 int main()
632    /* NOBUG_INIT;  // not needed because of NOBUG_INIT_FLAG */
633    NOBUG_INIT_FLAG (test);
635    TRACE (test, "Logging enabled");
636    TRACE (NOBUG_ON, "Always on");
638 ----
640 .test it:
641 [source,sh]
642 ----
643 $ cc -DEBUG_ALPHA -lnobug example.c
644 $ ./a.out
645 0000000002: TRACE: example.c:11: main: Always on
647 $ NOBUG_LOG=test:TRACE ./a.out
648 0000000001: TRACE: example.c:10: main: Logging enabled
649 0000000002: TRACE: example.c:11: main: Always on
650 ----
652 // src/nobug.c:38 //
653 Predefined Flags
654 ~~~~~~~~~~~~~~~~
656 There are some debugging flags which are predefined by NoBug.
658 [[NOBUG_ON]]
659 .NOBUG_ON
661 The flag `NOBUG_ON` is always enabled at LOG_DEBUG level. This is
662 static and can not be changed.
664 [[NOBUG_ANN]]
665 .NOBUG_ANN
667 The flag `NOBUG_ANN` is used for the source annotations. This is
668 static and can not be changed. It differs from `NOBUG_ON` as in
669 never logging to syslog and only define a LOG_WARNING limit for the
670 application callback.
672 [[nobug_flag]]
673 .nobug (flag)
675 Actions on NoBug itself will be logged under the `nobug` flag itself.
676 When you want to see whats going on (useful to check if you call
677 `NOBUG_INIT_FLAG()` on all flags) you can enable it with `NOBUG_LOG=nobug:TRACE`.
679 // doc/macros.txt:1 //
680 Macros
681 ------
683 The NoBug interface is almost completely implemented using
684 preprocessor macros. This is required because NoBug uses the
685 `+++__FILE__+++` and `+++__LINE__+++` macros to log information on the
686 current file and the current line number within that file. Moreover,
687 all the flat namespace uppercase identifiers make it ease to recognise
688 the macros in source code.
690 All macros are available without condition with a `NOBUG_...` prefix.
691 Macros are also available without this prefix as a convenience,
692 however macros without this prefix must not have been previously
693 defined. When `NOBUG_DISABLE_SHORTNAMES` is defined before including
694 'nobug.h', then only the `NOBUG_` prefixed macros are available and
695 the short forms will never be defined.
697 All assertion and logging macros have a corresponding form
698 postfixed by `..._DBG`. Such macros will only be active within a
699 debugger.
701 A set of macros are provided by NoBug that are postfixed by `..._IF`.
702 These macros have the following form:
704   * `..._IF(when, ...)`
706 They perform the desired action only if `when` is true. For example:
708   * `REQUIRE_IF(foo != NULL, foo->something == constrained)`
710 The assertion will only be performed if `foo` is non `NULL`.
712 Debugger versions are available using `..._IF_DBG` postfixed to the name
713 of the macro.
715 // doc/parametertable.txt:1 //
716 Parameters types
717 ~~~~~~~~~~~~~~~~
719 We use names for parameters which descripe their type. This names are
720 orthogonal through all macro definitions.
722 [grid="all"]
723 `---------`------------------------------------------------------------------
724 `when`    Assertion is only performed if expression `when` is true at runtime
725 `expr`    Test without side effects
726 `fmt`     printf-like format string
727 `...`     If not preceded by `fmt`, then printf-like format string followed by its arguments; otherwise, only its arguments
728 `flag`    Flag to enable custom logging groups
729 `type`    Data type to be checked as a single identifier name
730 `pointer` Pointer to type
731 `lvl`     Log level
732 `depth`   Depth for invariants and dumps
733 ---------------------------------------------------------------------------
735 // src/nobug.h:102 //
736 Assertions
737 ----------
739 [[CHECK]]
740 .CHECK
741  CHECK(expr, ...)
742  CHECK_IF(when, expr, ...)
744 This assertion is never optimized out. Its main purpose is for implementing
745 testsuites where one want to assert tests independent of the build level
747 [[REQUIRE]]
748 .REQUIRE
749  REQUIRE(expr, ...)
750  REQUIRE_DBG(expr, ...)
751  REQUIRE_IF(when, expr, ...)
752  REQUIRE_IF_DBG(when, expr, ...)
754 Precondition (input) check. Use these macros to validate input a
755 function receives. The checks are enabled in *ALPHA* and *BETA* builds and
756 optimized out in *RELEASE* builds.
758 [[ENSURE]]
759 .ENSURE
760  ENSURE(expr, ...)
761  ENSURE_DBG(expr, ...)
762  ENSURE_IF(when, expr, ...)
763  ENSURE_IF_DBG(when, expr, ...)
765 Postcondition (progress/output) check. Use these macros to validate the
766 data a function produces (example: return value). The checks enabled
767 unconditionally in *ALPHA* builds and optimized out in *BETA* builds for
768 scopes which are tagged as `CHECKED`. In *RELEASE* builds this checks are
769 always optimized out, but scopes tagged as `UNCHECKED` are not permitted.
771 [[ASSERT]]
772 .ASSERT
773  ASSERT(expr, ...)
774  ASSERT_DBG(expr, ...)
775  ASSERT_IF(when, expr, ...)
776  ASSERT_IF_DBG(when, expr, ...)
778 Generic check. Use these macros when you want to validate something
779 which doesn't fall into one of the above categories. A example is when
780 a library function can return a unexpected result (scanf with syntax
781 error in the formatstring, when a constant/literal formatstring is
782 expected). The checks are enabled in *ALPHA* and *BETA* builds and
783 optimized out in *RELEASE* builds.
785 [[assert]]
786 .assert
787  assert(expr)
789 NoBug overrides the standard assert macro in *ALPHA* and *BETA* builds.
790 This is just a compatibility feature, its use is not suggested.
792 [[INVARIANT]]
793 .INVARIANT
794  INVARIANT(type, pointer, depth)
795  INVARIANT_DBG(type, pointer, depth)
796  INVARIANT_IF(when,type, pointer, depth)
797  INVARIANT_IF_DBG(when, type, pointer, depth)
799 Checking invariants. You can provide more complex checking functions
800 which test the validity of datastructures. Invariants are only enabled
801 in *ALPHA* builds for scopes which are not tagged as `CHECKED` and
802 otherwise optimized out.
804  TODO: describe how to create invariant checks
806 // src/nobug.h:405 //
807 Logging Macros
808 --------------
810 [[ECHO]]
811 .ECHO
812  ECHO(...)
814 Never optimized out, logs at LOG_NOTICE level. Its main purpose is for implementing
815 testsuites where one want to print and log messages independent of the build level
817 [[ALERT]]
818 .ALERT
819  ALERT(flag, ...)
820  ALERT_DBG(flag, ...)
821  ALERT_IF(when, flag, ...)
822  ALERT_IF_DBG(when, flag, ...)
824 This is the most critical condition an application might log. This might be used
825 if an error occurs which can not be handled except a safe shutdown for example.
827 [[CRITICAL]]
828 .CRITICAL
829  CRITICAL(flag, ...)
830  CRITICAL_DBG(flag, ...)
831  CRITICAL_IF(when, flag, ...)
832  CRITICAL_IF_DBG(when, flag, ...)
834 An error which can not be handled occured but the application does not need to be
835 shutdowen, perhaps waiting for an operator to fix the cause.
837 [[ERROR]]
838 .ERROR
839  ERROR(flag, fmt, ...)
840  ERROR_DBG(flag, fmt, ...)
841  ERROR_IF(when, flag, fmt, ...)
842  ERROR_IF_DBG(when, flag, fmt, ...)
844 Application takes a error handling brach
846 [[WARN]]
847 .WARN
848  WARN(flag, fmt, ...)
849  WARN_DBG(flag, fmt, ...)
850  WARN_IF(when, flag, fmt, ...)
851  WARN_IF_DBG(when, flag, fmt, ...)
853 Rare, handled but unexpected branch
855 [[INFO]]
856 .INFO
857  INFO(flag, fmt, ...)
858  INFO_DBG(flag, fmt, ...)
859  INFO_IF(when, flag, fmt, ...)
860  INFO_IF_DBG(when, flag, fmt, ...)
862 Message about program progress
864 [[NOTICE]]
865 .NOTICE
866  NOTICE(flag, fmt, ...)
867  NOTICE_DBG(flag, fmt, ...)
868  NOTICE_IF(when, flag, fmt, ...)
869  NOTICE_IF_DBG(when, flag, fmt, ...)
871 More detailed progress message
873 [[TRACE]]
874 .TRACE
875  TRACE(flag, fmt, ...)
876  TRACE_DBG(flag, fmt, ...)
877  TRACE_IF(when, flag, fmt, ...)
878  TRACEIF_DBG(when, flag, fmt, ...)
880 Very fine grained messages
882 NOTE: that `TRACE` corresponds to `LOG_DEBUG`, because using `DEBUG` could be ambiguous.
884 [[LOG]]
885 .LOG
886  LOG(flag, lvl, fmt, ...)
887  LOG_DBG(flag, lvl, fmt, ...)
888  LOG_IF(when, flag, lvl, fmt, ...)
889  LOG_IF_DBG(when, flag, lvl, fmt, ...)
891 Generic logging macro which takes the level explicitly,
892 avoid this, unless you implement your own logging facilities.
894 [[LOG_BASELIMIT]]
895 .LOG_BASELIMIT
896  NOBUG_LOG_BASELIMIT_ALPHA
897  NOBUG_LOG_BASELIMIT_BETA
898  NOBUG_LOG_BASELIMIT_RELEASE
899  NOBUG_LOG_BASELIMIT
901 anything more detailed than this base limits will be optimized out.
902 This is used to reduce the logging overhead for *RELEASE* builds.
903 By default the limit is set to `LOG_DEBUG` for *ALPHA* and *BETA*
904 builds, so all logging is retained and `LOG_NOTICE` in *RELEASE*
905 builds to log the application progress only coarsely then.
907 This macros can be defined before including 'nobug.h' to some other
908 log level (as defined in 'syslog.h').
910 NOTE: there is no logging macro for `LOG_EMERG` since this is used by the assertions as fatal message
912 // doc/dumping.txt:1 //
913 [[dumping]]
914 Dumping Datastructures
915 ----------------------
917 How to write DUMP handlers
919 One can write functions for dumping complex datastructures using the NoBug
920 facilities. This is done by writing a custom function for each
921 datastructure to be dumped which may recursively call other dumping
922 functions. There are macros for logging within such a dumper function
923 and for initiating a dump of a given datastructure.
925 // src/nobug.h:320 //
926 [[DUMP]]
927 .DUMP
928  DUMP(flag, type, pointer, depth)
929  DUMP_IF(when, flag, type, pointer, depth)
931 This macros call a datastructure dump of the object (`pointer`) in question.
932 `DUMP_IF` is the only enabled dumping macro for the RELEASE build level.
934 [[DUMP_LOG]]
935 .DUMP_LOG
936  DUMP_LOG(fmt, ...)
937  DUMP_LOG_DBG(fmt, ...)
938  DUMP_LOG_IF(when, fmt, ...)
939  DUMP_LOG_IF_DBG(when, fmt, ...)
941 Any output from `DUMP` handlers should be done by these macros.
943 Dumping is by default done on level `LOG_DEBUG`, this can be overridden by
944 defining `NOBUG_DUMP_LEVEL` to some other level.
946 // doc/dumpexample.txt:1 //
947 PARA How to use the DUMP facilities
949 [source,c]
950 -------------------------------------------------------
951 struct STRUCTNAME
953   int INTEGER_MEMBER;
954   char * STRING_MEMBER;
955   struct STRUCTNAME* next;
957 -------------------------------------------------------
959 then you define a function like:
961 [source,c]
962 -------------------------------------------------------
963 void
964 nobug_STRUCTNAME_dump (const struct STRUCTNAME* self,
965                        const int depth,
966                        const char* file,
967                        const int line,
968                        const char* func)
970   // check for self != NULL and that the depth
971   // limit did not exceed in recursive datastructures
972   if (self && depth)
973   {
974     // use DUMP_LOG not LOG to print the data
975     DUMP_LOG("STRUCTNAME %p: int is %d, string is %s", self,
976                              self->INTEGER_MEMBER,
977                              self->STRING_MEMBER);
978     // now recurse with decremented depth
979     nobug_STRUCTNAME_dump (self->next, depth-1, file, line, func);
980   }
982 -------------------------------------------------------
984 now you can use the DUMP() macros within the code
986 [source,c]
987 -------------------------------------------------------
988 example()
990   struct STRUCTNAME foo;
991   init(&foo);
992   DUMP (my_flag, STRUCTNAME, &foo, 2);
994 -------------------------------------------------------
996 // src/nobug.h:651 //
997 Source Annotations
998 ------------------
1000 One can tagging features as:
1002 [[DEPRECATED]]
1003 .DEPRECATED
1004  DEPRECATED(...)
1006 Something which shouldn't be used in future
1008 [[UNIMPLEMENTED]]
1009 .UNIMPLEMENTED
1010  UNIMPLEMENTED(...)
1012 not yet finished feature
1014 [[FIXME]]
1015 .FIXME
1016  FIXME(...)
1018 known bug to be fixed later
1020 [[TODO]]
1021 .TODO
1022  TODO(...)
1024 enhancement to be done soon
1026 [[PLANNED]]
1027 .PLANNED
1028  PLANNED(...)
1030 future enhancement
1032 [[NOTREACHED]]
1033 .NOTREACHED
1034  NOTREACHED
1036 used to tag code-path which shall be never executed.
1038 [[ELSE_NOTREACHED]]
1039 .ELSE_NOTREACHED
1040  ELSE_NOTREACHED
1042 same as `else NOTREACHED()`, but wholly optimized out in release builds.
1044 // doc/annotationtable.txt:1 //
1046 The advantage of this tagging over plain source comments is that we can take
1047 some actions if we run in such a tag at compile or runtime:
1049 the action to be taken when such a macro is hit depends on the build level:
1051 [grid="all"]
1052 `-------------`-----`------------`-----------------------------------------
1053               ALPHA BETA         RELEASE
1054 ---------------------------------------------------------------------------
1055 DEPRECATED    log   nothing      wont compile
1056 UNIMPLEMENTED abort abort        wont compile
1057 FIXME         log   wont compile wont compile
1058 TODO          log   log          wont compile
1059 PLANNED       log   nothing      nothing
1060 NOTREACHED    abort abort        removed
1061 ---------------------------------------------------------------------------
1063 Legend:
1065   * abort means first log and then abort
1066   * log will only log once for each sourceline (not on each hit)
1067   * wont compile will abort compilation with a error message
1068   * nothing optimized out, sane way
1069   * removed optimized out for performance reasons
1071 // doc/scopechecks.txt:1 //
1072 [[CHECKED]]
1073 Scope Checks
1074 ------------
1075 [[UNCHECKED]]
1077 The programmer can tag any scope as `UNCHECKED` or `CHECKED`. In *ALPHA* and *BETA*
1078 builds, a global `UNCHECKED` is implied. In *RELEASE* builds, `UNCHECKED` scopes are
1079 not allowed.
1081 // doc/assertiontable.txt:1 //
1082 .Assertions active depending on Build level and Scope
1083 [grid="all"]
1084 `-----------`-----------------------------------------`-----------------------------`-------------------
1085             *ALPHA*                                   *BETA*                        *RELEASE*
1086 *UNCHECKED* Preconditions, Postconditions, Invariants Preconditions, Postconditions compiling will abort
1087 *CHECKED*   Preconditions, Postconditions             Preconditions
1088 ------------------------------------------------------------------------------------------------------
1090 // src/nobug.h:778 //
1091 Fault injection
1092 ---------------
1094 NoBug has some macros which can be used to simulate errorneous behaviour:
1096 [[INJECT_GOODBAD]]
1097 .INJECT_GOODBAD
1098  INJECT_GOODBAD(expr, good, bad)
1100 substitutes to an expression and returns good when expr is false and
1101 bad when expr is true. In BETA and RELEASE builds 'good' is always returned.
1103 [[INJECT_FAULT]]
1104 .INJECT_FAULT
1105  INJECT_FAULT(expr, bad)
1107 substitutes to a statement which executes 'bad'
1108 when expr is true. Optimitzed out in BETA and RELEASE builds.
1110 [[INJECT_LEVEL]]
1111 .INJECT_LEVEL
1112 In both cases, when a fault is injected it will be logged at
1113 `NOBUG_INJECT_LEVEL` (default: `LOG_NOTICE`). This can be defined
1114 before including 'nobug.h' to override it.
1116 // doc/resourcetracking.txt:1 //
1117 Resource Tracking
1118 -----------------
1120 With little effort, NoBug can watch all kinds of resources a program uses. This
1121 becomes useful for resources which are distributed over a multithreaded
1122 program. Resource tracking is only active in ALPHA builds and optimized out in
1123 BETA and RELEASE builds.
1125 Concepts
1126 ~~~~~~~~
1128 Resources are abstracted, NoBug has little knowledge about the semantics of a
1129 resource, it only keeps records of resources and the code using it and ensures
1130 basic constraints. Detailed usage checks of resource have to be done with other
1131 NoBug facilities.
1133 Resources are identified by a arbitrary identifier which is just a
1134 pointer. Additionally a name, the type and the source locations which
1135 announced the resource are stored.
1137 Code which wants to use a resource calls a enter macro with its own identifier
1138 and state, then might alter the state and finally a leave macro when finished
1139 with it.
1141 When a resource is used one has to pass one of this states:
1143   * NOBUG_RESOURCE_WAITING
1144       + For resources where acquisition could block (locks) you enter it with a
1145         WAITING state first and as soon you acquired it you change the state to one
1146         of the following.
1147   * NOBUG_RESOURCE_EXCLUSIVE
1148       + Acquired the resource exclusively. It must not be acquired
1149         again, not even from the same thread.
1150   * NOBUG_RESOURCE_RECURSIVE
1151       + The resource might be entered multiple times from the same
1152         thread with this state.
1153   * NOBUG_RESOURCE_SHARED
1154       + The resource might be entered multiple times from any thread
1155         with this state.
1157 Possible state transitions:
1159 ["graphviz", "resource-transistinons.png"]
1160 ---------------------------------------------------------------------
1161 strict digraph G
1163         edge [fontname=Courier fontsize=10]
1165         start [shape=ellipse]
1167         node [shape=box]
1169         start -> Waiting [label="ENTER()"]
1170         start -> Exclusive [label="ENTER()"]
1171         start -> Recursive [label="ENTER()"]
1173         Waiting -> Exclusive [label="STATE()"]
1174         Waiting -> Recursive [label="STATE()"]
1176         Recursive -> Recursive [label="ENTER()\nSTATE()"]
1178         Waiting -> end [label="LEAVE()"]
1179         Exclusive -> end [label="LEAVE()"]
1180         Recursive -> end [label="LEAVE()"]
1182         end [shape=ellipse]
1184 ---------------------------------------------------------------------
1186 Notes
1187 ~~~~~
1189 There are small race conditions in the time we
1190 announce/forget/enter/remove resources and doing the actual call to a
1191 resource. These race conditions affect the reporting exactness and are
1192 a design decision. When this poses a problem it will be fixed.
1194 The Resource Tracker relies on that announce/forget and enter/leave
1195 are properly paired. The programmer should ensure that this is done
1196 right, else it will become unusable.
1198 // src/nobug.h:995 //
1199 Resource tracking macros
1200 ~~~~~~~~~~~~~~~~~~~~~~~~
1202 [[RESOURCE_LOGGING]]
1203 [[RESOURCE_LOG_LEVEL]]
1205 Unless the user defines `NOBUG_RESOURCE_LOGGING` to 0 each of the above macros
1206 will emit a log message at `NOBUG_RESOURCE_LOG_LEVEL` which defaults to
1207 `LOG_DEBUG`.
1209 [[RESOURCE_HANDLE]]
1210 .RESOURCE_HANDLE
1211  RESOURCE_HANDLE(name)
1213 Defines handles to access resources.
1214 Resources are accessed through handles.
1216  `name`::
1217      identifer to be used for the handle
1219 This macro takes care that the declaration is optimized out in the same manner
1220 the rest of the resource tracker would be disabled. You can still instantiate
1221 handles as `struct nobug_resource_header*` in structures which must have a
1222 constant size unconditional of the build level.
1224 [[RESOURCE_ANNOUNCE]]
1225 .RESOURCE_ANNOUNCE
1226  RESOURCE_ANNOUNCE(flag, type, name, identifier, handle)
1228 Publishes resources.
1230  `flag`::
1231      the NoBug flag which turns logging on for this macro
1232  `type`::
1233      a string which should denote the domain of the resource,
1234      examples are "file", "mutex", "lock", "database" and so on
1235  `name`::
1236      the actual name of a named resource this as string which
1237      together with type forms a unique identifier of the resource. `type` and
1238      `name` must be available through the entire lifetime of the resource, using
1239      literal strings is recommended
1240  `identifier`::
1241      a pointer which should be unique for this resource, any
1242      kind of pointer will suffice, it is only used for identification. In
1243      multithreaded applications the thread identifier becomes an additional
1244      identifier
1245  `handle`::
1246      a `NOBUG_RESOURCE_HANDLE` which will be initialized to point to
1247      the newly created resource.
1249 Resources must be unique, it is a fatal error when a resource it tried to be
1250 announced more than one time.
1252 [[RESOURCE_FORGET]]
1253 .RESOURCE_FORGET
1254  RESOURCE_FORGET(flag, handle)
1256 Removes resources that have become unavailable from the registry.
1258 `flag`::
1259     the NoBug flag which turns logging on for this macro
1260 `handle`::
1261     the `NOBUG_RESOURCE_HANDLE` used to track this resource
1263 The resource must still exist and no users must be attached to it, else a fatal
1264 error is raised.
1266 [[RESOURCE_ENTER]]
1267 .RESOURCE_ENTER
1268  RESOURCE_ENTER(flag, announced, name, identifier, state, handle)
1270 Acquire a resource.
1272 `flag`::
1273     nobug flag which turns logging on for this macro
1274 `resource`::
1275     the handle set by `RESOURCE_ANNOUNCE`
1276 `name`::
1277     a free-form identifier
1278 `user`::
1279     some pointer which must be unique for the user of the
1280     resource (see above)
1281 `state`::
1282     the initial state, one of `NOBUG_RESOURCE_WAITING`,
1283     `NOBUG_RESOURCE_EXCLUSIVE`, `NOBUG_RESOURCE_RECURSIVE` or `NOBUG_RESOURCE_SHARED`
1284 `handle`::
1285     a `NOBUG_RESOURCE_HANDLE` which will be initialized to the
1286     entering node
1288 [[RESOURCE_STATE]]
1289 .RESOURCE_STATE
1290  RESOURCE_STATE(flag, entered, state)
1292 Changes resource's state.
1294 `flag`::
1295     is nobug flag which turns logging on for this macro
1296 `state`::
1297     the new state Note that only certain state transitions are
1298     allowed, see discussion/diagram above
1299 `entered`::
1300     the handle set by `RESOURCE_ENTER`
1302 [[RESOURCE_LEAVE]]
1303 .RESOURCE_LEAVE
1304  RESOURCE_LEAVE(flag, handle)
1306 Disconnect from a resource identified with its handle.
1308 `flag`::
1309     nobug flag which turns logging on for this macro
1310 `handle`::
1311     the handle you got while entering the resource
1313 [[RESOURCE_DUMP]]
1314 .RESOURCE_DUMP
1315  NOBUG_RESOURCE_DUMP(flag, handle)
1316  NOBUG_RESOURCE_DUMP_IF(when, flag, handle)
1318 Dump the state of a single resource.
1320 `when`::
1321     Condition which must be true to dump the resource
1322 `flag`::
1323     Nobug flag for the log channel
1324 `handle`::
1325     handle of the resource to be dumped
1327 [[RESOURCE_DUMPALL]]
1328 .RESOURCE_DUMPALL
1329  NOBUG_RESOURCE_DUMPALL(flag)
1330  NOBUG_RESOURCE_DUMPALL_IF(when, flag)
1332 Dump the state of all resources.
1334 `when`::
1335     Condition which must be true to dump the resources
1336 `flag`::
1337     Nobug flag for the log channel
1339 [[RESOURCE_LIST]]
1340 .RESOURCE_LIST
1341  NOBUG_RESOURCE_LIST(flag)
1342  NOBUG_RESOURCE_LIST_IF(when, flag)
1344 List all registered resources.
1346 `when`::
1347     Condition which must be true to list the resources
1348 `flag`::
1349     Nobug flag for the log channel
1351 // doc/resourceexample.txt:1 //
1352 .How to use the Resourcetracker
1353 [source,c]
1354 ----
1355 NOBUG_DEFINE_FLAG_LIMIT(test, LOG_DEBUG);
1357 void example()
1359   // define a mutex and announce it
1360   pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;
1361   RESOURCE_HANDLE(resource);
1363   // 'example' is just a pointer to this function which suffices as unique id
1364   RESOURCE_ANNOUNCE(test, "mutex", "my_mutex", example, resource);
1366   // the following would be done in a different thread in a real program
1367   RESOURCE_HANDLE(enter);                                  // define a handle
1369   RESOURCE_ENTER(flag, resource, &enter, NOBUG_RESOURCE_WAITING, enter);
1370                                                            // announce that we want to use the resource
1371                                                            // &enter also suffices as unique pointer
1372   pthread_mutex_lock (&my_mutex);                          // this might block
1373   RESOURCE_STATE(test, NOBUG_RESOURCE_EXCLUSIVE, enter);   // we got it, change the state
1375   // program does something useful here
1377   pthread_mutex_lock (&my_mutex);                          // and instantly unlock
1378   RESOURCE_LEAVE(test, enter);                             // we don't need it anymore
1380   // back in the main thread
1381   RESOURCE_FORGET(test, resource);                         // remove the resource from the public registry
1383 ----
1385 // doc/resourcedeadlock.txt:1 //
1386 Deadlock Detection
1387 ~~~~~~~~~~~~~~~~~~
1389 The Resource Tracker is able to detect potential deadlocks. This is done by
1390 learning the relations between locks (precedence). A possible deadlock results
1391 in a log message and a fatal abort. Note that only waiting on resources can
1392 lead to a deadlock. Deadlock detection is implemented in the Resource Tracker
1393 and active in ALPHA builds and optimized out on any other build level.
1395 For details about the deadlock detection algorithm see
1396 xref:deadlock_detection[Appendix: Resource Tracking Alorithm].
1398 // src/nobug.h:2209 //
1399 Callbacks
1400 ---------
1402 NoBug provides callbacks, applications can use these
1403 to present logging information in some custom way or hook some special processing in.
1404 The callbacks are initialized to NULL and never modified by NoBug, its the solve responsibility
1405 of the user to manage them.
1407 CAUTION: There are certain constraints what and what not can be done in callbacks
1408          documented below which must be followed.
1410 [[logging_cb]]
1411 .type of logging callbacks
1412  typedef void (*nobug_logging_cb)(const struct nobug_flag* flag, int priority, const char *log, void* data)
1414 used for the logging callbacks
1416  `flag`::
1417     Flag structure which defines the logging configuration for this event
1418  `priority`::
1419     Log level of the current event
1420  `log`::
1421     Pointing to the current log line in the ringbuffer or `NULL`
1422  `data`::
1423     Global pointer defined by the user, passed arround (see below)
1425 [[abort_cb]]
1426 .type of abort callback
1427  typedef void (*nobug_abort_cb)(void* data)
1429 used for the abort callback
1431  `data`::
1432     Global data defined by the user, passed arround (see below)
1434 [[callback_data]]
1435 .passing data to callbacks
1436  void* nobug_callback_data
1438 This global variable is initialized to `NULL` and will never be touched by NoBug. One can use it
1439 to pass extra data to the callback functions.
1441 [[logging_callback]]
1442 .callback when logging
1443  nobug_logging_cb nobug_logging_callback
1445 This callback gets called when something gets logged.
1446 NoBug will still hold its mutexes when calling this hook, calling NoBug logging or resource tracking
1447 functions from here recursively will deadlock and must be avoided.
1448 The `log` parameter points to the logging message in the ringbuffer.
1449 Unlike other logging targets it is not automatically limited to the log level configured
1450 in the flag but called unconditionally. The callback should implement its own limiting.
1452 When one wants to do complex calls which may include recursion into logging and resource tracking
1453 functions, the intended way is to pass contextual information possibly including a __copy__ of the
1454 `log` parameter in xref:THREAD_DATA[NOBUG_THREAD_DATA] to the postlogging callback (see below).
1455 Other internal NoBug facilties, like the ringbuffer etc, are protected by the mutexes and may be accessed
1456 from this function.
1458 [[postlogging_callback]]
1459 .callback after logging
1460  nobug_logging_cb nobug_postlogging_callback
1462 This callback gets called after something got logged. The `log` parameter is always NULL and all
1463 NoBug mutexes are released. This means that this function may call any complex things, including
1464 calling logging and resource tracking, but may not call internal NoBug facilities.
1465 Contextual created in the `nobug_logging_callback` and stored in texref:NOBUG_THREAD_DATA can be
1466 retrieved here and may need to be cleaned up here.
1468 [[abort_callback]]
1469 .callback for aborting
1470  nobug_abort_cb nobug_abort_callback
1472 This callback gets called when the application shall be terminated due an error.
1473 It can be used to hook exceptions or similar things in. When it returns, `abort()`
1474 is called.
1476 IMPORTANT: Errors detected by NoBug are always fatal. If one handles and possible
1477            throws an exception here, the application must shut down as soon as possible.
1478            Most causes for aborts are optimitzed out in `RELEASE` builds.
1480 // src/nobug.h:1404 //
1482 [[NOBUG_FLAG_RAW]]
1483 .NOBUG_FLAG_RAW
1484  NOBUG_FLAG_RAW(ptr)
1486 Using this macro one can pass a direct pointer to a flag where a name would
1487 be expected. This is sometimes convinient when flag pointers are passed around
1488 in management strutures and one wants to tie logging to dynamic targets.
1490 [source,c]
1491 ----
1492 NOBUG_DEFINE_FLAG(myflag);
1494 struct nobug_flag* ptr = &NOBUG_FLAG(myflag);
1495 TRACE(NOBUG_FLAG_RAW(ptr), "Passed flag by pointer")
1496 ----
1498 [[BACKTRACE]]
1499 .Backtraces
1500  BACKTRACE
1502 The backtrace macro logs a stacktrace using the NoBug facilities.
1503 This is automatically called when NoBug finds an error and is due
1504 to abort. But one might call it manually too.
1506 [[ABORT]]
1507 .Aborting
1508  NOBUG_ABORT_
1510 This is the default implementation for aborting the program, it first calls the
1511 abort callback if defined and then `abort()`.
1513  NOBUG_ABORT
1515 If not overridden, evaluates to `NOBUG_ABORT_`. One can override this before including
1516 `nobug.h` to customize abortion behaviour. This will be local to the translation unit then.
1518 [[NOBUG_ALPHA_COMMA]]
1519 .NOBUG_ALPHA_COMMA
1520  NOBUG_ALPHA_COMMA(something)
1521  NOBUG_ALPHA_COMMA_NULL
1523 Sometimes it is useful to have initializer code only in *ALPHA* builds, for example when you
1524 conditionally include resource handles only in *ALPHA* versions. An initializer can then
1525 use this macros to append a comman and something else only in *ALPHA* builds as in:
1526  struct foo = {"foo", "bar" NOBUG_ALPHA_COMMA_NULL };
1528 [[NOBUG_IF]]
1529 .NOBUG_IF_*
1530  NOBUG_IF_ALPHA(...)
1531  NOBUG_IF_NOT_ALPHA(...)
1532  NOBUG_IF_BETA(...)
1533  NOBUG_IF_NOT_BETA(...)
1534  NOBUG_IF_RELEASE(...)
1535  NOBUG_IF_NOT_RELEASE(...)
1537 This macros allow one to conditionally include the code in '(...)' only if the
1538 criteria on the build level is met. If not, nothing gets substituted. Mostly used
1539 internally, but can also be used for custom things.
1541 // doc/multithreading.txt:1 //
1542 [[multithreading]]
1543 Multithreading
1544 --------------
1546 It is important that NoBug protects certain operations with locks in
1547 multithreaded programs. You have to ensure that 'HAVE_PTHREAD_H' is defined by
1548 the configuration system and use the 'libnobugmt' library for linking. It is
1549 particular important that libraries using NoBug are compiled with
1550 'HAVE_PTHREAD_H' enabled when they are intended to be used in multithreaded
1551 programs.
1553 When Multithreading is used, log messages contain a identifier of the
1554 originating thread. This identifier should be set by
1556 [[THREAD_ID_SET]]
1557 .NOBUG_THREAD_ID_SET
1558  NOBUG_THREAD_ID_SET(name)
1560 `name`::
1561         New name for the thread
1563 Nobug will assemble a unique identifier by appending a underscore and a
1564 number to name, for example `NOBUG_THREAD_ID_SET("gui")` will result in a
1565 identifier like "gui_5". When you don't set a thread identifier, then NoBug
1566 assigns one automatically with the name 'thread' preprended if needed. Thread
1567 identifiers may be reset with a new call to this macro.
1569 [[THREAD_ID_GET]]
1570 .NOBUG_THREAD_ID_GET
1571  NOBUG_THREAD_ID_GET
1573 Will return a const char* of the thread id in multithreaded programs and
1574 a pointer to a literal empty string in singlethreaded programs.
1576 [[THREAD_DATA]]
1577 .NOBUG_THREAD_DATA
1578  NOBUG_THREAD_DATA
1580 Evaluates to a variable of type `void*` which can be used to store
1581 thread local information. This is useable for xref:callbacks which may
1582 prepare context information to be reused later.
1584 This macro is also available in singlethreaded programs, refering to a
1585 single global variable.
1587 Nobug initializes this variable to `NULL` and then touches it never again.
1590 // src/nobug_rbdump.c:31 //
1591 [[rbdump]]
1592 Dumping Persistent Ringbuffers
1593 ------------------------------
1595 NoBug installs the `nobug_rbdump` tool for dumping the content of a persistent
1596 ringbuffer. It is invoked with the filename of the ringbuffer, the content is then
1597 printed to stdout.
1599 // doc/testsuite.txt:1 //
1600 Testsuite
1601 ---------
1603  TODO Documentation to be written, use the source Luke!
1605 NoBug maintains a `test.sh` script which drives extensive testsuites.
1606 Look at into the 'tests/' folder about how to apply this.
1608 // doc/bestpractices.txt:1 //
1609 Best Practices
1610 --------------
1612 NOTE: this section is very work in progress
1614 .Workflow
1616  1. Development
1617       * Write a testsuite, build your program with -O0 -g -DEBUG_ALPHA and run
1618         the testsuite under valgrind control. Hack until the program mets the
1619         specifications defined by the testsuite.
1620  2. Beta Test
1621       * Build with desired optimization level and -g -DEBUG_BETA and give the
1622         program to your beta testers.
1623  3. Release
1624       * Build it with optimization and without -g -DEBUG_*
1626 .What and when to check
1628   * Add REQUIRE checks on your interfaces (incoming parameters). Especially if
1629     a argument might not cover the whole range of the underlying type.
1630   * Don't waste your and your CPU's time with unnecessary checks. The testsuite
1631     should validate your program. NoBug aids in debugging. You can add
1632     Postconditions (ENSURE) and Invariants when you have a bug somewhere and
1633     want to nail it down.
1634   * Added checks don't need to be removed.
1635   * When you use the CHECKED/UNCHECKED features then don't forget C scoping
1636     rules, tag things as CHECKED from the leaves to the root.
1638 .Tips & Tricks
1640   * TRACE(flagname) or TRACE_DBG(flagname) at the begin of every nontrivial
1641     function will easily log the progress of your application.
1642   * Trying a RELEASE build will abort on certain conditions (known BUG, TODO's,
1643     UNCHECKED code), you can use this to find these spots.
1645 Appendix
1646 --------
1648 // src/nobug_resources.c:306 //
1649 [[deadlock_detection]]
1650 The Resource Tracking Algorithm
1651 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1653 Each resource registers a global 'resource_record'.
1655 Every new locking path discovered is stored as 'resource_node' structures which refer to the associated
1656 'resource_record'.
1658 Threads keep a trail of 'resource_user' strcutures for each resource entered. This 'resource_user' struct
1659 refer to the 'resource_nodes' and thus indirectly to the associated 'resource_record'.
1661 The deadlock checker uses this information to test if the acqusition of a new resource would yield a
1662 potential deadlock.
1664 [[nobug_resource_enter]]
1665 Entering Resources
1666 ^^^^^^^^^^^^^^^^^^
1668 In multithreaded programs, whenever a thread wants to wait for a 'resource_record'
1669 the deadlock checker jumps in.
1671 The deadlock checking algorithm is anticipatory as it will find and abort on conditions which may lead
1672 to a potential deadlock by violating the locking order learned earlier.
1674 Each thread holds a stack (list) of each 'resource_user' it created. Leaving
1675 a resource will remove it from this stacklist.
1677 Each 'resource_record' stores the trail which other 'resource_records' are already entered. This relations
1678 are implemented with the 'resource_node' helper structure.
1680 ////
1681 TODO: insert diagram here
1682   2-3
1684   3-4-2
1686 1-3-2-4
1688 3-4-2
1690 1-4-2
1692 ////
1694 First we find out if there is already a node from the to be acquired resource back to
1695 the topmost node of the current threads user stack.
1697 [source,c]
1698 ---------------------------------------------------------------------
1699   struct nobug_resource_user* user = NULL;
1700   struct nobug_resource_node* node = NULL;
1702   if (!llist_is_empty (&tls->res_stack))
1703     {
1704       user = LLIST_TO_STRUCTP (llist_tail (&tls->res_stack),
1705                                struct nobug_resource_user,
1706                                res_stack);
1708       struct nobug_resource_node templ =
1709         {
1710          ...
1711           user->current->resource,
1712          ...
1713         };
1715       node = (struct nobug_resource_node*)
1716         llist_ufind (&resource->nodes,
1717                      &templ.node,
1718                      nobug_resource_node_resource_cmpfn,
1719                      NULL);
1720     }
1721   ...
1722 ---------------------------------------------------------------------
1724 Deadlock checking is only done when the node is entered in `WAITING` state and only
1725 available in multithreaded programs.
1727 [source,c]
1728 ---------------------------------------------------------------------
1729   if (state == NOBUG_RESOURCE_WAITING)
1730     {
1731 #if NOBUG_USE_PTHREAD
1732       ...
1733 ---------------------------------------------------------------------
1735 If node was found above, then this locking path is already validated and no deadlock can happen,
1736 else, if this stack already holds a resource (user is set) we have to go on with checking.
1738 [source,c]
1739 ---------------------------------------------------------------------
1740       if (!node && user)
1741         {
1742           ...
1743 ---------------------------------------------------------------------
1745 If not then its checked that the resource to be entered is not on any parent trail of the current topmost resource,
1746 if it is then this could be a deadlock which needs to be further investigated.
1748 [source,c]
1749 ---------------------------------------------------------------------
1750           LLIST_FOREACH (&user->current->resource->nodes, n)
1751             {
1752               for (struct nobug_resource_node* itr =
1753                      ((struct nobug_resource_node*)n)->parent;
1754                    itr;
1755                    itr = itr->parent)
1756                 {
1757                   if (itr->resource == resource)
1758                     {
1759                       ...
1760 ---------------------------------------------------------------------
1762 if the resource was on the trail, we search if there is a common ancestor before the resource
1763 on the trail and the threads current chain,
1764 if yes then this ancestor protects against deadlocks and we can continue.
1766 [source,c]
1767 ---------------------------------------------------------------------
1768                       for (struct nobug_resource_node* itr2 = itr->parent;
1769                            itr2;
1770                            itr2 = itr2->parent)
1771                         {
1772                           LLIST_FOREACH_REV (&tls->res_stack, p)
1773                             {
1774                               struct nobug_resource_user* user =
1775                                 LLIST_TO_STRUCTP (p,
1776                                                   struct nobug_resource_user,
1777                                                   res_stack);
1778                               if (user->current->resource == itr2->resource)
1779                                 goto done;
1780                             }
1781 ---------------------------------------------------------------------
1783 If no ancestor found, we finally abort with a potential deadlock condition.
1785 [source,c]
1786 ---------------------------------------------------------------------
1787                           nobug_resource_error = "possible deadlock detected";
1788                           return NULL;
1789                           ...
1790 ---------------------------------------------------------------------
1793 [[nobug_resource_leave]]
1794 Leaving Resources
1795 ^^^^^^^^^^^^^^^^^
1797 store the tail and next aside, we need it later
1799 [source,c]
1800 ---------------------------------------------------------------------
1801 #if NOBUG_USE_PTHREAD
1802       struct nobug_resource_user* tail =
1803         LLIST_TO_STRUCTP (llist_tail (&user->thread->res_stack),
1804                           struct nobug_resource_user,
1805                           res_stack);
1806       struct nobug_resource_user* next =
1807         LLIST_TO_STRUCTP (llist_next (&user->res_stack),
1808                           struct nobug_resource_user,
1809                           res_stack);
1810 ---------------------------------------------------------------------
1812 remove user struct from thread stack
1813 The res_stack is now like it is supposed to look like with the 'user' removed.
1814 We now need to fix the node tree up to match this list.
1816 [source,c]
1817 ---------------------------------------------------------------------
1818       llist_unlink_fast_ (&user->res_stack);
1819 ---------------------------------------------------------------------
1821 When the the user node was not the tail or only node of the thread stack, we have to check
1822 (and possibly construct) a new node chain for it. No valdation of this chain needs to be done,
1823 since it was already validated when entering the resources first.
1825 [source,c]
1826 ---------------------------------------------------------------------
1827       if (user != tail && !llist_is_empty (&user->thread->res_stack))
1828         {
1829           struct nobug_resource_user* parent = NULL;
1830           if (llist_head (&user->thread->res_stack) != &next->res_stack)
1831             {
1832               parent =
1833                 LLIST_TO_STRUCTP (llist_prev (&next->res_stack),
1834                                   struct nobug_resource_user,
1835                                   res_stack);
1836             }
1837 ---------------------------------------------------------------------
1839 iterate over all users following the removed node, finding nodes pointing to this users or
1840 create new nodes.
1842 [source,c]
1843 ---------------------------------------------------------------------
1844           LLIST_FORRANGE (&next->res_stack, &user->thread->res_stack, n)
1845             {
1846               struct nobug_resource_user* cur =
1847                 LLIST_TO_STRUCTP (n,
1848                                   struct nobug_resource_user,
1849                                   res_stack);
1851 ---------------------------------------------------------------------
1852 // src/nobug_resources.c:652 //
1854 find the node pointing back to parent, create a new one if not found, rinse repeat
1856 [source,c]
1857 ---------------------------------------------------------------------
1858               struct nobug_resource_node templ =
1859                 {
1860                   ...
1861                   NULL,
1862                   ...
1863                 };
1865               struct nobug_resource_node* node = (struct nobug_resource_node*)
1866                 llist_ufind (&resource->nodes,
1867                              &templ.node,
1868                              nobug_resource_node_parent_cmpfn,
1869                              NULL);
1871               if (!node)
1872                 {
1873                   node = nobug_resource_node_new (resource, parent->current);
1874                   if (!node)
1875                     {
1876                       nobug_resource_error = "internal allocation error";
1877                       return 0;
1878                     }
1879                 }
1881               parent = cur;
1882             }
1883         }
1884 ---------------------------------------------------------------------
1887 Index
1888 -----
1890 xref:ABORT[Aborting]:: abort the program
1891 xref:abort_callback[callback for aborting]:: hook to handle a termination
1892 xref:abort_cb[type of abort callback]:: type of a abort callback function
1893 xref:ALERT[ALERT]:: about to die
1894 xref:ASSERT[ASSERT]:: generic assertion
1895 xref:assert[assert]:: C standard assertion
1896 xref:BACKTRACE[Backtraces]:: generate a backtrace
1897 xref:buildlevel[Build Levels]:: selecting the build level
1898 xref:callback_data[passing data to callbacks]:: data to be passed to callbacks
1899 xref:CHECK[CHECK]:: unnconditional assertion for testsuites
1900 xref:CHECKED[CHECKED, Scope]:: tag scope as reviewed
1901 xref:CRITICAL[CRITICAL]:: can not continue
1902 xref:deadlock_detection[The Resource Tracking Algorithm]:: how resources are tracked
1903 xref:DECLARE_FLAG[DECLARE_FLAG]:: declaring a flag
1904 xref:DECLARE_ONLY[DECLARE_ONLY]:: force flag declarations only
1905 xref:DEFINE_FLAG[DEFINE_FLAG]:: defining a flag
1906 xref:DEFINE_FLAG_LIMIT[DEFINE_FLAG_LIMIT]:: defining a flag w/ log limit
1907 xref:DEFINE_FLAG_PARENT[DEFINE_FLAG_PARENT]:: defining a flag hierarchy
1908 xref:DEFINE_FLAG_PARENT_LIMIT[DEFINE_FLAG_PARENT_LIMIT]:: defining a flag hierarchy, w/ log limit
1909 xref:DEPRECATED[DEPRECATED]:: to be discarded in future
1910 xref:DUMP[DUMP]:: dumping datastructures
1911 xref:DUMP_LOG[DUMP_LOG]:: logging helper for dumping
1912 xref:ECHO[ECHO]:: unconditional logging for tests
1913 xref:ELSE_NOTREACHED[ELSE_NOTREACHED]:: alternative never taken
1914 xref:ENSURE[ENSURE]:: postconditions (computation outcomes)
1915 xref:ERROR[ERROR]:: something gone wrong
1916 xref:FIXME[FIXME]:: known bug
1917 xref:INFO[INFO]:: progress message
1918 xref:INJECT_FAULT[INJECT_FAULT]:: fault injection statement
1919 xref:INJECT_GOODBAD[INJECT_GOODBAD]:: fault injection expression
1920 xref:INJECT_LEVEL[INJECT_LEVEL]:: log level for fault injection
1921 xref:INVARIANT[INVARIANT]:: validate invariant state
1922 xref:LOG[LOG]:: generic logging
1923 xref:LOG_BASELIMIT[LOG_BASELIMIT]:: minimum compliled-in logging limit
1924 xref:logflags[Log Flags]:: define hierarchies for logging output
1925 xref:logging_callback[callback when logging]:: hook when something get logged
1926 xref:logging_cb[type of logging callbacks]:: type of a logging callback function
1927 xref:multithreading[Multithreading]:: using NoBug in multithreaded programs
1928 xref:NOBUG_ALPHA_COMMA[NOBUG_ALPHA_COMMA]:: append something after a comma in *ALPHA* builds
1929 xref:NOBUG_ANN[NOBUG_ANN]:: log flag for annotations
1930 xref:NOBUG_ENV[Control what gets logged]:: environment variable for loging control
1931 xref:nobug_flag[nobug (flag)]:: log flag used to show nobug actions
1932 xref:NOBUG_FLAG_RAW[NOBUG_FLAG_RAW]:: pass direct flag pointer
1933 xref:NOBUG_IF[NOBUG_IF_*]:: include code conditionally on build level
1934 xref:NOBUG_ON[NOBUG_ON]:: log flag which is always enabled
1935 xref:nobug_resource_enter[Entering Resources]:: deadlock check on enter
1936 xref:nobug_resource_leave[Leaving Resources]:: fix resource lists
1937 xref:NOTICE[NOTICE]:: detailed progress message
1938 xref:NOTREACHED[NOTREACHED]:: code path never taken
1939 xref:PLANNED[PLANNED]:: ideas for future
1940 xref:postlogging_callback[callback after logging]:: hook after something get logged
1941 xref:rbdump[Dumping Persistent Ringbuffers]:: dumping persistent ringbuffers
1942 xref:REQUIRE[REQUIRE]:: preconditions (input parameters)
1943 xref:RESOURCE_ANNOUNCE[RESOURCE_ANNOUNCE]:: publish new resources
1944 xref:RESOURCE_DUMP[RESOURCE_DUMP]:: dump the state of a single resource
1945 xref:RESOURCE_DUMPALL[RESOURCE_DUMPALL]:: dump the state of all resources
1946 xref:RESOURCE_ENTER[RESOURCE_ENTER]:: claim a resource
1947 xref:RESOURCE_FORGET[RESOURCE_FORGET]:: remove resources
1948 xref:RESOURCE_HANDLE[RESOURCE_HANDLE]:: define an resource handle
1949 xref:RESOURCE_LEAVE[RESOURCE_LEAVE]:: relinquish a claimed resource
1950 xref:RESOURCE_LIST[RESOURCE_LIST]:: enumerate all registered resources
1951 xref:RESOURCE_LOG_LEVEL[RESOURCE_LOG_LEVEL]:: select the log level for resource logging
1952 xref:RESOURCE_LOGGING[RESOURCE_LOGGING]:: switch resource logging on and off
1953 xref:RESOURCE_STATE[RESOURCE_STATE]:: change the state of a resource
1954 xref:THREAD_DATA[NOBUG_THREAD_DATA]:: thread local data for application use
1955 xref:THREAD_ID_GET[NOBUG_THREAD_ID_GET]:: query thread id
1956 xref:THREAD_ID_SET[NOBUG_THREAD_ID_SET]:: set or reset thread id
1957 xref:TODO[TODO]:: things to be done
1958 xref:TRACE[TRACE]:: debugging level message
1959 xref:UNCHECKED[UNCHECKED, Scope]:: tag scope as unreviewed
1960 xref:UNIMPLEMENTED[UNIMPLEMENTED]:: not yet implemented
1961 xref:WARN[WARN]:: unexpected fixable error
1963 // doc/license.txt:1 //
1964 License
1965 -------
1967     NoBug
1968     Copyright (C) 2009          Christian Thäter <ct@pipapo.org>
1970     This program is free software; you can redistribute it and/or modify
1971     it under the terms of the GNU General Public License as published by
1972     the Free Software Foundation; either version 2 of the License, or
1973     (at your option) any later version.
1975     This program is distributed in the hope that it will be useful,
1976     but WITHOUT ANY WARRANTY; without even the implied warranty of
1977     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1978     GNU General Public License for more details.
1980     You should have received a copy of the GNU General Public License along
1981     with this program; if not, write to the Free Software Foundation, Inc.,
1982     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1984 .License Rationale
1986 NoBug is released under the "GNU General Public License version 2 or
1987 any later" to protect its freedom. If one wants to use NoBug in a
1988 propietary program, please contact the main author for
1989 acknowledging relicensing terms.
1991 For BSD license style Free Software, this means you can not distribute
1992 binaries linking NoBug without making its source available. To make
1993 this compatible, it is suggested that you dual-license your software
1994 with your prefered BSD like license and the GPL. As long as it uses
1995 NoBug, the GPL will take over and you have to make the source
1996 available, while one can ship a BSD or LGPL Licensed headerfile which
1997 defines all NoBug macros as empty macros and remove libnobug from the
1998 linking, then NoBug isn't used anymore and you may apply BSD license
1999 terms for resulting binaries.
2002 Contributor Agreement
2003 ~~~~~~~~~~~~~~~~~~~~~
2005 Improvements and patches must be licensed as "GPL v2 or any later" to
2006 be acceptable. Further a contributor must either assign his copyright
2007 to the main NoBug author or agree with the possibility that NoBug can
2008 be relicensed for propietary use:
2010  Independent of the GPL license as stated above, The main author of
2011  NoBug explicitly reserve the right to relicense NoBug under
2012  different, even propietary terms. Any contributor agrees to such
2013  a possiblility by sending his contribution to be included into
2014  the official releases.
2016  This agreement is bilateral, every contributor who worked on a
2017  substantial part of NoBug has the right to relicense it after
2018  negotiation with the NoBug main author. Exact terms of such
2019  relicensing are worked out on a per case base.
2021 The intention is that anyone who worked on NoBug should be able to
2022 benefit from his work. This means one should be able to use it at his
2023 workplace, to gain a job or as well as relicense it for a customer.
2024 Unlike other projects which simply ask for transfering the copyright
2025 to the main author, NoBug tries to make it possible to retain the
2026 copyright by anyone who helped the project.
2028 This additional agreement has no impact on the GPL, it's sole purpose
2029 is to define relicensing policies between the NoBug main author and
2030 contributors. When you recieve NoBug it will be licensed under
2031 GPL unless you personally acknowledged other terms with the NoBug main
2032 author (or any other main contributor).
2034 If anyone feels he is not credited in the 'AUTHORS' file or in any
2035 copyright notice, please contact the main author for inclusion.