2 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "LIB/LIB-LOGGING"
24 #include "lib/logging.h"
34 #include "common/common.h"
35 #include "common/uuid.h"
36 #include <babeltrace2/babeltrace.h>
39 #include "assert-pre.h"
40 #include "assert-post.h"
42 #include "object-pool.h"
43 #include "graph/component-class.h"
44 #include "graph/component-class-sink-colander.h"
45 #include "graph/component-filter.h"
46 #include "graph/component.h"
47 #include "graph/component-sink.h"
48 #include "graph/component-source.h"
49 #include "graph/connection.h"
50 #include "graph/graph.h"
51 #include "graph/message/discarded-items.h"
52 #include "graph/message/event.h"
53 #include "graph/message/iterator.h"
54 #include "graph/message/message.h"
55 #include "graph/message/message-iterator-inactivity.h"
56 #include "graph/message/packet.h"
57 #include "graph/message/stream.h"
58 #include "graph/port.h"
59 #include "plugin/plugin.h"
60 #include "plugin/plugin-so.h"
61 #include "trace-ir/clock-class.h"
62 #include "trace-ir/clock-snapshot.h"
63 #include "trace-ir/event-class.h"
64 #include "trace-ir/event.h"
65 #include "trace-ir/field-class.h"
66 #include "trace-ir/field.h"
67 #include "trace-ir/field-path.h"
68 #include "trace-ir/packet.h"
69 #include "trace-ir/stream-class.h"
70 #include "trace-ir/stream.h"
71 #include "trace-ir/trace-class.h"
72 #include "trace-ir/trace.h"
73 #include "trace-ir/utils.h"
75 #include "assert-pre.h"
77 #define LIB_LOGGING_BUF_SIZE (4096 * 4)
79 static __thread
char lib_logging_buf
[LIB_LOGGING_BUF_SIZE
];
81 #define BUF_APPEND(_fmt, ...) \
84 size_t _size = LIB_LOGGING_BUF_SIZE - \
85 (size_t) (*buf_ch - lib_logging_buf); \
86 _count = snprintf(*buf_ch, _size, (_fmt), __VA_ARGS__); \
87 BT_ASSERT(_count >= 0); \
88 *buf_ch += MIN(_count, _size); \
89 if (*buf_ch >= lib_logging_buf + LIB_LOGGING_BUF_SIZE - 1) { \
94 #define BUF_APPEND_UUID(_uuid) \
96 BUF_APPEND(", %suuid=", prefix); \
97 format_uuid(buf_ch, (_uuid)); \
100 #define PRFIELD(_expr) prefix, (_expr)
102 #define PRFIELD_GSTRING(_expr) PRFIELD((_expr) ? (_expr)->str : NULL)
104 #define TMP_PREFIX_LEN 128
105 #define SET_TMP_PREFIX(_prefix2) \
108 snprintf(tmp_prefix, TMP_PREFIX_LEN - 1, "%s%s", \
109 prefix, (_prefix2)); \
111 if (snprintf_ret < 0 || snprintf_ret >= TMP_PREFIX_LEN - 1) { \
115 tmp_prefix[TMP_PREFIX_LEN - 1] = '\0'; \
118 static inline void format_component(char **buf_ch
, bool extended
,
119 const char *prefix
, const struct bt_component
*component
);
121 static inline void format_port(char **buf_ch
, bool extended
,
122 const char *prefix
, const struct bt_port
*port
);
124 static inline void format_connection(char **buf_ch
, bool extended
,
125 const char *prefix
, const struct bt_connection
*connection
);
127 static inline void format_clock_snapshot(char **buf_ch
, bool extended
,
128 const char *prefix
, const struct bt_clock_snapshot
*clock_snapshot
);
130 static inline void format_field_path(char **buf_ch
, bool extended
,
131 const char *prefix
, const struct bt_field_path
*field_path
);
133 static inline void format_object(char **buf_ch
, bool extended
,
134 const char *prefix
, const struct bt_object
*obj
)
136 BUF_APPEND(", %sref-count=%llu", prefix
, obj
->ref_count
);
139 static inline void format_uuid(char **buf_ch
, bt_uuid uuid
)
141 BUF_APPEND("\"" BT_UUID_FMT
"\"", BT_UUID_FMT_VALUES(uuid
));
144 static inline void format_object_pool(char **buf_ch
, bool extended
,
145 const char *prefix
, const struct bt_object_pool
*pool
)
147 BUF_APPEND(", %ssize=%zu", PRFIELD(pool
->size
));
150 BUF_APPEND(", %scap=%u", PRFIELD(pool
->objects
->len
));
154 static inline void format_integer_field_class(char **buf_ch
,
155 bool extended
, const char *prefix
,
156 const struct bt_field_class
*field_class
)
158 const struct bt_field_class_integer
*int_fc
=
159 (const void *) field_class
;
161 BUF_APPEND(", %srange-size=%" PRIu64
", %sbase=%s",
162 PRFIELD(int_fc
->range
),
163 PRFIELD(bt_common_field_class_integer_preferred_display_base_string(int_fc
->base
)));
166 static inline void format_array_field_class(char **buf_ch
,
167 bool extended
, const char *prefix
,
168 const struct bt_field_class
*field_class
)
170 const struct bt_field_class_array
*array_fc
=
171 (const void *) field_class
;
173 BUF_APPEND(", %selement-fc-addr=%p, %selement-fc-type=%s",
174 PRFIELD(array_fc
->element_fc
),
175 PRFIELD(bt_common_field_class_type_string(array_fc
->element_fc
->type
)));
178 static inline void format_field_class(char **buf_ch
, bool extended
,
179 const char *prefix
, const struct bt_field_class
*field_class
)
181 char tmp_prefix
[TMP_PREFIX_LEN
];
183 BUF_APPEND(", %stype=%s",
184 PRFIELD(bt_common_field_class_type_string(field_class
->type
)));
187 BUF_APPEND(", %sis-frozen=%d", PRFIELD(field_class
->frozen
));
188 BUF_APPEND(", %sis-part-of-trace-class=%d",
189 PRFIELD(field_class
->part_of_trace_class
));
194 switch (field_class
->type
) {
195 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
196 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
198 format_integer_field_class(buf_ch
, extended
, prefix
, field_class
);
201 case BT_FIELD_CLASS_TYPE_REAL
:
203 const struct bt_field_class_real
*real_fc
= (void *) field_class
;
205 BUF_APPEND(", %sis-single-precision=%d",
206 PRFIELD(real_fc
->is_single_precision
));
209 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
210 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
212 const struct bt_field_class_enumeration
*enum_fc
=
213 (const void *) field_class
;
215 format_integer_field_class(buf_ch
, extended
, prefix
, field_class
);
216 BUF_APPEND(", %smapping-count=%u",
217 PRFIELD(enum_fc
->mappings
->len
));
220 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
222 const struct bt_field_class_structure
*struct_fc
=
223 (const void *) field_class
;
225 if (struct_fc
->common
.named_fcs
) {
226 BUF_APPEND(", %smember-count=%u",
227 PRFIELD(struct_fc
->common
.named_fcs
->len
));
232 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
234 const struct bt_field_class_static_array
*array_fc
=
235 (const void *) field_class
;
237 format_array_field_class(buf_ch
, extended
, prefix
, field_class
);
238 BUF_APPEND(", %slength=%" PRIu64
, PRFIELD(array_fc
->length
));
241 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
243 const struct bt_field_class_dynamic_array
*array_fc
=
244 (const void *) field_class
;
246 format_array_field_class(buf_ch
, extended
, prefix
, field_class
);
248 if (array_fc
->length_fc
) {
249 SET_TMP_PREFIX("length-fc-");
250 format_field_class(buf_ch
, extended
, tmp_prefix
,
251 array_fc
->length_fc
);
254 if (array_fc
->length_field_path
) {
255 SET_TMP_PREFIX("length-field-path-");
256 format_field_path(buf_ch
, extended
, tmp_prefix
,
257 array_fc
->length_field_path
);
262 case BT_FIELD_CLASS_TYPE_VARIANT
:
264 const struct bt_field_class_variant
*var_fc
=
265 (const void *) field_class
;
267 if (var_fc
->common
.named_fcs
) {
268 BUF_APPEND(", %soption-count=%u",
269 PRFIELD(var_fc
->common
.named_fcs
->len
));
272 if (var_fc
->selector_fc
) {
273 SET_TMP_PREFIX("selector-fc-");
274 format_field_class(buf_ch
, extended
, tmp_prefix
,
275 var_fc
->selector_fc
);
278 if (var_fc
->selector_field_path
) {
279 SET_TMP_PREFIX("selector-field-path-");
280 format_field_path(buf_ch
, extended
, tmp_prefix
,
281 var_fc
->selector_field_path
);
291 static inline void format_field_integer_extended(char **buf_ch
,
292 const char *prefix
, const struct bt_field
*field
)
294 const struct bt_field_integer
*integer
= (void *) field
;
295 const struct bt_field_class_integer
*field_class
=
296 (void *) field
->class;
297 const char *fmt
= NULL
;
299 BT_ASSERT(field_class
);
301 if (field_class
->base
== BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
) {
302 fmt
= ", %svalue=%" PRIo64
;
303 } else if (field_class
->base
== BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
) {
304 fmt
= ", %svalue=%" PRIx64
;
307 if (field_class
->common
.type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
308 field_class
->common
.type
== BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
) {
310 fmt
= ", %svalue=%" PRId64
;
313 BUF_APPEND(fmt
, PRFIELD(integer
->value
.i
));
316 fmt
= ", %svalue=%" PRIu64
;
319 BUF_APPEND(fmt
, PRFIELD(integer
->value
.u
));
323 static inline void format_field(char **buf_ch
, bool extended
,
324 const char *prefix
, const struct bt_field
*field
)
326 BUF_APPEND(", %sis-set=%d", PRFIELD(field
->is_set
));
329 BUF_APPEND(", %sis-frozen=%d", PRFIELD(field
->frozen
));
332 BUF_APPEND(", %sclass-addr=%p", PRFIELD(field
->class));
338 BUF_APPEND(", %sclass-type=%s",
339 PRFIELD(bt_common_field_class_type_string(field
->class->type
)));
341 if (!extended
|| !field
->is_set
) {
345 switch (field
->class->type
) {
346 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
347 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
348 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
349 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
351 format_field_integer_extended(buf_ch
, prefix
, field
);
354 case BT_FIELD_CLASS_TYPE_REAL
:
356 const struct bt_field_real
*real_field
= (const void *) field
;
358 BUF_APPEND(", %svalue=%f", PRFIELD(real_field
->value
));
361 case BT_FIELD_CLASS_TYPE_STRING
:
363 const struct bt_field_string
*str
= (const void *) field
;
366 BT_ASSERT(str
->buf
->data
);
367 BUF_APPEND(", %spartial-value=\"%.32s\"",
368 PRFIELD(str
->buf
->data
));
373 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
374 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
376 const struct bt_field_array
*array_field
= (const void *) field
;
378 BUF_APPEND(", %slength=%" PRIu64
, PRFIELD(array_field
->length
));
380 if (array_field
->fields
) {
381 BUF_APPEND(", %sallocated-length=%u",
382 PRFIELD(array_field
->fields
->len
));
387 case BT_FIELD_CLASS_TYPE_VARIANT
:
389 const struct bt_field_variant
*var_field
= (const void *) field
;
391 BUF_APPEND(", %sselected-field-index=%" PRIu64
,
392 PRFIELD(var_field
->selected_index
));
400 static inline void format_field_path(char **buf_ch
, bool extended
,
401 const char *prefix
, const struct bt_field_path
*field_path
)
405 if (field_path
->items
) {
406 BT_ASSERT(field_path
->items
);
407 BUF_APPEND(", %sitem-count=%u",
408 PRFIELD(field_path
->items
->len
));
411 if (!extended
|| !field_path
->items
) {
415 BUF_APPEND(", %spath=[%s",
416 PRFIELD(bt_common_scope_string(field_path
->root
)));
418 for (i
= 0; i
< bt_field_path_get_item_count(field_path
); i
++) {
419 const struct bt_field_path_item
*fp_item
=
420 bt_field_path_borrow_item_by_index_const(field_path
, i
);
422 switch (bt_field_path_item_get_type(fp_item
)) {
423 case BT_FIELD_PATH_ITEM_TYPE_INDEX
:
424 BUF_APPEND(", %" PRIu64
,
425 bt_field_path_item_index_get_index(fp_item
));
427 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
:
428 BUF_APPEND("%s", ", <CUR>");
435 BUF_APPEND("%s", "]");
438 static inline void format_trace_class(char **buf_ch
, bool extended
,
439 const char *prefix
, const struct bt_trace_class
*trace_class
)
445 BUF_APPEND(", %sis-frozen=%d", PRFIELD(trace_class
->frozen
));
447 if (trace_class
->stream_classes
) {
448 BUF_APPEND(", %sstream-class-count=%u",
449 PRFIELD(trace_class
->stream_classes
->len
));
452 BUF_APPEND(", %sassigns-auto-sc-id=%d",
453 PRFIELD(trace_class
->assigns_automatic_stream_class_id
));
456 static inline void format_trace(char **buf_ch
, bool extended
,
457 const char *prefix
, const struct bt_trace
*trace
)
459 char tmp_prefix
[TMP_PREFIX_LEN
];
461 if (trace
->name
.value
) {
462 BUF_APPEND(", %sname=\"%s\"", PRFIELD(trace
->name
.value
));
469 if (trace
->uuid
.value
) {
470 BUF_APPEND_UUID(trace
->uuid
.value
);
473 BUF_APPEND(", %sis-frozen=%d", PRFIELD(trace
->frozen
));
475 if (trace
->streams
) {
476 BUF_APPEND(", %sstream-count=%u",
477 PRFIELD(trace
->streams
->len
));
484 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace
->class));
485 SET_TMP_PREFIX("trace-class-");
486 format_trace_class(buf_ch
, false, tmp_prefix
, trace
->class);
489 static inline void format_stream_class(char **buf_ch
, bool extended
,
491 const struct bt_stream_class
*stream_class
)
493 const struct bt_trace_class
*trace_class
;
494 char tmp_prefix
[TMP_PREFIX_LEN
];
496 BUF_APPEND(", %sid=%" PRIu64
, PRFIELD(stream_class
->id
));
498 if (stream_class
->name
.value
) {
499 BUF_APPEND(", %sname=\"%s\"",
500 PRFIELD(stream_class
->name
.value
));
507 BUF_APPEND(", %sis-frozen=%d", PRFIELD(stream_class
->frozen
));
509 if (stream_class
->event_classes
) {
510 BUF_APPEND(", %sevent-class-count=%u",
511 PRFIELD(stream_class
->event_classes
->len
));
514 BUF_APPEND(", %spacket-context-fc-addr=%p, "
515 "%sevent-common-context-fc-addr=%p",
516 PRFIELD(stream_class
->packet_context_fc
),
517 PRFIELD(stream_class
->event_common_context_fc
));
518 trace_class
= bt_stream_class_borrow_trace_class_inline(stream_class
);
523 BUF_APPEND(", %sassigns-auto-ec-id=%d, %sassigns-auto-stream-id=%d, "
524 "%ssupports-packets=%d, "
525 "%spackets-have-begin-default-cs=%d, "
526 "%spackets-have-end-default-cs=%d, "
527 "%ssupports-discarded-events=%d, "
528 "%sdiscarded-events-have-default-cs=%d, "
529 "%ssupports-discarded-packets=%d, "
530 "%sdiscarded-packets-have-default-cs=%d",
531 PRFIELD(stream_class
->assigns_automatic_event_class_id
),
532 PRFIELD(stream_class
->assigns_automatic_stream_id
),
533 PRFIELD(stream_class
->supports_packets
),
534 PRFIELD(stream_class
->packets_have_beginning_default_clock_snapshot
),
535 PRFIELD(stream_class
->packets_have_end_default_clock_snapshot
),
536 PRFIELD(stream_class
->supports_discarded_events
),
537 PRFIELD(stream_class
->discarded_events_have_default_clock_snapshots
),
538 PRFIELD(stream_class
->supports_discarded_packets
),
539 PRFIELD(stream_class
->discarded_packets_have_default_clock_snapshots
));
540 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace_class
));
541 SET_TMP_PREFIX("trace-class-");
542 format_trace_class(buf_ch
, false, tmp_prefix
, trace_class
);
543 SET_TMP_PREFIX("pcf-pool-");
544 format_object_pool(buf_ch
, extended
, tmp_prefix
,
545 &stream_class
->packet_context_field_pool
);
548 static inline void format_event_class(char **buf_ch
, bool extended
,
549 const char *prefix
, const struct bt_event_class
*event_class
)
551 const struct bt_stream_class
*stream_class
;
552 const struct bt_trace_class
*trace_class
;
553 char tmp_prefix
[TMP_PREFIX_LEN
];
555 BUF_APPEND(", %sid=%" PRIu64
, PRFIELD(event_class
->id
));
557 if (event_class
->name
.value
) {
558 BUF_APPEND(", %sname=\"%s\"",
559 PRFIELD(event_class
->name
.value
));
566 BUF_APPEND(", %sis-frozen=%d", PRFIELD(event_class
->frozen
));
568 if (event_class
->log_level
.base
.avail
) {
569 BUF_APPEND(", %slog-level=%s",
570 PRFIELD(bt_common_event_class_log_level_string(
571 (int) event_class
->log_level
.value
)));
574 if (event_class
->emf_uri
.value
) {
575 BUF_APPEND(", %semf-uri=\"%s\"",
576 PRFIELD(event_class
->emf_uri
.value
));
579 BUF_APPEND(", %sspecific-context-fc-addr=%p, %spayload-fc-addr=%p",
580 PRFIELD(event_class
->specific_context_fc
),
581 PRFIELD(event_class
->payload_fc
));
583 stream_class
= bt_event_class_borrow_stream_class_const(event_class
);
588 BUF_APPEND(", %sstream-class-addr=%p", PRFIELD(stream_class
));
589 SET_TMP_PREFIX("stream-class-");
590 format_stream_class(buf_ch
, false, tmp_prefix
, stream_class
);
591 trace_class
= bt_stream_class_borrow_trace_class_inline(stream_class
);
596 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace_class
));
597 SET_TMP_PREFIX("trace-class-");
598 format_trace_class(buf_ch
, false, tmp_prefix
, trace_class
);
599 SET_TMP_PREFIX("event-pool-");
600 format_object_pool(buf_ch
, extended
, tmp_prefix
,
601 &event_class
->event_pool
);
604 static inline void format_stream(char **buf_ch
, bool extended
,
605 const char *prefix
, const struct bt_stream
*stream
)
607 const struct bt_stream_class
*stream_class
;
608 const struct bt_trace_class
*trace_class
= NULL
;
609 const struct bt_trace
*trace
= NULL
;
610 char tmp_prefix
[TMP_PREFIX_LEN
];
612 BUF_APPEND(", %sid=%" PRIu64
, PRFIELD(stream
->id
));
614 if (stream
->name
.value
) {
615 BUF_APPEND(", %sname=\"%s\"", PRFIELD(stream
->name
.value
));
622 stream_class
= bt_stream_borrow_class_const(stream
);
624 BUF_APPEND(", %sstream-class-addr=%p", PRFIELD(stream_class
));
625 SET_TMP_PREFIX("stream-class-");
626 format_stream_class(buf_ch
, false, tmp_prefix
, stream_class
);
627 trace_class
= bt_stream_class_borrow_trace_class_inline(stream_class
);
631 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace_class
));
632 SET_TMP_PREFIX("trace-class-");
633 format_trace_class(buf_ch
, false, tmp_prefix
, trace_class
);
636 trace
= bt_stream_borrow_trace_inline(stream
);
638 BUF_APPEND(", %strace-addr=%p", PRFIELD(trace
));
639 SET_TMP_PREFIX("trace-");
640 format_trace(buf_ch
, false, tmp_prefix
, trace
);
643 SET_TMP_PREFIX("packet-pool-");
644 format_object_pool(buf_ch
, extended
, tmp_prefix
, &stream
->packet_pool
);
647 static inline void format_packet(char **buf_ch
, bool extended
,
648 const char *prefix
, const struct bt_packet
*packet
)
650 const struct bt_stream
*stream
;
651 const struct bt_trace_class
*trace_class
;
652 char tmp_prefix
[TMP_PREFIX_LEN
];
658 BUF_APPEND(", %sis-frozen=%d, %scontext-field-addr=%p",
659 PRFIELD(packet
->frozen
),
660 PRFIELD(packet
->context_field
? packet
->context_field
->field
: NULL
));
661 stream
= bt_packet_borrow_stream_const(packet
);
666 BUF_APPEND(", %sstream-addr=%p", PRFIELD(stream
));
667 SET_TMP_PREFIX("stream-");
668 format_stream(buf_ch
, false, tmp_prefix
, stream
);
669 trace_class
= (const struct bt_trace_class
*) bt_object_borrow_parent(&stream
->base
);
674 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace_class
));
675 SET_TMP_PREFIX("trace-class-");
676 format_trace_class(buf_ch
, false, tmp_prefix
, trace_class
);
679 static inline void format_event(char **buf_ch
, bool extended
,
680 const char *prefix
, const struct bt_event
*event
)
682 const struct bt_trace_class
*trace_class
;
683 const struct bt_stream_class
*stream_class
;
684 char tmp_prefix
[TMP_PREFIX_LEN
];
690 BUF_APPEND(", %sis-frozen=%d, "
691 "%scommon-context-field-addr=%p, "
692 "%sspecific-context-field-addr=%p, "
693 "%spayload-field-addr=%p, ",
694 PRFIELD(event
->frozen
),
695 PRFIELD(event
->common_context_field
),
696 PRFIELD(event
->specific_context_field
),
697 PRFIELD(event
->payload_field
));
698 BUF_APPEND(", %sevent-class-addr=%p", PRFIELD(event
->class));
704 SET_TMP_PREFIX("event-class-");
705 format_event_class(buf_ch
, false, tmp_prefix
, event
->class);
706 stream_class
= bt_event_class_borrow_stream_class(event
->class);
708 BUF_APPEND(", %sstream-class-addr=%p", PRFIELD(stream_class
));
709 SET_TMP_PREFIX("stream-class-");
710 format_stream_class(buf_ch
, false, tmp_prefix
,
713 trace_class
= bt_stream_class_borrow_trace_class_inline(
716 BUF_APPEND(", %strace-class-addr=%p",
717 PRFIELD(trace_class
));
718 SET_TMP_PREFIX("trace-class-");
719 format_trace_class(buf_ch
, false, tmp_prefix
,
725 BUF_APPEND(", %sstream-addr=%p", PRFIELD(event
->stream
));
726 SET_TMP_PREFIX("stream-");
727 format_stream(buf_ch
, false, tmp_prefix
, event
->stream
);
731 BUF_APPEND(", %spacket-addr=%p", PRFIELD(event
->packet
));
732 SET_TMP_PREFIX("packet-");
733 format_packet(buf_ch
, false, tmp_prefix
, event
->packet
);
737 static inline void format_clock_class(char **buf_ch
, bool extended
,
738 const char *prefix
, const struct bt_clock_class
*clock_class
)
740 char tmp_prefix
[TMP_PREFIX_LEN
];
742 if (clock_class
->name
.value
) {
743 BUF_APPEND(", %sname=\"%s\"", PRFIELD(clock_class
->name
.value
));
746 BUF_APPEND(", %sfreq=%" PRIu64
, PRFIELD(clock_class
->frequency
));
752 if (clock_class
->description
.value
) {
753 BUF_APPEND(", %spartial-descr=\"%.32s\"",
754 PRFIELD(clock_class
->description
.value
));
757 if (clock_class
->uuid
.value
) {
758 BUF_APPEND_UUID(clock_class
->uuid
.value
);
761 BUF_APPEND(", %sis-frozen=%d, %sprecision=%" PRIu64
", "
762 "%soffset-s=%" PRId64
", "
763 "%soffset-cycles=%" PRIu64
", %sorigin-is-unix-epoch=%d, "
764 "%sbase-offset-ns=%" PRId64
,
765 PRFIELD(clock_class
->frozen
), PRFIELD(clock_class
->precision
),
766 PRFIELD(clock_class
->offset_seconds
),
767 PRFIELD(clock_class
->offset_cycles
),
768 PRFIELD(clock_class
->origin_is_unix_epoch
),
769 PRFIELD(clock_class
->base_offset
.value_ns
));
771 SET_TMP_PREFIX("cs-pool-");
772 format_object_pool(buf_ch
, extended
, tmp_prefix
,
773 &clock_class
->cs_pool
);
776 static inline void format_clock_snapshot(char **buf_ch
, bool extended
,
777 const char *prefix
, const struct bt_clock_snapshot
*clock_snapshot
)
779 char tmp_prefix
[TMP_PREFIX_LEN
];
780 BUF_APPEND(", %svalue=%" PRIu64
", %sns-from-origin=%" PRId64
,
781 PRFIELD(clock_snapshot
->value_cycles
),
782 PRFIELD(clock_snapshot
->ns_from_origin
));
788 BUF_APPEND(", %sis-set=%d", PRFIELD(clock_snapshot
->is_set
));
790 if (clock_snapshot
->clock_class
) {
791 BUF_APPEND(", %sclock-class-addr=%p",
792 PRFIELD(clock_snapshot
->clock_class
));
793 SET_TMP_PREFIX("clock-class-");
794 format_clock_class(buf_ch
, false, tmp_prefix
,
795 clock_snapshot
->clock_class
);
799 static inline void format_value(char **buf_ch
, bool extended
,
800 const char *prefix
, const struct bt_value
*value
)
802 BUF_APPEND(", %stype=%s",
803 PRFIELD(bt_common_value_type_string(bt_value_get_type(value
))));
809 switch (bt_value_get_type(value
)) {
810 case BT_VALUE_TYPE_BOOL
:
812 bt_bool val
= bt_value_bool_get(value
);
814 BUF_APPEND(", %svalue=%d", PRFIELD(val
));
817 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
819 BUF_APPEND(", %svalue=%" PRIu64
,
820 PRFIELD(bt_value_unsigned_integer_get(value
)));
823 case BT_VALUE_TYPE_SIGNED_INTEGER
:
825 BUF_APPEND(", %svalue=%" PRId64
,
826 PRFIELD(bt_value_signed_integer_get(value
)));
829 case BT_VALUE_TYPE_REAL
:
831 double val
= bt_value_real_get(value
);
833 BUF_APPEND(", %svalue=%f", PRFIELD(val
));
836 case BT_VALUE_TYPE_STRING
:
838 const char *val
= bt_value_string_get(value
);
840 BUF_APPEND(", %spartial-value=\"%.32s\"", PRFIELD(val
));
843 case BT_VALUE_TYPE_ARRAY
:
845 int64_t count
= bt_value_array_get_size(value
);
847 BT_ASSERT(count
>= 0);
848 BUF_APPEND(", %selement-count=%" PRId64
, PRFIELD(count
));
851 case BT_VALUE_TYPE_MAP
:
853 int64_t count
= bt_value_map_get_size(value
);
855 BT_ASSERT(count
>= 0);
856 BUF_APPEND(", %selement-count=%" PRId64
, PRFIELD(count
));
864 static inline void format_message(char **buf_ch
, bool extended
,
865 const char *prefix
, const struct bt_message
*msg
)
867 char tmp_prefix
[TMP_PREFIX_LEN
];
869 BUF_APPEND(", %stype=%s",
870 PRFIELD(bt_message_type_string(msg
->type
)));
876 BUF_APPEND(", %sis-frozen=%d, %sgraph-addr=%p",
877 PRFIELD(msg
->frozen
), PRFIELD(msg
->graph
));
880 case BT_MESSAGE_TYPE_EVENT
:
882 const struct bt_message_event
*msg_event
=
885 if (msg_event
->event
) {
886 SET_TMP_PREFIX("event-");
887 format_event(buf_ch
, true, tmp_prefix
,
891 if (msg_event
->default_cs
) {
892 SET_TMP_PREFIX("default-cs-");
893 format_clock_snapshot(buf_ch
, true, tmp_prefix
,
894 msg_event
->default_cs
);
899 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
900 case BT_MESSAGE_TYPE_STREAM_END
:
902 const struct bt_message_stream
*msg_stream
= (const void *) msg
;
904 if (msg_stream
->stream
) {
905 SET_TMP_PREFIX("stream-");
906 format_stream(buf_ch
, true, tmp_prefix
,
910 BUF_APPEND(", %sdefault-cs-state=%s",
911 PRFIELD(bt_message_stream_clock_snapshot_state_string(
912 msg_stream
->default_cs_state
)));
914 if (msg_stream
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
915 SET_TMP_PREFIX("default-cs-");
916 format_clock_snapshot(buf_ch
, true, tmp_prefix
,
917 msg_stream
->default_cs
);
922 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
923 case BT_MESSAGE_TYPE_PACKET_END
:
925 const struct bt_message_packet
*msg_packet
= (const void *) msg
;
927 if (msg_packet
->packet
) {
928 SET_TMP_PREFIX("packet-");
929 format_packet(buf_ch
, true, tmp_prefix
,
933 if (msg_packet
->default_cs
) {
934 SET_TMP_PREFIX("default-cs-");
935 format_clock_snapshot(buf_ch
, true, tmp_prefix
,
936 msg_packet
->default_cs
);
941 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
942 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
944 const struct bt_message_discarded_items
*msg_disc_items
=
947 if (msg_disc_items
->stream
) {
948 SET_TMP_PREFIX("stream-");
949 format_stream(buf_ch
, true, tmp_prefix
,
950 msg_disc_items
->stream
);
953 if (msg_disc_items
->default_begin_cs
) {
954 SET_TMP_PREFIX("begin-default-cs-");
955 format_clock_snapshot(buf_ch
, true, tmp_prefix
,
956 msg_disc_items
->default_begin_cs
);
959 if (msg_disc_items
->default_end_cs
) {
960 SET_TMP_PREFIX("end-default-cs-");
961 format_clock_snapshot(buf_ch
, true, tmp_prefix
,
962 msg_disc_items
->default_end_cs
);
965 if (msg_disc_items
->count
.base
.avail
) {
966 BUF_APPEND(", %scount=%" PRIu64
,
967 PRFIELD(msg_disc_items
->count
.value
));
977 static inline void format_plugin_so_shared_lib_handle(char **buf_ch
,
979 const struct bt_plugin_so_shared_lib_handle
*handle
)
981 BUF_APPEND(", %saddr=%p", PRFIELD(handle
));
984 BUF_APPEND(", %spath=\"%s\"", PRFIELD_GSTRING(handle
->path
));
988 static inline void format_component_class(char **buf_ch
, bool extended
,
990 const struct bt_component_class
*comp_class
)
992 char tmp_prefix
[TMP_PREFIX_LEN
];
994 BUF_APPEND(", %stype=%s, %sname=\"%s\"",
995 PRFIELD(bt_component_class_type_string(comp_class
->type
)),
996 PRFIELD_GSTRING(comp_class
->name
));
998 if (comp_class
->description
) {
999 BUF_APPEND(", %spartial-descr=\"%.32s\"",
1000 PRFIELD_GSTRING(comp_class
->description
));
1007 BUF_APPEND(", %sis-frozen=%d", PRFIELD(comp_class
->frozen
));
1009 if (comp_class
->so_handle
) {
1010 SET_TMP_PREFIX("so-handle-");
1011 format_plugin_so_shared_lib_handle(buf_ch
, tmp_prefix
,
1012 comp_class
->so_handle
);
1016 static inline void format_component(char **buf_ch
, bool extended
,
1017 const char *prefix
, const struct bt_component
*component
)
1019 char tmp_prefix
[TMP_PREFIX_LEN
];
1021 BUF_APPEND(", %sname=\"%s\", %slog-level=%s",
1022 PRFIELD_GSTRING(component
->name
),
1023 PRFIELD(bt_common_logging_level_string(component
->log_level
)));
1025 if (component
->class) {
1026 SET_TMP_PREFIX("class-");
1027 format_component_class(buf_ch
, extended
, tmp_prefix
,
1035 if (component
->input_ports
) {
1036 BUF_APPEND(", %sinput-port-count=%u",
1037 PRFIELD(component
->input_ports
->len
));
1040 if (component
->output_ports
) {
1041 BUF_APPEND(", %soutput-port-count=%u",
1042 PRFIELD(component
->output_ports
->len
));
1046 static inline void format_port(char **buf_ch
, bool extended
,
1047 const char *prefix
, const struct bt_port
*port
)
1049 char tmp_prefix
[TMP_PREFIX_LEN
];
1051 BUF_APPEND(", %stype=%s, %sname=\"%s\"",
1052 PRFIELD(bt_port_type_string(port
->type
)),
1053 PRFIELD_GSTRING(port
->name
));
1059 if (port
->connection
) {
1060 SET_TMP_PREFIX("conn-");
1061 format_connection(buf_ch
, false, tmp_prefix
, port
->connection
);
1065 static inline void format_connection(char **buf_ch
, bool extended
,
1066 const char *prefix
, const struct bt_connection
*connection
)
1068 char tmp_prefix
[TMP_PREFIX_LEN
];
1074 if (connection
->upstream_port
) {
1075 SET_TMP_PREFIX("upstream-port-");
1076 format_port(buf_ch
, false, tmp_prefix
,
1077 connection
->upstream_port
);
1080 if (connection
->downstream_port
) {
1081 SET_TMP_PREFIX("downstream-port-");
1082 format_port(buf_ch
, false, tmp_prefix
,
1083 connection
->downstream_port
);
1087 static inline void format_graph(char **buf_ch
, bool extended
,
1088 const char *prefix
, const struct bt_graph
*graph
)
1090 char tmp_prefix
[TMP_PREFIX_LEN
];
1092 BUF_APPEND(", %sis-canceled=%d, %scan-consume=%d, "
1093 "%sconfig-state=%s",
1094 PRFIELD(graph
->canceled
),
1095 PRFIELD(graph
->can_consume
),
1096 PRFIELD(bt_graph_configuration_state_string(graph
->config_state
)));
1102 if (graph
->components
) {
1103 BUF_APPEND(", %scomp-count=%u",
1104 PRFIELD(graph
->components
->len
));
1107 if (graph
->connections
) {
1108 BUF_APPEND(", %sconn-count=%u",
1109 PRFIELD(graph
->connections
->len
));
1112 SET_TMP_PREFIX("en-pool-");
1113 format_object_pool(buf_ch
, extended
, tmp_prefix
,
1114 &graph
->event_msg_pool
);
1115 SET_TMP_PREFIX("pbn-pool-");
1116 format_object_pool(buf_ch
, extended
, tmp_prefix
,
1117 &graph
->packet_begin_msg_pool
);
1118 SET_TMP_PREFIX("pen-pool-");
1119 format_object_pool(buf_ch
, extended
, tmp_prefix
,
1120 &graph
->packet_end_msg_pool
);
1123 static inline void format_message_iterator(char **buf_ch
,
1124 bool extended
, const char *prefix
,
1125 const struct bt_message_iterator
*iterator
)
1128 char tmp_prefix
[TMP_PREFIX_LEN
];
1130 if (iterator
->type
== BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT
) {
1131 type
= "BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT";
1132 } else if (iterator
->type
== BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT
) {
1133 type
= "BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT";
1138 BUF_APPEND(", %stype=%s", PRFIELD(type
));
1140 switch (iterator
->type
) {
1141 case BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT
:
1143 const struct bt_self_component_port_input_message_iterator
*
1144 port_in_iter
= (const void *) iterator
;
1146 if (port_in_iter
->upstream_component
) {
1147 SET_TMP_PREFIX("upstream-comp-");
1148 format_component(buf_ch
, false, tmp_prefix
,
1149 port_in_iter
->upstream_component
);
1152 if (port_in_iter
->upstream_port
) {
1153 SET_TMP_PREFIX("upstream-port-");
1154 format_port(buf_ch
, false, tmp_prefix
,
1155 port_in_iter
->upstream_port
);
1158 if (port_in_iter
->connection
) {
1159 SET_TMP_PREFIX("upstream-conn-");
1160 format_connection(buf_ch
, false, tmp_prefix
,
1161 port_in_iter
->connection
);
1165 case BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT
:
1167 const struct bt_port_output_message_iterator
*port_out_iter
=
1168 (const void *) iterator
;
1170 if (port_out_iter
->graph
) {
1171 SET_TMP_PREFIX("graph-");
1172 format_graph(buf_ch
, false, tmp_prefix
,
1173 port_out_iter
->graph
);
1176 if (port_out_iter
->colander
) {
1177 SET_TMP_PREFIX("colander-comp-");
1178 format_component(buf_ch
, false, tmp_prefix
,
1179 (void *) port_out_iter
->colander
);
1189 static inline void format_plugin(char **buf_ch
, bool extended
,
1190 const char *prefix
, const struct bt_plugin
*plugin
)
1192 char tmp_prefix
[TMP_PREFIX_LEN
];
1194 BUF_APPEND(", %stype=%s", PRFIELD(bt_plugin_type_string(plugin
->type
)));
1196 if (plugin
->info
.path_set
) {
1197 BUF_APPEND(", %spath=\"%s\"",
1198 PRFIELD_GSTRING(plugin
->info
.path
));
1201 if (plugin
->info
.name_set
) {
1202 BUF_APPEND(", %sname=\"%s\"",
1203 PRFIELD_GSTRING(plugin
->info
.name
));
1210 if (plugin
->info
.author_set
) {
1211 BUF_APPEND(", %sauthor=\"%s\"",
1212 PRFIELD_GSTRING(plugin
->info
.author
));
1215 if (plugin
->info
.license_set
) {
1216 BUF_APPEND(", %slicense=\"%s\"",
1217 PRFIELD_GSTRING(plugin
->info
.license
));
1220 if (plugin
->info
.version_set
) {
1221 BUF_APPEND(", %sversion=%u.%u.%u%s",
1222 PRFIELD(plugin
->info
.version
.major
),
1223 plugin
->info
.version
.minor
,
1224 plugin
->info
.version
.patch
,
1225 plugin
->info
.version
.extra
?
1226 plugin
->info
.version
.extra
->str
: "");
1229 BUF_APPEND(", %ssrc-comp-class-count=%u, %sflt-comp-class-count=%u, "
1230 "%ssink-comp-class-count=%u",
1231 PRFIELD(plugin
->src_comp_classes
->len
),
1232 PRFIELD(plugin
->flt_comp_classes
->len
),
1233 PRFIELD(plugin
->sink_comp_classes
->len
));
1235 if (plugin
->spec_data
) {
1236 const struct bt_plugin_so_spec_data
*spec_data
=
1237 (const void *) plugin
->spec_data
;
1239 if (spec_data
->shared_lib_handle
) {
1240 SET_TMP_PREFIX("so-handle-");
1241 format_plugin_so_shared_lib_handle(buf_ch
, tmp_prefix
,
1242 spec_data
->shared_lib_handle
);
1247 static inline void format_error_cause(char **buf_ch
, bool extended
,
1248 const char *prefix
, const struct bt_error_cause
*cause
)
1250 const struct bt_error_cause_component_class_id
*comp_class_id
= NULL
;
1252 BUF_APPEND(", %sactor-type=%s, %smodule-name=\"%s\"",
1253 PRFIELD(bt_error_cause_actor_type_string(cause
->actor_type
)),
1254 PRFIELD_GSTRING(cause
->module_name
));
1260 BUF_APPEND(", %spartial-msg=\"%.32s\"",
1261 PRFIELD_GSTRING(cause
->message
));
1263 switch (cause
->actor_type
) {
1264 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
1266 const struct bt_error_cause_component_actor
*spec_cause
=
1267 (const void *) cause
;
1269 BUF_APPEND(", %scomp-name=\"%s\"",
1270 PRFIELD_GSTRING(spec_cause
->comp_name
));
1271 comp_class_id
= &spec_cause
->comp_class_id
;
1274 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
1276 const struct bt_error_cause_component_class_actor
*spec_cause
=
1277 (const void *) cause
;
1279 comp_class_id
= &spec_cause
->comp_class_id
;
1282 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
1284 const struct bt_error_cause_message_iterator_actor
*spec_cause
=
1285 (const void *) cause
;
1287 BUF_APPEND(", %scomp-name=\"%s\", %scomp-out-port-name=\"%s\"",
1288 PRFIELD_GSTRING(spec_cause
->comp_name
),
1289 PRFIELD_GSTRING(spec_cause
->output_port_name
));
1290 comp_class_id
= &spec_cause
->comp_class_id
;
1297 if (comp_class_id
) {
1298 BUF_APPEND(", %scomp-cls-type=%s, %scomp-cls-name=\"%s\", "
1299 "%splugin-name=\"%s\"",
1300 PRFIELD(bt_component_class_type_string(
1301 comp_class_id
->type
)),
1302 PRFIELD_GSTRING(comp_class_id
->name
),
1303 PRFIELD_GSTRING(comp_class_id
->plugin_name
));
1307 static inline void handle_conversion_specifier_bt(void *priv_data
,
1308 char **buf_ch
, size_t avail_size
,
1309 const char **out_fmt_ch
, va_list *args
)
1311 const char *fmt_ch
= *out_fmt_ch
;
1312 bool extended
= false;
1314 char *prefix_ch
= prefix
;
1320 if (*fmt_ch
== 'u') {
1322 obj
= va_arg(*args
, void *);
1323 format_uuid(buf_ch
, obj
);
1327 if (*fmt_ch
== '[') {
1332 if (*fmt_ch
== ']') {
1338 *prefix_ch
= *fmt_ch
;
1346 if (*fmt_ch
== '+') {
1351 obj
= va_arg(*args
, void *);
1352 BUF_APPEND("%saddr=%p", prefix
, obj
);
1360 format_field_class(buf_ch
, extended
, prefix
, obj
);
1363 format_field(buf_ch
, extended
, prefix
, obj
);
1366 format_field_path(buf_ch
, extended
, prefix
, obj
);
1369 format_event_class(buf_ch
, extended
, prefix
, obj
);
1372 format_event(buf_ch
, extended
, prefix
, obj
);
1375 format_stream_class(buf_ch
, extended
, prefix
, obj
);
1378 format_stream(buf_ch
, extended
, prefix
, obj
);
1381 format_packet(buf_ch
, extended
, prefix
, obj
);
1384 format_trace(buf_ch
, extended
, prefix
, obj
);
1387 format_trace_class(buf_ch
, extended
, prefix
, obj
);
1390 format_clock_class(buf_ch
, extended
, prefix
, obj
);
1393 format_clock_snapshot(buf_ch
, extended
, prefix
, obj
);
1396 format_value(buf_ch
, extended
, prefix
, obj
);
1399 format_message(buf_ch
, extended
, prefix
, obj
);
1402 format_message_iterator(buf_ch
, extended
, prefix
, obj
);
1405 format_component_class(buf_ch
, extended
, prefix
, obj
);
1408 format_component(buf_ch
, extended
, prefix
, obj
);
1411 format_port(buf_ch
, extended
, prefix
, obj
);
1414 format_connection(buf_ch
, extended
, prefix
, obj
);
1417 format_plugin(buf_ch
, extended
, prefix
, obj
);
1420 format_graph(buf_ch
, extended
, prefix
, obj
);
1423 format_object_pool(buf_ch
, extended
, prefix
, obj
);
1426 format_object(buf_ch
, extended
, prefix
, obj
);
1429 format_error_cause(buf_ch
, extended
, prefix
, obj
);
1437 *out_fmt_ch
= fmt_ch
;
1440 void bt_lib_log(const char *func
, const char *file
, unsigned line
,
1441 int lvl
, const char *tag
, const char *fmt
, ...)
1446 va_start(args
, fmt
);
1447 bt_common_custom_vsnprintf(lib_logging_buf
, LIB_LOGGING_BUF_SIZE
, '!',
1448 handle_conversion_specifier_bt
, NULL
, fmt
, &args
);
1450 _bt_log_write_d(func
, file
, line
, lvl
, tag
, "%s", lib_logging_buf
);
1453 void bt_lib_maybe_log_and_append_cause(const char *func
, const char *file
,
1454 unsigned line
, int lvl
, const char *tag
,
1455 const char *fmt
, ...)
1458 bt_current_thread_error_append_cause_status status
;
1461 va_start(args
, fmt
);
1462 bt_common_custom_vsnprintf(lib_logging_buf
, LIB_LOGGING_BUF_SIZE
, '!',
1463 handle_conversion_specifier_bt
, NULL
, fmt
, &args
);
1466 /* Log conditionally, but always append the error cause */
1467 if (BT_LOG_ON(lvl
)) {
1468 _bt_log_write_d(func
, file
, line
, lvl
, tag
, "%s",
1472 status
= bt_current_thread_error_append_cause_from_unknown(
1473 "Babeltrace library", file
, line
, "%s", lib_logging_buf
);
1476 * Worst case: this error cause is not appended to the
1477 * current thread's error.
1479 * We can accept this as it's an almost impossible
1480 * scenario and returning an error here would mean you
1481 * need to check the return value of each
1482 * BT_LIB_LOG*_APPEND_CAUSE() macro and that would be
1485 BT_LOGE("Cannot append error cause to current thread's "
1486 "error object: status=%s",
1487 bt_common_func_status_string(status
));