lib: introduce bt_message_iterator_class
[babeltrace.git] / src / lib / lib-logging.c
CommitLineData
476ef981
PP
1/*
2 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
20 * SOFTWARE.
21 */
22
3cd4c495
PP
23#define BT_LOG_TAG "LIB/LIB-LOGGING"
24#include "lib/logging.h"
3dca2276 25
476ef981
PP
26#include <stdarg.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <inttypes.h>
c4f23e30 31#include <stdbool.h>
476ef981 32#include <stdint.h>
476ef981
PP
33#include <wchar.h>
34#include <glib.h>
578e048b 35#include "common/common.h"
6162e6b7 36#include "common/uuid.h"
4fa90f32 37#include <babeltrace2/babeltrace.h>
578e048b 38
51375aa9
PP
39#include "logging.h"
40#include "assert-pre.h"
41#include "assert-post.h"
42#include "value.h"
6769570a 43#include "integer-range-set.h"
51375aa9 44#include "object-pool.h"
b70d57a1 45#include "graph/interrupter.h"
578e048b 46#include "graph/component-class.h"
578e048b
MJ
47#include "graph/component-filter.h"
48#include "graph/component.h"
49#include "graph/component-sink.h"
50#include "graph/component-source.h"
51#include "graph/connection.h"
52#include "graph/graph.h"
53#include "graph/message/discarded-items.h"
54#include "graph/message/event.h"
55#include "graph/message/iterator.h"
56#include "graph/message/message.h"
57#include "graph/message/message-iterator-inactivity.h"
58#include "graph/message/packet.h"
578e048b
MJ
59#include "graph/message/stream.h"
60#include "graph/port.h"
578e048b
MJ
61#include "plugin/plugin.h"
62#include "plugin/plugin-so.h"
63#include "trace-ir/clock-class.h"
64#include "trace-ir/clock-snapshot.h"
65#include "trace-ir/event-class.h"
66#include "trace-ir/event.h"
67#include "trace-ir/field-class.h"
68#include "trace-ir/field.h"
69#include "trace-ir/field-path.h"
70#include "trace-ir/packet.h"
71#include "trace-ir/stream-class.h"
72#include "trace-ir/stream.h"
73#include "trace-ir/trace-class.h"
74#include "trace-ir/trace.h"
75#include "trace-ir/utils.h"
553c4bab
PP
76#include "error.h"
77#include "assert-pre.h"
476ef981
PP
78
79#define LIB_LOGGING_BUF_SIZE (4096 * 4)
80
c9fe7f23 81static __thread char lib_logging_buf[LIB_LOGGING_BUF_SIZE];
476ef981
PP
82
83#define BUF_APPEND(_fmt, ...) \
84 do { \
85 int _count; \
86 size_t _size = LIB_LOGGING_BUF_SIZE - \
87 (size_t) (*buf_ch - lib_logging_buf); \
88 _count = snprintf(*buf_ch, _size, (_fmt), __VA_ARGS__); \
f6ccaed9 89 BT_ASSERT(_count >= 0); \
476ef981
PP
90 *buf_ch += MIN(_count, _size); \
91 if (*buf_ch >= lib_logging_buf + LIB_LOGGING_BUF_SIZE - 1) { \
92 return; \
93 } \
94 } while (0)
95
96#define BUF_APPEND_UUID(_uuid) \
44c440bc
PP
97 do { \
98 BUF_APPEND(", %suuid=", prefix); \
99 format_uuid(buf_ch, (_uuid)); \
100 } while (0)
476ef981
PP
101
102#define PRFIELD(_expr) prefix, (_expr)
103
d94d92ac
PP
104#define PRFIELD_GSTRING(_expr) PRFIELD((_expr) ? (_expr)->str : NULL)
105
b293c529 106#define TMP_PREFIX_LEN 128
476ef981
PP
107#define SET_TMP_PREFIX(_prefix2) \
108 do { \
b293c529
PP
109 int snprintf_ret = \
110 snprintf(tmp_prefix, TMP_PREFIX_LEN - 1, "%s%s", \
111 prefix, (_prefix2)); \
112 \
113 if (snprintf_ret < 0 || snprintf_ret >= TMP_PREFIX_LEN - 1) { \
498e7994 114 bt_common_abort(); \
b293c529
PP
115 } \
116 \
eea66fe0 117 tmp_prefix[TMP_PREFIX_LEN - 1] = '\0'; \
476ef981
PP
118 } while (0)
119
120static inline void format_component(char **buf_ch, bool extended,
862ca4ed 121 const char *prefix, const struct bt_component *component);
476ef981
PP
122
123static inline void format_port(char **buf_ch, bool extended,
862ca4ed 124 const char *prefix, const struct bt_port *port);
476ef981
PP
125
126static inline void format_connection(char **buf_ch, bool extended,
862ca4ed 127 const char *prefix, const struct bt_connection *connection);
476ef981 128
605e1019
PP
129static inline void format_clock_snapshot(char **buf_ch, bool extended,
130 const char *prefix, const struct bt_clock_snapshot *clock_snapshot);
e22b45d0 131
44c440bc 132static inline void format_field_path(char **buf_ch, bool extended,
862ca4ed 133 const char *prefix, const struct bt_field_path *field_path);
44c440bc 134
3fea54f6 135static inline void format_object(char **buf_ch, bool extended,
862ca4ed 136 const char *prefix, const struct bt_object *obj)
476ef981 137{
3fea54f6 138 BUF_APPEND(", %sref-count=%llu", prefix, obj->ref_count);
476ef981
PP
139}
140
44c440bc 141static inline void format_uuid(char **buf_ch, bt_uuid uuid)
e22b45d0 142{
6162e6b7 143 BUF_APPEND("\"" BT_UUID_FMT "\"", BT_UUID_FMT_VALUES(uuid));
e22b45d0
PP
144}
145
312c056a 146static inline void format_object_pool(char **buf_ch, bool extended,
862ca4ed 147 const char *prefix, const struct bt_object_pool *pool)
312c056a
PP
148{
149 BUF_APPEND(", %ssize=%zu", PRFIELD(pool->size));
150
151 if (pool->objects) {
152 BUF_APPEND(", %scap=%u", PRFIELD(pool->objects->len));
153 }
154}
155
5cd6d0e5 156static inline void format_integer_field_class(char **buf_ch,
44c440bc 157 bool extended, const char *prefix,
862ca4ed 158 const struct bt_field_class *field_class)
44c440bc 159{
862ca4ed
PP
160 const struct bt_field_class_integer *int_fc =
161 (const void *) field_class;
44c440bc
PP
162
163 BUF_APPEND(", %srange-size=%" PRIu64 ", %sbase=%s",
5cd6d0e5
PP
164 PRFIELD(int_fc->range),
165 PRFIELD(bt_common_field_class_integer_preferred_display_base_string(int_fc->base)));
44c440bc
PP
166}
167
5cd6d0e5 168static inline void format_array_field_class(char **buf_ch,
44c440bc 169 bool extended, const char *prefix,
862ca4ed 170 const struct bt_field_class *field_class)
44c440bc 171{
862ca4ed
PP
172 const struct bt_field_class_array *array_fc =
173 (const void *) field_class;
44c440bc 174
864cad70 175 BUF_APPEND(", %selement-fc-addr=%p, %selement-fc-type=%s",
5cd6d0e5 176 PRFIELD(array_fc->element_fc),
864cad70 177 PRFIELD(bt_common_field_class_type_string(array_fc->element_fc->type)));
44c440bc
PP
178}
179
5cd6d0e5 180static inline void format_field_class(char **buf_ch, bool extended,
862ca4ed 181 const char *prefix, const struct bt_field_class *field_class)
476ef981 182{
879e14e8 183 char tmp_prefix[TMP_PREFIX_LEN];
44c440bc 184
864cad70
PP
185 BUF_APPEND(", %stype=%s",
186 PRFIELD(bt_common_field_class_type_string(field_class->type)));
476ef981
PP
187
188 if (extended) {
5cd6d0e5 189 BUF_APPEND(", %sis-frozen=%d", PRFIELD(field_class->frozen));
862ca4ed
PP
190 BUF_APPEND(", %sis-part-of-trace-class=%d",
191 PRFIELD(field_class->part_of_trace_class));
476ef981
PP
192 } else {
193 return;
194 }
195
864cad70 196 switch (field_class->type) {
1094efa4
PP
197 case BT_FIELD_CLASS_TYPE_BIT_ARRAY:
198 {
199 const struct bt_field_class_bit_array *ba_fc =
200 (const void *) field_class;
201
202 BUF_APPEND(", %slength=%" PRIu64, PRFIELD(ba_fc->length));
203 break;
204 }
864cad70
PP
205 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
206 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
476ef981 207 {
5cd6d0e5 208 format_integer_field_class(buf_ch, extended, prefix, field_class);
476ef981
PP
209 break;
210 }
864cad70
PP
211 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
212 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
476ef981 213 {
862ca4ed
PP
214 const struct bt_field_class_enumeration *enum_fc =
215 (const void *) field_class;
476ef981 216
5cd6d0e5 217 format_integer_field_class(buf_ch, extended, prefix, field_class);
476ef981 218 BUF_APPEND(", %smapping-count=%u",
5cd6d0e5 219 PRFIELD(enum_fc->mappings->len));
476ef981
PP
220 break;
221 }
864cad70 222 case BT_FIELD_CLASS_TYPE_STRUCTURE:
476ef981 223 {
862ca4ed
PP
224 const struct bt_field_class_structure *struct_fc =
225 (const void *) field_class;
44c440bc 226
5cd6d0e5 227 if (struct_fc->common.named_fcs) {
44c440bc 228 BUF_APPEND(", %smember-count=%u",
5cd6d0e5 229 PRFIELD(struct_fc->common.named_fcs->len));
44c440bc 230 }
476ef981 231
476ef981
PP
232 break;
233 }
864cad70 234 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
476ef981 235 {
9c08c816 236 const struct bt_field_class_array_static *array_fc =
862ca4ed 237 (const void *) field_class;
476ef981 238
5cd6d0e5
PP
239 format_array_field_class(buf_ch, extended, prefix, field_class);
240 BUF_APPEND(", %slength=%" PRIu64, PRFIELD(array_fc->length));
476ef981
PP
241 break;
242 }
81b8fa44
PP
243 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD:
244 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD:
476ef981 245 {
9c08c816 246 const struct bt_field_class_array_dynamic *array_fc =
862ca4ed 247 (const void *) field_class;
44c440bc 248
5cd6d0e5 249 format_array_field_class(buf_ch, extended, prefix, field_class);
44c440bc 250
5cd6d0e5
PP
251 if (array_fc->length_fc) {
252 SET_TMP_PREFIX("length-fc-");
253 format_field_class(buf_ch, extended, tmp_prefix,
254 array_fc->length_fc);
44c440bc
PP
255 }
256
5cd6d0e5 257 if (array_fc->length_field_path) {
44c440bc
PP
258 SET_TMP_PREFIX("length-field-path-");
259 format_field_path(buf_ch, extended, tmp_prefix,
5cd6d0e5 260 array_fc->length_field_path);
44c440bc 261 }
476ef981 262
476ef981
PP
263 break;
264 }
de821fe5
PP
265 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD:
266 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD:
267 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
268 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
b38aea74
PP
269 {
270 const struct bt_field_class_option *opt_fc =
271 (const void *) field_class;
272
273 BUF_APPEND(", %scontent-fc-addr=%p, %scontent-fc-type=%s",
274 PRFIELD(opt_fc->content_fc),
275 PRFIELD(bt_common_field_class_type_string(opt_fc->content_fc->type)));
276
0aa006b7 277 if (field_class->type !=
de821fe5
PP
278 BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD) {
279 const struct bt_field_class_option_with_selector_field *opt_with_sel_fc =
0aa006b7
PP
280 (const void *) field_class;
281
282 if (opt_with_sel_fc->selector_fc) {
283 SET_TMP_PREFIX("selector-fc-");
284 format_field_class(buf_ch, extended, tmp_prefix,
285 opt_with_sel_fc->selector_fc);
286 }
287
288 if (opt_with_sel_fc->selector_field_path) {
289 SET_TMP_PREFIX("selector-field-path-");
290 format_field_path(buf_ch, extended, tmp_prefix,
291 opt_with_sel_fc->selector_field_path);
292 }
b38aea74
PP
293 }
294
295 break;
296 }
de821fe5
PP
297 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD:
298 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
299 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
476ef981 300 {
862ca4ed
PP
301 const struct bt_field_class_variant *var_fc =
302 (const void *) field_class;
44c440bc 303
5cd6d0e5 304 if (var_fc->common.named_fcs) {
44c440bc 305 BUF_APPEND(", %soption-count=%u",
5cd6d0e5 306 PRFIELD(var_fc->common.named_fcs->len));
44c440bc
PP
307 }
308
de821fe5
PP
309 if (field_class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD ||
310 field_class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD) {
311 const struct bt_field_class_variant_with_selector_field *var_with_sel_fc =
45c51519
PP
312 (const void *) var_fc;
313
314 if (var_with_sel_fc->selector_fc) {
315 SET_TMP_PREFIX("selector-fc-");
316 format_field_class(buf_ch, extended, tmp_prefix,
317 var_with_sel_fc->selector_fc);
318 }
319
320 if (var_with_sel_fc->selector_field_path) {
321 SET_TMP_PREFIX("selector-field-path-");
322 format_field_path(buf_ch, extended, tmp_prefix,
323 var_with_sel_fc->selector_field_path);
324 }
44c440bc 325 }
476ef981 326
476ef981
PP
327 break;
328 }
329 default:
330 break;
331 }
332}
333
cb6f1f7d 334static inline void format_field_integer_extended(char **buf_ch,
862ca4ed 335 const char *prefix, const struct bt_field *field)
476ef981 336{
862ca4ed
PP
337 const struct bt_field_integer *integer = (void *) field;
338 const struct bt_field_class_integer *field_class =
339 (void *) field->class;
476ef981
PP
340 const char *fmt = NULL;
341
5cd6d0e5 342 BT_ASSERT(field_class);
312c056a 343
5cd6d0e5 344 if (field_class->base == BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL) {
476ef981 345 fmt = ", %svalue=%" PRIo64;
5cd6d0e5 346 } else if (field_class->base == BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL) {
476ef981
PP
347 fmt = ", %svalue=%" PRIx64;
348 }
349
864cad70
PP
350 if (field_class->common.type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
351 field_class->common.type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION) {
476ef981
PP
352 if (!fmt) {
353 fmt = ", %svalue=%" PRId64;
354 }
355
44c440bc 356 BUF_APPEND(fmt, PRFIELD(integer->value.i));
476ef981
PP
357 } else {
358 if (!fmt) {
359 fmt = ", %svalue=%" PRIu64;
360 }
361
44c440bc 362 BUF_APPEND(fmt, PRFIELD(integer->value.u));
476ef981
PP
363 }
364}
365
cb6f1f7d 366static inline void format_field(char **buf_ch, bool extended,
862ca4ed 367 const char *prefix, const struct bt_field *field)
476ef981 368{
44c440bc 369 BUF_APPEND(", %sis-set=%d", PRFIELD(field->is_set));
476ef981
PP
370
371 if (extended) {
372 BUF_APPEND(", %sis-frozen=%d", PRFIELD(field->frozen));
373 }
374
864cad70 375 BUF_APPEND(", %sclass-addr=%p", PRFIELD(field->class));
312c056a 376
5cd6d0e5 377 if (!field->class) {
312c056a
PP
378 return;
379 }
380
864cad70
PP
381 BUF_APPEND(", %sclass-type=%s",
382 PRFIELD(bt_common_field_class_type_string(field->class->type)));
476ef981 383
44c440bc 384 if (!extended || !field->is_set) {
476ef981
PP
385 return;
386 }
387
864cad70 388 switch (field->class->type) {
5cebbe7f
PP
389 case BT_FIELD_CLASS_TYPE_BOOL:
390 {
391 const struct bt_field_bool *bool_field = (const void *) field;
392
393 BUF_APPEND(", %svalue=%d", PRFIELD(bool_field->value));
394 break;
395 }
1094efa4
PP
396 case BT_FIELD_CLASS_TYPE_BIT_ARRAY:
397 {
398 const struct bt_field_bit_array *ba_field = (const void *) field;
399
400 BUF_APPEND(", %svalue-as-int=%" PRIx64,
401 PRFIELD(ba_field->value_as_int));
402 break;
403 }
864cad70
PP
404 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
405 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
406 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
407 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
476ef981 408 {
cb6f1f7d 409 format_field_integer_extended(buf_ch, prefix, field);
476ef981
PP
410 break;
411 }
fe4df857
FD
412 case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL:
413 case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL:
476ef981 414 {
862ca4ed 415 const struct bt_field_real *real_field = (const void *) field;
476ef981 416
44c440bc 417 BUF_APPEND(", %svalue=%f", PRFIELD(real_field->value));
476ef981
PP
418 break;
419 }
864cad70 420 case BT_FIELD_CLASS_TYPE_STRING:
476ef981 421 {
862ca4ed 422 const struct bt_field_string *str = (const void *) field;
476ef981 423
4d4b475d
PP
424 if (str->buf) {
425 BT_ASSERT(str->buf->data);
312c056a 426 BUF_APPEND(", %spartial-value=\"%.32s\"",
4d4b475d 427 PRFIELD(str->buf->data));
312c056a 428 }
44c440bc 429
476ef981
PP
430 break;
431 }
864cad70 432 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
81b8fa44
PP
433 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD:
434 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD:
476ef981 435 {
862ca4ed 436 const struct bt_field_array *array_field = (const void *) field;
476ef981 437
44c440bc 438 BUF_APPEND(", %slength=%" PRIu64, PRFIELD(array_field->length));
312c056a 439
44c440bc 440 if (array_field->fields) {
312c056a 441 BUF_APPEND(", %sallocated-length=%u",
44c440bc 442 PRFIELD(array_field->fields->len));
312c056a 443 }
44c440bc 444
476ef981
PP
445 break;
446 }
de821fe5
PP
447 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD:
448 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
449 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
476ef981 450 {
862ca4ed 451 const struct bt_field_variant *var_field = (const void *) field;
312c056a 452
44c440bc
PP
453 BUF_APPEND(", %sselected-field-index=%" PRIu64,
454 PRFIELD(var_field->selected_index));
312c056a
PP
455 break;
456 }
457 default:
458 break;
459 }
3dca2276
PP
460}
461
476ef981 462static inline void format_field_path(char **buf_ch, bool extended,
862ca4ed 463 const char *prefix, const struct bt_field_path *field_path)
476ef981
PP
464{
465 uint64_t i;
466
66ddcddf
PP
467 if (field_path->items) {
468 BT_ASSERT(field_path->items);
469 BUF_APPEND(", %sitem-count=%u",
470 PRFIELD(field_path->items->len));
312c056a 471 }
476ef981 472
66ddcddf 473 if (!extended || !field_path->items) {
476ef981
PP
474 return;
475 }
476
cb6f1f7d
PP
477 BUF_APPEND(", %spath=[%s",
478 PRFIELD(bt_common_scope_string(field_path->root)));
476ef981 479
66ddcddf
PP
480 for (i = 0; i < bt_field_path_get_item_count(field_path); i++) {
481 const struct bt_field_path_item *fp_item =
482 bt_field_path_borrow_item_by_index_const(field_path, i);
483
484 switch (bt_field_path_item_get_type(fp_item)) {
485 case BT_FIELD_PATH_ITEM_TYPE_INDEX:
486 BUF_APPEND(", %" PRIu64,
487 bt_field_path_item_index_get_index(fp_item));
488 break;
489 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT:
490 BUF_APPEND("%s", ", <CUR>");
491 break;
492 default:
498e7994 493 bt_common_abort();
66ddcddf 494 }
476ef981
PP
495 }
496
497 BUF_APPEND("%s", "]");
498}
499
862ca4ed
PP
500static inline void format_trace_class(char **buf_ch, bool extended,
501 const char *prefix, const struct bt_trace_class *trace_class)
476ef981 502{
476ef981
PP
503 if (!extended) {
504 return;
505 }
506
862ca4ed 507 BUF_APPEND(", %sis-frozen=%d", PRFIELD(trace_class->frozen));
476ef981 508
862ca4ed 509 if (trace_class->stream_classes) {
312c056a 510 BUF_APPEND(", %sstream-class-count=%u",
862ca4ed 511 PRFIELD(trace_class->stream_classes->len));
312c056a
PP
512 }
513
83ebb7f1 514 BUF_APPEND(", %sassigns-auto-sc-id=%d",
862ca4ed 515 PRFIELD(trace_class->assigns_automatic_stream_class_id));
862ca4ed
PP
516}
517
518static inline void format_trace(char **buf_ch, bool extended,
519 const char *prefix, const struct bt_trace *trace)
520{
879e14e8 521 char tmp_prefix[TMP_PREFIX_LEN];
862ca4ed
PP
522
523 if (trace->name.value) {
524 BUF_APPEND(", %sname=\"%s\"", PRFIELD(trace->name.value));
525 }
526
527 if (!extended) {
528 return;
529 }
530
335a2da5
PP
531 if (trace->uuid.value) {
532 BUF_APPEND_UUID(trace->uuid.value);
533 }
534
862ca4ed
PP
535 BUF_APPEND(", %sis-frozen=%d", PRFIELD(trace->frozen));
536
312c056a
PP
537 if (trace->streams) {
538 BUF_APPEND(", %sstream-count=%u",
539 PRFIELD(trace->streams->len));
540 }
541
862ca4ed
PP
542 if (!trace->class) {
543 return;
544 }
545
546 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace->class));
547 SET_TMP_PREFIX("trace-class-");
548 format_trace_class(buf_ch, false, tmp_prefix, trace->class);
3dca2276
PP
549}
550
cb6f1f7d 551static inline void format_stream_class(char **buf_ch, bool extended,
862ca4ed
PP
552 const char *prefix,
553 const struct bt_stream_class *stream_class)
3dca2276 554{
862ca4ed 555 const struct bt_trace_class *trace_class;
879e14e8 556 char tmp_prefix[TMP_PREFIX_LEN];
476ef981 557
44c440bc 558 BUF_APPEND(", %sid=%" PRIu64, PRFIELD(stream_class->id));
476ef981 559
44c440bc
PP
560 if (stream_class->name.value) {
561 BUF_APPEND(", %sname=\"%s\"",
562 PRFIELD(stream_class->name.value));
476ef981
PP
563 }
564
565 if (!extended) {
566 return;
567 }
568
569 BUF_APPEND(", %sis-frozen=%d", PRFIELD(stream_class->frozen));
312c056a
PP
570
571 if (stream_class->event_classes) {
572 BUF_APPEND(", %sevent-class-count=%u",
573 PRFIELD(stream_class->event_classes->len));
574 }
575
5cd6d0e5 576 BUF_APPEND(", %spacket-context-fc-addr=%p, "
83ebb7f1 577 "%sevent-common-context-fc-addr=%p",
5cd6d0e5 578 PRFIELD(stream_class->packet_context_fc),
5cd6d0e5 579 PRFIELD(stream_class->event_common_context_fc));
862ca4ed
PP
580 trace_class = bt_stream_class_borrow_trace_class_inline(stream_class);
581 if (!trace_class) {
476ef981
PP
582 return;
583 }
584
7fe92073 585 BUF_APPEND(", %sassigns-auto-ec-id=%d, %sassigns-auto-stream-id=%d, "
26fc5aed
PP
586 "%ssupports-packets=%d, "
587 "%spackets-have-begin-default-cs=%d, "
588 "%spackets-have-end-default-cs=%d, "
2e90378a
PP
589 "%ssupports-discarded-events=%d, "
590 "%sdiscarded-events-have-default-cs=%d, "
591 "%ssupports-discarded-packets=%d, "
592 "%sdiscarded-packets-have-default-cs=%d",
44c440bc 593 PRFIELD(stream_class->assigns_automatic_event_class_id),
7fe92073 594 PRFIELD(stream_class->assigns_automatic_stream_id),
26fc5aed 595 PRFIELD(stream_class->supports_packets),
9b24b6aa
PP
596 PRFIELD(stream_class->packets_have_beginning_default_clock_snapshot),
597 PRFIELD(stream_class->packets_have_end_default_clock_snapshot),
2e90378a
PP
598 PRFIELD(stream_class->supports_discarded_events),
599 PRFIELD(stream_class->discarded_events_have_default_clock_snapshots),
600 PRFIELD(stream_class->supports_discarded_packets),
601 PRFIELD(stream_class->discarded_packets_have_default_clock_snapshots));
862ca4ed
PP
602 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace_class));
603 SET_TMP_PREFIX("trace-class-");
604 format_trace_class(buf_ch, false, tmp_prefix, trace_class);
5c563278 605 SET_TMP_PREFIX("pcf-pool-");
9a6502b6 606 format_object_pool(buf_ch, extended, tmp_prefix,
5c563278 607 &stream_class->packet_context_field_pool);
3dca2276
PP
608}
609
cb6f1f7d 610static inline void format_event_class(char **buf_ch, bool extended,
862ca4ed 611 const char *prefix, const struct bt_event_class *event_class)
3dca2276 612{
862ca4ed
PP
613 const struct bt_stream_class *stream_class;
614 const struct bt_trace_class *trace_class;
879e14e8 615 char tmp_prefix[TMP_PREFIX_LEN];
476ef981 616
44c440bc 617 BUF_APPEND(", %sid=%" PRIu64, PRFIELD(event_class->id));
476ef981 618
44c440bc 619 if (event_class->name.value) {
cb6f1f7d 620 BUF_APPEND(", %sname=\"%s\"",
44c440bc 621 PRFIELD(event_class->name.value));
476ef981
PP
622 }
623
624 if (!extended) {
625 return;
626 }
627
44c440bc
PP
628 BUF_APPEND(", %sis-frozen=%d", PRFIELD(event_class->frozen));
629
630 if (event_class->log_level.base.avail) {
631 BUF_APPEND(", %slog-level=%s",
632 PRFIELD(bt_common_event_class_log_level_string(
633 (int) event_class->log_level.value)));
634 }
476ef981 635
44c440bc 636 if (event_class->emf_uri.value) {
476ef981 637 BUF_APPEND(", %semf-uri=\"%s\"",
44c440bc 638 PRFIELD(event_class->emf_uri.value));
476ef981
PP
639 }
640
5cd6d0e5
PP
641 BUF_APPEND(", %sspecific-context-fc-addr=%p, %spayload-fc-addr=%p",
642 PRFIELD(event_class->specific_context_fc),
643 PRFIELD(event_class->payload_fc));
476ef981 644
862ca4ed 645 stream_class = bt_event_class_borrow_stream_class_const(event_class);
476ef981
PP
646 if (!stream_class) {
647 return;
648 }
649
650 BUF_APPEND(", %sstream-class-addr=%p", PRFIELD(stream_class));
651 SET_TMP_PREFIX("stream-class-");
cb6f1f7d 652 format_stream_class(buf_ch, false, tmp_prefix, stream_class);
862ca4ed
PP
653 trace_class = bt_stream_class_borrow_trace_class_inline(stream_class);
654 if (!trace_class) {
476ef981
PP
655 return;
656 }
657
862ca4ed
PP
658 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace_class));
659 SET_TMP_PREFIX("trace-class-");
660 format_trace_class(buf_ch, false, tmp_prefix, trace_class);
312c056a 661 SET_TMP_PREFIX("event-pool-");
9a6502b6
PP
662 format_object_pool(buf_ch, extended, tmp_prefix,
663 &event_class->event_pool);
3dca2276
PP
664}
665
cb6f1f7d 666static inline void format_stream(char **buf_ch, bool extended,
862ca4ed 667 const char *prefix, const struct bt_stream *stream)
3dca2276 668{
862ca4ed
PP
669 const struct bt_stream_class *stream_class;
670 const struct bt_trace_class *trace_class = NULL;
671 const struct bt_trace *trace = NULL;
879e14e8 672 char tmp_prefix[TMP_PREFIX_LEN];
476ef981 673
44c440bc 674 BUF_APPEND(", %sid=%" PRIu64, PRFIELD(stream->id));
476ef981 675
44c440bc
PP
676 if (stream->name.value) {
677 BUF_APPEND(", %sname=\"%s\"", PRFIELD(stream->name.value));
476ef981
PP
678 }
679
476ef981
PP
680 if (!extended) {
681 return;
682 }
683
862ca4ed
PP
684 stream_class = bt_stream_borrow_class_const(stream);
685 if (stream_class) {
686 BUF_APPEND(", %sstream-class-addr=%p", PRFIELD(stream_class));
687 SET_TMP_PREFIX("stream-class-");
688 format_stream_class(buf_ch, false, tmp_prefix, stream_class);
689 trace_class = bt_stream_class_borrow_trace_class_inline(stream_class);
476ef981
PP
690 }
691
862ca4ed
PP
692 if (trace_class) {
693 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace_class));
694 SET_TMP_PREFIX("trace-class-");
695 format_trace_class(buf_ch, false, tmp_prefix, trace_class);
696 }
697
698 trace = bt_stream_borrow_trace_inline(stream);
699 if (trace) {
700 BUF_APPEND(", %strace-addr=%p", PRFIELD(trace));
701 SET_TMP_PREFIX("trace-");
702 format_trace(buf_ch, false, tmp_prefix, trace);
476ef981
PP
703 }
704
312c056a 705 SET_TMP_PREFIX("packet-pool-");
9a6502b6 706 format_object_pool(buf_ch, extended, tmp_prefix, &stream->packet_pool);
3dca2276
PP
707}
708
476ef981 709static inline void format_packet(char **buf_ch, bool extended,
862ca4ed 710 const char *prefix, const struct bt_packet *packet)
476ef981 711{
862ca4ed
PP
712 const struct bt_stream *stream;
713 const struct bt_trace_class *trace_class;
879e14e8 714 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
715
716 if (!extended) {
717 return;
718 }
719
83ebb7f1 720 BUF_APPEND(", %sis-frozen=%d, %scontext-field-addr=%p",
476ef981 721 PRFIELD(packet->frozen),
44c440bc 722 PRFIELD(packet->context_field ? packet->context_field->field : NULL));
862ca4ed 723 stream = bt_packet_borrow_stream_const(packet);
476ef981
PP
724 if (!stream) {
725 return;
726 }
727
728 BUF_APPEND(", %sstream-addr=%p", PRFIELD(stream));
729 SET_TMP_PREFIX("stream-");
730 format_stream(buf_ch, false, tmp_prefix, stream);
862ca4ed
PP
731 trace_class = (const struct bt_trace_class *) bt_object_borrow_parent(&stream->base);
732 if (!trace_class) {
476ef981
PP
733 return;
734 }
735
862ca4ed
PP
736 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace_class));
737 SET_TMP_PREFIX("trace-class-");
738 format_trace_class(buf_ch, false, tmp_prefix, trace_class);
476ef981
PP
739}
740
cb6f1f7d 741static inline void format_event(char **buf_ch, bool extended,
862ca4ed 742 const char *prefix, const struct bt_event *event)
476ef981 743{
862ca4ed
PP
744 const struct bt_trace_class *trace_class;
745 const struct bt_stream_class *stream_class;
879e14e8 746 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
747
748 if (!extended) {
749 return;
750 }
751
83ebb7f1 752 BUF_APPEND(", %sis-frozen=%d, "
44c440bc 753 "%scommon-context-field-addr=%p, "
d94d92ac 754 "%sspecific-context-field-addr=%p, "
44c440bc 755 "%spayload-field-addr=%p, ",
476ef981 756 PRFIELD(event->frozen),
44c440bc
PP
757 PRFIELD(event->common_context_field),
758 PRFIELD(event->specific_context_field),
3dca2276
PP
759 PRFIELD(event->payload_field));
760 BUF_APPEND(", %sevent-class-addr=%p", PRFIELD(event->class));
312c056a
PP
761
762 if (!event->class) {
763 return;
764 }
765
476ef981 766 SET_TMP_PREFIX("event-class-");
cb6f1f7d
PP
767 format_event_class(buf_ch, false, tmp_prefix, event->class);
768 stream_class = bt_event_class_borrow_stream_class(event->class);
476ef981
PP
769 if (stream_class) {
770 BUF_APPEND(", %sstream-class-addr=%p", PRFIELD(stream_class));
771 SET_TMP_PREFIX("stream-class-");
cb6f1f7d 772 format_stream_class(buf_ch, false, tmp_prefix,
3dca2276 773 stream_class);
476ef981 774
862ca4ed
PP
775 trace_class = bt_stream_class_borrow_trace_class_inline(
776 stream_class);
777 if (trace_class) {
778 BUF_APPEND(", %strace-class-addr=%p",
779 PRFIELD(trace_class));
780 SET_TMP_PREFIX("trace-class-");
781 format_trace_class(buf_ch, false, tmp_prefix,
782 trace_class);
476ef981
PP
783 }
784 }
3dca2276 785
26fc5aed
PP
786 if (event->stream) {
787 BUF_APPEND(", %sstream-addr=%p", PRFIELD(event->stream));
788 SET_TMP_PREFIX("stream-");
789 format_stream(buf_ch, false, tmp_prefix, event->stream);
476ef981
PP
790 }
791
26fc5aed
PP
792 if (event->packet) {
793 BUF_APPEND(", %spacket-addr=%p", PRFIELD(event->packet));
794 SET_TMP_PREFIX("packet-");
795 format_packet(buf_ch, false, tmp_prefix, event->packet);
476ef981 796 }
476ef981
PP
797}
798
799static inline void format_clock_class(char **buf_ch, bool extended,
862ca4ed 800 const char *prefix, const struct bt_clock_class *clock_class)
476ef981 801{
879e14e8 802 char tmp_prefix[TMP_PREFIX_LEN];
312c056a 803
44c440bc
PP
804 if (clock_class->name.value) {
805 BUF_APPEND(", %sname=\"%s\"", PRFIELD(clock_class->name.value));
806 }
807
808 BUF_APPEND(", %sfreq=%" PRIu64, PRFIELD(clock_class->frequency));
476ef981
PP
809
810 if (!extended) {
811 return;
812 }
813
44c440bc
PP
814 if (clock_class->description.value) {
815 BUF_APPEND(", %spartial-descr=\"%.32s\"",
816 PRFIELD(clock_class->description.value));
817 }
818
819 if (clock_class->uuid.value) {
820 BUF_APPEND_UUID(clock_class->uuid.value);
476ef981
PP
821 }
822
823 BUF_APPEND(", %sis-frozen=%d, %sprecision=%" PRIu64 ", "
824 "%soffset-s=%" PRId64 ", "
5552377a 825 "%soffset-cycles=%" PRIu64 ", %sorigin-is-unix-epoch=%d, "
44c440bc 826 "%sbase-offset-ns=%" PRId64,
476ef981 827 PRFIELD(clock_class->frozen), PRFIELD(clock_class->precision),
44c440bc
PP
828 PRFIELD(clock_class->offset_seconds),
829 PRFIELD(clock_class->offset_cycles),
5552377a 830 PRFIELD(clock_class->origin_is_unix_epoch),
44c440bc 831 PRFIELD(clock_class->base_offset.value_ns));
312c056a 832
605e1019 833 SET_TMP_PREFIX("cs-pool-");
9a6502b6
PP
834 format_object_pool(buf_ch, extended, tmp_prefix,
835 &clock_class->cs_pool);
476ef981
PP
836}
837
605e1019
PP
838static inline void format_clock_snapshot(char **buf_ch, bool extended,
839 const char *prefix, const struct bt_clock_snapshot *clock_snapshot)
476ef981 840{
879e14e8 841 char tmp_prefix[TMP_PREFIX_LEN];
44c440bc 842 BUF_APPEND(", %svalue=%" PRIu64 ", %sns-from-origin=%" PRId64,
605e1019
PP
843 PRFIELD(clock_snapshot->value_cycles),
844 PRFIELD(clock_snapshot->ns_from_origin));
476ef981
PP
845
846 if (!extended) {
847 return;
848 }
849
605e1019 850 BUF_APPEND(", %sis-set=%d", PRFIELD(clock_snapshot->is_set));
d94d92ac 851
605e1019 852 if (clock_snapshot->clock_class) {
d94d92ac 853 BUF_APPEND(", %sclock-class-addr=%p",
605e1019 854 PRFIELD(clock_snapshot->clock_class));
d94d92ac
PP
855 SET_TMP_PREFIX("clock-class-");
856 format_clock_class(buf_ch, false, tmp_prefix,
605e1019 857 clock_snapshot->clock_class);
d94d92ac 858 }
476ef981
PP
859}
860
b70d57a1
PP
861static inline void format_interrupter(char **buf_ch, bool extended,
862 const char *prefix, const struct bt_interrupter *intr)
863{
864 BUF_APPEND(", %sis-set=%d", PRFIELD(intr->is_set));
865}
866
476ef981 867static inline void format_value(char **buf_ch, bool extended,
862ca4ed 868 const char *prefix, const struct bt_value *value)
476ef981
PP
869{
870 BUF_APPEND(", %stype=%s",
da91b29a 871 PRFIELD(bt_common_value_type_string(bt_value_get_type(value))));
476ef981
PP
872
873 if (!extended) {
874 return;
875 }
876
877 switch (bt_value_get_type(value)) {
878 case BT_VALUE_TYPE_BOOL:
879 {
601b0d3c 880 bt_bool val = bt_value_bool_get(value);
476ef981 881
476ef981
PP
882 BUF_APPEND(", %svalue=%d", PRFIELD(val));
883 break;
884 }
fdd3a2da 885 case BT_VALUE_TYPE_UNSIGNED_INTEGER:
476ef981 886 {
fdd3a2da 887 BUF_APPEND(", %svalue=%" PRIu64,
9c08c816 888 PRFIELD(bt_value_integer_unsigned_get(value)));
fdd3a2da
PP
889 break;
890 }
891 case BT_VALUE_TYPE_SIGNED_INTEGER:
892 {
893 BUF_APPEND(", %svalue=%" PRId64,
9c08c816 894 PRFIELD(bt_value_integer_signed_get(value)));
476ef981
PP
895 break;
896 }
a373bf69 897 case BT_VALUE_TYPE_REAL:
476ef981 898 {
601b0d3c 899 double val = bt_value_real_get(value);
476ef981 900
476ef981
PP
901 BUF_APPEND(", %svalue=%f", PRFIELD(val));
902 break;
903 }
904 case BT_VALUE_TYPE_STRING:
905 {
601b0d3c 906 const char *val = bt_value_string_get(value);
476ef981 907
476ef981
PP
908 BUF_APPEND(", %spartial-value=\"%.32s\"", PRFIELD(val));
909 break;
910 }
911 case BT_VALUE_TYPE_ARRAY:
912 {
f80e9ec1 913 uint64_t count = bt_value_array_get_length(value);
476ef981 914
476ef981
PP
915 BUF_APPEND(", %selement-count=%" PRId64, PRFIELD(count));
916 break;
917 }
918 case BT_VALUE_TYPE_MAP:
919 {
07208d85 920 int64_t count = bt_value_map_get_size(value);
476ef981 921
f6ccaed9 922 BT_ASSERT(count >= 0);
476ef981
PP
923 BUF_APPEND(", %selement-count=%" PRId64, PRFIELD(count));
924 break;
925 }
926 default:
927 break;
928 }
929}
930
6769570a
PP
931static inline void format_integer_range_set(char **buf_ch, bool extended,
932 const char *prefix,
933 const struct bt_integer_range_set *range_set)
934{
935 BUF_APPEND(", %srange-count=%u", PRFIELD(range_set->ranges->len));
936
937 if (!extended) {
938 return;
939 }
940
941 BUF_APPEND(", %sis-frozen=%d", PRFIELD(range_set->frozen));
942}
943
d6e69534
PP
944static inline void format_message(char **buf_ch, bool extended,
945 const char *prefix, const struct bt_message *msg)
476ef981 946{
879e14e8 947 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
948
949 BUF_APPEND(", %stype=%s",
d6e69534 950 PRFIELD(bt_message_type_string(msg->type)));
476ef981
PP
951
952 if (!extended) {
953 return;
954 }
955
5c563278 956 BUF_APPEND(", %sis-frozen=%d, %sgraph-addr=%p",
d6e69534 957 PRFIELD(msg->frozen), PRFIELD(msg->graph));
476ef981 958
d6e69534
PP
959 switch (msg->type) {
960 case BT_MESSAGE_TYPE_EVENT:
476ef981 961 {
d6e69534
PP
962 const struct bt_message_event *msg_event =
963 (const void *) msg;
476ef981 964
d6e69534 965 if (msg_event->event) {
d94d92ac 966 SET_TMP_PREFIX("event-");
5df26c89
PP
967 format_event(buf_ch, true, tmp_prefix,
968 msg_event->event);
d94d92ac
PP
969 }
970
8fc0367b
PP
971 if (msg_event->default_cs) {
972 SET_TMP_PREFIX("default-cs-");
973 format_clock_snapshot(buf_ch, true, tmp_prefix,
974 msg_event->default_cs);
975 }
976
476ef981
PP
977 break;
978 }
d6e69534 979 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
5df26c89 980 case BT_MESSAGE_TYPE_STREAM_END:
476ef981 981 {
5df26c89 982 const struct bt_message_stream *msg_stream = (const void *) msg;
476ef981 983
d6e69534 984 if (msg_stream->stream) {
d94d92ac 985 SET_TMP_PREFIX("stream-");
5df26c89
PP
986 format_stream(buf_ch, true, tmp_prefix,
987 msg_stream->stream);
d94d92ac
PP
988 }
989
5df26c89 990 BUF_APPEND(", %sdefault-cs-state=%s",
188edac1
SM
991 PRFIELD(bt_message_stream_clock_snapshot_state_string(
992 msg_stream->default_cs_state)));
476ef981 993
188edac1 994 if (msg_stream->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
5df26c89
PP
995 SET_TMP_PREFIX("default-cs-");
996 format_clock_snapshot(buf_ch, true, tmp_prefix,
188edac1 997 msg_stream->default_cs);
d94d92ac
PP
998 }
999
476ef981
PP
1000 break;
1001 }
5df26c89 1002 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
d6e69534 1003 case BT_MESSAGE_TYPE_PACKET_END:
476ef981 1004 {
5df26c89 1005 const struct bt_message_packet *msg_packet = (const void *) msg;
476ef981 1006
d6e69534 1007 if (msg_packet->packet) {
d94d92ac 1008 SET_TMP_PREFIX("packet-");
5df26c89
PP
1009 format_packet(buf_ch, true, tmp_prefix,
1010 msg_packet->packet);
d94d92ac
PP
1011 }
1012
8fc0367b
PP
1013 if (msg_packet->default_cs) {
1014 SET_TMP_PREFIX("default-cs-");
1015 format_clock_snapshot(buf_ch, true, tmp_prefix,
1016 msg_packet->default_cs);
1017 }
1018
476ef981
PP
1019 break;
1020 }
4c833281 1021 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
4237f1f2 1022 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
4c833281
PP
1023 {
1024 const struct bt_message_discarded_items *msg_disc_items =
1025 (const void *) msg;
1026
1027 if (msg_disc_items->stream) {
1028 SET_TMP_PREFIX("stream-");
1029 format_stream(buf_ch, true, tmp_prefix,
1030 msg_disc_items->stream);
1031 }
1032
1033 if (msg_disc_items->default_begin_cs) {
26fc5aed 1034 SET_TMP_PREFIX("begin-default-cs-");
4c833281
PP
1035 format_clock_snapshot(buf_ch, true, tmp_prefix,
1036 msg_disc_items->default_begin_cs);
1037 }
1038
1039 if (msg_disc_items->default_end_cs) {
26fc5aed 1040 SET_TMP_PREFIX("end-default-cs-");
4c833281
PP
1041 format_clock_snapshot(buf_ch, true, tmp_prefix,
1042 msg_disc_items->default_end_cs);
1043 }
1044
1045 if (msg_disc_items->count.base.avail) {
1046 BUF_APPEND(", %scount=%" PRIu64,
1047 PRFIELD(msg_disc_items->count.value));
1048 }
1049
1050 break;
1051 }
476ef981
PP
1052 default:
1053 break;
1054 }
1055}
1056
1057static inline void format_plugin_so_shared_lib_handle(char **buf_ch,
1058 const char *prefix,
862ca4ed 1059 const struct bt_plugin_so_shared_lib_handle *handle)
476ef981
PP
1060{
1061 BUF_APPEND(", %saddr=%p", PRFIELD(handle));
1062
1063 if (handle->path) {
d94d92ac 1064 BUF_APPEND(", %spath=\"%s\"", PRFIELD_GSTRING(handle->path));
476ef981
PP
1065 }
1066}
1067
1068static inline void format_component_class(char **buf_ch, bool extended,
862ca4ed
PP
1069 const char *prefix,
1070 const struct bt_component_class *comp_class)
476ef981 1071{
879e14e8 1072 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
1073
1074 BUF_APPEND(", %stype=%s, %sname=\"%s\"",
1075 PRFIELD(bt_component_class_type_string(comp_class->type)),
d94d92ac 1076 PRFIELD_GSTRING(comp_class->name));
476ef981
PP
1077
1078 if (comp_class->description) {
1079 BUF_APPEND(", %spartial-descr=\"%.32s\"",
d94d92ac 1080 PRFIELD_GSTRING(comp_class->description));
476ef981
PP
1081 }
1082
1083 if (!extended) {
1084 return;
1085 }
1086
1087 BUF_APPEND(", %sis-frozen=%d", PRFIELD(comp_class->frozen));
1088
1089 if (comp_class->so_handle) {
1090 SET_TMP_PREFIX("so-handle-");
1091 format_plugin_so_shared_lib_handle(buf_ch, tmp_prefix,
1092 comp_class->so_handle);
1093 }
1094}
1095
1096static inline void format_component(char **buf_ch, bool extended,
862ca4ed 1097 const char *prefix, const struct bt_component *component)
476ef981 1098{
879e14e8 1099 char tmp_prefix[TMP_PREFIX_LEN];
476ef981 1100
e874da19
PP
1101 BUF_APPEND(", %sname=\"%s\", %slog-level=%s",
1102 PRFIELD_GSTRING(component->name),
1103 PRFIELD(bt_common_logging_level_string(component->log_level)));
d94d92ac
PP
1104
1105 if (component->class) {
1106 SET_TMP_PREFIX("class-");
1107 format_component_class(buf_ch, extended, tmp_prefix,
1108 component->class);
1109 }
476ef981
PP
1110
1111 if (!extended) {
1112 return;
1113 }
1114
312c056a
PP
1115 if (component->input_ports) {
1116 BUF_APPEND(", %sinput-port-count=%u",
1117 PRFIELD(component->input_ports->len));
1118 }
1119
1120 if (component->output_ports) {
1121 BUF_APPEND(", %soutput-port-count=%u",
1122 PRFIELD(component->output_ports->len));
1123 }
476ef981
PP
1124}
1125
1126static inline void format_port(char **buf_ch, bool extended,
862ca4ed 1127 const char *prefix, const struct bt_port *port)
476ef981 1128{
879e14e8 1129 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
1130
1131 BUF_APPEND(", %stype=%s, %sname=\"%s\"",
1132 PRFIELD(bt_port_type_string(port->type)),
d94d92ac 1133 PRFIELD_GSTRING(port->name));
476ef981
PP
1134
1135 if (!extended) {
1136 return;
1137 }
1138
1139 if (port->connection) {
1140 SET_TMP_PREFIX("conn-");
1141 format_connection(buf_ch, false, tmp_prefix, port->connection);
1142 }
1143}
1144
1145static inline void format_connection(char **buf_ch, bool extended,
862ca4ed 1146 const char *prefix, const struct bt_connection *connection)
476ef981 1147{
879e14e8 1148 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
1149
1150 if (!extended) {
1151 return;
1152 }
1153
1154 if (connection->upstream_port) {
1155 SET_TMP_PREFIX("upstream-port-");
1156 format_port(buf_ch, false, tmp_prefix,
1157 connection->upstream_port);
1158 }
1159
1160 if (connection->downstream_port) {
1161 SET_TMP_PREFIX("downstream-port-");
1162 format_port(buf_ch, false, tmp_prefix,
1163 connection->downstream_port);
1164 }
1165}
1166
1167static inline void format_graph(char **buf_ch, bool extended,
862ca4ed 1168 const char *prefix, const struct bt_graph *graph)
476ef981 1169{
879e14e8 1170 char tmp_prefix[TMP_PREFIX_LEN];
5c563278 1171
9b4f9b42 1172 BUF_APPEND(", %scan-consume=%d, %sconfig-state=%s",
5badd463
PP
1173 PRFIELD(graph->can_consume),
1174 PRFIELD(bt_graph_configuration_state_string(graph->config_state)));
476ef981
PP
1175
1176 if (!extended) {
1177 return;
1178 }
1179
312c056a
PP
1180 if (graph->components) {
1181 BUF_APPEND(", %scomp-count=%u",
1182 PRFIELD(graph->components->len));
1183 }
1184
1185 if (graph->connections) {
1186 BUF_APPEND(", %sconn-count=%u",
1187 PRFIELD(graph->connections->len));
1188 }
5c563278
PP
1189
1190 SET_TMP_PREFIX("en-pool-");
9a6502b6 1191 format_object_pool(buf_ch, extended, tmp_prefix,
d6e69534 1192 &graph->event_msg_pool);
5c563278 1193 SET_TMP_PREFIX("pbn-pool-");
9a6502b6 1194 format_object_pool(buf_ch, extended, tmp_prefix,
d6e69534 1195 &graph->packet_begin_msg_pool);
5c563278 1196 SET_TMP_PREFIX("pen-pool-");
9a6502b6 1197 format_object_pool(buf_ch, extended, tmp_prefix,
d6e69534 1198 &graph->packet_end_msg_pool);
476ef981
PP
1199}
1200
a3f0c7db
SM
1201static inline void format_message_iterator_class(char **buf_ch,
1202 bool extended, const char *prefix,
1203 const struct bt_message_iterator_class *iterator_class)
1204{
1205 /* Empty, the address is automatically printed. */
1206}
1207
d6e69534 1208static inline void format_message_iterator(char **buf_ch,
476ef981 1209 bool extended, const char *prefix,
d6e69534 1210 const struct bt_message_iterator *iterator)
476ef981 1211{
879e14e8 1212 char tmp_prefix[TMP_PREFIX_LEN];
6c373cc9
PP
1213 const struct bt_self_component_port_input_message_iterator *
1214 port_in_iter = (const void *) iterator;
476ef981 1215
6c373cc9
PP
1216 if (port_in_iter->upstream_component) {
1217 SET_TMP_PREFIX("upstream-comp-");
1218 format_component(buf_ch, false, tmp_prefix,
1219 port_in_iter->upstream_component);
476ef981
PP
1220 }
1221
6c373cc9
PP
1222 if (!extended) {
1223 goto end;
476ef981 1224 }
476ef981 1225
6c373cc9
PP
1226 if (port_in_iter->upstream_port) {
1227 SET_TMP_PREFIX("upstream-port-");
1228 format_port(buf_ch, false, tmp_prefix,
1229 port_in_iter->upstream_port);
476ef981 1230 }
6c373cc9
PP
1231
1232 if (port_in_iter->connection) {
1233 SET_TMP_PREFIX("upstream-conn-");
1234 format_connection(buf_ch, false, tmp_prefix,
1235 port_in_iter->connection);
476ef981 1236 }
6c373cc9
PP
1237
1238end:
1239 return;
476ef981
PP
1240}
1241
1242static inline void format_plugin(char **buf_ch, bool extended,
92fed4e1 1243 const char *prefix, const struct bt_plugin *plugin)
476ef981 1244{
879e14e8 1245 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
1246
1247 BUF_APPEND(", %stype=%s", PRFIELD(bt_plugin_type_string(plugin->type)));
1248
1249 if (plugin->info.path_set) {
1250 BUF_APPEND(", %spath=\"%s\"",
d94d92ac 1251 PRFIELD_GSTRING(plugin->info.path));
476ef981
PP
1252 }
1253
1254 if (plugin->info.name_set) {
1255 BUF_APPEND(", %sname=\"%s\"",
d94d92ac 1256 PRFIELD_GSTRING(plugin->info.name));
476ef981
PP
1257 }
1258
1259 if (!extended) {
1260 return;
1261 }
1262
1263 if (plugin->info.author_set) {
1264 BUF_APPEND(", %sauthor=\"%s\"",
d94d92ac 1265 PRFIELD_GSTRING(plugin->info.author));
476ef981
PP
1266 }
1267
1268 if (plugin->info.license_set) {
1269 BUF_APPEND(", %slicense=\"%s\"",
d94d92ac 1270 PRFIELD_GSTRING(plugin->info.license));
476ef981
PP
1271 }
1272
1273 if (plugin->info.version_set) {
1274 BUF_APPEND(", %sversion=%u.%u.%u%s",
1275 PRFIELD(plugin->info.version.major),
1276 plugin->info.version.minor,
1277 plugin->info.version.patch,
1278 plugin->info.version.extra ?
1279 plugin->info.version.extra->str : "");
1280 }
1281
d94d92ac
PP
1282 BUF_APPEND(", %ssrc-comp-class-count=%u, %sflt-comp-class-count=%u, "
1283 "%ssink-comp-class-count=%u",
1284 PRFIELD(plugin->src_comp_classes->len),
1285 PRFIELD(plugin->flt_comp_classes->len),
1286 PRFIELD(plugin->sink_comp_classes->len));
476ef981
PP
1287
1288 if (plugin->spec_data) {
862ca4ed
PP
1289 const struct bt_plugin_so_spec_data *spec_data =
1290 (const void *) plugin->spec_data;
476ef981
PP
1291
1292 if (spec_data->shared_lib_handle) {
1293 SET_TMP_PREFIX("so-handle-");
1294 format_plugin_so_shared_lib_handle(buf_ch, tmp_prefix,
1295 spec_data->shared_lib_handle);
1296 }
1297 }
1298}
1299
553c4bab
PP
1300static inline void format_error_cause(char **buf_ch, bool extended,
1301 const char *prefix, const struct bt_error_cause *cause)
1302{
1303 const struct bt_error_cause_component_class_id *comp_class_id = NULL;
1304
1305 BUF_APPEND(", %sactor-type=%s, %smodule-name=\"%s\"",
1306 PRFIELD(bt_error_cause_actor_type_string(cause->actor_type)),
1307 PRFIELD_GSTRING(cause->module_name));
1308
1309 if (!extended) {
1310 return;
1311 }
1312
1313 BUF_APPEND(", %spartial-msg=\"%.32s\"",
1314 PRFIELD_GSTRING(cause->message));
1315
1316 switch (cause->actor_type) {
1317 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT:
1318 {
1319 const struct bt_error_cause_component_actor *spec_cause =
1320 (const void *) cause;
1321
1322 BUF_APPEND(", %scomp-name=\"%s\"",
1323 PRFIELD_GSTRING(spec_cause->comp_name));
1324 comp_class_id = &spec_cause->comp_class_id;
1325 break;
1326 }
1327 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS:
1328 {
1329 const struct bt_error_cause_component_class_actor *spec_cause =
1330 (const void *) cause;
1331
1332 comp_class_id = &spec_cause->comp_class_id;
1333 break;
1334 }
1335 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR:
1336 {
1337 const struct bt_error_cause_message_iterator_actor *spec_cause =
1338 (const void *) cause;
1339
1340 BUF_APPEND(", %scomp-name=\"%s\", %scomp-out-port-name=\"%s\"",
1341 PRFIELD_GSTRING(spec_cause->comp_name),
1342 PRFIELD_GSTRING(spec_cause->output_port_name));
1343 comp_class_id = &spec_cause->comp_class_id;
1344 break;
1345 }
1346 default:
1347 break;
1348 }
1349
1350 if (comp_class_id) {
1351 BUF_APPEND(", %scomp-cls-type=%s, %scomp-cls-name=\"%s\", "
1352 "%splugin-name=\"%s\"",
1353 PRFIELD(bt_component_class_type_string(
1354 comp_class_id->type)),
1355 PRFIELD_GSTRING(comp_class_id->name),
1356 PRFIELD_GSTRING(comp_class_id->plugin_name));
1357 }
1358}
1359
476ef981
PP
1360static inline void handle_conversion_specifier_bt(void *priv_data,
1361 char **buf_ch, size_t avail_size,
1362 const char **out_fmt_ch, va_list *args)
1363{
1364 const char *fmt_ch = *out_fmt_ch;
1365 bool extended = false;
1366 char prefix[64];
1367 char *prefix_ch = prefix;
862ca4ed 1368 const void *obj;
476ef981
PP
1369
1370 /* skip "%!" */
1371 fmt_ch += 2;
1372
44c440bc
PP
1373 if (*fmt_ch == 'u') {
1374 /* UUID */
1375 obj = va_arg(*args, void *);
1376 format_uuid(buf_ch, obj);
1377 goto update_fmt;
1378 }
1379
476ef981
PP
1380 if (*fmt_ch == '[') {
1381 /* local prefix */
1382 fmt_ch++;
1383
1384 while (true) {
1385 if (*fmt_ch == ']') {
1386 *prefix_ch = '\0';
1387 fmt_ch++;
1388 break;
1389 }
1390
1391 *prefix_ch = *fmt_ch;
1392 prefix_ch++;
1393 fmt_ch++;
1394 }
1395 }
1396
1397 *prefix_ch = '\0';
1398
1399 if (*fmt_ch == '+') {
1400 extended = true;
1401 fmt_ch++;
1402 }
1403
1404 obj = va_arg(*args, void *);
1405 BUF_APPEND("%saddr=%p", prefix, obj);
1406
1407 if (!obj) {
1408 goto update_fmt;
1409 }
1410
cb6f1f7d
PP
1411 switch (*fmt_ch) {
1412 case 'F':
5cd6d0e5 1413 format_field_class(buf_ch, extended, prefix, obj);
476ef981 1414 break;
cb6f1f7d
PP
1415 case 'f':
1416 format_field(buf_ch, extended, prefix, obj);
1417 break;
1418 case 'P':
1419 format_field_path(buf_ch, extended, prefix, obj);
1420 break;
1421 case 'E':
1422 format_event_class(buf_ch, extended, prefix, obj);
1423 break;
1424 case 'e':
1425 format_event(buf_ch, extended, prefix, obj);
1426 break;
1427 case 'S':
1428 format_stream_class(buf_ch, extended, prefix, obj);
476ef981 1429 break;
cb6f1f7d
PP
1430 case 's':
1431 format_stream(buf_ch, extended, prefix, obj);
1432 break;
1433 case 'a':
1434 format_packet(buf_ch, extended, prefix, obj);
1435 break;
1436 case 't':
1437 format_trace(buf_ch, extended, prefix, obj);
1438 break;
862ca4ed
PP
1439 case 'T':
1440 format_trace_class(buf_ch, extended, prefix, obj);
1441 break;
cb6f1f7d
PP
1442 case 'K':
1443 format_clock_class(buf_ch, extended, prefix, obj);
1444 break;
1445 case 'k':
605e1019 1446 format_clock_snapshot(buf_ch, extended, prefix, obj);
cb6f1f7d
PP
1447 break;
1448 case 'v':
1449 format_value(buf_ch, extended, prefix, obj);
1450 break;
6769570a
PP
1451 case 'R':
1452 format_integer_range_set(buf_ch, extended, prefix, obj);
1453 break;
cb6f1f7d 1454 case 'n':
d6e69534 1455 format_message(buf_ch, extended, prefix, obj);
cb6f1f7d 1456 break;
a3f0c7db
SM
1457 case 'I':
1458 format_message_iterator_class(buf_ch, extended, prefix, obj);
1459 break;
cb6f1f7d 1460 case 'i':
d6e69534 1461 format_message_iterator(buf_ch, extended, prefix, obj);
cb6f1f7d
PP
1462 break;
1463 case 'C':
1464 format_component_class(buf_ch, extended, prefix, obj);
1465 break;
1466 case 'c':
1467 format_component(buf_ch, extended, prefix, obj);
1468 break;
1469 case 'p':
1470 format_port(buf_ch, extended, prefix, obj);
1471 break;
1472 case 'x':
1473 format_connection(buf_ch, extended, prefix, obj);
1474 break;
44c440bc 1475 case 'l':
cb6f1f7d
PP
1476 format_plugin(buf_ch, extended, prefix, obj);
1477 break;
1478 case 'g':
1479 format_graph(buf_ch, extended, prefix, obj);
1480 break;
b70d57a1
PP
1481 case 'z':
1482 format_interrupter(buf_ch, extended, prefix, obj);
1483 break;
cb6f1f7d
PP
1484 case 'o':
1485 format_object_pool(buf_ch, extended, prefix, obj);
1486 break;
1487 case 'O':
1488 format_object(buf_ch, extended, prefix, obj);
1489 break;
553c4bab
PP
1490 case 'r':
1491 format_error_cause(buf_ch, extended, prefix, obj);
1492 break;
cb6f1f7d 1493 default:
498e7994 1494 bt_common_abort();
476ef981
PP
1495 }
1496
1497update_fmt:
1498 fmt_ch++;
1499 *out_fmt_ch = fmt_ch;
1500}
1501
476ef981
PP
1502void bt_lib_log(const char *func, const char *file, unsigned line,
1503 int lvl, const char *tag, const char *fmt, ...)
1504{
1505 va_list args;
1506
f6ccaed9 1507 BT_ASSERT(fmt);
476ef981
PP
1508 va_start(args, fmt);
1509 bt_common_custom_vsnprintf(lib_logging_buf, LIB_LOGGING_BUF_SIZE, '!',
1510 handle_conversion_specifier_bt, NULL, fmt, &args);
1511 va_end(args);
1512 _bt_log_write_d(func, file, line, lvl, tag, "%s", lib_logging_buf);
1513}
3cd4c495
PP
1514
1515void bt_lib_maybe_log_and_append_cause(const char *func, const char *file,
1516 unsigned line, int lvl, const char *tag,
1517 const char *fmt, ...)
1518{
1519 va_list args;
1520 bt_current_thread_error_append_cause_status status;
1521
1522 BT_ASSERT(fmt);
1523 va_start(args, fmt);
1524 bt_common_custom_vsnprintf(lib_logging_buf, LIB_LOGGING_BUF_SIZE, '!',
1525 handle_conversion_specifier_bt, NULL, fmt, &args);
1526 va_end(args);
1527
1528 /* Log conditionally, but always append the error cause */
1529 if (BT_LOG_ON(lvl)) {
1530 _bt_log_write_d(func, file, line, lvl, tag, "%s",
1531 lib_logging_buf);
1532 }
1533
1534 status = bt_current_thread_error_append_cause_from_unknown(
d5becf10
FD
1535 BT_LIB_LOG_LIBBABELTRACE2_NAME, file, line, "%s",
1536 lib_logging_buf);
3cd4c495
PP
1537 if (status) {
1538 /*
1539 * Worst case: this error cause is not appended to the
1540 * current thread's error.
1541 *
1542 * We can accept this as it's an almost impossible
1543 * scenario and returning an error here would mean you
1544 * need to check the return value of each
1545 * BT_LIB_LOG*_APPEND_CAUSE() macro and that would be
1546 * cumbersome.
1547 */
1548 BT_LOGE("Cannot append error cause to current thread's "
1549 "error object: status=%s",
1550 bt_common_func_status_string(status));
1551 }
1552}
This page took 0.136801 seconds and 4 git commands to generate.