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