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