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