Fix: shadowed variables
[babeltrace.git] / src / plugins / text / details / write.c
1 /*
2 * Copyright 2019 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 #include <babeltrace2/babeltrace.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "common/assert.h"
28 #include "common/common.h"
29 #include "common/uuid.h"
30 #include "details.h"
31 #include "write.h"
32 #include "obj-lifetime-mgmt.h"
33 #include "colors.h"
34
35 static inline
36 const char *plural(uint64_t value)
37 {
38 return value == 1 ? "" : "s";
39 }
40
41 static inline
42 void incr_indent_by(struct details_write_ctx *ctx, unsigned int value)
43 {
44 BT_ASSERT_DBG(ctx);
45 ctx->indent_level += value;
46 }
47
48 static inline
49 void incr_indent(struct details_write_ctx *ctx)
50 {
51 incr_indent_by(ctx, 2);
52 }
53
54 static inline
55 void decr_indent_by(struct details_write_ctx *ctx, unsigned int value)
56 {
57 BT_ASSERT_DBG(ctx);
58 BT_ASSERT_DBG(ctx->indent_level >= value);
59 ctx->indent_level -= value;
60 }
61
62 static inline
63 void decr_indent(struct details_write_ctx *ctx)
64 {
65 decr_indent_by(ctx, 2);
66 }
67
68 static inline
69 void format_uint(char *buf, uint64_t value, unsigned int base)
70 {
71 const char *spec = "%" PRIu64;
72 char *buf_start = buf;
73 unsigned int digits_per_group = 3;
74 char sep = ',';
75 bool sep_digits = true;
76
77 switch (base) {
78 case 2:
79 case 16:
80 /* TODO: Support binary format */
81 spec = "%" PRIx64;
82 strcpy(buf, "0x");
83 buf_start = buf + 2;
84 digits_per_group = 4;
85 sep = ':';
86 break;
87 case 8:
88 spec = "%" PRIo64;
89 strcpy(buf, "0");
90 buf_start = buf + 1;
91 sep = ':';
92 break;
93 case 10:
94 if (value <= 9999) {
95 /*
96 * Do not insert digit separators for numbers
97 * under 10,000 as it looks weird.
98 */
99 sep_digits = false;
100 }
101
102 break;
103 default:
104 abort();
105 }
106
107 sprintf(buf_start, spec, value);
108
109 if (sep_digits) {
110 bt_common_sep_digits(buf_start, digits_per_group, sep);
111 }
112 }
113
114 static inline
115 void format_int(char *buf, int64_t value, unsigned int base)
116 {
117 const char *spec = "%" PRIu64;
118 char *buf_start = buf;
119 unsigned int digits_per_group = 3;
120 char sep = ',';
121 bool sep_digits = true;
122 uint64_t abs_value = value < 0 ? (uint64_t) -value : (uint64_t) value;
123
124 if (value < 0) {
125 buf[0] = '-';
126 buf_start++;
127 }
128
129 switch (base) {
130 case 2:
131 case 16:
132 /* TODO: Support binary format */
133 spec = "%" PRIx64;
134 strcpy(buf_start, "0x");
135 buf_start += 2;
136 digits_per_group = 4;
137 sep = ':';
138 break;
139 case 8:
140 spec = "%" PRIo64;
141 strcpy(buf_start, "0");
142 buf_start++;
143 sep = ':';
144 break;
145 case 10:
146 if (value >= -9999 && value <= 9999) {
147 /*
148 * Do not insert digit separators for numbers
149 * over -10,000 and under 10,000 as it looks
150 * weird.
151 */
152 sep_digits = false;
153 }
154
155 break;
156 default:
157 abort();
158 }
159
160 sprintf(buf_start, spec, abs_value);
161
162 if (sep_digits) {
163 bt_common_sep_digits(buf_start, digits_per_group, sep);
164 }
165 }
166
167 static inline
168 void write_nl(struct details_write_ctx *ctx)
169 {
170 BT_ASSERT_DBG(ctx);
171 g_string_append_c(ctx->str, '\n');
172 }
173
174 static inline
175 void write_sp(struct details_write_ctx *ctx)
176 {
177 BT_ASSERT_DBG(ctx);
178 g_string_append_c(ctx->str, ' ');
179 }
180
181 static inline
182 void write_indent(struct details_write_ctx *ctx)
183 {
184 uint64_t i;
185
186 BT_ASSERT_DBG(ctx);
187
188 for (i = 0; i < ctx->indent_level; i++) {
189 write_sp(ctx);
190 }
191 }
192
193 static inline
194 void write_compound_member_name(struct details_write_ctx *ctx, const char *name)
195 {
196 write_indent(ctx);
197 g_string_append_printf(ctx->str, "%s%s%s:",
198 color_fg_cyan(ctx), name, color_reset(ctx));
199 }
200
201 static inline
202 void write_array_index(struct details_write_ctx *ctx, uint64_t index,
203 const char *color)
204 {
205 char buf[32];
206
207 write_indent(ctx);
208 format_uint(buf, index, 10);
209 g_string_append_printf(ctx->str, "%s[%s]%s:",
210 color, buf, color_reset(ctx));
211 }
212
213 static inline
214 void write_obj_type_name(struct details_write_ctx *ctx, const char *name)
215 {
216 g_string_append_printf(ctx->str, "%s%s%s%s",
217 color_fg_yellow(ctx), color_bold(ctx), name, color_reset(ctx));
218 }
219
220 static inline
221 void write_prop_name(struct details_write_ctx *ctx, const char *prop_name)
222 {
223 g_string_append_printf(ctx->str, "%s%s%s",
224 color_fg_magenta(ctx), prop_name, color_reset(ctx));
225 }
226
227 static inline
228 void write_prop_name_line(struct details_write_ctx *ctx, const char *prop_name)
229 {
230 write_indent(ctx);
231 g_string_append_printf(ctx->str, "%s%s%s:",
232 color_fg_magenta(ctx), prop_name, color_reset(ctx));
233 }
234
235 static inline
236 void write_str_prop_value(struct details_write_ctx *ctx, const char *value)
237 {
238 g_string_append_printf(ctx->str, "%s%s%s",
239 color_bold(ctx), value, color_reset(ctx));
240 }
241
242 static inline
243 void write_none_prop_value(struct details_write_ctx *ctx, const char *value)
244 {
245 g_string_append_printf(ctx->str, "%s%s%s%s",
246 color_bold(ctx), color_fg_magenta(ctx),
247 value, color_reset(ctx));
248 }
249
250 static inline
251 void write_uint_str_prop_value(struct details_write_ctx *ctx, const char *value)
252 {
253 write_str_prop_value(ctx, value);
254 }
255
256 static inline
257 void write_uint_prop_value(struct details_write_ctx *ctx, uint64_t value)
258 {
259 char buf[32];
260
261 format_uint(buf, value, 10);
262 write_uint_str_prop_value(ctx, buf);
263 }
264
265 static inline
266 void write_int_prop_value(struct details_write_ctx *ctx, int64_t value)
267 {
268 char buf[32];
269
270 format_int(buf, value, 10);
271 write_uint_str_prop_value(ctx, buf);
272 }
273
274 static inline
275 void write_float_prop_value(struct details_write_ctx *ctx, double value)
276 {
277 g_string_append_printf(ctx->str, "%s%f%s",
278 color_bold(ctx), value, color_reset(ctx));
279 }
280
281 static inline
282 void write_str_prop_line(struct details_write_ctx *ctx, const char *prop_name,
283 const char *prop_value)
284 {
285 BT_ASSERT_DBG(prop_value);
286 write_indent(ctx);
287 write_prop_name(ctx, prop_name);
288 g_string_append(ctx->str, ": ");
289 write_str_prop_value(ctx, prop_value);
290 write_nl(ctx);
291 }
292
293 static inline
294 void write_uint_prop_line(struct details_write_ctx *ctx, const char *prop_name,
295 uint64_t prop_value)
296 {
297 write_indent(ctx);
298 write_prop_name(ctx, prop_name);
299 g_string_append(ctx->str, ": ");
300 write_uint_prop_value(ctx, prop_value);
301 write_nl(ctx);
302 }
303
304 static inline
305 void write_int_prop_line(struct details_write_ctx *ctx, const char *prop_name,
306 int64_t prop_value)
307 {
308 write_indent(ctx);
309 write_prop_name(ctx, prop_name);
310 g_string_append(ctx->str, ": ");
311 write_int_prop_value(ctx, prop_value);
312 write_nl(ctx);
313 }
314
315 static inline
316 void write_int_str_prop_value(struct details_write_ctx *ctx, const char *value)
317 {
318 write_str_prop_value(ctx, value);
319 }
320
321 static inline
322 void write_bool_prop_value(struct details_write_ctx *ctx, bt_bool prop_value)
323 {
324 const char *str;
325
326 g_string_append(ctx->str, color_bold(ctx));
327
328 if (prop_value) {
329 g_string_append(ctx->str, color_fg_green(ctx));
330 str = "Yes";
331 } else {
332 g_string_append(ctx->str, color_fg_red(ctx));
333 str = "No";
334 }
335
336 g_string_append_printf(ctx->str, "%s%s", str, color_reset(ctx));
337 }
338
339 static inline
340 void write_bool_prop_line(struct details_write_ctx *ctx, const char *prop_name,
341 bt_bool prop_value)
342 {
343 write_indent(ctx);
344 write_prop_name(ctx, prop_name);
345 g_string_append(ctx->str, ": ");
346 write_bool_prop_value(ctx, prop_value);
347 write_nl(ctx);
348 }
349
350 static inline
351 void write_uuid_prop_line(struct details_write_ctx *ctx, const char *prop_name,
352 bt_uuid uuid)
353 {
354 BT_ASSERT_DBG(uuid);
355 write_indent(ctx);
356 write_prop_name(ctx, prop_name);
357 g_string_append_printf(ctx->str,
358 ": %s" BT_UUID_FMT "%s\n",
359 color_bold(ctx),
360 BT_UUID_FMT_VALUES(uuid),
361 color_reset(ctx));
362 }
363
364 static
365 gint compare_strings(const char **a, const char **b)
366 {
367 return strcmp(*a, *b);
368 }
369
370 static
371 bt_bool map_value_foreach_add_key_to_array(const char *key,
372 const bt_value *object, void *data)
373 {
374 GPtrArray *keys = data;
375
376 BT_ASSERT_DBG(keys);
377 g_ptr_array_add(keys, (void *) key);
378 return BT_TRUE;
379 }
380
381 static
382 void write_value(struct details_write_ctx *ctx, const bt_value *value,
383 const char *name)
384 {
385 uint64_t i;
386 bt_value_type value_type = bt_value_get_type(value);
387 GPtrArray *keys = g_ptr_array_new();
388 char buf[64];
389
390 BT_ASSERT_DBG(keys);
391
392 /* Write field's name */
393 if (name) {
394 write_prop_name_line(ctx, name);
395 }
396
397 /* Write field's value */
398 switch (value_type) {
399 case BT_VALUE_TYPE_NULL:
400 write_sp(ctx);
401 write_none_prop_value(ctx, "Null");
402 break;
403 case BT_VALUE_TYPE_BOOL:
404 write_sp(ctx);
405 write_bool_prop_value(ctx, bt_value_bool_get(value));
406 break;
407 case BT_VALUE_TYPE_UNSIGNED_INTEGER:
408 format_uint(buf, bt_value_integer_unsigned_get(value), 10);
409 write_sp(ctx);
410 write_uint_str_prop_value(ctx, buf);
411 break;
412 case BT_VALUE_TYPE_SIGNED_INTEGER:
413 format_int(buf, bt_value_integer_signed_get(value), 10);
414 write_sp(ctx);
415 write_int_str_prop_value(ctx, buf);
416 break;
417 case BT_VALUE_TYPE_REAL:
418 write_sp(ctx);
419 write_float_prop_value(ctx, bt_value_real_get(value));
420 break;
421 case BT_VALUE_TYPE_STRING:
422 write_sp(ctx);
423 write_str_prop_value(ctx, bt_value_string_get(value));
424 break;
425 case BT_VALUE_TYPE_ARRAY:
426 {
427 uint64_t length = bt_value_array_get_length(value);
428
429 if (length == 0) {
430 write_sp(ctx);
431 write_none_prop_value(ctx, "Empty");
432 } else {
433 g_string_append(ctx->str, " Length ");
434 write_uint_prop_value(ctx, length);
435 g_string_append_c(ctx->str, ':');
436 }
437
438 incr_indent(ctx);
439
440 for (i = 0; i < length; i++) {
441 const bt_value *elem_value =
442 bt_value_array_borrow_element_by_index_const(
443 value, i);
444
445 write_nl(ctx);
446 write_array_index(ctx, i, color_fg_magenta(ctx));
447 write_value(ctx, elem_value, NULL);
448 }
449
450 decr_indent(ctx);
451 break;
452 }
453 case BT_VALUE_TYPE_MAP:
454 {
455 bt_value_map_foreach_entry_const_status foreach_status =
456 bt_value_map_foreach_entry_const(value,
457 map_value_foreach_add_key_to_array, keys);
458
459 BT_ASSERT_DBG(foreach_status ==
460 BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK);
461 g_ptr_array_sort(keys, (GCompareFunc) compare_strings);
462
463 if (keys->len > 0) {
464 incr_indent(ctx);
465
466 for (i = 0; i < keys->len; i++) {
467 const char *key = keys->pdata[i];
468 const bt_value *entry_value =
469 bt_value_map_borrow_entry_value_const(
470 value, key);
471
472 write_nl(ctx);
473 write_value(ctx, entry_value, key);
474 }
475
476 decr_indent(ctx);
477 } else {
478 write_sp(ctx);
479 write_none_prop_value(ctx, "Empty");
480 }
481
482 break;
483 }
484 default:
485 abort();
486 }
487
488 g_ptr_array_free(keys, TRUE);
489 }
490
491 static
492 void write_user_attributes(struct details_write_ctx *ctx,
493 const bt_value *user_attrs, bool write_newline, bool *written)
494 {
495 BT_ASSERT_DBG(user_attrs);
496
497 if (!bt_value_map_is_empty(user_attrs)) {
498 write_value(ctx, user_attrs, "User attributes");
499
500 if (write_newline) {
501 write_nl(ctx);
502 }
503
504 if (written) {
505 *written = true;
506 }
507 }
508 }
509
510 static
511 void write_int_field_class_props(struct details_write_ctx *ctx,
512 const bt_field_class *fc, bool close)
513 {
514 g_string_append_printf(ctx->str, "(%s%" PRIu64 "-bit%s, Base ",
515 color_bold(ctx),
516 bt_field_class_integer_get_field_value_range(fc),
517 color_reset(ctx));
518
519 switch (bt_field_class_integer_get_preferred_display_base(fc)) {
520 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
521 write_uint_prop_value(ctx, 2);
522 break;
523 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
524 write_uint_prop_value(ctx, 8);
525 break;
526 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
527 write_uint_prop_value(ctx, 10);
528 break;
529 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
530 write_uint_prop_value(ctx, 16);
531 break;
532 default:
533 abort();
534 }
535
536 if (close) {
537 g_string_append(ctx->str, ")");
538 }
539 }
540
541 struct int_range {
542 union {
543 uint64_t u;
544 int64_t i;
545 } lower;
546
547 union {
548 uint64_t u;
549 int64_t i;
550 } upper;
551 };
552
553 struct enum_field_class_mapping {
554 /* Weak */
555 const char *label;
556
557 /* Array of `struct int_range` */
558 GArray *ranges;
559 };
560
561 static
562 gint compare_enum_field_class_mappings(struct enum_field_class_mapping **a,
563 struct enum_field_class_mapping **b)
564 {
565 return strcmp((*a)->label, (*b)->label);
566 }
567
568 static
569 gint compare_int_ranges_signed(struct int_range *a, struct int_range *b)
570 {
571
572 if (a->lower.i < b->lower.i) {
573 return -1;
574 } else if (a->lower.i > b->lower.i) {
575 return 1;
576 } else {
577 if (a->upper.i < b->upper.i) {
578 return -1;
579 } else if (a->upper.i > b->upper.i) {
580 return 1;
581 } else {
582 return 0;
583 }
584 }
585 }
586
587 static
588 gint compare_int_ranges_unsigned(struct int_range *a, struct int_range *b)
589 {
590 if (a->lower.u < b->lower.u) {
591 return -1;
592 } else if (a->lower.u > b->lower.u) {
593 return 1;
594 } else {
595 if (a->upper.u < b->upper.u) {
596 return -1;
597 } else if (a->upper.u > b->upper.u) {
598 return 1;
599 } else {
600 return 0;
601 }
602 }
603 }
604
605 static
606 GArray *range_set_to_int_ranges(const void *spec_range_set, bool is_signed)
607 {
608 uint64_t i;
609 const bt_integer_range_set *range_set;
610 GArray *ranges = g_array_new(FALSE, TRUE, sizeof(struct int_range));
611
612 if (!ranges) {
613 goto end;
614 }
615
616 if (is_signed) {
617 range_set = bt_integer_range_set_signed_as_range_set_const(
618 spec_range_set);
619 } else {
620 range_set = bt_integer_range_set_unsigned_as_range_set_const(
621 spec_range_set);
622 }
623
624 for (i = 0; i < bt_integer_range_set_get_range_count(range_set); i++) {
625 struct int_range range;
626
627 if (is_signed) {
628 const bt_integer_range_signed *orig_range =
629 bt_integer_range_set_signed_borrow_range_by_index_const(
630 spec_range_set, i);
631
632 range.lower.i = bt_integer_range_signed_get_lower(orig_range);
633 range.upper.i = bt_integer_range_signed_get_upper(orig_range);
634 } else {
635 const bt_integer_range_unsigned *orig_range =
636 bt_integer_range_set_unsigned_borrow_range_by_index_const(
637 spec_range_set, i);
638
639 range.lower.u = bt_integer_range_unsigned_get_lower(orig_range);
640 range.upper.u = bt_integer_range_unsigned_get_upper(orig_range);
641 }
642
643 g_array_append_val(ranges, range);
644 }
645
646 if (is_signed) {
647 g_array_sort(ranges, (GCompareFunc) compare_int_ranges_signed);
648 } else {
649 g_array_sort(ranges,
650 (GCompareFunc) compare_int_ranges_unsigned);
651 }
652
653 end:
654 return ranges;
655 }
656
657 static
658 void destroy_enum_field_class_mapping(struct enum_field_class_mapping *mapping)
659 {
660 if (mapping->ranges) {
661 g_array_free(mapping->ranges, TRUE);
662 mapping->ranges = NULL;
663 }
664
665 g_free(mapping);
666 }
667
668 static
669 struct int_range *int_range_at(GArray *ranges, uint64_t index)
670 {
671 return &g_array_index(ranges, struct int_range, index);
672 }
673
674 static
675 void write_int_range(struct details_write_ctx *ctx,
676 struct int_range *range, bool is_signed)
677 {
678 g_string_append(ctx->str, "[");
679
680 if (is_signed) {
681 write_int_prop_value(ctx, range->lower.i);
682 } else {
683 write_int_prop_value(ctx, range->lower.u);
684 }
685
686 if (range->lower.u != range->upper.u) {
687 g_string_append(ctx->str, ", ");
688
689 if (is_signed) {
690 write_int_prop_value(ctx, range->upper.i);
691 } else {
692 write_int_prop_value(ctx, range->upper.u);
693 }
694 }
695
696 g_string_append(ctx->str, "]");
697 }
698
699 static
700 void write_enum_field_class_mappings(struct details_write_ctx *ctx,
701 const bt_field_class *fc)
702 {
703 GPtrArray *mappings;
704 uint64_t i;
705 uint64_t range_i;
706 bool is_signed = bt_field_class_get_type(fc) ==
707 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION;
708
709 mappings = g_ptr_array_new_with_free_func(
710 (GDestroyNotify) destroy_enum_field_class_mapping);
711 BT_ASSERT_DBG(mappings);
712
713 /*
714 * Copy field class's mappings to our own arrays and structures
715 * to sort them.
716 */
717 for (i = 0; i < bt_field_class_enumeration_get_mapping_count(fc); i++) {
718 const void *fc_mapping;
719 const void *fc_range_set;
720 struct enum_field_class_mapping *mapping = g_new0(
721 struct enum_field_class_mapping, 1);
722
723 BT_ASSERT_DBG(mapping);
724
725 if (is_signed) {
726 fc_mapping = bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
727 fc, i);
728 fc_range_set = bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
729 fc_mapping);
730 } else {
731 fc_mapping = bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
732 fc, i);
733 fc_range_set = bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
734 fc_mapping);
735 }
736
737 mapping->label = bt_field_class_enumeration_mapping_get_label(
738 bt_field_class_enumeration_signed_mapping_as_mapping_const(
739 fc_mapping));
740 mapping->ranges = range_set_to_int_ranges(fc_range_set,
741 is_signed);
742 BT_ASSERT_DBG(mapping->ranges);
743 g_ptr_array_add(mappings, mapping);
744 }
745
746 /* Sort mappings (ranges are already sorted within mappings) */
747 g_ptr_array_sort(mappings,
748 (GCompareFunc) compare_enum_field_class_mappings);
749
750 /* Write mappings */
751 for (i = 0; i < mappings->len; i++) {
752 struct enum_field_class_mapping *mapping = mappings->pdata[i];
753
754 write_nl(ctx);
755 write_prop_name_line(ctx, mapping->label);
756
757 for (range_i = 0; range_i < mapping->ranges->len; range_i++) {
758 write_sp(ctx);
759 write_int_range(ctx,
760 int_range_at(mapping->ranges, range_i),
761 is_signed);
762 }
763 }
764
765 g_ptr_array_free(mappings, TRUE);
766 }
767
768 static
769 void write_field_path(struct details_write_ctx *ctx,
770 const bt_field_path *field_path)
771 {
772 uint64_t i;
773
774 g_string_append_c(ctx->str, '[');
775
776 switch (bt_field_path_get_root_scope(field_path)) {
777 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT:
778 write_str_prop_value(ctx, "Packet context");
779 break;
780 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT:
781 write_str_prop_value(ctx, "Event common context");
782 break;
783 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT:
784 write_str_prop_value(ctx, "Event specific context");
785 break;
786 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD:
787 write_str_prop_value(ctx, "Event payload");
788 break;
789 default:
790 abort();
791 }
792
793 g_string_append(ctx->str, ": ");
794
795 for (i = 0; i < bt_field_path_get_item_count(field_path); i++) {
796 const bt_field_path_item *fp_item =
797 bt_field_path_borrow_item_by_index_const(field_path, i);
798
799 if (i != 0) {
800 g_string_append(ctx->str, ", ");
801 }
802
803 switch (bt_field_path_item_get_type(fp_item)) {
804 case BT_FIELD_PATH_ITEM_TYPE_INDEX:
805 write_uint_prop_value(ctx,
806 bt_field_path_item_index_get_index(fp_item));
807 break;
808 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT:
809 write_str_prop_value(ctx, "<current>");
810 break;
811 default:
812 abort();
813 }
814 }
815
816 g_string_append_c(ctx->str, ']');
817 }
818
819 static
820 void write_field_class(struct details_write_ctx *ctx, const bt_field_class *fc);
821
822 static
823 void write_variant_field_class_option(struct details_write_ctx *ctx,
824 const bt_field_class *fc, uint64_t index)
825 {
826 bt_field_class_type fc_type = bt_field_class_get_type(fc);
827 const bt_field_class_variant_option *option =
828 bt_field_class_variant_borrow_option_by_index_const(
829 fc, index);
830 const void *orig_ranges = NULL;
831 GArray *int_ranges = NULL;
832 bool is_signed;
833 const bt_value *user_attrs =
834 bt_field_class_variant_option_borrow_user_attributes_const(
835 option);
836 const bt_field_class *option_fc =
837 bt_field_class_variant_option_borrow_field_class_const(option);
838
839 write_nl(ctx);
840 write_compound_member_name(ctx,
841 bt_field_class_variant_option_get_name(option));
842
843 if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD) {
844 const bt_field_class_variant_with_selector_field_integer_unsigned_option *spec_opt =
845 bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const(
846 fc, index);
847
848 orig_ranges =
849 bt_field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const(
850 spec_opt);
851 is_signed = false;
852 } else if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD) {
853 const bt_field_class_variant_with_selector_field_integer_signed_option *spec_opt =
854 bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const(
855 fc, index);
856
857 orig_ranges =
858 bt_field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const(
859 spec_opt);
860 is_signed = true;
861 }
862
863 if (orig_ranges) {
864 uint64_t i;
865
866 int_ranges = range_set_to_int_ranges(orig_ranges, is_signed);
867 BT_ASSERT_DBG(int_ranges);
868
869 for (i = 0; i < int_ranges->len; i++) {
870 struct int_range *range = int_range_at(int_ranges, i);
871
872 write_sp(ctx);
873 write_int_range(ctx, range, is_signed);
874 }
875
876 g_string_append(ctx->str, ": ");
877 } else {
878 write_sp(ctx);
879 }
880
881 if (bt_value_map_is_empty(user_attrs)) {
882 write_field_class(ctx, option_fc);
883 } else {
884 write_nl(ctx);
885 incr_indent(ctx);
886
887 /* Field class */
888 write_prop_name_line(ctx, "Field class");
889 write_sp(ctx);
890 write_field_class(ctx, option_fc);
891 write_nl(ctx);
892
893 /* User attributes */
894 write_user_attributes(ctx, user_attrs,
895 false, NULL);
896
897 decr_indent(ctx);
898 }
899
900 if (int_ranges) {
901 g_array_free(int_ranges, TRUE);
902 }
903 }
904
905 static
906 void write_field_class(struct details_write_ctx *ctx, const bt_field_class *fc)
907 {
908 uint64_t i;
909 const char *type;
910 bt_field_class_type fc_type = bt_field_class_get_type(fc);
911 const bt_value *user_attrs;
912 bool wrote_user_attrs = false;
913
914 /* Write field class's type */
915 switch (fc_type) {
916 case BT_FIELD_CLASS_TYPE_BOOL:
917 type = "Boolean";
918 break;
919 case BT_FIELD_CLASS_TYPE_BIT_ARRAY:
920 type = "Bit array";
921 break;
922 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
923 type = "Unsigned integer";
924 break;
925 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
926 type = "Signed integer";
927 break;
928 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
929 type = "Unsigned enumeration";
930 break;
931 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
932 type = "Signed enumeration";
933 break;
934 case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL:
935 type = "Single-precision real";
936 break;
937 case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL:
938 type = "Double-precision real";
939 break;
940 case BT_FIELD_CLASS_TYPE_STRING:
941 type = "String";
942 break;
943 case BT_FIELD_CLASS_TYPE_STRUCTURE:
944 type = "Structure";
945 break;
946 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
947 type = "Static array";
948 break;
949 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD:
950 type = "Dynamic array (no length field)";
951 break;
952 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD:
953 type = "Dynamic array (with length field)";
954 break;
955 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD:
956 type = "Option (no selector)";
957 break;
958 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD:
959 type = "Option (boolean selector)";
960 break;
961 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
962 type = "Option (unsigned integer selector)";
963 break;
964 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
965 type = "Option (signed integer selector)";
966 break;
967 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD:
968 type = "Variant (no selector)";
969 break;
970 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
971 type = "Variant (unsigned integer selector)";
972 break;
973 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
974 type = "Variant (signed integer selector)";
975 break;
976 default:
977 abort();
978 }
979
980 g_string_append_printf(ctx->str, "%s%s%s",
981 color_fg_blue(ctx), type, color_reset(ctx));
982
983 /* Write field class's single-line properties */
984 if (bt_field_class_type_is(fc_type, BT_FIELD_CLASS_TYPE_ENUMERATION)) {
985 uint64_t mapping_count =
986 bt_field_class_enumeration_get_mapping_count(fc);
987
988 write_sp(ctx);
989 write_int_field_class_props(ctx, fc, false);
990 g_string_append(ctx->str, ", ");
991 write_uint_prop_value(ctx, mapping_count);
992 g_string_append_printf(ctx->str, " mapping%s)",
993 plural(mapping_count));
994 } else if (bt_field_class_type_is(fc_type,
995 BT_FIELD_CLASS_TYPE_INTEGER)) {
996 write_sp(ctx);
997 write_int_field_class_props(ctx, fc, true);
998 } else if (fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) {
999 uint64_t member_count =
1000 bt_field_class_structure_get_member_count(fc);
1001
1002 g_string_append(ctx->str, " (");
1003 write_uint_prop_value(ctx, member_count);
1004 g_string_append_printf(ctx->str, " member%s)",
1005 plural(member_count));
1006 } else if (fc_type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY) {
1007 g_string_append(ctx->str, " (Length ");
1008 write_uint_prop_value(ctx,
1009 bt_field_class_array_static_get_length(fc));
1010 g_string_append_c(ctx->str, ')');
1011 } else if (fc_type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD) {
1012 const bt_field_path *length_field_path =
1013 bt_field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
1014 fc);
1015
1016 g_string_append(ctx->str, " (Length field path ");
1017 write_field_path(ctx, length_field_path);
1018 g_string_append_c(ctx->str, ')');
1019 } else if (bt_field_class_type_is(fc_type,
1020 BT_FIELD_CLASS_TYPE_OPTION_WITH_SELECTOR_FIELD)) {
1021 const bt_field_path *selector_field_path =
1022 bt_field_class_option_with_selector_field_borrow_selector_field_path_const(
1023 fc);
1024
1025 g_string_append(ctx->str, " (Selector field path ");
1026 write_field_path(ctx, selector_field_path);
1027 g_string_append_c(ctx->str, ')');
1028 } else if (bt_field_class_type_is(fc_type,
1029 BT_FIELD_CLASS_TYPE_VARIANT)) {
1030 uint64_t option_count =
1031 bt_field_class_variant_get_option_count(fc);
1032 const bt_field_path *sel_field_path = NULL;
1033
1034 if (bt_field_class_type_is(fc_type,
1035 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SELECTOR_FIELD)) {
1036 sel_field_path =
1037 bt_field_class_variant_with_selector_field_borrow_selector_field_path_const(
1038 fc);
1039 BT_ASSERT_DBG(sel_field_path);
1040 }
1041
1042 g_string_append(ctx->str, " (");
1043 write_uint_prop_value(ctx, option_count);
1044 g_string_append_printf(ctx->str, " option%s",
1045 plural(option_count));
1046
1047 if (sel_field_path) {
1048 g_string_append(ctx->str, ", Selector field path ");
1049 write_field_path(ctx, sel_field_path);
1050 }
1051
1052 g_string_append_c(ctx->str, ')');
1053 }
1054
1055 incr_indent(ctx);
1056 user_attrs = bt_field_class_borrow_user_attributes_const(fc);
1057 if (!bt_value_map_is_empty(user_attrs)) {
1058 g_string_append(ctx->str, ":\n");
1059 write_user_attributes(ctx, user_attrs, false, NULL);
1060 wrote_user_attrs = true;
1061 }
1062
1063 /* Write field class's complex properties */
1064 if (bt_field_class_type_is(fc_type, BT_FIELD_CLASS_TYPE_ENUMERATION)) {
1065 uint64_t mapping_count =
1066 bt_field_class_enumeration_get_mapping_count(fc);
1067
1068 if (mapping_count > 0) {
1069 if (wrote_user_attrs) {
1070 write_nl(ctx);
1071 write_indent(ctx);
1072 write_prop_name(ctx, "Mappings");
1073 g_string_append_c(ctx->str, ':');
1074 incr_indent(ctx);
1075 } else {
1076 /* Each mapping starts with its own newline */
1077 g_string_append_c(ctx->str, ':');
1078 }
1079
1080 write_enum_field_class_mappings(ctx, fc);
1081
1082 if (wrote_user_attrs) {
1083 decr_indent(ctx);
1084 }
1085 }
1086 } else if (fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) {
1087 uint64_t member_count =
1088 bt_field_class_structure_get_member_count(fc);
1089
1090 if (member_count > 0) {
1091 if (wrote_user_attrs) {
1092 write_nl(ctx);
1093 write_indent(ctx);
1094 write_prop_name(ctx, "Members");
1095 g_string_append_c(ctx->str, ':');
1096 incr_indent(ctx);
1097 } else {
1098 /* Each member starts with its own newline */
1099 g_string_append_c(ctx->str, ':');
1100 }
1101
1102 for (i = 0; i < member_count; i++) {
1103 const bt_field_class_structure_member *member =
1104 bt_field_class_structure_borrow_member_by_index_const(
1105 fc, i);
1106 const bt_value *member_user_attrs;
1107 const bt_field_class *member_fc =
1108 bt_field_class_structure_member_borrow_field_class_const(member);
1109
1110 write_nl(ctx);
1111 write_compound_member_name(ctx,
1112 bt_field_class_structure_member_get_name(member));
1113 member_user_attrs = bt_field_class_structure_member_borrow_user_attributes_const(
1114 member);
1115
1116 if (bt_value_map_is_empty(member_user_attrs)) {
1117 write_sp(ctx);
1118 write_field_class(ctx, member_fc);
1119 } else {
1120 write_nl(ctx);
1121 incr_indent(ctx);
1122
1123 /* Field class */
1124 write_prop_name_line(ctx, "Field class");
1125 write_sp(ctx);
1126 write_field_class(ctx, member_fc);
1127 write_nl(ctx);
1128
1129 /* User attributes */
1130 write_user_attributes(ctx, member_user_attrs,
1131 false, NULL);
1132
1133 decr_indent(ctx);
1134 }
1135 }
1136
1137 if (wrote_user_attrs) {
1138 decr_indent(ctx);
1139 }
1140 }
1141 } else if (bt_field_class_type_is(fc_type, BT_FIELD_CLASS_TYPE_ARRAY)) {
1142 if (wrote_user_attrs) {
1143 write_nl(ctx);
1144 } else {
1145 g_string_append(ctx->str, ":\n");
1146 }
1147
1148 write_prop_name_line(ctx, "Element");
1149 write_sp(ctx);
1150 write_field_class(ctx,
1151 bt_field_class_array_borrow_element_field_class_const(fc));
1152 } else if (bt_field_class_type_is(fc_type,
1153 BT_FIELD_CLASS_TYPE_OPTION)) {
1154 const void *ranges = NULL;
1155 bool selector_is_signed = false;
1156
1157 if (wrote_user_attrs) {
1158 write_nl(ctx);
1159 } else {
1160 g_string_append(ctx->str, ":\n");
1161 }
1162
1163 if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD) {
1164 write_bool_prop_line(ctx, "Selector is reversed",
1165 bt_field_class_option_with_selector_field_bool_selector_is_reversed(fc));
1166 } else if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD) {
1167 ranges = bt_field_class_option_with_selector_field_integer_unsigned_borrow_selector_ranges_const(fc);
1168 } else if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD) {
1169 ranges = bt_field_class_option_with_selector_field_integer_signed_borrow_selector_ranges_const(fc);
1170 selector_is_signed = true;
1171 }
1172
1173 if (ranges) {
1174 GArray *sorted_ranges = range_set_to_int_ranges(
1175 ranges, selector_is_signed);
1176
1177 BT_ASSERT_DBG(sorted_ranges);
1178 BT_ASSERT_DBG(sorted_ranges->len > 0);
1179 write_prop_name_line(ctx, "Selector ranges");
1180
1181 for (i = 0; i < sorted_ranges->len; i++) {
1182 write_sp(ctx);
1183 write_int_range(ctx,
1184 int_range_at(sorted_ranges, i),
1185 selector_is_signed);
1186 }
1187
1188 write_nl(ctx);
1189 g_array_free(sorted_ranges, TRUE);
1190 }
1191
1192 write_prop_name_line(ctx, "Content");
1193 write_sp(ctx);
1194 write_field_class(ctx,
1195 bt_field_class_option_borrow_field_class_const(fc));
1196 } else if (bt_field_class_type_is(fc_type,
1197 BT_FIELD_CLASS_TYPE_VARIANT)) {
1198 uint64_t option_count =
1199 bt_field_class_variant_get_option_count(fc);
1200
1201 if (option_count > 0) {
1202 if (wrote_user_attrs) {
1203 write_nl(ctx);
1204 write_indent(ctx);
1205 write_prop_name(ctx, "Options");
1206 g_string_append_c(ctx->str, ':');
1207 incr_indent(ctx);
1208 } else {
1209 /* Each option starts with its own newline */
1210 g_string_append_c(ctx->str, ':');
1211 }
1212
1213 for (i = 0; i < option_count; i++) {
1214 write_variant_field_class_option(ctx, fc, i);
1215 }
1216
1217 if (wrote_user_attrs) {
1218 decr_indent(ctx);
1219 }
1220 }
1221 }
1222
1223 decr_indent(ctx);
1224 }
1225
1226 static
1227 void write_root_field_class(struct details_write_ctx *ctx, const char *name,
1228 const bt_field_class *fc)
1229 {
1230 BT_ASSERT_DBG(name);
1231 BT_ASSERT_DBG(fc);
1232 write_indent(ctx);
1233 write_prop_name(ctx, name);
1234 g_string_append(ctx->str, ": ");
1235 write_field_class(ctx, fc);
1236 write_nl(ctx);
1237 }
1238
1239 static
1240 void write_event_class(struct details_write_ctx *ctx, const bt_event_class *ec)
1241 {
1242 const char *name = bt_event_class_get_name(ec);
1243 const char *emf_uri;
1244 const bt_field_class *fc;
1245 bt_event_class_log_level log_level;
1246
1247 write_indent(ctx);
1248 write_obj_type_name(ctx, "Event class");
1249
1250 /* Write name and ID */
1251 if (name) {
1252 g_string_append_printf(ctx->str, " `%s%s%s`",
1253 color_fg_green(ctx), name, color_reset(ctx));
1254 }
1255
1256 g_string_append(ctx->str, " (ID ");
1257 write_uint_prop_value(ctx, bt_event_class_get_id(ec));
1258 g_string_append(ctx->str, "):\n");
1259
1260 /* Write properties */
1261 incr_indent(ctx);
1262
1263 /* Write user attributes */
1264 write_user_attributes(ctx,
1265 bt_event_class_borrow_user_attributes_const(ec), true, NULL);
1266
1267 /* Write log level */
1268 if (bt_event_class_get_log_level(ec, &log_level) ==
1269 BT_PROPERTY_AVAILABILITY_AVAILABLE) {
1270 const char *ll_str = NULL;
1271
1272 switch (log_level) {
1273 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY:
1274 ll_str = "Emergency";
1275 break;
1276 case BT_EVENT_CLASS_LOG_LEVEL_ALERT:
1277 ll_str = "Alert";
1278 break;
1279 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL:
1280 ll_str = "Critical";
1281 break;
1282 case BT_EVENT_CLASS_LOG_LEVEL_ERROR:
1283 ll_str = "Error";
1284 break;
1285 case BT_EVENT_CLASS_LOG_LEVEL_WARNING:
1286 ll_str = "Warning";
1287 break;
1288 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE:
1289 ll_str = "Notice";
1290 break;
1291 case BT_EVENT_CLASS_LOG_LEVEL_INFO:
1292 ll_str = "Info";
1293 break;
1294 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM:
1295 ll_str = "Debug (system)";
1296 break;
1297 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM:
1298 ll_str = "Debug (program)";
1299 break;
1300 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS:
1301 ll_str = "Debug (process)";
1302 break;
1303 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE:
1304 ll_str = "Debug (module)";
1305 break;
1306 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT:
1307 ll_str = "Debug (unit)";
1308 break;
1309 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION:
1310 ll_str = "Debug (function)";
1311 break;
1312 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE:
1313 ll_str = "Debug (line)";
1314 break;
1315 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG:
1316 ll_str = "Debug";
1317 break;
1318 default:
1319 abort();
1320 }
1321
1322 write_str_prop_line(ctx, "Log level", ll_str);
1323 }
1324
1325 /* Write EMF URI */
1326 emf_uri = bt_event_class_get_emf_uri(ec);
1327 if (emf_uri) {
1328 write_str_prop_line(ctx, "EMF URI", emf_uri);
1329 }
1330
1331 /* Write specific context field class */
1332 fc = bt_event_class_borrow_specific_context_field_class_const(ec);
1333 if (fc) {
1334 write_root_field_class(ctx, "Specific context field class", fc);
1335 }
1336
1337 /* Write payload field class */
1338 fc = bt_event_class_borrow_payload_field_class_const(ec);
1339 if (fc) {
1340 write_root_field_class(ctx, "Payload field class", fc);
1341 }
1342
1343 decr_indent(ctx);
1344 }
1345
1346 static
1347 void write_clock_class_prop_lines(struct details_write_ctx *ctx,
1348 const bt_clock_class *cc)
1349 {
1350 int64_t offset_seconds;
1351 uint64_t offset_cycles;
1352 const char *str;
1353
1354 str = bt_clock_class_get_name(cc);
1355 if (str) {
1356 write_str_prop_line(ctx, "Name", str);
1357 }
1358
1359 write_user_attributes(ctx,
1360 bt_clock_class_borrow_user_attributes_const(cc), true, NULL);
1361 str = bt_clock_class_get_description(cc);
1362 if (str) {
1363 write_str_prop_line(ctx, "Description", str);
1364 }
1365
1366 write_uint_prop_line(ctx, "Frequency (Hz)",
1367 bt_clock_class_get_frequency(cc));
1368 write_uint_prop_line(ctx, "Precision (cycles)",
1369 bt_clock_class_get_precision(cc));
1370 bt_clock_class_get_offset(cc, &offset_seconds, &offset_cycles);
1371 write_int_prop_line(ctx, "Offset (s)", offset_seconds);
1372 write_uint_prop_line(ctx, "Offset (cycles)", offset_cycles);
1373 write_bool_prop_line(ctx, "Origin is Unix epoch",
1374 bt_clock_class_origin_is_unix_epoch(cc));
1375
1376 if (ctx->details_comp->cfg.with_uuid) {
1377 bt_uuid uuid = bt_clock_class_get_uuid(cc);
1378
1379 if (uuid) {
1380 write_uuid_prop_line(ctx, "UUID", uuid);
1381 }
1382 }
1383 }
1384
1385 static
1386 gint compare_event_classes(const bt_event_class **a, const bt_event_class **b)
1387 {
1388 uint64_t id_a = bt_event_class_get_id(*a);
1389 uint64_t id_b = bt_event_class_get_id(*b);
1390
1391 if (id_a < id_b) {
1392 return -1;
1393 } else if (id_a > id_b) {
1394 return 1;
1395 } else {
1396 return 0;
1397 }
1398 }
1399
1400 static
1401 void write_stream_class(struct details_write_ctx *ctx,
1402 const bt_stream_class *sc)
1403 {
1404 const bt_field_class *fc;
1405 GPtrArray *event_classes = g_ptr_array_new();
1406 uint64_t i;
1407
1408 write_indent(ctx);
1409 write_obj_type_name(ctx, "Stream class");
1410
1411 /* Write name and ID */
1412 if (ctx->details_comp->cfg.with_stream_class_name) {
1413 const char *name = bt_stream_class_get_name(sc);
1414
1415 if (name) {
1416 g_string_append(ctx->str, " `");
1417 write_str_prop_value(ctx, name);
1418 g_string_append(ctx->str, "`");
1419 }
1420 }
1421
1422 g_string_append(ctx->str, " (ID ");
1423 write_uint_prop_value(ctx, bt_stream_class_get_id(sc));
1424 g_string_append(ctx->str, "):\n");
1425
1426 /* Write properties */
1427 incr_indent(ctx);
1428
1429 /* Write user attributes */
1430 write_user_attributes(ctx,
1431 bt_stream_class_borrow_user_attributes_const(sc), true, NULL);
1432
1433 /* Write configuration */
1434 write_bool_prop_line(ctx,
1435 "Supports packets", bt_stream_class_supports_packets(sc));
1436
1437 if (bt_stream_class_supports_packets(sc)) {
1438 write_bool_prop_line(ctx,
1439 "Packets have beginning default clock snapshot",
1440 bt_stream_class_packets_have_beginning_default_clock_snapshot(sc));
1441 write_bool_prop_line(ctx,
1442 "Packets have end default clock snapshot",
1443 bt_stream_class_packets_have_end_default_clock_snapshot(sc));
1444 }
1445
1446 write_bool_prop_line(ctx,
1447 "Supports discarded events",
1448 bt_stream_class_supports_discarded_events(sc));
1449
1450 if (bt_stream_class_supports_discarded_events(sc)) {
1451 write_bool_prop_line(ctx,
1452 "Discarded events have default clock snapshots",
1453 bt_stream_class_discarded_events_have_default_clock_snapshots(sc));
1454 }
1455
1456 write_bool_prop_line(ctx,
1457 "Supports discarded packets",
1458 bt_stream_class_supports_discarded_packets(sc));
1459
1460 if (bt_stream_class_supports_discarded_packets(sc)) {
1461 write_bool_prop_line(ctx,
1462 "Discarded packets have default clock snapshots",
1463 bt_stream_class_discarded_packets_have_default_clock_snapshots(sc));
1464 }
1465
1466 /* Write default clock class */
1467 if (bt_stream_class_borrow_default_clock_class_const(sc)) {
1468 write_indent(ctx);
1469 write_prop_name(ctx, "Default clock class");
1470 g_string_append_c(ctx->str, ':');
1471 write_nl(ctx);
1472 incr_indent(ctx);
1473 write_clock_class_prop_lines(ctx,
1474 bt_stream_class_borrow_default_clock_class_const(sc));
1475 decr_indent(ctx);
1476 }
1477
1478 fc = bt_stream_class_borrow_packet_context_field_class_const(sc);
1479 if (fc) {
1480 write_root_field_class(ctx, "Packet context field class", fc);
1481 }
1482
1483 fc = bt_stream_class_borrow_event_common_context_field_class_const(sc);
1484 if (fc) {
1485 write_root_field_class(ctx, "Event common context field class",
1486 fc);
1487 }
1488
1489 for (i = 0; i < bt_stream_class_get_event_class_count(sc); i++) {
1490 g_ptr_array_add(event_classes,
1491 (gpointer) bt_stream_class_borrow_event_class_by_index_const(
1492 sc, i));
1493 }
1494
1495 g_ptr_array_sort(event_classes, (GCompareFunc) compare_event_classes);
1496
1497 for (i = 0; i < event_classes->len; i++) {
1498 write_event_class(ctx, event_classes->pdata[i]);
1499 }
1500
1501 decr_indent(ctx);
1502 g_ptr_array_free(event_classes, TRUE);
1503 }
1504
1505 static
1506 gint compare_stream_classes(const bt_stream_class **a, const bt_stream_class **b)
1507 {
1508 uint64_t id_a = bt_stream_class_get_id(*a);
1509 uint64_t id_b = bt_stream_class_get_id(*b);
1510
1511 if (id_a < id_b) {
1512 return -1;
1513 } else if (id_a > id_b) {
1514 return 1;
1515 } else {
1516 return 0;
1517 }
1518 }
1519
1520 static
1521 void write_trace_class(struct details_write_ctx *ctx, const bt_trace_class *tc)
1522 {
1523 GPtrArray *stream_classes = g_ptr_array_new();
1524 uint64_t i;
1525 bool printed_prop = false;
1526
1527 write_indent(ctx);
1528 write_obj_type_name(ctx, "Trace class");
1529
1530
1531 for (i = 0; i < bt_trace_class_get_stream_class_count(tc); i++) {
1532 g_ptr_array_add(stream_classes,
1533 (gpointer) bt_trace_class_borrow_stream_class_by_index_const(
1534 tc, i));
1535 }
1536
1537 g_ptr_array_sort(stream_classes, (GCompareFunc) compare_stream_classes);
1538
1539 if (stream_classes->len > 0) {
1540 if (!printed_prop) {
1541 g_string_append(ctx->str, ":\n");
1542 printed_prop = true;
1543 }
1544 }
1545
1546 incr_indent(ctx);
1547
1548 /* Write user attributes */
1549 write_user_attributes(ctx,
1550 bt_trace_class_borrow_user_attributes_const(tc), true,
1551 &printed_prop);
1552
1553 /* Write stream classes */
1554 for (i = 0; i < stream_classes->len; i++) {
1555 write_stream_class(ctx, stream_classes->pdata[i]);
1556 }
1557
1558 if (!printed_prop) {
1559 write_nl(ctx);
1560 }
1561
1562 decr_indent(ctx);
1563 g_ptr_array_free(stream_classes, TRUE);
1564 }
1565
1566 static
1567 int try_write_meta(struct details_write_ctx *ctx, const bt_trace_class *tc,
1568 const bt_stream_class *sc, const bt_event_class *ec)
1569 {
1570 int ret = 0;
1571
1572 BT_ASSERT_DBG(tc);
1573
1574 if (details_need_to_write_trace_class(ctx, tc)) {
1575 uint64_t sc_i;
1576
1577 if (ctx->details_comp->cfg.compact &&
1578 ctx->details_comp->printed_something) {
1579 /*
1580 * There are no empty line between messages in
1581 * compact mode, so write one here to decouple
1582 * the trace class from the next message.
1583 */
1584 write_nl(ctx);
1585 }
1586
1587 /*
1588 * write_trace_class() also writes all its stream
1589 * classes their event classes, so we don't need to
1590 * rewrite `sc`.
1591 */
1592 write_trace_class(ctx, tc);
1593
1594 /*
1595 * Mark this trace class as written, as well as all
1596 * its stream classes and their event classes.
1597 */
1598 ret = details_did_write_trace_class(ctx, tc);
1599 if (ret) {
1600 goto end;
1601 }
1602
1603 for (sc_i = 0; sc_i < bt_trace_class_get_stream_class_count(tc);
1604 sc_i++) {
1605 uint64_t ec_i;
1606 const bt_stream_class *tc_sc =
1607 bt_trace_class_borrow_stream_class_by_index_const(
1608 tc, sc_i);
1609
1610 details_did_write_meta_object(ctx, tc, tc_sc);
1611
1612 for (ec_i = 0; ec_i <
1613 bt_stream_class_get_event_class_count(tc_sc);
1614 ec_i++) {
1615 details_did_write_meta_object(ctx, tc,
1616 bt_stream_class_borrow_event_class_by_index_const(
1617 tc_sc, ec_i));
1618 }
1619 }
1620
1621 goto end;
1622 }
1623
1624 if (sc && details_need_to_write_meta_object(ctx, tc, sc)) {
1625 uint64_t ec_i;
1626
1627 BT_ASSERT_DBG(tc);
1628
1629 if (ctx->details_comp->cfg.compact &&
1630 ctx->details_comp->printed_something) {
1631 /*
1632 * There are no empty line between messages in
1633 * compact mode, so write one here to decouple
1634 * the stream class from the next message.
1635 */
1636 write_nl(ctx);
1637 }
1638
1639 /*
1640 * write_stream_class() also writes all its event
1641 * classes, so we don't need to rewrite `ec`.
1642 */
1643 write_stream_class(ctx, sc);
1644
1645 /*
1646 * Mark this stream class as written, as well as all its
1647 * event classes.
1648 */
1649 details_did_write_meta_object(ctx, tc, sc);
1650
1651 for (ec_i = 0; ec_i <
1652 bt_stream_class_get_event_class_count(sc);
1653 ec_i++) {
1654 details_did_write_meta_object(ctx, tc,
1655 bt_stream_class_borrow_event_class_by_index_const(
1656 sc, ec_i));
1657 }
1658
1659 goto end;
1660 }
1661
1662 if (ec && details_need_to_write_meta_object(ctx, tc, ec)) {
1663 BT_ASSERT_DBG(sc);
1664
1665 if (ctx->details_comp->cfg.compact &&
1666 ctx->details_comp->printed_something) {
1667 /*
1668 * There are no empty line between messages in
1669 * compact mode, so write one here to decouple
1670 * the event class from the next message.
1671 */
1672 write_nl(ctx);
1673 }
1674
1675 write_event_class(ctx, ec);
1676 details_did_write_meta_object(ctx, tc, ec);
1677 goto end;
1678 }
1679
1680 end:
1681 return ret;
1682 }
1683
1684 static
1685 void write_time_str(struct details_write_ctx *ctx, const char *str)
1686 {
1687 if (!ctx->details_comp->cfg.with_time) {
1688 goto end;
1689 }
1690
1691 g_string_append_printf(ctx->str, "[%s%s%s%s]",
1692 color_bold(ctx), color_fg_blue(ctx), str, color_reset(ctx));
1693
1694 if (ctx->details_comp->cfg.compact) {
1695 write_sp(ctx);
1696 } else {
1697 write_nl(ctx);
1698 }
1699
1700 end:
1701 return;
1702 }
1703
1704 static
1705 void write_time(struct details_write_ctx *ctx, const bt_clock_snapshot *cs)
1706 {
1707 bt_clock_snapshot_get_ns_from_origin_status cs_status;
1708 int64_t ns_from_origin;
1709 char buf[32];
1710
1711 if (!ctx->details_comp->cfg.with_time) {
1712 goto end;
1713 }
1714
1715 format_uint(buf, bt_clock_snapshot_get_value(cs), 10);
1716 g_string_append_printf(ctx->str, "[%s%s%s%s%s",
1717 color_bold(ctx), color_fg_blue(ctx), buf,
1718 color_reset(ctx),
1719 ctx->details_comp->cfg.compact ? "" : " cycles");
1720 cs_status = bt_clock_snapshot_get_ns_from_origin(cs, &ns_from_origin);
1721 if (cs_status == BT_CLOCK_SNAPSHOT_GET_NS_FROM_ORIGIN_STATUS_OK) {
1722 format_int(buf, ns_from_origin, 10);
1723 g_string_append_printf(ctx->str, "%s %s%s%s%s%s",
1724 ctx->details_comp->cfg.compact ? "" : ",",
1725 color_bold(ctx), color_fg_blue(ctx), buf,
1726 color_reset(ctx),
1727 ctx->details_comp->cfg.compact ? "" : " ns from origin");
1728 }
1729
1730 g_string_append(ctx->str, "]");
1731
1732 if (ctx->details_comp->cfg.compact) {
1733 write_sp(ctx);
1734 } else {
1735 write_nl(ctx);
1736 }
1737
1738 end:
1739 return;
1740 }
1741
1742 static
1743 int write_message_follow_tag(struct details_write_ctx *ctx,
1744 const bt_stream *stream)
1745 {
1746 int ret;
1747 uint64_t unique_trace_id;
1748 const bt_stream_class *sc = bt_stream_borrow_class_const(stream);
1749 const bt_trace *trace = bt_stream_borrow_trace_const(stream);
1750
1751 ret = details_trace_unique_id(ctx, trace, &unique_trace_id);
1752 if (ret) {
1753 goto end;
1754 }
1755
1756 if (ctx->details_comp->cfg.compact) {
1757 g_string_append_printf(ctx->str,
1758 "%s{%s%" PRIu64 " %" PRIu64 " %" PRIu64 "%s%s}%s ",
1759 color_fg_cyan(ctx), color_bold(ctx),
1760 unique_trace_id, bt_stream_class_get_id(sc),
1761 bt_stream_get_id(stream),
1762 color_reset(ctx), color_fg_cyan(ctx), color_reset(ctx));
1763 } else {
1764 g_string_append_printf(ctx->str,
1765 "%s{Trace %s%" PRIu64 "%s%s, Stream class ID %s%" PRIu64 "%s%s, Stream ID %s%" PRIu64 "%s%s}%s\n",
1766 color_fg_cyan(ctx),
1767 color_bold(ctx), unique_trace_id,
1768 color_reset(ctx), color_fg_cyan(ctx),
1769 color_bold(ctx), bt_stream_class_get_id(sc),
1770 color_reset(ctx), color_fg_cyan(ctx),
1771 color_bold(ctx), bt_stream_get_id(stream),
1772 color_reset(ctx), color_fg_cyan(ctx),
1773 color_reset(ctx));
1774 }
1775
1776 end:
1777 return ret;
1778 }
1779
1780 static
1781 void write_field(struct details_write_ctx *ctx, const bt_field *field,
1782 const char *name)
1783 {
1784 uint64_t i;
1785 bt_field_class_type fc_type = bt_field_get_class_type(field);
1786 const bt_field_class *fc;
1787 char buf[64];
1788
1789 /* Write field's name */
1790 if (name) {
1791 write_compound_member_name(ctx, name);
1792 }
1793
1794 /* Write field's value */
1795 if (fc_type == BT_FIELD_CLASS_TYPE_BOOL) {
1796 write_sp(ctx);
1797 write_bool_prop_value(ctx, bt_field_bool_get_value(field));
1798 } else if (fc_type == BT_FIELD_CLASS_TYPE_BIT_ARRAY) {
1799 format_uint(buf, bt_field_bit_array_get_value_as_integer(field),
1800 16);
1801 write_sp(ctx);
1802 write_uint_str_prop_value(ctx, buf);
1803 } else if (bt_field_class_type_is(fc_type,
1804 BT_FIELD_CLASS_TYPE_INTEGER)) {
1805 unsigned int fmt_base;
1806 bt_field_class_integer_preferred_display_base base;
1807
1808 fc = bt_field_borrow_class_const(field);
1809 base = bt_field_class_integer_get_preferred_display_base(fc);
1810
1811 switch (base) {
1812 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
1813 fmt_base = 10;
1814 break;
1815 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
1816 fmt_base = 8;
1817 break;
1818 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
1819 fmt_base = 2;
1820 break;
1821 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
1822 fmt_base = 16;
1823 break;
1824 default:
1825 abort();
1826 }
1827
1828 if (bt_field_class_type_is(fc_type,
1829 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) {
1830 format_uint(buf,
1831 bt_field_integer_unsigned_get_value(field),
1832 fmt_base);
1833 write_sp(ctx);
1834 write_uint_str_prop_value(ctx, buf);
1835 } else {
1836 format_int(buf,
1837 bt_field_integer_signed_get_value(field),
1838 fmt_base);
1839 write_sp(ctx);
1840 write_int_str_prop_value(ctx, buf);
1841 }
1842 } else if (fc_type == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) {
1843 write_sp(ctx);
1844 write_float_prop_value(ctx, bt_field_real_single_precision_get_value(field));
1845 } else if (fc_type == BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL) {
1846 write_sp(ctx);
1847 write_float_prop_value(ctx, bt_field_real_double_precision_get_value(field));
1848 } else if (fc_type == BT_FIELD_CLASS_TYPE_STRING) {
1849 write_sp(ctx);
1850 write_str_prop_value(ctx, bt_field_string_get_value(field));
1851 } else if (fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) {
1852 uint64_t member_count;
1853
1854 fc = bt_field_borrow_class_const(field);
1855 member_count = bt_field_class_structure_get_member_count(fc);
1856
1857 if (member_count > 0) {
1858 incr_indent(ctx);
1859
1860 for (i = 0; i < member_count; i++) {
1861 const bt_field_class_structure_member *member =
1862 bt_field_class_structure_borrow_member_by_index_const(
1863 fc, i);
1864 const bt_field *member_field =
1865 bt_field_structure_borrow_member_field_by_index_const(
1866 field, i);
1867
1868 write_nl(ctx);
1869 write_field(ctx, member_field,
1870 bt_field_class_structure_member_get_name(member));
1871 }
1872
1873 decr_indent(ctx);
1874 } else {
1875 write_sp(ctx);
1876 write_none_prop_value(ctx, "Empty");
1877 }
1878 } else if (bt_field_class_type_is(fc_type, BT_FIELD_CLASS_TYPE_ARRAY)) {
1879 uint64_t length = bt_field_array_get_length(field);
1880
1881 if (length == 0) {
1882 write_sp(ctx);
1883 write_none_prop_value(ctx, "Empty");
1884 } else {
1885 g_string_append(ctx->str, " Length ");
1886 write_uint_prop_value(ctx, length);
1887 g_string_append_c(ctx->str, ':');
1888 }
1889
1890 incr_indent(ctx);
1891
1892 for (i = 0; i < length; i++) {
1893 const bt_field *elem_field =
1894 bt_field_array_borrow_element_field_by_index_const(
1895 field, i);
1896
1897 write_nl(ctx);
1898 write_array_index(ctx, i, color_fg_cyan(ctx));
1899 write_field(ctx, elem_field, NULL);
1900 }
1901
1902 decr_indent(ctx);
1903 } else if (bt_field_class_type_is(fc_type,
1904 BT_FIELD_CLASS_TYPE_OPTION)) {
1905 const bt_field *content_field =
1906 bt_field_option_borrow_field_const(field);
1907
1908 if (!content_field) {
1909 write_sp(ctx);
1910 write_none_prop_value(ctx, "None");
1911 } else {
1912 write_field(ctx, content_field, NULL);
1913 }
1914 } else if (bt_field_class_type_is(fc_type,
1915 BT_FIELD_CLASS_TYPE_VARIANT)) {
1916 write_field(ctx,
1917 bt_field_variant_borrow_selected_option_field_const(
1918 field), NULL);
1919 } else {
1920 abort();
1921 }
1922 }
1923
1924 static
1925 void write_root_field(struct details_write_ctx *ctx, const char *name,
1926 const bt_field *field)
1927 {
1928 BT_ASSERT_DBG(name);
1929 BT_ASSERT_DBG(field);
1930 write_indent(ctx);
1931 write_prop_name(ctx, name);
1932 g_string_append(ctx->str, ":");
1933 write_field(ctx, field, NULL);
1934 write_nl(ctx);
1935 }
1936
1937 static
1938 int write_event_message(struct details_write_ctx *ctx,
1939 const bt_message *msg)
1940 {
1941 int ret = 0;
1942 const bt_event *event = bt_message_event_borrow_event_const(msg);
1943 const bt_stream *stream = bt_event_borrow_stream_const(event);
1944 const bt_event_class *ec = bt_event_borrow_class_const(event);
1945 const bt_stream_class *sc = bt_event_class_borrow_stream_class_const(ec);
1946 const bt_trace_class *tc = bt_stream_class_borrow_trace_class_const(sc);
1947 const char *ec_name;
1948 const bt_field *field;
1949
1950 ret = try_write_meta(ctx, tc, sc, ec);
1951 if (ret) {
1952 goto end;
1953 }
1954
1955 if (!ctx->details_comp->cfg.with_data) {
1956 goto end;
1957 }
1958
1959 if (ctx->str->len > 0) {
1960 /*
1961 * Output buffer contains metadata: separate blocks with
1962 * newline.
1963 */
1964 write_nl(ctx);
1965 }
1966
1967 /* Write time */
1968 if (bt_stream_class_borrow_default_clock_class_const(sc)) {
1969 write_time(ctx,
1970 bt_message_event_borrow_default_clock_snapshot_const(
1971 msg));
1972 }
1973
1974 /* Write follow tag for message */
1975 ret = write_message_follow_tag(ctx, stream);
1976 if (ret) {
1977 goto end;
1978 }
1979
1980 /* Write object's basic properties */
1981 write_obj_type_name(ctx, "Event");
1982 ec_name = bt_event_class_get_name(ec);
1983 if (ec_name) {
1984 g_string_append_printf(ctx->str, " `%s%s%s`",
1985 color_fg_green(ctx), ec_name, color_reset(ctx));
1986 }
1987
1988 g_string_append(ctx->str, " (");
1989
1990 if (!ctx->details_comp->cfg.compact) {
1991 g_string_append(ctx->str, "Class ID ");
1992 }
1993
1994 write_uint_prop_value(ctx, bt_event_class_get_id(ec));
1995 g_string_append(ctx->str, ")");
1996
1997 if (ctx->details_comp->cfg.compact) {
1998 write_nl(ctx);
1999 goto end;
2000 }
2001
2002 /* Write fields */
2003 g_string_append(ctx->str, ":\n");
2004 incr_indent(ctx);
2005 field = bt_event_borrow_common_context_field_const(event);
2006 if (field) {
2007 write_root_field(ctx, "Common context", field);
2008 }
2009
2010 field = bt_event_borrow_specific_context_field_const(event);
2011 if (field) {
2012 write_root_field(ctx, "Specific context", field);
2013 }
2014
2015 field = bt_event_borrow_payload_field_const(event);
2016 if (field) {
2017 write_root_field(ctx, "Payload", field);
2018 }
2019
2020 decr_indent(ctx);
2021
2022 end:
2023 return ret;
2024 }
2025
2026 static
2027 gint compare_streams(const bt_stream **a, const bt_stream **b)
2028 {
2029 uint64_t id_a = bt_stream_get_id(*a);
2030 uint64_t id_b = bt_stream_get_id(*b);
2031
2032 if (id_a < id_b) {
2033 return -1;
2034 } else if (id_a > id_b) {
2035 return 1;
2036 } else {
2037 const bt_stream_class *a_sc = bt_stream_borrow_class_const(*a);
2038 const bt_stream_class *b_sc = bt_stream_borrow_class_const(*b);
2039 uint64_t a_sc_id = bt_stream_class_get_id(a_sc);
2040 uint64_t b_sc_id = bt_stream_class_get_id(b_sc);
2041
2042 if (a_sc_id < b_sc_id) {
2043 return -1;
2044 } else if (a_sc_id > b_sc_id) {
2045 return 1;
2046 } else {
2047 return 0;
2048 }
2049 }
2050 }
2051
2052 static
2053 void write_trace(struct details_write_ctx *ctx, const bt_trace *trace)
2054 {
2055 GPtrArray *streams = g_ptr_array_new();
2056 uint64_t i;
2057 bool printed_prop = false;
2058 GPtrArray *env_names = g_ptr_array_new();
2059 uint64_t env_count;
2060
2061 write_indent(ctx);
2062 write_obj_type_name(ctx, "Trace");
2063
2064 /* Write name */
2065 if (ctx->details_comp->cfg.with_trace_name) {
2066 const char *name = bt_trace_get_name(trace);
2067 if (name) {
2068 g_string_append(ctx->str, " `");
2069 write_str_prop_value(ctx, name);
2070 g_string_append(ctx->str, "`");
2071 }
2072 }
2073
2074 /* Write properties */
2075 incr_indent(ctx);
2076
2077 /* Write UUID */
2078 if (ctx->details_comp->cfg.with_uuid) {
2079 bt_uuid uuid = bt_trace_get_uuid(trace);
2080
2081 if (uuid) {
2082 if (!printed_prop) {
2083 g_string_append(ctx->str, ":\n");
2084 printed_prop = true;
2085 }
2086
2087 write_uuid_prop_line(ctx, "UUID", uuid);
2088 }
2089 }
2090
2091 /* Write environment */
2092 env_count = bt_trace_get_environment_entry_count(trace);
2093 if (env_count > 0) {
2094 if (!printed_prop) {
2095 g_string_append(ctx->str, ":\n");
2096 printed_prop = true;
2097 }
2098
2099 write_indent(ctx);
2100 write_prop_name(ctx, "Environment");
2101 g_string_append(ctx->str, " (");
2102 write_uint_prop_value(ctx, env_count);
2103 g_string_append_printf(ctx->str, " entr%s):",
2104 env_count == 1 ? "y" : "ies");
2105 write_nl(ctx);
2106 incr_indent(ctx);
2107
2108 for (i = 0; i < env_count; i++) {
2109 const char *name;
2110 const bt_value *value;
2111
2112 bt_trace_borrow_environment_entry_by_index_const(
2113 trace, i, &name, &value);
2114 g_ptr_array_add(env_names, (gpointer) name);
2115 }
2116
2117 g_ptr_array_sort(env_names, (GCompareFunc) compare_strings);
2118
2119 for (i = 0; i < env_names->len; i++) {
2120 const char *name = env_names->pdata[i];
2121 const bt_value *value =
2122 bt_trace_borrow_environment_entry_value_by_name_const(
2123 trace, name);
2124
2125 BT_ASSERT_DBG(value);
2126 write_compound_member_name(ctx, name);
2127 write_sp(ctx);
2128
2129 if (bt_value_get_type(value) ==
2130 BT_VALUE_TYPE_SIGNED_INTEGER) {
2131 write_int_prop_value(ctx,
2132 bt_value_integer_signed_get(value));
2133 } else if (bt_value_get_type(value) ==
2134 BT_VALUE_TYPE_STRING) {
2135 write_str_prop_value(ctx,
2136 bt_value_string_get(value));
2137 } else {
2138 abort();
2139 }
2140
2141 write_nl(ctx);
2142 }
2143
2144 decr_indent(ctx);
2145 }
2146
2147 for (i = 0; i < bt_trace_get_stream_count(trace); i++) {
2148 g_ptr_array_add(streams,
2149 (gpointer) bt_trace_borrow_stream_by_index_const(
2150 trace, i));
2151 }
2152
2153 g_ptr_array_sort(streams, (GCompareFunc) compare_streams);
2154
2155 if (streams->len > 0 && !printed_prop) {
2156 g_string_append(ctx->str, ":\n");
2157 printed_prop = true;
2158 }
2159
2160 for (i = 0; i < streams->len; i++) {
2161 const bt_stream *stream = streams->pdata[i];
2162
2163 write_indent(ctx);
2164 write_obj_type_name(ctx, "Stream");
2165 g_string_append(ctx->str, " (ID ");
2166 write_uint_prop_value(ctx, bt_stream_get_id(stream));
2167 g_string_append(ctx->str, ", Class ID ");
2168 write_uint_prop_value(ctx, bt_stream_class_get_id(
2169 bt_stream_borrow_class_const(stream)));
2170 g_string_append(ctx->str, ")");
2171 write_nl(ctx);
2172 }
2173
2174 decr_indent(ctx);
2175
2176 if (!printed_prop) {
2177 write_nl(ctx);
2178 }
2179
2180 g_ptr_array_free(streams, TRUE);
2181 g_ptr_array_free(env_names, TRUE);
2182 }
2183
2184 static
2185 int write_stream_beginning_message(struct details_write_ctx *ctx,
2186 const bt_message *msg)
2187 {
2188 int ret = 0;
2189 const bt_stream *stream =
2190 bt_message_stream_beginning_borrow_stream_const(msg);
2191 const bt_trace *trace = bt_stream_borrow_trace_const(stream);
2192 const bt_stream_class *sc = bt_stream_borrow_class_const(stream);
2193 const bt_clock_class *cc = bt_stream_class_borrow_default_clock_class_const(sc);
2194 const bt_trace_class *tc = bt_stream_class_borrow_trace_class_const(sc);
2195 const char *name;
2196
2197 ret = try_write_meta(ctx, tc, sc, NULL);
2198 if (ret) {
2199 goto end;
2200 }
2201
2202 if (!ctx->details_comp->cfg.with_data) {
2203 goto end;
2204 }
2205
2206 if (ctx->str->len > 0) {
2207 /*
2208 * Output buffer contains metadata: separate blocks with
2209 * newline.
2210 */
2211 write_nl(ctx);
2212 }
2213
2214 /* Write time */
2215 if (cc) {
2216 const bt_clock_snapshot *cs;
2217 bt_message_stream_clock_snapshot_state cs_state =
2218 bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg, &cs);
2219
2220 if (cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
2221 write_time(ctx, cs);
2222 } else {
2223 write_time_str(ctx, "Unknown");
2224 }
2225 }
2226
2227 /* Write follow tag for message */
2228 ret = write_message_follow_tag(ctx, stream);
2229 if (ret) {
2230 goto end;
2231 }
2232
2233 /* Write stream properties */
2234 write_obj_type_name(ctx, "Stream beginning");
2235
2236 if (ctx->details_comp->cfg.compact) {
2237 write_nl(ctx);
2238 goto end;
2239 }
2240
2241 g_string_append(ctx->str, ":\n");
2242 incr_indent(ctx);
2243
2244 if (ctx->details_comp->cfg.with_stream_name) {
2245 name = bt_stream_get_name(stream);
2246 if (name) {
2247 write_str_prop_line(ctx, "Name", name);
2248 }
2249 }
2250
2251 if (ctx->details_comp->cfg.with_stream_class_name) {
2252 name = bt_stream_class_get_name(sc);
2253 if (name) {
2254 write_str_prop_line(ctx, "Class name", name);
2255 }
2256 }
2257
2258 write_trace(ctx, trace);
2259 decr_indent(ctx);
2260
2261 end:
2262 return ret;
2263 }
2264
2265 static
2266 int write_stream_end_message(struct details_write_ctx *ctx,
2267 const bt_message *msg)
2268 {
2269 int ret = 0;
2270 const bt_stream *stream =
2271 bt_message_stream_end_borrow_stream_const(msg);
2272 const bt_stream_class *sc =
2273 bt_stream_borrow_class_const(stream);
2274 const bt_clock_class *cc =
2275 bt_stream_class_borrow_default_clock_class_const(sc);
2276
2277 if (!ctx->details_comp->cfg.with_data) {
2278 goto end;
2279 }
2280
2281 /* Write time */
2282 if (cc) {
2283 const bt_clock_snapshot *cs;
2284 bt_message_stream_clock_snapshot_state cs_state =
2285 bt_message_stream_end_borrow_default_clock_snapshot_const(msg, &cs);
2286
2287 if (cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
2288 write_time(ctx, cs);
2289 } else {
2290 write_time_str(ctx, "Unknown");
2291 }
2292 }
2293
2294 /* Write follow tag for message */
2295 ret = write_message_follow_tag(ctx, stream);
2296 if (ret) {
2297 goto end;
2298 }
2299
2300 /* Write stream properties */
2301 write_obj_type_name(ctx, "Stream end\n");
2302
2303 end:
2304 return ret;
2305 }
2306
2307 static
2308 int write_packet_beginning_message(struct details_write_ctx *ctx,
2309 const bt_message *msg)
2310 {
2311 int ret = 0;
2312 const bt_packet *packet =
2313 bt_message_packet_beginning_borrow_packet_const(msg);
2314 const bt_stream *stream = bt_packet_borrow_stream_const(packet);
2315 const bt_stream_class *sc = bt_stream_borrow_class_const(stream);
2316 const bt_field *field;
2317
2318 if (!ctx->details_comp->cfg.with_data) {
2319 goto end;
2320 }
2321
2322 /* Write time */
2323 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(sc)) {
2324 write_time(ctx,
2325 bt_message_packet_beginning_borrow_default_clock_snapshot_const(
2326 msg));
2327 }
2328
2329 /* Write follow tag for message */
2330 ret = write_message_follow_tag(ctx, stream);
2331 if (ret) {
2332 goto end;
2333 }
2334
2335 write_obj_type_name(ctx, "Packet beginning");
2336
2337 if (ctx->details_comp->cfg.compact) {
2338 write_nl(ctx);
2339 goto end;
2340 }
2341
2342 /* Write field */
2343 field = bt_packet_borrow_context_field_const(packet);
2344 if (field) {
2345 g_string_append(ctx->str, ":\n");
2346 incr_indent(ctx);
2347 write_root_field(ctx, "Context", field);
2348 decr_indent(ctx);
2349 } else {
2350 write_nl(ctx);
2351 }
2352
2353 end:
2354 return ret;
2355 }
2356
2357 static
2358 int write_discarded_items_message(struct details_write_ctx *ctx,
2359 const char *name, const bt_stream *stream,
2360 const bt_clock_snapshot *beginning_cs,
2361 const bt_clock_snapshot *end_cs, uint64_t count)
2362 {
2363 int ret = 0;
2364
2365 /* Write times */
2366 if (beginning_cs) {
2367 write_time(ctx, beginning_cs);
2368 BT_ASSERT_DBG(end_cs);
2369 write_time(ctx, end_cs);
2370 }
2371
2372 /* Write follow tag for message */
2373 ret = write_message_follow_tag(ctx, stream);
2374 if (ret) {
2375 goto end;
2376 }
2377
2378 write_obj_type_name(ctx, "Discarded ");
2379 write_obj_type_name(ctx, name);
2380
2381 /* Write count */
2382 if (count == UINT64_C(-1)) {
2383 write_nl(ctx);
2384 goto end;
2385 }
2386
2387 g_string_append(ctx->str, " (");
2388 write_uint_prop_value(ctx, count);
2389 g_string_append_printf(ctx->str, " %s)\n", name);
2390
2391 end:
2392 return ret;
2393 }
2394
2395 static
2396 int write_discarded_events_message(struct details_write_ctx *ctx,
2397 const bt_message *msg)
2398 {
2399 int ret = 0;
2400 const bt_stream *stream = bt_message_discarded_events_borrow_stream_const(
2401 msg);
2402 const bt_stream_class *sc = bt_stream_borrow_class_const(stream);
2403 const bt_clock_snapshot *beginning_cs = NULL;
2404 const bt_clock_snapshot *end_cs = NULL;
2405 uint64_t count;
2406
2407 if (!ctx->details_comp->cfg.with_data) {
2408 goto end;
2409 }
2410
2411 if (bt_stream_class_discarded_events_have_default_clock_snapshots(sc)) {
2412 beginning_cs =
2413 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
2414 msg);
2415 end_cs =
2416 bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
2417 msg);
2418 }
2419
2420 if (bt_message_discarded_events_get_count(msg, &count) !=
2421 BT_PROPERTY_AVAILABILITY_AVAILABLE) {
2422 count = UINT64_C(-1);
2423 }
2424
2425 ret = write_discarded_items_message(ctx, "events", stream,
2426 beginning_cs, end_cs, count);
2427
2428 end:
2429 return ret;
2430 }
2431
2432 static
2433 int write_discarded_packets_message(struct details_write_ctx *ctx,
2434 const bt_message *msg)
2435 {
2436 int ret = 0;
2437 const bt_stream *stream = bt_message_discarded_packets_borrow_stream_const(
2438 msg);
2439 const bt_stream_class *sc = bt_stream_borrow_class_const(stream);
2440 const bt_clock_snapshot *beginning_cs = NULL;
2441 const bt_clock_snapshot *end_cs = NULL;
2442 uint64_t count;
2443
2444 if (!ctx->details_comp->cfg.with_data) {
2445 goto end;
2446 }
2447
2448 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(sc)) {
2449 beginning_cs =
2450 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
2451 msg);
2452 end_cs =
2453 bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
2454 msg);
2455 }
2456
2457 if (bt_message_discarded_packets_get_count(msg, &count) !=
2458 BT_PROPERTY_AVAILABILITY_AVAILABLE) {
2459 count = UINT64_C(-1);
2460 }
2461
2462 ret = write_discarded_items_message(ctx, "packets", stream,
2463 beginning_cs, end_cs, count);
2464
2465 end:
2466 return ret;
2467 }
2468
2469 static
2470 int write_packet_end_message(struct details_write_ctx *ctx,
2471 const bt_message *msg)
2472 {
2473 int ret = 0;
2474 const bt_packet *packet =
2475 bt_message_packet_end_borrow_packet_const(msg);
2476 const bt_stream *stream = bt_packet_borrow_stream_const(packet);
2477 const bt_stream_class *sc = bt_stream_borrow_class_const(stream);
2478
2479 if (!ctx->details_comp->cfg.with_data) {
2480 goto end;
2481 }
2482
2483 /* Write time */
2484 if (bt_stream_class_packets_have_end_default_clock_snapshot(sc)) {
2485 write_time(ctx,
2486 bt_message_packet_end_borrow_default_clock_snapshot_const(
2487 msg));
2488 }
2489
2490 /* Write follow tag for message */
2491 ret = write_message_follow_tag(ctx, stream);
2492 if (ret) {
2493 goto end;
2494 }
2495
2496 write_obj_type_name(ctx, "Packet end");
2497 write_nl(ctx);
2498
2499 end:
2500 return ret;
2501 }
2502
2503 static
2504 int write_message_iterator_inactivity_message(struct details_write_ctx *ctx,
2505 const bt_message *msg)
2506 {
2507 int ret = 0;
2508 const bt_clock_snapshot *cs =
2509 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
2510 msg);
2511
2512 /* Write time */
2513 write_time(ctx, cs);
2514 write_obj_type_name(ctx, "Message iterator inactivity");
2515
2516 if (ctx->details_comp->cfg.compact) {
2517 write_nl(ctx);
2518 goto end;
2519 }
2520
2521 /* Write clock class properties */
2522 g_string_append(ctx->str, ":\n");
2523 incr_indent(ctx);
2524 write_indent(ctx);
2525 write_prop_name(ctx, "Clock class");
2526 g_string_append_c(ctx->str, ':');
2527 write_nl(ctx);
2528 incr_indent(ctx);
2529 write_clock_class_prop_lines(ctx,
2530 bt_clock_snapshot_borrow_clock_class_const(cs));
2531 decr_indent(ctx);
2532
2533 end:
2534 return ret;
2535 }
2536
2537 BT_HIDDEN
2538 int details_write_message(struct details_comp *details_comp,
2539 const bt_message *msg)
2540 {
2541 int ret = 0;
2542 struct details_write_ctx ctx = {
2543 .details_comp = details_comp,
2544 .str = details_comp->str,
2545 .indent_level = 0,
2546 };
2547
2548 /* Reset output buffer */
2549 g_string_assign(details_comp->str, "");
2550
2551 switch (bt_message_get_type(msg)) {
2552 case BT_MESSAGE_TYPE_EVENT:
2553 ret = write_event_message(&ctx, msg);
2554 break;
2555 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
2556 ret = write_message_iterator_inactivity_message(&ctx, msg);
2557 break;
2558 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
2559 ret = write_stream_beginning_message(&ctx, msg);
2560 break;
2561 case BT_MESSAGE_TYPE_STREAM_END:
2562 ret = write_stream_end_message(&ctx, msg);
2563 break;
2564 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
2565 ret = write_packet_beginning_message(&ctx, msg);
2566 break;
2567 case BT_MESSAGE_TYPE_PACKET_END:
2568 ret = write_packet_end_message(&ctx, msg);
2569 break;
2570 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
2571 ret = write_discarded_events_message(&ctx, msg);
2572 break;
2573 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
2574 ret = write_discarded_packets_message(&ctx, msg);
2575 break;
2576 default:
2577 abort();
2578 }
2579
2580 /*
2581 * If this component printed at least one character so far, and
2582 * we're not in compact mode, and there's something in the
2583 * output buffer for this message, then prepend a newline to the
2584 * output buffer to visually separate message blocks.
2585 */
2586 if (details_comp->printed_something && !details_comp->cfg.compact &&
2587 details_comp->str->len > 0) {
2588 /* TODO: Optimize this */
2589 g_string_prepend_c(details_comp->str, '\n');
2590 }
2591
2592 return ret;
2593 }
This page took 0.111432 seconds and 5 git commands to generate.