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