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