1 // Render with Asciidoctor
3 = Babeltrace contributor's guide
4 Jérémie Galarneau, Philippe Proulx
10 This is a partial contributor's guide for the
11 http://diamon.org/babeltrace[Babeltrace] project. If you have any
12 questions that are not answered by this guide, please post them on
13 https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev[Babeltrace's
19 === Object reference counting and lifetime
21 This section covers the rationale behind the design of Babeltrace's
22 object lifetime management. This applies to the Babeltrace library, as
23 well as to the CTF writer library (although the public reference
24 counting functions are not named the same way).
26 Starting from Babeltrace 2.0, all publicly exposed objects inherit a
27 common base: `bt_object`. This base provides a number of facilities to
28 all objects, chief amongst which are lifetime management functions.
30 The lifetime of some public objects is managed by reference counting. In
31 this case, the API offers the `+bt_*_get_ref()+` and `+bt_*_put_ref()+`
32 functions which respectively increment and decrement an object's
35 As far as lifetime management in concerned, Babeltrace makes a clear
36 distinction between regular objects, which have a single parent, and
37 root objects, which don't.
42 Let us consider a problematic case to illustrate the need for this
45 A user of the Babeltrace library creates a trace class, which _has_ a
46 stream class (the class of a stream) and that stream class, in turn,
47 _has_ an event class (the class of an event).
49 Nothing prevents this user from releasing his reference on any one of
50 these objects in any order. However, all objects in the
51 __trace--stream class--event class__ hierarchy can be retrieved
54 For instance, the user could discard his reference on both the event
55 class and the stream class, only keeping a reference on the trace class.
56 From this trace class reference, stream classes can be enumerated,
57 providing the user with a new reference to the stream class he discarded
58 earlier. Event classes can also be enumerated from stream classes,
59 providing the user with references to the individual event classes.
61 Conversely, the user could also hold a reference to an event class and
62 retrieve its parent stream class. The trace class, in turn, can then be
63 retrieved from the stream class.
65 This example illustrates what could be interpreted as a circular
66 reference dependency existing between these objects. Of course, if the
67 objects in such a scenario were to hold references to each other (in
68 both directions), we would be in presence of a circular ownership
69 resulting in a leak of both objects as their reference counts would
72 Nonetheless, the API must offer the guarantee that holding a node to any
73 node of the graph keeps all other reachable nodes alive.
78 The scheme employed in Babeltrace to break this cycle consists in the
79 "children" holding _reverse component references_ to their parents. That
80 is, in the context of the trace IR, that event classes hold a reference
81 to their parent stream class and stream classes hold a reference to
82 their parent trace class.
84 On the other hand, parents hold _claiming aggregation references_ to
85 their children. A claiming aggregation reference means that the object
86 being referenced should not be deleted as long as the reference still
87 exists. In this respect, it can be said that parents truly hold the
88 ownership of their children, since they control their lifetime.
89 Conversely, the reference counting mechanism is leveraged by children to
90 notify parents that no other child indirectly exposes the parent.
92 When a parented object's reference count reaches zero, it invokes
93 `+bt_*_put_ref()+` on its parent and does _not_ free itself. However,
94 from that point, the object depends on its parent to signal the moment
95 when it can be safely reclaimed.
97 The invocation of `+bt_*_put_ref()+` by the last children holding a
98 reference to its parent might trigger a cascade of `+bt_*_put_ref()+`
99 from child to parent. Eventually, a **root** object is reached. At that
100 point, if this orphaned object's reference count reaches zero, the
101 object invokes the destructor method defined by everyone of its children
102 as part of their base `struct bt_object`. The key point here is that the
103 cascade of destructor will necessarily originate from the root and
104 propagate in preorder to the children. These children will propagate the
105 destruction to their own children before reclaiming their own memory.
106 This ensures that a node's pointer to its parent is _always_ valid since
107 the parent has the responsibility of tearing-down their children before
108 cleaning themselves up.
110 Assuming a reference to an object is _acquired_ by calling
111 `+bt_*_get_ref()+` while its reference count is zero, the object
112 acquires, in turn, a reference on its parent using `+bt_*_get_ref()+`.
113 At that point, the child can be thought of as having converted its weak
114 reference to its parent into a regular reference. That is why this
115 reference is referred to as a _claiming_ aggregation reference.
120 This scheme imposes a number of strict rules defining the relation
123 * Objects may only have one parent.
124 * Objects, beside the root, are only retrievable from their direct
130 The initial situation is rather simple: **User{nbsp}A** is holding a
131 reference to a trace class, **TC1**. As per the rules previously
132 enounced, stream classes **SC1** and **SC2** don't hold a reference to
133 **TC1** since their own reference counts are zero. The same holds true
134 for **EC1**, **EC2** and **EC3** with respect to **SC1** and **SC2**.
136 image::doc/contributing-images/bt-ref01.png[]
138 In this second step, we can see that **User{nbsp}A** has acquired a
139 reference on **SC2** through the trace class, **TC1**.
141 The stream class's reference count transitions from zero to one,
142 triggering the acquisition of a strong reference on **TC1** from
145 Hence, at this point, the trace class's ownership is shared by
146 **User{nbsp}A** and **SC2**.
148 image::doc/contributing-images/bt-ref02.png[]
150 Next, **User{nbsp}A** acquires a reference on the **EC3** event class
151 through its parent stream class, **SC2**. Again, the transition of an
152 object's reference count from 0 to 1 triggers the acquisition of a
153 reference on its parent.
155 Note that SC2's reference count was incremented to 2. The trace class's
156 reference count remains unchanged.
158 image::doc/contributing-images/bt-ref03.png[]
160 **User{nbsp}A** decides to drop its reference on **SC2**. **SC2**'s
161 reference count returns back to 1, everything else remaining unchanged.
163 image::doc/contributing-images/bt-ref04.png[]
165 **User{nbsp}A** can then decide to drop its reference on the trace
166 class. This results in a reversal of the initial situation:
167 **User{nbsp}A** now owns an event, **EC3**, which is keeping everything
168 else alive and reachable.
170 image::doc/contributing-images/bt-ref05.png[]
172 If another object, **User{nbsp}B**, enters the picture and acquires a
173 reference on the **SC1** stream class, we see that **SC1**'s reference
174 count transitioned from 0 to 1, triggering the acquisition of a
175 reference on **TC1**.
177 image::doc/contributing-images/bt-ref06.png[]
179 **User{nbsp}B** hands off a reference to **EC1**, acquired through
180 **SC1**, to another object, **User{nbsp}C**. The acquisition of a
181 reference on **EC1**, which transitions from 0 to 1, triggers the
182 acquisition of a reference on its parent, **SC1**.
184 image::doc/contributing-images/bt-ref07.png[]
186 At some point, **User{nbsp}A** releases its reference on **EC3**. Since
187 **EC3**'s reference count transitions to zero, it releases its reference
188 on **SC2**. **SC2**'s reference count, in turn, reaches zero and it
189 releases its reference to **TC1**.
191 **TC1**'s reference count is now 1 and no further action is taken.
193 image::doc/contributing-images/bt-ref08.png[]
195 **User{nbsp}B** releases its reference on **SC1**. **User{nbsp}C**
196 becomes the sole owner of the whole hierarchy through his ownership of
199 image::doc/contributing-images/bt-ref09.png[]
201 Finally, **User{nbsp}C** releases his ownership of **EC1**, triggering
202 the release of the whole hierarchy. Let's walk through the reclamation
205 Mirroring what happened when **User{nbsp}A** released its last reference
206 on **EC3**, the release of **EC1** by **User{nbsp}C** causes its
207 reference count to fall to zero.
209 This transition to zero causes **EC1** to release its reference on
210 **SC1**. **SC1**'s reference count reaching zero causes it to release
211 its reference on **TC1**.
213 image::doc/contributing-images/bt-ref10.png[]
215 Since the reference count of **TC1**, a root object, has reached zero,
216 it invokes the destructor method on its children. This method is
217 recursive and causes the stream classes to call the destructor method on
220 The event classes are reached and, having no children of their own, are
223 image::doc/contributing-images/bt-ref11.png[]
225 The stream classes having destroyed their children, are then reclaimed
228 image::doc/contributing-images/bt-ref12.png[]
230 Finally, the stream classes having been reclaimed, **TC1** is reclaimed.
232 image::doc/contributing-images/bt-ref13.png[]
237 Logging is a great instrument for a developer to be able to collect
238 information about a running software.
240 Babeltrace is a complex software with many layers. When a Babeltrace
241 graph fails to run, what caused the failure? It could be caused by any
242 component, any message iterator, and any deeply nested validation of a
243 CTF IR object (within the `ctf` plugin), for example. With the
244 appropriate logging statements manually placed in the source code, we
245 can find the cause of a bug faster.
247 While <<choose-a-log-level,care must be taken>> when placing _DEBUG_ to
248 _FATAL_ logging statements, you should liberally instrument your
249 Babeltrace module with _TRACE_ logging statements to help future you
250 and other developers understand what's happening at run time.
255 The Babeltrace logging API is internal: it is not exposed to the users
256 of the library; only to their developers. The only thing that a library
257 user can control is the current log level of the library itself with
258 `bt_logging_set_global_level()` and the initial library's log level with
259 the `LIBBABELTRACE2_INIT_LOG_LEVEL` environment variable.
261 This API is based on https://github.com/wonder-mice/zf_log[zf_log], a
262 lightweight, yet featureful, MIT-licensed core logging library for C and
263 pass:[C++]. The zf_log source files were modified to have the `BT_` and
264 `bt_` prefixes, and other small changes, like color support and using
265 the project's `BT_DEBUG_MODE` definition instead of the standard
268 The logging functions are implemented in the logging convenience
269 library (`src/logging` directory).
275 The logging API headers are:
277 `<babeltrace2/logging.h>`::
278 Public header which a library user can use to set and get
279 libbabeltrace2's current log level.
282 Internal, generic logging API which you can use in any Babeltrace
283 module. This is the translation of `zf_log.h`.
285 This header offers the <<gen-logging-statements,generic logging
289 Specific internal header to use within the library.
291 This header defines `BT_LOG_OUTPUT_LEVEL` to a custom, library-wide
292 hidden symbol which is the library's current log level before including
295 This header offers the <<lib-logging-statements,library-specific logging
298 `"plugins/comp-logging.h"`::
299 Specific internal header to use within a component class.
301 This header offers the <<comp-logging-statements,component-specific
302 logging statement macros>>.
308 The internal logging API offers the following log levels, in ascending
311 [options="header,autowidth",cols="4"]
314 |Log level short name
315 |Internal API enumerator
316 |Public API enumerator
321 |`BT_LOGGING_LEVEL_TRACE`
326 |`BT_LOGGING_LEVEL_DEBUG`
331 |`BT_LOGGING_LEVEL_INFO`
336 |`BT_LOGGING_LEVEL_WARNING`
341 |`BT_LOGGING_LEVEL_ERROR`
346 |`BT_LOGGING_LEVEL_FATAL`
351 |`BT_LOGGING_LEVEL_NONE`
354 The short name is accepted by the log level environment variables and by
355 the CLI's `--log-level` options.
357 See <<choose-a-log-level,how to decide which one to use>> below.
359 There are two important log level expressions:
361 [[build-time-log-level]]Build-time, minimal log level::
362 The minimal log level, or build-time log level, is set at build time
363 and determines the minimal log level of the logging statements which
364 can be executed. This applies to all the modules (CLI, library,
365 plugins, bindings, etc.).
367 All the logging statements with a level below this level are **not built
368 at all**. All the logging statements with a level equal to or greater
369 than this level _can_ be executed, depending on the
370 <<run-time-log-level,run-time log level>>.
372 You can set this level at configuration time with the
373 `BABELTRACE_MINIMAL_LOG_LEVEL` environment variable, for example:
377 $ BABELTRACE_MINIMAL_LOG_LEVEL=INFO ./configure
381 The default build-time log level is `DEBUG`. For optimal performance,
382 set it to `INFO`, which effectively disables all fast path logging in
383 all the Babeltrace modules. You can't set it to `WARNING`, `ERROR`,
384 `FATAL`, or `NONE` because the impact on performance is minuscule
385 starting from the _INFO_ log level anyway and we want any Babeltrace
386 build to always be able to print _INFO_-level logs.
388 The library's public API provides `bt_logging_get_minimal_level()` to
389 get the configured minimal log level.
391 [[run-time-log-level]]Run-time, dynamic log level::
392 The dynamic log level is set at run time and determines the current,
393 _active_ log level. All the logging statements with a level below
394 this level are not executed, **but they still evaluate the
395 condition**. All the logging statements with a level equal to or
396 greater than this level are executed, provided that their level is
397 also <<build-time-log-level,enabled at build time>>.
399 `zf_log` has a concept of a global run-time log level which uses the
400 `_bt_log_global_output_lvl` symbol. In practice, we never use this
401 symbol, and always make sure that `BT_LOG_OUTPUT_LEVEL` is defined to a
402 module-wise expression before including `"logging/log.h"`.
404 In the library, `"lib/logging.h"` defines its own
405 `BT_LOG_OUTPUT_LEVEL` to the library's log level symbol before it
406 includes `"logging/log.h"` itself.
408 In libbabeltrace2, the user can set the current run-time log level with
409 the `bt_logging_set_global_level()` function, for example:
414 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO);
418 The library's initial run-time log level is defined by the
419 `LIBBABELTRACE2_INIT_LOG_LEVEL` environment variable, or set to _NONE_
420 if this environment variable is undefined.
422 Other modules have their own way of setting their run-time log level.
424 For example, the CLI uses the `BABELTRACE_CLI_LOG_LEVEL` environment
425 variable, as well as its global `--log-level` option:
428 $ babeltrace2 --log-level=I ...
431 The components use their own log level (as returned by
432 `bt_component_get_logging_level()`). With the CLI, you can set a
433 specific component's log level with its own, position-dependent
434 `--log-level` option:
437 $ babeltrace2 /path/to/trace -c sink.ctf.fs --log-level=D
440 Code which is common to the whole project, for example `src/common`
441 and `src/compat`, use function parameters to get its run-time log
447 char *bt_common_get_home_plugin_path(int log_level);
450 Typically, when a logging-enabled module calls such a function, it
451 passes its own log level expression directly (`BT_LOG_OUTPUT_LEVEL`):
455 path = bt_common_get_home_plugin_path(BT_LOG_OUTPUT_LEVEL);
458 Otherwise, just pass `BT_LOG_NONE`:
461 path = bt_common_get_home_plugin_path(BT_LOG_NONE);
465 [[gen-logging-statements]]
466 ==== Generic logging statement macros
468 The Babeltrace logging statement macros work just like `printf()`
469 (except the `+BT_LOG*_STR()+` ones) and contain their <<log-levels,log
470 level>> (short name) in their name.
472 Each of the following macros evaluate the
473 <<build-time-log-level,build-time log level>> definition and
474 <<run-time-log-level,run-time log level>> expression (as defined by
475 `BT_LOG_OUTPUT_LEVEL`) to log conditionally.
477 See <<logging-instrument-c-file-gen,Instrument a C source file
478 (generic)>> and <<logging-instrument-h-file-gen,Instrument a C header
479 file (generic)>> to learn how to be able to use the following macros.
481 `+BT_LOGT("format string", ...)+`::
482 Generic trace logging statement.
484 `+BT_LOGD("format string", ...)+`::
485 Generic debug logging statement.
487 `+BT_LOGI("format string", ...)+`::
488 Generic info logging statement.
490 `+BT_LOGW("format string", ...)+`::
491 Generic warning logging statement.
493 `+BT_LOGE("format string", ...)+`::
494 Generic error logging statement.
496 `+BT_LOGF("format string", ...)+`::
497 Generic fatal logging statement.
499 `+BT_LOGT_STR("preformatted string")+`::
500 Generic preformatted string trace logging statement.
502 `+BT_LOGD_STR("preformatted string")+`::
503 Generic preformatted string debug logging statement.
505 `+BT_LOGI_STR("preformatted string")+`::
506 Generic preformatted string info logging statement.
508 `+BT_LOGW_STR("preformatted string")+`::
509 Generic preformatted string warning logging statement.
511 `+BT_LOGE_STR("preformatted string")+`::
512 Generic preformatted string error logging statement.
514 `+BT_LOGF_STR("preformatted string")+`::
515 Generic preformatted string fatal logging statement.
517 `+BT_LOGT_MEM(data_ptr, data_size, "format string", ...)+`::
518 Generic memory trace logging statement.
520 `+BT_LOGD_MEM(data_ptr, data_size, "format string", ...)+`::
521 Generic memory debug logging statement.
523 `+BT_LOGI_MEM(data_ptr, data_size, "format string", ...)+`::
524 Generic memory info logging statement.
526 `+BT_LOGW_MEM(data_ptr, data_size, "format string", ...)+`::
527 Generic memory warning logging statement.
529 `+BT_LOGE_MEM(data_ptr, data_size, "format string", ...)+`::
530 Generic memory error logging statement.
532 `+BT_LOGF_MEM(data_ptr, data_size, "format string", ...)+`::
533 Generic memory fatal logging statement.
535 `+BT_LOGT_ERRNO("initial message", "format string", ...)+`::
536 Generic `errno` string trace logging statement.
538 `+BT_LOGD_ERRNO("initial message", "format string", ...)+`::
539 Generic `errno` string debug logging statement.
541 `+BT_LOGI_ERRNO("initial message", "format string", ...)+`::
542 Generic `errno` string info logging statement.
544 `+BT_LOGW_ERRNO("initial message", "format string", ...)+`::
545 Generic `errno` string warning logging statement.
547 `+BT_LOGE_ERRNO("initial message", "format string", ...)+`::
548 Generic `errno` string error logging statement.
550 `+BT_LOGF_ERRNO("initial message", "format string", ...)+`::
551 Generic `errno` string fatal logging statement.
554 [[lib-logging-statements]]
555 ==== Library-specific logging statement macros
557 The Babeltrace library contains an internal logging API based on the
558 generic logging framework. You can use it to log known Babeltrace
559 objects without having to manually log each member.
561 See <<logging-instrument-c-file-lib,Instrument a library C source file>>
562 and <<logging-instrument-h-file-lib,Instrument a library C header file>> to
563 learn how to be able to use the following macros.
565 The library logging statement macros are named `+BT_LIB_LOG*()+` instead
568 `+BT_LIB_LOGT("format string", ...)+`::
569 Library trace logging statement.
571 `+BT_LIB_LOGD("format string", ...)+`::
572 Library debug logging statement.
574 `+BT_LIB_LOGI("format string", ...)+`::
575 Library info logging statement.
577 `+BT_LIB_LOGW("format string", ...)+`::
578 Library warning logging statement.
580 `+BT_LIB_LOGE("format string", ...)+`::
581 Library error logging statement.
583 `+BT_LIB_LOGF("format string", ...)+`::
584 Library fatal logging statement.
586 `+BT_LIB_LOGW_APPEND_CAUSE("format string", ...)+`::
587 Library warning logging statement, and unconditional error cause
590 `+BT_LIB_LOGE_APPEND_CAUSE("format string", ...)+`::
591 Library error logging statement, and unconditional error cause
594 `+BT_LIB_LOGF_APPEND_CAUSE("format string", ...)+`::
595 Library fatal logging statement, and unconditional error cause
598 The macros above accept the typical `printf()` conversion specifiers
599 with the following limitations:
601 * The `+*+` width specifier is not accepted.
602 * The `+*+` precision specifier is not accepted.
603 * The `j` and `t` length modifiers are not accepted.
604 * The `n` format specifier is not accepted.
605 * The format specifiers defined in `<inttypes.h>` are not accepted,
606 except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
609 The Babeltrace library custom conversion specifier is accepted. Its
610 syntax is either `%!u` to format a UUID (`bt_uuid` type), or:
612 . Introductory `%!` sequence.
614 . **Optional**: `[` followed by a custom prefix for the printed fields
615 of this specifier, followed by `]`. The standard form is to end this
616 prefix with `-` so that, for example, with the prefix `tc-`, the
617 complete field name becomes `tc-addr`.
619 . **Optional**: `pass:[+]` to print extended object members. This
620 depends on the provided format specifier.
622 . Format specifier (see below).
624 The available format specifiers are:
626 [options="header,autowidth",cols="3"]
633 |Trace IR field class
634 |`+struct bt_field_class *+`
638 |`+struct bt_field *+`
642 |`+struct bt_field_path *+`
645 |Trace IR event class
646 |`+struct bt_event_class *+`
650 |`+struct bt_event *+`
653 |Trace IR stream class.
654 |`+struct bt_stream_class *+`
658 |`+struct bt_stream *+`
662 |`+struct bt_packet *+`
665 |Trace IR trace class
666 |`+struct bt_trace_class *+`
670 |`+struct bt_trace *+`
673 |Trace IR clock class
674 |`+struct bt_clock_class *+`
677 |Trace IR clock snapshot
678 |`+struct bt_clock_snapshot *+`
682 |`+struct bt_value *+`
686 |`+struct bt_message *+`
690 |`struct bt_message_iterator *`
694 |`struct bt_component_class *`
698 |`+struct bt_component *+`
702 |`+struct bt_port *+`
706 |`+struct bt_connection *+`
710 |`+struct bt_graph *+`
714 |`const struct bt_plugin *`
718 |`const struct bt_error_cause *`
722 |`+struct bt_object_pool *+`
726 |`+struct bt_object *+`
729 Conversion specifier examples:
736 The ``, `` string (comma and space) is printed between individual
737 fields, but **not after the last one**. Therefore, you must put this
738 separator in the format string between two conversion specifiers, for
743 BT_LIB_LOGW("Message: count=%u, %!E, %!+K", count, event_class, clock_class);
746 Example with a custom prefix:
750 BT_LIB_LOGI("Some message: %![ec-a-]e, %![ec-b-]+e", ec_a, ec_b);
753 It is safe to pass `NULL` as any Babeltrace object parameter: the macros
754 only print its null address.
756 WARNING: Build-time `printf()` format checks are disabled for the
757 `+BT_LIB_LOG*()+` macros because there are custom conversion specifiers,
758 so make sure to test your logging statements.
761 [[comp-logging-statements]]
762 ==== Component-specific logging statement macros
764 There are available logging macros for components. They prepend a prefix
765 including the component's name to the logging message.
767 See <<logging-instrument-c-file-compcls,Instrument a component class C
768 source file>> and <<logging-instrument-h-file-compcls,Instrument a
769 component class C header file>> to learn how to be able to use the
772 The component logging statement macros are named `+BT_COMP_LOG*()+`
773 instead of `+BT_LOG*()+`:
775 `+BT_COMP_LOGT("format string", ...)+`::
776 Component trace logging statement.
778 `+BT_COMP_LOGD("format string", ...)+`::
779 Component debug logging statement.
781 `+BT_COMP_LOGI("format string", ...)+`::
782 Component info logging statement.
784 `+BT_COMP_LOGW("format string", ...)+`::
785 Component warning logging statement.
787 `+BT_COMP_LOGE("format string", ...)+`::
788 Component error logging statement.
790 `+BT_COMP_LOGF("format string", ...)+`::
791 Component fatal logging statement.
793 `+BT_COMP_LOGT_STR("preformatted string")+`::
794 Component preformatted string trace logging statement.
796 `+BT_COMP_LOGD_STR("preformatted string")+`::
797 Component preformatted string debug logging statement.
799 `+BT_COMP_LOGI_STR("preformatted string")+`::
800 Component preformatted string info logging statement.
802 `+BT_COMP_LOGW_STR("preformatted string")+`::
803 Component preformatted string warning logging statement.
805 `+BT_COMP_LOGE_STR("preformatted string")+`::
806 Component preformatted string error logging statement.
808 `+BT_COMP_LOGF_STR("preformatted string")+`::
809 Component preformatted string fatal logging statement.
811 `+BT_COMP_LOGT_ERRNO("initial message", "format string", ...)+`::
812 Component `errno` string trace logging statement.
814 `+BT_COMP_LOGD_ERRNO("initial message", "format string", ...)+`::
815 Component `errno` string debug logging statement.
817 `+BT_COMP_LOGI_ERRNO("initial message", "format string", ...)+`::
818 Component `errno` string info logging statement.
820 `+BT_COMP_LOGW_ERRNO("initial message", "format string", ...)+`::
821 Component `errno` string warning logging statement.
823 `+BT_COMP_LOGE_ERRNO("initial message", "format string", ...)+`::
824 Component `errno` string error logging statement.
826 `+BT_COMP_LOGF_ERRNO("initial message", "format string", ...)+`::
827 Component `errno` string fatal logging statement.
829 `+BT_COMP_LOGT_MEM(data_ptr, data_size, "format string", ...)+`::
830 Component memory trace logging statement.
832 `+BT_COMP_LOGD_MEM(data_ptr, data_size, "format string", ...)+`::
833 Component memory debug logging statement.
835 `+BT_COMP_LOGI_MEM(data_ptr, data_size, "format string", ...)+`::
836 Component memory info logging statement.
838 `+BT_COMP_LOGW_MEM(data_ptr, data_size, "format string", ...)+`::
839 Component memory warning logging statement.
841 `+BT_COMP_LOGE_MEM(data_ptr, data_size, "format string", ...)+`::
842 Component memory error logging statement.
844 `+BT_COMP_LOGF_MEM(data_ptr, data_size, "format string", ...)+`::
845 Component memory fatal logging statement.
848 ==== Conditional logging
850 `+BT_LOG_IF(cond, statement)+`::
851 Execute `statement` only if `cond` is true.
858 BT_LOG_IF(i < count / 2, BT_LOGD("Log this: i=%d", i));
862 To check the <<build-time-log-level,build-time log level>>:
866 #if BT_LOG_ENABLED_DEBUG
871 This tests if the _DEBUG_ level was enabled at build time. This means
872 that the current, <<run-time-log-level,run-time log level>> _could_ be
873 _DEBUG_, but it could also be higher. The rule of thumb is to use only
874 logging statements at the same level in a `BT_LOG_ENABLED_*` conditional
877 The available definitions for build-time conditions are:
879 * `BT_LOG_ENABLED_TRACE`
880 * `BT_LOG_ENABLED_DEBUG`
881 * `BT_LOG_ENABLED_INFO`
882 * `BT_LOG_ENABLED_WARNING`
883 * `BT_LOG_ENABLED_ERROR`
884 * `BT_LOG_ENABLED_FATAL`
886 To check the current, <<run-time-log-level,run-time log level>>:
890 if (BT_LOG_ON_DEBUG) {
895 This tests if the _DEBUG_ log level is dynamically turned on
896 (implies that it's also enabled at build time). This check could have a
897 noticeable impact on performance.
899 The available definitions for run-time conditions are:
904 * `BT_LOG_ON_WARNING`
908 Those macros check the module-specific log level symbol (defined by
909 `BT_LOG_OUTPUT_LEVEL`).
911 Never, ever write code which would be executed only to compute the
912 fields of a logging statement outside a conditional logging scope,
917 int number = get_number_of_event_classes_with_property_x(...);
918 BT_LOGD("Bla bla: number=%d", number);
925 if (BT_LOG_ON_DEBUG) {
926 int number = get_number_of_event_classes_with_property_x(...);
927 BT_LOGD("Bla bla: number=%d", number);
935 BT_LOGD("Bla bla: number=%d", get_number_of_event_classes_with_property_x(...));
941 [[logging-instrument-c-file-gen]]
942 ==== Instrument a C source file (generic)
944 To instrument a C source file (`.c`):
946 . At the top of the file, before the first `#include` line (if any),
947 define your file's <<choose-a-logging-tag,logging tag>> name:
952 #define BT_LOG_TAG "SUBSYS/MY-MODULE/MY-FILE"
956 . Below the line above, define the source file's log level expression,
957 `BT_LOG_OUTPUT_LEVEL`. This expression is evaluated for each
958 <<gen-logging-statements,logging statement>> to know the current
959 <<run-time-log-level,run-time log level>>.
965 /* Global log level variable */
966 #define BT_LOG_OUTPUT_LEVEL module_global_log_level
971 /* Local log level variable; must exist where you use BT_LOG*() */
972 #define BT_LOG_OUTPUT_LEVEL log_level
977 /* Object's log level; `obj` must exist where you use BT_LOG*() */
978 #define BT_LOG_OUTPUT_LEVEL (obj->log_level)
981 . Include `"logging/log.h"`:
985 #include "logging/log.h"
988 . In the file, instrument your code with the
989 <<gen-logging-statements,generic logging statement macros>>.
992 [[logging-instrument-h-file-gen]]
993 ==== Instrument a C header file (generic)
995 To instrument a C header file (`.h`), if you have `static inline`
998 . Do not include `"logging/log.h"`!
1002 .. In the file, instrument your code with the
1003 <<gen-logging-statements,generic logging statement macros>>, making
1004 each of them conditional to the existence of the macro you're using:
1009 int some_function(int x)
1027 The C source files which include this header file determine if logging
1028 is enabled or not for them, and if so, what is their
1029 <<choose-a-logging-tag,logging tag>> and <<run-time-log-level,run-time
1030 log level>> expression.
1032 .. Require that logging be enabled:
1036 /* Protection: this file uses BT_LOG*() macros directly */
1037 #ifndef BT_LOG_SUPPORTED
1038 # error Please include "logging/log.h" before including this file.
1042 Then, in the file, instrument your code with the
1043 <<gen-logging-statements,generic logging statement macros>>.
1046 [[logging-instrument-c-file-lib]]
1047 ==== Instrument a library C source file
1049 To instrument a library C source file (`.c`):
1051 . At the top of the file, before the first `#include` line (if any),
1052 define your file's <<choose-a-logging-tag,logging tag>> name (this
1053 tag must start with `LIB/`):
1058 #define BT_LOG_TAG "LIB/THE-FILE"
1062 . Include `"lib/logging.h"`:
1066 #include "lib/logging.h"
1069 . In the file, instrument your code with the
1070 <<lib-logging-statements,library logging statement macros>> or with
1071 the <<gen-logging-statements,generic logging statement macros>>.
1074 [[logging-instrument-h-file-lib]]
1075 ==== Instrument a library C header file
1077 To instrument a library C header file (`.h`), if you have `static
1078 inline` functions in it:
1080 . Do not include `"lib/logging.h"`!
1082 . Require that library logging be enabled:
1086 /* Protection: this file uses BT_LIB_LOG*() macros directly */
1087 #ifndef BT_LIB_LOG_SUPPORTED
1088 # error Please include "lib/logging.h" before including this file.
1092 . In the file, instrument your code with the
1093 <<lib-logging-statements,library logging statement macros>> or with
1094 the <<gen-logging-statements,generic logging statement macros>>.
1097 [[logging-instrument-c-file-compcls]]
1098 ==== Instrument a component class C source file
1100 To instrument a component class C source file (`.c`):
1102 . At the top of the file, before the first `#include` line (if any),
1103 define your file's <<choose-a-logging-tag,logging tag>> name (this tag
1104 must start with `PLUGIN/` followed by the component class identifier):
1109 #define BT_LOG_TAG "PLUGIN/SRC.MY-PLUGIN.MY-SRC"
1113 . Below the line above, define the source file's log level expression,
1114 `BT_LOG_OUTPUT_LEVEL`. This expression is evaluated for each
1115 <<comp-logging-statements,logging statement>> to know the current
1116 <<run-time-log-level,run-time log level>>.
1118 For a component class file, it is usually a member of a local component
1119 private structure variable:
1123 #define BT_LOG_OUTPUT_LEVEL (my_comp->log_level)
1126 . Below the line above, define `BT_COMP_LOG_SELF_COMP` to an expression
1127 which, evaluated in the context of the
1128 <<comp-logging-statements,logging statements>>, evaluates to the self
1129 component address (`+bt_self_component *+`) of the component.
1131 This is usually a member of a local component private structure
1136 #define BT_COMP_LOG_SELF_COMP (my_comp->self_comp)
1139 . Include `"plugins/comp-logging.h"`:
1143 #include "plugins/comp-logging.h"
1146 . In the component initialization method, make sure to set the
1147 component private structure's log level member to the initial
1148 component's log level:
1153 bt_logging_level log_level;
1158 bt_self_component_status my_comp_init(
1159 bt_self_component_source *self_comp_src,
1160 bt_value *params, void *init_method_data)
1162 struct my_comp *my_comp = g_new0(struct my_comp, 1);
1163 bt_self_component *self_comp =
1164 bt_self_component_source_as_self_component(self_comp_src);
1165 const bt_component *comp = bt_self_component_as_component(self_comp);
1168 my_comp->log_level = bt_component_get_logging_level(comp);
1174 . In the file, instrument your code with the
1175 <<comp-logging-statements,component logging statement macros>>.
1178 [[logging-instrument-h-file-compcls]]
1179 ==== Instrument a component class C header file
1181 To instrument a component class C header file (`.h`), if you have
1182 `static inline` functions in it:
1184 . Do not include `"plugins/comp-logging.h"`!
1186 . Require that component logging be enabled:
1190 /* Protection: this file uses BT_COMP_LOG*() macros directly */
1191 #ifndef BT_COMP_LOG_SUPPORTED
1192 # error Please include "plugins/comp-logging.h" before including this file.
1196 . In the file, instrument your code with the
1197 <<comp-logging-statements,component logging statement macros>>.
1200 [[choose-a-logging-tag]]
1201 ==== Choose a logging tag
1203 Each logging-enabled C source file must define `BT_LOG_TAG` to a logging
1204 tag. A logging tag is a namespace to identify the logging messages of
1205 this specific source file.
1207 In general, a logging tag name _must_ be only uppercase letters, digits,
1208 and the `-`, `.`, and `/` characters.
1210 Use `/` to show the subsystem to source file hierarchy.
1212 For the Babeltrace library, start with `LIB/`.
1214 For the CTF writer library, start with `CTF-WRITER/`.
1216 For component classes, use:
1219 `PLUGIN/__CCTYPE__.__PNAME__.__CCNAME__[/__FILE__]`
1224 Component class's type (`SRC`, `FLT`, or `SINK`).
1230 Component class's name.
1233 Additional information to specify the source file name or module.
1235 For plugins (files common to many component classes), use:
1238 `PLUGIN/__PNAME__[/__FILE__]`
1246 Additional information to specify the source file name or module.
1249 [[choose-a-log-level]]
1250 ==== Choose a log level
1252 Choosing the appropriate level for your logging statement is very
1255 [options="header,autowidth",cols="1,2,3a,4"]
1257 |Log level |Description |Use cases |Expected impact on performance
1261 The program, library, or plugin cannot continue to work in this
1262 condition: it must be terminated immediately.
1264 A _FATAL_-level logging statement should always be followed by
1267 * Unexpected return values from system calls.
1268 * Logic error in internal code, for example an unexpected value in a
1270 * Failed assertion (within `BT_ASSERT()`).
1271 * Unsatisfied library precondition (within `BT_ASSERT_PRE()` or
1272 `BT_ASSERT_PRE_DEV()`).
1273 * Unsatisfied library postcondition (within `BT_ASSERT_POST()` or
1274 `BT_ASSERT_POST_DEV()`).
1275 |Almost none: always enabled.
1279 An important error which is somewhat not fatal, that is, the program,
1280 library, or plugin can continue to work after this, but you judge that
1281 it should be reported to the user.
1283 Usually, the program cannot recover from such an error, but it can at
1286 * Memory allocation errors.
1287 * Wrong component initialization parameters.
1288 * Corrupted, unrecoverable trace data.
1289 * Failed to perform an operation which should work considering the
1290 implementation and the satisfied preconditions. For example, the
1291 failure to create an empty object (no parameters): most probably
1292 failed internally because of an allocation error.
1293 * Almost any error in terminal elements: CLI and plugins.
1294 |Almost none: always enabled.
1298 An error which still allows the execution to continue, but you judge
1299 that it should be reported to the user.
1301 _WARNING_-level logging statements are for any error or weird action
1302 that is directly or indirectly caused by the user, often through some
1303 bad input data. For example, not having enough memory is considered
1304 beyond the user's control, so we always log memory errors with an
1305 _ERROR_ level (not _FATAL_ because we usually don't abort in this
1308 * Missing data within something that is expected to have it, but there's
1310 * Invalid file, but recoverable/fixable.
1311 |Almost none: always enabled.
1315 Any useful information which a non-developer user would possibly
1318 Anything logged with this level must _not_ happen repetitively on the
1319 fast path, that is, nothing related to each message, for example. This
1320 level is used for sporadic and one-shot events.
1322 * CLI or component configuration report.
1323 * Successful plugin, component, or message iterator initialization.
1324 * In the library: anything related to plugins, graphs, component
1325 classes, components, message iterators, connections, and ports which
1326 is not on the fast path.
1327 * Successful connection to or disconnection from another system.
1328 * An _optional_ subsystem cannot be loaded.
1329 * An _optional_ field/datum cannot be found.
1331 Very little: always enabled.
1335 Something that only Babeltrace developers would be interested into,
1336 which can occur on the fast path, but not more often than once per
1339 The _DEBUG_ level is the default <<build-time-log-level,build-time log
1340 level>> as, since it's not _too_ verbose, the performance is similar to
1343 * Object construction and destruction.
1344 * Object recycling (except fields).
1345 * Object copying (except fields and values).
1346 * Object freezing (whatever the type, as freezing only occurs in
1348 * Object cancellation.
1349 * Calling user methods and logging the result.
1350 * Setting object properties (except fields and values).
1352 Noticeable, but not as much as the _TRACE_ level: could be executed
1353 in production if you're going to need a thorough log for support
1354 tickets without having to rebuild the project.
1358 Low-level debugging context information (anything that does not fit the
1359 other log levels). More appropriate for tracing in general.
1361 * Reference count change.
1362 * Fast path, low level state machine's state change.
1363 * Get or set an object's property.
1364 * Object comparison's intermediate results.
1365 |Huge: not executed in production.
1370 Make sure not to use a _WARNING_ (or higher) log level when the
1371 condition leading to the logging statement can occur under normal
1374 For example, a public function to get some object or
1375 property from an object by name or key that fails to find the value is
1376 not a warning scenario: the user could legitimately use this function to
1377 check if the name/key exists in the object. In this case, use the
1378 _TRACE_ level (or do not log at all).
1383 ==== Write an appropriate message
1385 Follow those rules when you write a logging statement's message:
1387 * Use an English sentence which starts with a capital letter.
1389 * Start the sentence with the appropriate verb tense depending on the
1390 context. For example:
1393 ** Beginning of operation (present continuous): _Creating ..._,
1394 _Copying ..._, _Serializing ..._, _Freezing ..._, _Destroying ..._
1395 ** End of operation (simple past): _Created ..._, _Successfully created ..._,
1396 _Failed to create ..._, _Set ..._ (simple past of _to set_ which is
1400 For warning and error messages, you can start the message with _Cannot_
1401 or _Failed to_ followed by a verb if it's appropriate.
1403 * Do not include the log level in the message itself. For example,
1404 do not start the message with _Error while_ or _Warning:_.
1406 * Do not put newlines, tabs, or other special characters in the message,
1407 unless you want to log a string with such characters. Note that
1408 multiline logging messages can be hard to parse, analyze, and filter,
1409 however, so prefer multiple logging statements over a single statement
1412 * **If there are fields that your logging statement must record**,
1413 follow the message with `:` followed by a space, then with the list of
1414 fields (more about this below). If there are no fields, end the
1415 sentence with a period.
1417 The statement's fields _must_ be a comma-separated list of
1418 `__name__=__value__` tokens. Keep `__name__` as simple as possible; use
1419 kebab case if possible. If `__value__` is a non-alphanumeric string, put
1420 it between double quotes (`"%s"` specifier). Always use the `PRId64` and
1421 `PRIu64` specifiers to log an `int64_t` or an `uint64_t` value. Use `%d`
1422 to log a boolean value.
1426 "Cannot read stream data for indexing: path=\"%s\", name=\"%s\", "
1427 "stream-id=%" PRIu64 ", stream-fd=%d, "
1428 "index=%" PRIu64 ", status=%s, is-mapped=%d"
1430 By following a standard format for the statement fields, it is easier to
1431 use tools like https://www.elastic.co/products/logstash[Logstash] or
1432 even https://www.splunk.com/[Splunk] to split fields and analyze logs.
1434 Prefer the following suffixes in field names:
1436 [options="header,autowidth"]
1438 |Field name suffix |Description |Format specifier
1440 |`-addr` |Memory address |`%p`
1441 |`-fd` |File descriptor |`%d`
1442 |`-fp` |File stream (`+FILE *+`) |`%p`
1443 |`-id` |Object's ID |`%" PRIu64 "`
1444 |`-index` |Index |`%" PRIu64 "`
1445 |`-name` |Object's name |`\"%s\"`
1451 The log is printed to the standard error stream. A log line contains the
1452 time, the process and thread IDs, the <<log-levels,log level>>, the
1453 <<choose-a-logging-tag,logging tag>>, the source's function name, file
1454 name and line number, and the <<message,message>>.
1456 When Babeltrace supports terminal color codes (depends on the
1457 `BABELTRACE_TERM_COLOR` environment variable's value and what the
1458 standard output and error streams are plugged into), _INFO_-level lines
1459 are blue, _WARNING_-level lines are yellow, and _ERROR_-level and
1460 _FATAL_-level lines are red.
1465 05-11 00:58:03.691 23402 23402 D VALUES bt_value_destroy@values.c:498 Destroying value: addr=0xb9c3eb0
1468 You can easily filter the log with `grep` or `ag`. For example, to keep
1469 only the _DEBUG_-level logging messages that the `FIELD-CLASS` module
1473 $ babeltrace2 --log-level=D /path/to/trace |& ag 'D FIELD-CLASS'
1479 To use Valgrind on an application (for example, the CLI or a test) which
1480 loads libbabeltrace2, use:
1483 $ G_SLICE=always-malloc G_DEBUG=gc-friendly PYTHONMALLOC=malloc \
1484 LIBBABELTRACE2_NO_DLCLOSE=1 valgrind --leak-check=full \
1485 --suppressions=/path/to/babeltrace/extras/valgrind/popt.supp app
1488 `G_SLICE=always-malloc` and `G_DEBUG=gc-friendly` is for GLib and
1489 `PYTHONMALLOC=malloc` is for the Python interpreter, if it is used by
1490 the Python plugin provider (Valgrind will probably show a lot of errors
1491 which originate from the Python interpreter anyway).
1493 `LIBBABELTRACE2_NO_DLCLOSE=1` makes libbabeltrace2 not close the shared
1494 libraries (plugins) which it loads. You need this to see the appropriate
1495 backtrace when Valgrind shows errors.
1501 To run all the `bt2` Python package tests use:
1504 $ BT_TESTS_BUILDDIR=/path/to/build/babeltrace/tests \
1505 ./tests/bindings/python/bt2/test_python_bt2
1508 To run all the tests in a test module (e.g. `test_event.py`) use:
1511 $ BT_TESTS_BUILDDIR=/path/to/build/babeltrace/tests \
1512 ./tests/utils/run_python_bt2 python3 ./tests/utils/python/testrunner.py \
1514 ./tests/bindings/python/bt2/
1517 To run a specific test (e.g. `EventTestCase.test_clock_value`) in a test module
1518 (e.g. `test_event.py`) use:
1521 $ BT_TESTS_BUILDDIR=/path/to/build/babeltrace/tests \
1522 ./tests/utils/run_python_bt2 python3 ./tests/utils/python/testrunner.py \
1523 -t test_event.EventTestCase.test_clock_value \
1524 ./tests/bindings/python/bt2/