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