2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
7 #include <babeltrace2/babeltrace.h>
12 #include "common/assert.h"
13 #include "common/common.h"
14 #include "common/uuid.h"
17 #include "obj-lifetime-mgmt.h"
21 const char *plural(uint64_t value
)
23 return value
== 1 ? "" : "s";
27 void incr_indent_by(struct details_write_ctx
*ctx
, unsigned int value
)
30 ctx
->indent_level
+= value
;
34 void incr_indent(struct details_write_ctx
*ctx
)
36 incr_indent_by(ctx
, 2);
40 void decr_indent_by(struct details_write_ctx
*ctx
, unsigned int value
)
43 BT_ASSERT_DBG(ctx
->indent_level
>= value
);
44 ctx
->indent_level
-= value
;
48 void decr_indent(struct details_write_ctx
*ctx
)
50 decr_indent_by(ctx
, 2);
54 void format_uint(char *buf
, uint64_t value
, unsigned int base
)
56 const char *spec
= "%" PRIu64
;
57 char *buf_start
= buf
;
58 unsigned int digits_per_group
= 3;
60 bool sep_digits
= true;
65 /* TODO: Support binary format */
81 * Do not insert digit separators for numbers
82 * under 10,000 as it looks weird.
92 #pragma GCC diagnostic push
93 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
94 sprintf(buf_start
, spec
, value
);
95 #pragma GCC diagnostic pop
98 bt_common_sep_digits(buf_start
, digits_per_group
, sep
);
103 void format_int(char *buf
, int64_t value
, unsigned int base
)
105 const char *spec
= "%" PRIu64
;
106 char *buf_start
= buf
;
107 unsigned int digits_per_group
= 3;
109 bool sep_digits
= true;
110 uint64_t abs_value
= value
< 0 ? (uint64_t) -value
: (uint64_t) value
;
120 /* TODO: Support binary format */
122 strcpy(buf_start
, "0x");
124 digits_per_group
= 4;
129 strcpy(buf_start
, "0");
134 if (value
>= -9999 && value
<= 9999) {
136 * Do not insert digit separators for numbers
137 * over -10,000 and under 10,000 as it looks
148 #pragma GCC diagnostic push
149 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
150 sprintf(buf_start
, spec
, abs_value
);
151 #pragma GCC diagnostic pop
154 bt_common_sep_digits(buf_start
, digits_per_group
, sep
);
159 void write_nl(struct details_write_ctx
*ctx
)
162 g_string_append_c(ctx
->str
, '\n');
166 void write_sp(struct details_write_ctx
*ctx
)
169 g_string_append_c(ctx
->str
, ' ');
173 void write_indent(struct details_write_ctx
*ctx
)
179 for (i
= 0; i
< ctx
->indent_level
; i
++) {
185 void write_compound_member_name(struct details_write_ctx
*ctx
, const char *name
)
188 g_string_append_printf(ctx
->str
, "%s%s%s:",
189 color_fg_cyan(ctx
), name
, color_reset(ctx
));
193 void write_array_index(struct details_write_ctx
*ctx
, uint64_t index
,
199 format_uint(buf
, index
, 10);
200 g_string_append_printf(ctx
->str
, "%s[%s]%s:",
201 color
, buf
, color_reset(ctx
));
205 void write_obj_type_name(struct details_write_ctx
*ctx
, const char *name
)
207 g_string_append_printf(ctx
->str
, "%s%s%s%s",
208 color_bold(ctx
), color_fg_bright_yellow(ctx
), name
,
213 void write_prop_name(struct details_write_ctx
*ctx
, const char *prop_name
)
215 g_string_append_printf(ctx
->str
, "%s%s%s",
216 color_fg_magenta(ctx
), prop_name
, color_reset(ctx
));
220 void write_prop_name_line(struct details_write_ctx
*ctx
, const char *prop_name
)
223 g_string_append_printf(ctx
->str
, "%s%s%s:",
224 color_fg_magenta(ctx
), prop_name
, color_reset(ctx
));
228 void write_str_prop_value(struct details_write_ctx
*ctx
, const char *value
)
230 g_string_append_printf(ctx
->str
, "%s%s%s",
231 color_bold(ctx
), value
, color_reset(ctx
));
235 void write_none_prop_value(struct details_write_ctx
*ctx
, const char *value
)
237 g_string_append_printf(ctx
->str
, "%s%s%s%s",
238 color_bold(ctx
), color_fg_bright_magenta(ctx
),
239 value
, color_reset(ctx
));
243 void write_uint_str_prop_value(struct details_write_ctx
*ctx
, const char *value
)
245 write_str_prop_value(ctx
, value
);
249 void write_uint_prop_value(struct details_write_ctx
*ctx
, uint64_t value
)
253 format_uint(buf
, value
, 10);
254 write_uint_str_prop_value(ctx
, buf
);
258 void write_int_prop_value(struct details_write_ctx
*ctx
, int64_t value
)
262 format_int(buf
, value
, 10);
263 write_uint_str_prop_value(ctx
, buf
);
267 void write_float_prop_value(struct details_write_ctx
*ctx
, double value
)
269 g_string_append_printf(ctx
->str
, "%s%f%s",
270 color_bold(ctx
), value
, color_reset(ctx
));
274 void write_str_prop_line(struct details_write_ctx
*ctx
, const char *prop_name
,
275 const char *prop_value
)
277 BT_ASSERT_DBG(prop_value
);
279 write_prop_name(ctx
, prop_name
);
280 g_string_append(ctx
->str
, ": ");
281 write_str_prop_value(ctx
, prop_value
);
286 void write_uint_prop_line(struct details_write_ctx
*ctx
, const char *prop_name
,
290 write_prop_name(ctx
, prop_name
);
291 g_string_append(ctx
->str
, ": ");
292 write_uint_prop_value(ctx
, prop_value
);
297 void write_int_prop_line(struct details_write_ctx
*ctx
, const char *prop_name
,
301 write_prop_name(ctx
, prop_name
);
302 g_string_append(ctx
->str
, ": ");
303 write_int_prop_value(ctx
, prop_value
);
308 void write_int_str_prop_value(struct details_write_ctx
*ctx
, const char *value
)
310 write_str_prop_value(ctx
, value
);
314 void write_bool_prop_value(struct details_write_ctx
*ctx
, bt_bool prop_value
)
318 g_string_append(ctx
->str
, color_bold(ctx
));
321 g_string_append(ctx
->str
, color_fg_bright_green(ctx
));
324 g_string_append(ctx
->str
, color_fg_bright_red(ctx
));
328 g_string_append_printf(ctx
->str
, "%s%s", str
, color_reset(ctx
));
332 void write_bool_prop_line(struct details_write_ctx
*ctx
, const char *prop_name
,
336 write_prop_name(ctx
, prop_name
);
337 g_string_append(ctx
->str
, ": ");
338 write_bool_prop_value(ctx
, prop_value
);
343 void write_uuid_prop_line(struct details_write_ctx
*ctx
, const char *prop_name
,
348 write_prop_name(ctx
, prop_name
);
349 g_string_append_printf(ctx
->str
,
350 ": %s" BT_UUID_FMT
"%s\n",
352 BT_UUID_FMT_VALUES(uuid
),
357 gint
compare_strings(const char **a
, const char **b
)
359 return strcmp(*a
, *b
);
363 bt_value_map_foreach_entry_const_func_status
map_value_foreach_add_key_to_array(
365 const bt_value
*object
__attribute__((unused
)),
368 GPtrArray
*keys
= data
;
371 g_ptr_array_add(keys
, (void *) key
);
372 return BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK
;
376 void write_value(struct details_write_ctx
*ctx
, const bt_value
*value
,
380 bt_value_type value_type
= bt_value_get_type(value
);
381 GPtrArray
*keys
= g_ptr_array_new();
386 /* Write field's name */
388 write_prop_name_line(ctx
, name
);
391 /* Write field's value */
392 switch (value_type
) {
393 case BT_VALUE_TYPE_NULL
:
395 write_none_prop_value(ctx
, "Null");
397 case BT_VALUE_TYPE_BOOL
:
399 write_bool_prop_value(ctx
, bt_value_bool_get(value
));
401 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
402 format_uint(buf
, bt_value_integer_unsigned_get(value
), 10);
404 write_uint_str_prop_value(ctx
, buf
);
406 case BT_VALUE_TYPE_SIGNED_INTEGER
:
407 format_int(buf
, bt_value_integer_signed_get(value
), 10);
409 write_int_str_prop_value(ctx
, buf
);
411 case BT_VALUE_TYPE_REAL
:
413 write_float_prop_value(ctx
, bt_value_real_get(value
));
415 case BT_VALUE_TYPE_STRING
:
416 if (strlen(bt_value_string_get(value
)) > 0) {
418 write_str_prop_value(ctx
, bt_value_string_get(value
));
422 case BT_VALUE_TYPE_ARRAY
:
424 uint64_t length
= bt_value_array_get_length(value
);
428 write_none_prop_value(ctx
, "Empty");
430 g_string_append(ctx
->str
, " Length ");
431 write_uint_prop_value(ctx
, length
);
432 g_string_append_c(ctx
->str
, ':');
437 for (i
= 0; i
< length
; i
++) {
438 const bt_value
*elem_value
=
439 bt_value_array_borrow_element_by_index_const(
443 write_array_index(ctx
, i
, color_fg_magenta(ctx
));
444 write_value(ctx
, elem_value
, NULL
);
450 case BT_VALUE_TYPE_MAP
:
452 bt_value_map_foreach_entry_const_status foreach_status
=
453 bt_value_map_foreach_entry_const(value
,
454 map_value_foreach_add_key_to_array
, keys
);
456 BT_ASSERT_DBG(foreach_status
==
457 BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK
);
458 g_ptr_array_sort(keys
, (GCompareFunc
) compare_strings
);
463 for (i
= 0; i
< keys
->len
; i
++) {
464 const char *key
= keys
->pdata
[i
];
465 const bt_value
*entry_value
=
466 bt_value_map_borrow_entry_value_const(
470 write_value(ctx
, entry_value
, key
);
476 write_none_prop_value(ctx
, "Empty");
485 g_ptr_array_free(keys
, TRUE
);
489 void write_user_attributes(struct details_write_ctx
*ctx
,
490 const bt_value
*user_attrs
, bool write_newline
, bool *written
)
492 BT_ASSERT_DBG(user_attrs
);
494 if (!bt_value_map_is_empty(user_attrs
)) {
495 write_value(ctx
, user_attrs
, "User attributes");
508 void write_int_field_class_props(struct details_write_ctx
*ctx
,
509 const bt_field_class
*fc
, bool close
)
511 g_string_append_printf(ctx
->str
, "(%s%" PRIu64
"-bit%s, Base ",
513 bt_field_class_integer_get_field_value_range(fc
),
516 switch (bt_field_class_integer_get_preferred_display_base(fc
)) {
517 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
518 write_uint_prop_value(ctx
, 2);
520 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
521 write_uint_prop_value(ctx
, 8);
523 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
524 write_uint_prop_value(ctx
, 10);
526 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
527 write_uint_prop_value(ctx
, 16);
534 g_string_append(ctx
->str
, ")");
550 struct enum_field_class_mapping
{
554 /* Array of `struct int_range` */
559 gint
compare_enum_field_class_mappings(struct enum_field_class_mapping
**a
,
560 struct enum_field_class_mapping
**b
)
562 return strcmp((*a
)->label
, (*b
)->label
);
566 gint
compare_int_ranges_signed(struct int_range
*a
, struct int_range
*b
)
569 if (a
->lower
.i
< b
->lower
.i
) {
571 } else if (a
->lower
.i
> b
->lower
.i
) {
574 if (a
->upper
.i
< b
->upper
.i
) {
576 } else if (a
->upper
.i
> b
->upper
.i
) {
585 gint
compare_int_ranges_unsigned(struct int_range
*a
, struct int_range
*b
)
587 if (a
->lower
.u
< b
->lower
.u
) {
589 } else if (a
->lower
.u
> b
->lower
.u
) {
592 if (a
->upper
.u
< b
->upper
.u
) {
594 } else if (a
->upper
.u
> b
->upper
.u
) {
603 GArray
*range_set_to_int_ranges(const void *spec_range_set
, bool is_signed
)
606 const bt_integer_range_set
*range_set
;
607 GArray
*ranges
= g_array_new(FALSE
, TRUE
, sizeof(struct int_range
));
614 range_set
= bt_integer_range_set_signed_as_range_set_const(
617 range_set
= bt_integer_range_set_unsigned_as_range_set_const(
621 for (i
= 0; i
< bt_integer_range_set_get_range_count(range_set
); i
++) {
622 struct int_range range
;
625 const bt_integer_range_signed
*orig_range
=
626 bt_integer_range_set_signed_borrow_range_by_index_const(
629 range
.lower
.i
= bt_integer_range_signed_get_lower(orig_range
);
630 range
.upper
.i
= bt_integer_range_signed_get_upper(orig_range
);
632 const bt_integer_range_unsigned
*orig_range
=
633 bt_integer_range_set_unsigned_borrow_range_by_index_const(
636 range
.lower
.u
= bt_integer_range_unsigned_get_lower(orig_range
);
637 range
.upper
.u
= bt_integer_range_unsigned_get_upper(orig_range
);
640 g_array_append_val(ranges
, range
);
644 g_array_sort(ranges
, (GCompareFunc
) compare_int_ranges_signed
);
647 (GCompareFunc
) compare_int_ranges_unsigned
);
655 void destroy_enum_field_class_mapping(struct enum_field_class_mapping
*mapping
)
657 if (mapping
->ranges
) {
658 g_array_free(mapping
->ranges
, TRUE
);
659 mapping
->ranges
= NULL
;
666 struct int_range
*int_range_at(GArray
*ranges
, uint64_t index
)
668 return &bt_g_array_index(ranges
, struct int_range
, index
);
672 void write_int_range(struct details_write_ctx
*ctx
,
673 struct int_range
*range
, bool is_signed
)
675 g_string_append(ctx
->str
, "[");
678 write_int_prop_value(ctx
, range
->lower
.i
);
680 write_uint_prop_value(ctx
, range
->lower
.u
);
683 if (range
->lower
.u
!= range
->upper
.u
) {
684 g_string_append(ctx
->str
, ", ");
687 write_int_prop_value(ctx
, range
->upper
.i
);
689 write_uint_prop_value(ctx
, range
->upper
.u
);
693 g_string_append(ctx
->str
, "]");
697 void write_enum_field_class_mappings(struct details_write_ctx
*ctx
,
698 const bt_field_class
*fc
)
703 bool is_signed
= bt_field_class_get_type(fc
) ==
704 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
;
706 mappings
= g_ptr_array_new_with_free_func(
707 (GDestroyNotify
) destroy_enum_field_class_mapping
);
708 BT_ASSERT_DBG(mappings
);
711 * Copy field class's mappings to our own arrays and structures
714 for (i
= 0; i
< bt_field_class_enumeration_get_mapping_count(fc
); i
++) {
715 const void *fc_mapping
;
716 const void *fc_range_set
;
717 struct enum_field_class_mapping
*mapping
= g_new0(
718 struct enum_field_class_mapping
, 1);
720 BT_ASSERT_DBG(mapping
);
723 fc_mapping
= bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
725 fc_range_set
= bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
728 fc_mapping
= bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
730 fc_range_set
= bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
734 mapping
->label
= bt_field_class_enumeration_mapping_get_label(
735 bt_field_class_enumeration_signed_mapping_as_mapping_const(
737 mapping
->ranges
= range_set_to_int_ranges(fc_range_set
,
739 BT_ASSERT_DBG(mapping
->ranges
);
740 g_ptr_array_add(mappings
, mapping
);
743 /* Sort mappings (ranges are already sorted within mappings) */
744 g_ptr_array_sort(mappings
,
745 (GCompareFunc
) compare_enum_field_class_mappings
);
748 for (i
= 0; i
< mappings
->len
; i
++) {
749 struct enum_field_class_mapping
*mapping
= mappings
->pdata
[i
];
752 write_prop_name_line(ctx
, mapping
->label
);
754 for (range_i
= 0; range_i
< mapping
->ranges
->len
; range_i
++) {
757 int_range_at(mapping
->ranges
, range_i
),
762 g_ptr_array_free(mappings
, TRUE
);
766 void write_field_path(struct details_write_ctx
*ctx
,
767 const bt_field_path
*field_path
)
771 g_string_append_c(ctx
->str
, '[');
773 switch (bt_field_path_get_root_scope(field_path
)) {
774 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
:
775 write_str_prop_value(ctx
, "Packet context");
777 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
:
778 write_str_prop_value(ctx
, "Event common context");
780 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
:
781 write_str_prop_value(ctx
, "Event specific context");
783 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
:
784 write_str_prop_value(ctx
, "Event payload");
790 g_string_append(ctx
->str
, ": ");
792 for (i
= 0; i
< bt_field_path_get_item_count(field_path
); i
++) {
793 const bt_field_path_item
*fp_item
=
794 bt_field_path_borrow_item_by_index_const(field_path
, i
);
797 g_string_append(ctx
->str
, ", ");
800 switch (bt_field_path_item_get_type(fp_item
)) {
801 case BT_FIELD_PATH_ITEM_TYPE_INDEX
:
802 write_uint_prop_value(ctx
,
803 bt_field_path_item_index_get_index(fp_item
));
805 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
:
806 write_str_prop_value(ctx
, "<current>");
813 g_string_append_c(ctx
->str
, ']');
817 void write_field_class(struct details_write_ctx
*ctx
, const bt_field_class
*fc
);
820 void write_variant_field_class_option(struct details_write_ctx
*ctx
,
821 const bt_field_class
*fc
, uint64_t index
)
823 bt_field_class_type fc_type
= bt_field_class_get_type(fc
);
824 const bt_field_class_variant_option
*option
=
825 bt_field_class_variant_borrow_option_by_index_const(
827 const void *orig_ranges
= NULL
;
828 GArray
*int_ranges
= NULL
;
830 const bt_value
*user_attrs
=
831 bt_field_class_variant_option_borrow_user_attributes_const(
833 const bt_field_class
*option_fc
=
834 bt_field_class_variant_option_borrow_field_class_const(option
);
837 write_compound_member_name(ctx
,
838 bt_field_class_variant_option_get_name(option
));
840 if (fc_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
) {
841 const bt_field_class_variant_with_selector_field_integer_unsigned_option
*spec_opt
=
842 bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const(
846 bt_field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const(
849 } else if (fc_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
) {
850 const bt_field_class_variant_with_selector_field_integer_signed_option
*spec_opt
=
851 bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const(
855 bt_field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const(
863 int_ranges
= range_set_to_int_ranges(orig_ranges
, is_signed
);
864 BT_ASSERT_DBG(int_ranges
);
866 for (i
= 0; i
< int_ranges
->len
; i
++) {
867 struct int_range
*range
= int_range_at(int_ranges
, i
);
870 write_int_range(ctx
, range
, is_signed
);
873 g_string_append(ctx
->str
, ": ");
878 if (bt_value_map_is_empty(user_attrs
)) {
879 write_field_class(ctx
, option_fc
);
885 write_prop_name_line(ctx
, "Field class");
887 write_field_class(ctx
, option_fc
);
890 /* User attributes */
891 write_user_attributes(ctx
, user_attrs
,
898 g_array_free(int_ranges
, TRUE
);
903 void write_field_class(struct details_write_ctx
*ctx
, const bt_field_class
*fc
)
907 bt_field_class_type fc_type
= bt_field_class_get_type(fc
);
908 const bt_value
*user_attrs
;
909 bool wrote_user_attrs
= false;
911 /* Write field class's type */
913 case BT_FIELD_CLASS_TYPE_BOOL
:
916 case BT_FIELD_CLASS_TYPE_BIT_ARRAY
:
919 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
920 type
= "Unsigned integer";
922 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
923 type
= "Signed integer";
925 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
926 type
= "Unsigned enumeration";
928 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
929 type
= "Signed enumeration";
931 case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
:
932 type
= "Single-precision real";
934 case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
:
935 type
= "Double-precision real";
937 case BT_FIELD_CLASS_TYPE_STRING
:
940 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
943 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
944 type
= "Static array";
946 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
:
947 type
= "Dynamic array (no length field)";
949 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
:
950 type
= "Dynamic array (with length field)";
952 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
:
953 type
= "Option (no selector)";
955 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
:
956 type
= "Option (boolean selector)";
958 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
:
959 type
= "Option (unsigned integer selector)";
961 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
:
962 type
= "Option (signed integer selector)";
964 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
:
965 type
= "Variant (no selector)";
967 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
:
968 type
= "Variant (unsigned integer selector)";
970 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
:
971 type
= "Variant (signed integer selector)";
977 g_string_append_printf(ctx
->str
, "%s%s%s",
978 color_fg_blue(ctx
), type
, color_reset(ctx
));
980 /* Write field class's single-line properties */
981 if (bt_field_class_type_is(fc_type
, BT_FIELD_CLASS_TYPE_ENUMERATION
)) {
982 uint64_t mapping_count
=
983 bt_field_class_enumeration_get_mapping_count(fc
);
986 write_int_field_class_props(ctx
, fc
, false);
987 g_string_append(ctx
->str
, ", ");
988 write_uint_prop_value(ctx
, mapping_count
);
989 g_string_append_printf(ctx
->str
, " mapping%s)",
990 plural(mapping_count
));
991 } else if (bt_field_class_type_is(fc_type
,
992 BT_FIELD_CLASS_TYPE_INTEGER
)) {
994 write_int_field_class_props(ctx
, fc
, true);
995 } else if (fc_type
== BT_FIELD_CLASS_TYPE_STRUCTURE
) {
996 uint64_t member_count
=
997 bt_field_class_structure_get_member_count(fc
);
999 g_string_append(ctx
->str
, " (");
1000 write_uint_prop_value(ctx
, member_count
);
1001 g_string_append_printf(ctx
->str
, " member%s)",
1002 plural(member_count
));
1003 } else if (fc_type
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
) {
1004 g_string_append(ctx
->str
, " (Length ");
1005 write_uint_prop_value(ctx
,
1006 bt_field_class_array_static_get_length(fc
));
1007 g_string_append_c(ctx
->str
, ')');
1008 } else if (fc_type
== BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
) {
1009 const bt_field_path
*length_field_path
=
1010 bt_field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
1013 g_string_append(ctx
->str
, " (Length field path ");
1014 write_field_path(ctx
, length_field_path
);
1015 g_string_append_c(ctx
->str
, ')');
1016 } else if (bt_field_class_type_is(fc_type
,
1017 BT_FIELD_CLASS_TYPE_OPTION_WITH_SELECTOR_FIELD
)) {
1018 const bt_field_path
*selector_field_path
=
1019 bt_field_class_option_with_selector_field_borrow_selector_field_path_const(
1022 g_string_append(ctx
->str
, " (Selector field path ");
1023 write_field_path(ctx
, selector_field_path
);
1024 g_string_append_c(ctx
->str
, ')');
1025 } else if (bt_field_class_type_is(fc_type
,
1026 BT_FIELD_CLASS_TYPE_VARIANT
)) {
1027 uint64_t option_count
=
1028 bt_field_class_variant_get_option_count(fc
);
1029 const bt_field_path
*sel_field_path
= NULL
;
1031 if (bt_field_class_type_is(fc_type
,
1032 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SELECTOR_FIELD
)) {
1034 bt_field_class_variant_with_selector_field_borrow_selector_field_path_const(
1036 BT_ASSERT_DBG(sel_field_path
);
1039 g_string_append(ctx
->str
, " (");
1040 write_uint_prop_value(ctx
, option_count
);
1041 g_string_append_printf(ctx
->str
, " option%s",
1042 plural(option_count
));
1044 if (sel_field_path
) {
1045 g_string_append(ctx
->str
, ", Selector field path ");
1046 write_field_path(ctx
, sel_field_path
);
1049 g_string_append_c(ctx
->str
, ')');
1053 user_attrs
= bt_field_class_borrow_user_attributes_const(fc
);
1054 if (!bt_value_map_is_empty(user_attrs
)) {
1055 g_string_append(ctx
->str
, ":\n");
1056 write_user_attributes(ctx
, user_attrs
, false, NULL
);
1057 wrote_user_attrs
= true;
1060 /* Write field class's complex properties */
1061 if (bt_field_class_type_is(fc_type
, BT_FIELD_CLASS_TYPE_ENUMERATION
)) {
1062 uint64_t mapping_count
=
1063 bt_field_class_enumeration_get_mapping_count(fc
);
1065 if (mapping_count
> 0) {
1066 if (wrote_user_attrs
) {
1069 write_prop_name(ctx
, "Mappings");
1070 g_string_append_c(ctx
->str
, ':');
1073 /* Each mapping starts with its own newline */
1074 g_string_append_c(ctx
->str
, ':');
1077 write_enum_field_class_mappings(ctx
, fc
);
1079 if (wrote_user_attrs
) {
1083 } else if (fc_type
== BT_FIELD_CLASS_TYPE_STRUCTURE
) {
1084 uint64_t member_count
=
1085 bt_field_class_structure_get_member_count(fc
);
1087 if (member_count
> 0) {
1088 if (wrote_user_attrs
) {
1091 write_prop_name(ctx
, "Members");
1092 g_string_append_c(ctx
->str
, ':');
1095 /* Each member starts with its own newline */
1096 g_string_append_c(ctx
->str
, ':');
1099 for (i
= 0; i
< member_count
; i
++) {
1100 const bt_field_class_structure_member
*member
=
1101 bt_field_class_structure_borrow_member_by_index_const(
1103 const bt_value
*member_user_attrs
;
1104 const bt_field_class
*member_fc
=
1105 bt_field_class_structure_member_borrow_field_class_const(member
);
1108 write_compound_member_name(ctx
,
1109 bt_field_class_structure_member_get_name(member
));
1110 member_user_attrs
= bt_field_class_structure_member_borrow_user_attributes_const(
1113 if (bt_value_map_is_empty(member_user_attrs
)) {
1115 write_field_class(ctx
, member_fc
);
1121 write_prop_name_line(ctx
, "Field class");
1123 write_field_class(ctx
, member_fc
);
1126 /* User attributes */
1127 write_user_attributes(ctx
, member_user_attrs
,
1134 if (wrote_user_attrs
) {
1138 } else if (bt_field_class_type_is(fc_type
, BT_FIELD_CLASS_TYPE_ARRAY
)) {
1139 if (wrote_user_attrs
) {
1142 g_string_append(ctx
->str
, ":\n");
1145 write_prop_name_line(ctx
, "Element");
1147 write_field_class(ctx
,
1148 bt_field_class_array_borrow_element_field_class_const(fc
));
1149 } else if (bt_field_class_type_is(fc_type
,
1150 BT_FIELD_CLASS_TYPE_OPTION
)) {
1151 const void *ranges
= NULL
;
1152 bool selector_is_signed
= false;
1154 if (wrote_user_attrs
) {
1157 g_string_append(ctx
->str
, ":\n");
1160 if (fc_type
== BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
) {
1161 write_bool_prop_line(ctx
, "Selector is reversed",
1162 bt_field_class_option_with_selector_field_bool_selector_is_reversed(fc
));
1163 } else if (fc_type
== BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
) {
1164 ranges
= bt_field_class_option_with_selector_field_integer_unsigned_borrow_selector_ranges_const(fc
);
1165 } else if (fc_type
== BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
) {
1166 ranges
= bt_field_class_option_with_selector_field_integer_signed_borrow_selector_ranges_const(fc
);
1167 selector_is_signed
= true;
1171 GArray
*sorted_ranges
= range_set_to_int_ranges(
1172 ranges
, selector_is_signed
);
1174 BT_ASSERT_DBG(sorted_ranges
);
1175 BT_ASSERT_DBG(sorted_ranges
->len
> 0);
1176 write_prop_name_line(ctx
, "Selector ranges");
1178 for (i
= 0; i
< sorted_ranges
->len
; i
++) {
1180 write_int_range(ctx
,
1181 int_range_at(sorted_ranges
, i
),
1182 selector_is_signed
);
1186 g_array_free(sorted_ranges
, TRUE
);
1189 write_prop_name_line(ctx
, "Content");
1191 write_field_class(ctx
,
1192 bt_field_class_option_borrow_field_class_const(fc
));
1193 } else if (bt_field_class_type_is(fc_type
,
1194 BT_FIELD_CLASS_TYPE_VARIANT
)) {
1195 uint64_t option_count
=
1196 bt_field_class_variant_get_option_count(fc
);
1198 if (option_count
> 0) {
1199 if (wrote_user_attrs
) {
1202 write_prop_name(ctx
, "Options");
1203 g_string_append_c(ctx
->str
, ':');
1206 /* Each option starts with its own newline */
1207 g_string_append_c(ctx
->str
, ':');
1210 for (i
= 0; i
< option_count
; i
++) {
1211 write_variant_field_class_option(ctx
, fc
, i
);
1214 if (wrote_user_attrs
) {
1224 void write_root_field_class(struct details_write_ctx
*ctx
, const char *name
,
1225 const bt_field_class
*fc
)
1227 BT_ASSERT_DBG(name
);
1230 write_prop_name(ctx
, name
);
1231 g_string_append(ctx
->str
, ": ");
1232 write_field_class(ctx
, fc
);
1237 void write_event_class(struct details_write_ctx
*ctx
, const bt_event_class
*ec
)
1239 const char *name
= bt_event_class_get_name(ec
);
1240 const char *emf_uri
;
1241 const bt_field_class
*fc
;
1242 bt_event_class_log_level log_level
;
1245 write_obj_type_name(ctx
, "Event class");
1247 /* Write name and ID */
1249 g_string_append_printf(ctx
->str
, " `%s%s%s`",
1250 color_fg_green(ctx
), name
, color_reset(ctx
));
1253 g_string_append(ctx
->str
, " (ID ");
1254 write_uint_prop_value(ctx
, bt_event_class_get_id(ec
));
1255 g_string_append(ctx
->str
, "):\n");
1257 /* Write properties */
1260 /* Write user attributes */
1261 write_user_attributes(ctx
,
1262 bt_event_class_borrow_user_attributes_const(ec
), true, NULL
);
1264 /* Write log level */
1265 if (bt_event_class_get_log_level(ec
, &log_level
) ==
1266 BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
1267 const char *ll_str
= NULL
;
1269 switch (log_level
) {
1270 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
:
1271 ll_str
= "Emergency";
1273 case BT_EVENT_CLASS_LOG_LEVEL_ALERT
:
1276 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
:
1277 ll_str
= "Critical";
1279 case BT_EVENT_CLASS_LOG_LEVEL_ERROR
:
1282 case BT_EVENT_CLASS_LOG_LEVEL_WARNING
:
1285 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE
:
1288 case BT_EVENT_CLASS_LOG_LEVEL_INFO
:
1291 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
:
1292 ll_str
= "Debug (system)";
1294 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
:
1295 ll_str
= "Debug (program)";
1297 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
:
1298 ll_str
= "Debug (process)";
1300 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
:
1301 ll_str
= "Debug (module)";
1303 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
:
1304 ll_str
= "Debug (unit)";
1306 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
:
1307 ll_str
= "Debug (function)";
1309 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
:
1310 ll_str
= "Debug (line)";
1312 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG
:
1319 write_str_prop_line(ctx
, "Log level", ll_str
);
1323 emf_uri
= bt_event_class_get_emf_uri(ec
);
1325 write_str_prop_line(ctx
, "EMF URI", emf_uri
);
1328 /* Write specific context field class */
1329 fc
= bt_event_class_borrow_specific_context_field_class_const(ec
);
1331 write_root_field_class(ctx
, "Specific context field class", fc
);
1334 /* Write payload field class */
1335 fc
= bt_event_class_borrow_payload_field_class_const(ec
);
1337 write_root_field_class(ctx
, "Payload field class", fc
);
1344 void write_clock_class_prop_lines(struct details_write_ctx
*ctx
,
1345 const bt_clock_class
*cc
)
1347 int64_t offset_seconds
;
1348 uint64_t offset_cycles
;
1351 str
= bt_clock_class_get_name(cc
);
1353 write_str_prop_line(ctx
, "Name", str
);
1356 write_user_attributes(ctx
,
1357 bt_clock_class_borrow_user_attributes_const(cc
), true, NULL
);
1358 str
= bt_clock_class_get_description(cc
);
1360 write_str_prop_line(ctx
, "Description", str
);
1363 write_uint_prop_line(ctx
, "Frequency (Hz)",
1364 bt_clock_class_get_frequency(cc
));
1365 write_uint_prop_line(ctx
, "Precision (cycles)",
1366 bt_clock_class_get_precision(cc
));
1367 bt_clock_class_get_offset(cc
, &offset_seconds
, &offset_cycles
);
1368 write_int_prop_line(ctx
, "Offset (s)", offset_seconds
);
1369 write_uint_prop_line(ctx
, "Offset (cycles)", offset_cycles
);
1370 write_bool_prop_line(ctx
, "Origin is Unix epoch",
1371 bt_clock_class_origin_is_unix_epoch(cc
));
1373 if (ctx
->details_comp
->cfg
.with_uuid
) {
1374 bt_uuid uuid
= bt_clock_class_get_uuid(cc
);
1377 write_uuid_prop_line(ctx
, "UUID", uuid
);
1383 gint
compare_event_classes(const bt_event_class
**a
, const bt_event_class
**b
)
1385 uint64_t id_a
= bt_event_class_get_id(*a
);
1386 uint64_t id_b
= bt_event_class_get_id(*b
);
1390 } else if (id_a
> id_b
) {
1398 void write_stream_class(struct details_write_ctx
*ctx
,
1399 const bt_stream_class
*sc
)
1401 const bt_field_class
*fc
;
1402 GPtrArray
*event_classes
= g_ptr_array_new();
1406 write_obj_type_name(ctx
, "Stream class");
1408 /* Write name and ID */
1409 if (ctx
->details_comp
->cfg
.with_stream_class_name
) {
1410 const char *name
= bt_stream_class_get_name(sc
);
1413 g_string_append(ctx
->str
, " `");
1414 write_str_prop_value(ctx
, name
);
1415 g_string_append(ctx
->str
, "`");
1419 g_string_append(ctx
->str
, " (ID ");
1420 write_uint_prop_value(ctx
, bt_stream_class_get_id(sc
));
1421 g_string_append(ctx
->str
, "):\n");
1423 /* Write properties */
1426 /* Write user attributes */
1427 write_user_attributes(ctx
,
1428 bt_stream_class_borrow_user_attributes_const(sc
), true, NULL
);
1430 /* Write configuration */
1431 write_bool_prop_line(ctx
,
1432 "Supports packets", bt_stream_class_supports_packets(sc
));
1434 if (bt_stream_class_supports_packets(sc
)) {
1435 write_bool_prop_line(ctx
,
1436 "Packets have beginning default clock snapshot",
1437 bt_stream_class_packets_have_beginning_default_clock_snapshot(sc
));
1438 write_bool_prop_line(ctx
,
1439 "Packets have end default clock snapshot",
1440 bt_stream_class_packets_have_end_default_clock_snapshot(sc
));
1443 write_bool_prop_line(ctx
,
1444 "Supports discarded events",
1445 bt_stream_class_supports_discarded_events(sc
));
1447 if (bt_stream_class_supports_discarded_events(sc
)) {
1448 write_bool_prop_line(ctx
,
1449 "Discarded events have default clock snapshots",
1450 bt_stream_class_discarded_events_have_default_clock_snapshots(sc
));
1453 write_bool_prop_line(ctx
,
1454 "Supports discarded packets",
1455 bt_stream_class_supports_discarded_packets(sc
));
1457 if (bt_stream_class_supports_discarded_packets(sc
)) {
1458 write_bool_prop_line(ctx
,
1459 "Discarded packets have default clock snapshots",
1460 bt_stream_class_discarded_packets_have_default_clock_snapshots(sc
));
1463 /* Write default clock class */
1464 if (bt_stream_class_borrow_default_clock_class_const(sc
)) {
1466 write_prop_name(ctx
, "Default clock class");
1467 g_string_append_c(ctx
->str
, ':');
1470 write_clock_class_prop_lines(ctx
,
1471 bt_stream_class_borrow_default_clock_class_const(sc
));
1475 fc
= bt_stream_class_borrow_packet_context_field_class_const(sc
);
1477 write_root_field_class(ctx
, "Packet context field class", fc
);
1480 fc
= bt_stream_class_borrow_event_common_context_field_class_const(sc
);
1482 write_root_field_class(ctx
, "Event common context field class",
1486 for (i
= 0; i
< bt_stream_class_get_event_class_count(sc
); i
++) {
1487 g_ptr_array_add(event_classes
,
1488 (gpointer
) bt_stream_class_borrow_event_class_by_index_const(
1492 g_ptr_array_sort(event_classes
, (GCompareFunc
) compare_event_classes
);
1494 for (i
= 0; i
< event_classes
->len
; i
++) {
1495 write_event_class(ctx
, event_classes
->pdata
[i
]);
1499 g_ptr_array_free(event_classes
, TRUE
);
1503 gint
compare_stream_classes(const bt_stream_class
**a
, const bt_stream_class
**b
)
1505 uint64_t id_a
= bt_stream_class_get_id(*a
);
1506 uint64_t id_b
= bt_stream_class_get_id(*b
);
1510 } else if (id_a
> id_b
) {
1518 void write_trace_class(struct details_write_ctx
*ctx
, const bt_trace_class
*tc
)
1520 GPtrArray
*stream_classes
= g_ptr_array_new();
1522 bool printed_prop
= false;
1525 write_obj_type_name(ctx
, "Trace class");
1528 for (i
= 0; i
< bt_trace_class_get_stream_class_count(tc
); i
++) {
1529 g_ptr_array_add(stream_classes
,
1530 (gpointer
) bt_trace_class_borrow_stream_class_by_index_const(
1534 g_ptr_array_sort(stream_classes
, (GCompareFunc
) compare_stream_classes
);
1536 if (stream_classes
->len
> 0) {
1537 if (!printed_prop
) {
1538 g_string_append(ctx
->str
, ":\n");
1539 printed_prop
= true;
1545 /* Write user attributes */
1546 write_user_attributes(ctx
,
1547 bt_trace_class_borrow_user_attributes_const(tc
), true,
1550 /* Write stream classes */
1551 for (i
= 0; i
< stream_classes
->len
; i
++) {
1552 write_stream_class(ctx
, stream_classes
->pdata
[i
]);
1555 if (!printed_prop
) {
1560 g_ptr_array_free(stream_classes
, TRUE
);
1564 int try_write_meta(struct details_write_ctx
*ctx
, const bt_trace_class
*tc
,
1565 const bt_stream_class
*sc
, const bt_event_class
*ec
)
1571 if (details_need_to_write_trace_class(ctx
, tc
)) {
1574 if (ctx
->details_comp
->cfg
.compact
&&
1575 ctx
->details_comp
->printed_something
) {
1577 * There are no empty line between messages in
1578 * compact mode, so write one here to decouple
1579 * the trace class from the next message.
1585 * write_trace_class() also writes all its stream
1586 * classes their event classes, so we don't need to
1589 write_trace_class(ctx
, tc
);
1592 * Mark this trace class as written, as well as all
1593 * its stream classes and their event classes.
1595 ret
= details_did_write_trace_class(ctx
, tc
);
1600 for (sc_i
= 0; sc_i
< bt_trace_class_get_stream_class_count(tc
);
1603 const bt_stream_class
*tc_sc
=
1604 bt_trace_class_borrow_stream_class_by_index_const(
1607 details_did_write_meta_object(ctx
, tc
, tc_sc
);
1609 for (ec_i
= 0; ec_i
<
1610 bt_stream_class_get_event_class_count(tc_sc
);
1612 details_did_write_meta_object(ctx
, tc
,
1613 bt_stream_class_borrow_event_class_by_index_const(
1621 if (sc
&& details_need_to_write_meta_object(ctx
, tc
, sc
)) {
1626 if (ctx
->details_comp
->cfg
.compact
&&
1627 ctx
->details_comp
->printed_something
) {
1629 * There are no empty line between messages in
1630 * compact mode, so write one here to decouple
1631 * the stream class from the next message.
1637 * write_stream_class() also writes all its event
1638 * classes, so we don't need to rewrite `ec`.
1640 write_stream_class(ctx
, sc
);
1643 * Mark this stream class as written, as well as all its
1646 details_did_write_meta_object(ctx
, tc
, sc
);
1648 for (ec_i
= 0; ec_i
<
1649 bt_stream_class_get_event_class_count(sc
);
1651 details_did_write_meta_object(ctx
, tc
,
1652 bt_stream_class_borrow_event_class_by_index_const(
1659 if (ec
&& details_need_to_write_meta_object(ctx
, tc
, ec
)) {
1662 if (ctx
->details_comp
->cfg
.compact
&&
1663 ctx
->details_comp
->printed_something
) {
1665 * There are no empty line between messages in
1666 * compact mode, so write one here to decouple
1667 * the event class from the next message.
1672 write_event_class(ctx
, ec
);
1673 details_did_write_meta_object(ctx
, tc
, ec
);
1682 void write_time_str(struct details_write_ctx
*ctx
, const char *str
)
1684 if (!ctx
->details_comp
->cfg
.with_time
) {
1688 g_string_append_printf(ctx
->str
, "[%s%s%s%s]",
1689 color_bold(ctx
), color_fg_bright_blue(ctx
), str
,
1692 if (ctx
->details_comp
->cfg
.compact
) {
1703 void write_time(struct details_write_ctx
*ctx
, const bt_clock_snapshot
*cs
)
1705 bt_clock_snapshot_get_ns_from_origin_status cs_status
;
1706 int64_t ns_from_origin
;
1709 if (!ctx
->details_comp
->cfg
.with_time
) {
1713 format_uint(buf
, bt_clock_snapshot_get_value(cs
), 10);
1714 g_string_append_printf(ctx
->str
, "[%s%s%s%s%s",
1715 color_bold(ctx
), color_fg_bright_blue(ctx
), buf
,
1717 ctx
->details_comp
->cfg
.compact
? "" : " cycles");
1718 cs_status
= bt_clock_snapshot_get_ns_from_origin(cs
, &ns_from_origin
);
1719 if (cs_status
== BT_CLOCK_SNAPSHOT_GET_NS_FROM_ORIGIN_STATUS_OK
) {
1720 format_int(buf
, ns_from_origin
, 10);
1721 g_string_append_printf(ctx
->str
, "%s %s%s%s%s%s",
1722 ctx
->details_comp
->cfg
.compact
? "" : ",",
1723 color_bold(ctx
), color_fg_bright_blue(ctx
), buf
,
1725 ctx
->details_comp
->cfg
.compact
? "" : " ns from origin");
1728 g_string_append(ctx
->str
, "]");
1730 if (ctx
->details_comp
->cfg
.compact
) {
1741 int write_message_follow_tag(struct details_write_ctx
*ctx
,
1742 const bt_stream
*stream
)
1745 uint64_t unique_trace_id
;
1746 const bt_stream_class
*sc
= bt_stream_borrow_class_const(stream
);
1747 const bt_trace
*trace
= bt_stream_borrow_trace_const(stream
);
1749 ret
= details_trace_unique_id(ctx
, trace
, &unique_trace_id
);
1754 if (ctx
->details_comp
->cfg
.compact
) {
1755 g_string_append_printf(ctx
->str
,
1756 "%s{%s%s%" PRIu64
" %" PRIu64
" %" PRIu64
"%s%s}%s ",
1757 color_fg_cyan(ctx
), color_bold(ctx
),
1758 color_fg_bright_cyan(ctx
),
1759 unique_trace_id
, bt_stream_class_get_id(sc
),
1760 bt_stream_get_id(stream
),
1761 color_reset(ctx
), color_fg_cyan(ctx
), color_reset(ctx
));
1763 g_string_append_printf(ctx
->str
,
1764 "%s{Trace %s%s%" PRIu64
"%s%s, Stream class ID %s%s%" PRIu64
"%s%s, Stream ID %s%s%" PRIu64
"%s%s}%s\n",
1766 color_bold(ctx
), color_fg_bright_cyan(ctx
),
1767 unique_trace_id
, color_reset(ctx
),
1769 color_bold(ctx
), color_fg_bright_cyan(ctx
),
1770 bt_stream_class_get_id(sc
), color_reset(ctx
),
1772 color_bold(ctx
), color_fg_bright_cyan(ctx
),
1773 bt_stream_get_id(stream
), color_reset(ctx
),
1774 color_fg_cyan(ctx
), color_reset(ctx
));
1782 void write_field(struct details_write_ctx
*ctx
, const bt_field
*field
,
1786 bt_field_class_type fc_type
= bt_field_get_class_type(field
);
1787 const bt_field_class
*fc
;
1790 /* Write field's name */
1792 write_compound_member_name(ctx
, name
);
1795 /* Write field's value */
1796 if (fc_type
== BT_FIELD_CLASS_TYPE_BOOL
) {
1798 write_bool_prop_value(ctx
, bt_field_bool_get_value(field
));
1799 } else if (fc_type
== BT_FIELD_CLASS_TYPE_BIT_ARRAY
) {
1800 format_uint(buf
, bt_field_bit_array_get_value_as_integer(field
),
1803 write_uint_str_prop_value(ctx
, buf
);
1804 } else if (bt_field_class_type_is(fc_type
,
1805 BT_FIELD_CLASS_TYPE_INTEGER
)) {
1806 unsigned int fmt_base
;
1807 bt_field_class_integer_preferred_display_base base
;
1809 fc
= bt_field_borrow_class_const(field
);
1810 base
= bt_field_class_integer_get_preferred_display_base(fc
);
1813 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
1816 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
1819 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
1822 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
1829 if (bt_field_class_type_is(fc_type
,
1830 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
)) {
1832 bt_field_integer_unsigned_get_value(field
),
1835 write_uint_str_prop_value(ctx
, buf
);
1838 bt_field_integer_signed_get_value(field
),
1841 write_int_str_prop_value(ctx
, buf
);
1843 } else if (fc_type
== BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
) {
1845 write_float_prop_value(ctx
, bt_field_real_single_precision_get_value(field
));
1846 } else if (fc_type
== BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
) {
1848 write_float_prop_value(ctx
, bt_field_real_double_precision_get_value(field
));
1849 } else if (fc_type
== BT_FIELD_CLASS_TYPE_STRING
) {
1851 write_str_prop_value(ctx
, bt_field_string_get_value(field
));
1852 } else if (fc_type
== BT_FIELD_CLASS_TYPE_STRUCTURE
) {
1853 uint64_t member_count
;
1855 fc
= bt_field_borrow_class_const(field
);
1856 member_count
= bt_field_class_structure_get_member_count(fc
);
1858 if (member_count
> 0) {
1861 for (i
= 0; i
< member_count
; i
++) {
1862 const bt_field_class_structure_member
*member
=
1863 bt_field_class_structure_borrow_member_by_index_const(
1865 const bt_field
*member_field
=
1866 bt_field_structure_borrow_member_field_by_index_const(
1870 write_field(ctx
, member_field
,
1871 bt_field_class_structure_member_get_name(member
));
1877 write_none_prop_value(ctx
, "Empty");
1879 } else if (bt_field_class_type_is(fc_type
, BT_FIELD_CLASS_TYPE_ARRAY
)) {
1880 uint64_t length
= bt_field_array_get_length(field
);
1884 write_none_prop_value(ctx
, "Empty");
1886 g_string_append(ctx
->str
, " Length ");
1887 write_uint_prop_value(ctx
, length
);
1888 g_string_append_c(ctx
->str
, ':');
1893 for (i
= 0; i
< length
; i
++) {
1894 const bt_field
*elem_field
=
1895 bt_field_array_borrow_element_field_by_index_const(
1899 write_array_index(ctx
, i
, color_fg_cyan(ctx
));
1900 write_field(ctx
, elem_field
, NULL
);
1904 } else if (bt_field_class_type_is(fc_type
,
1905 BT_FIELD_CLASS_TYPE_OPTION
)) {
1906 const bt_field
*content_field
=
1907 bt_field_option_borrow_field_const(field
);
1909 if (!content_field
) {
1911 write_none_prop_value(ctx
, "None");
1913 write_field(ctx
, content_field
, NULL
);
1915 } else if (bt_field_class_type_is(fc_type
,
1916 BT_FIELD_CLASS_TYPE_VARIANT
)) {
1918 bt_field_variant_borrow_selected_option_field_const(
1926 void write_root_field(struct details_write_ctx
*ctx
, const char *name
,
1927 const bt_field
*field
)
1929 BT_ASSERT_DBG(name
);
1930 BT_ASSERT_DBG(field
);
1932 write_prop_name(ctx
, name
);
1933 g_string_append(ctx
->str
, ":");
1934 write_field(ctx
, field
, NULL
);
1939 int write_event_message(struct details_write_ctx
*ctx
,
1940 const bt_message
*msg
)
1943 const bt_event
*event
= bt_message_event_borrow_event_const(msg
);
1944 const bt_stream
*stream
= bt_event_borrow_stream_const(event
);
1945 const bt_event_class
*ec
= bt_event_borrow_class_const(event
);
1946 const bt_stream_class
*sc
= bt_event_class_borrow_stream_class_const(ec
);
1947 const bt_trace_class
*tc
= bt_stream_class_borrow_trace_class_const(sc
);
1948 const char *ec_name
;
1949 const bt_field
*field
;
1951 ret
= try_write_meta(ctx
, tc
, sc
, ec
);
1956 if (!ctx
->details_comp
->cfg
.with_data
) {
1960 if (ctx
->str
->len
> 0) {
1962 * Output buffer contains metadata: separate blocks with
1969 if (bt_stream_class_borrow_default_clock_class_const(sc
)) {
1971 bt_message_event_borrow_default_clock_snapshot_const(
1975 /* Write follow tag for message */
1976 ret
= write_message_follow_tag(ctx
, stream
);
1981 /* Write object's basic properties */
1982 write_obj_type_name(ctx
, "Event");
1983 ec_name
= bt_event_class_get_name(ec
);
1985 g_string_append_printf(ctx
->str
, " `%s%s%s`",
1986 color_fg_green(ctx
), ec_name
, color_reset(ctx
));
1989 g_string_append(ctx
->str
, " (");
1991 if (!ctx
->details_comp
->cfg
.compact
) {
1992 g_string_append(ctx
->str
, "Class ID ");
1995 write_uint_prop_value(ctx
, bt_event_class_get_id(ec
));
1996 g_string_append(ctx
->str
, ")");
1998 if (ctx
->details_comp
->cfg
.compact
) {
2004 g_string_append(ctx
->str
, ":\n");
2006 field
= bt_event_borrow_common_context_field_const(event
);
2008 write_root_field(ctx
, "Common context", field
);
2011 field
= bt_event_borrow_specific_context_field_const(event
);
2013 write_root_field(ctx
, "Specific context", field
);
2016 field
= bt_event_borrow_payload_field_const(event
);
2018 write_root_field(ctx
, "Payload", field
);
2028 gint
compare_streams(const bt_stream
**a
, const bt_stream
**b
)
2030 uint64_t id_a
= bt_stream_get_id(*a
);
2031 uint64_t id_b
= bt_stream_get_id(*b
);
2035 } else if (id_a
> id_b
) {
2038 const bt_stream_class
*a_sc
= bt_stream_borrow_class_const(*a
);
2039 const bt_stream_class
*b_sc
= bt_stream_borrow_class_const(*b
);
2040 uint64_t a_sc_id
= bt_stream_class_get_id(a_sc
);
2041 uint64_t b_sc_id
= bt_stream_class_get_id(b_sc
);
2043 if (a_sc_id
< b_sc_id
) {
2045 } else if (a_sc_id
> b_sc_id
) {
2054 void write_trace(struct details_write_ctx
*ctx
, const bt_trace
*trace
)
2056 GPtrArray
*streams
= g_ptr_array_new();
2058 bool printed_prop
= false;
2059 GPtrArray
*env_names
= g_ptr_array_new();
2063 write_obj_type_name(ctx
, "Trace");
2066 if (ctx
->details_comp
->cfg
.with_trace_name
) {
2067 const char *name
= bt_trace_get_name(trace
);
2069 g_string_append(ctx
->str
, " `");
2070 write_str_prop_value(ctx
, name
);
2071 g_string_append(ctx
->str
, "`");
2075 /* Write properties */
2079 if (ctx
->details_comp
->cfg
.with_uuid
) {
2080 bt_uuid uuid
= bt_trace_get_uuid(trace
);
2083 if (!printed_prop
) {
2084 g_string_append(ctx
->str
, ":\n");
2085 printed_prop
= true;
2088 write_uuid_prop_line(ctx
, "UUID", uuid
);
2092 /* Write environment */
2093 env_count
= bt_trace_get_environment_entry_count(trace
);
2094 if (env_count
> 0) {
2095 if (!printed_prop
) {
2096 g_string_append(ctx
->str
, ":\n");
2097 printed_prop
= true;
2101 write_prop_name(ctx
, "Environment");
2102 g_string_append(ctx
->str
, " (");
2103 write_uint_prop_value(ctx
, env_count
);
2104 g_string_append_printf(ctx
->str
, " entr%s):",
2105 env_count
== 1 ? "y" : "ies");
2109 for (i
= 0; i
< env_count
; i
++) {
2111 const bt_value
*value
;
2113 bt_trace_borrow_environment_entry_by_index_const(
2114 trace
, i
, &name
, &value
);
2115 g_ptr_array_add(env_names
, (gpointer
) name
);
2118 g_ptr_array_sort(env_names
, (GCompareFunc
) compare_strings
);
2120 for (i
= 0; i
< env_names
->len
; i
++) {
2121 const char *name
= env_names
->pdata
[i
];
2122 const bt_value
*value
=
2123 bt_trace_borrow_environment_entry_value_by_name_const(
2126 BT_ASSERT_DBG(value
);
2127 write_compound_member_name(ctx
, name
);
2129 if (bt_value_get_type(value
) ==
2130 BT_VALUE_TYPE_SIGNED_INTEGER
) {
2132 write_int_prop_value(ctx
,
2133 bt_value_integer_signed_get(value
));
2134 } else if (bt_value_get_type(value
) ==
2135 BT_VALUE_TYPE_STRING
) {
2136 if (strlen(bt_value_string_get(value
)) > 0) {
2138 write_str_prop_value(ctx
,
2139 bt_value_string_get(value
));
2151 for (i
= 0; i
< bt_trace_get_stream_count(trace
); i
++) {
2152 g_ptr_array_add(streams
,
2153 (gpointer
) bt_trace_borrow_stream_by_index_const(
2157 g_ptr_array_sort(streams
, (GCompareFunc
) compare_streams
);
2159 if (streams
->len
> 0 && !printed_prop
) {
2160 g_string_append(ctx
->str
, ":\n");
2161 printed_prop
= true;
2164 for (i
= 0; i
< streams
->len
; i
++) {
2165 const bt_stream
*stream
= streams
->pdata
[i
];
2168 write_obj_type_name(ctx
, "Stream");
2169 g_string_append(ctx
->str
, " (ID ");
2170 write_uint_prop_value(ctx
, bt_stream_get_id(stream
));
2171 g_string_append(ctx
->str
, ", Class ID ");
2172 write_uint_prop_value(ctx
, bt_stream_class_get_id(
2173 bt_stream_borrow_class_const(stream
)));
2174 g_string_append(ctx
->str
, ")");
2180 if (!printed_prop
) {
2184 g_ptr_array_free(streams
, TRUE
);
2185 g_ptr_array_free(env_names
, TRUE
);
2189 int write_stream_beginning_message(struct details_write_ctx
*ctx
,
2190 const bt_message
*msg
)
2193 const bt_stream
*stream
=
2194 bt_message_stream_beginning_borrow_stream_const(msg
);
2195 const bt_trace
*trace
= bt_stream_borrow_trace_const(stream
);
2196 const bt_stream_class
*sc
= bt_stream_borrow_class_const(stream
);
2197 const bt_clock_class
*cc
= bt_stream_class_borrow_default_clock_class_const(sc
);
2198 const bt_trace_class
*tc
= bt_stream_class_borrow_trace_class_const(sc
);
2201 ret
= try_write_meta(ctx
, tc
, sc
, NULL
);
2206 if (!ctx
->details_comp
->cfg
.with_data
) {
2210 if (ctx
->str
->len
> 0) {
2212 * Output buffer contains metadata: separate blocks with
2220 const bt_clock_snapshot
*cs
;
2221 bt_message_stream_clock_snapshot_state cs_state
=
2222 bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg
, &cs
);
2224 if (cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
2225 write_time(ctx
, cs
);
2227 write_time_str(ctx
, "Unknown");
2231 /* Write follow tag for message */
2232 ret
= write_message_follow_tag(ctx
, stream
);
2237 /* Write stream properties */
2238 write_obj_type_name(ctx
, "Stream beginning");
2240 if (ctx
->details_comp
->cfg
.compact
) {
2245 g_string_append(ctx
->str
, ":\n");
2248 if (ctx
->details_comp
->cfg
.with_stream_name
) {
2249 name
= bt_stream_get_name(stream
);
2251 write_str_prop_line(ctx
, "Name", name
);
2255 if (ctx
->details_comp
->cfg
.with_stream_class_name
) {
2256 name
= bt_stream_class_get_name(sc
);
2258 write_str_prop_line(ctx
, "Class name", name
);
2262 write_trace(ctx
, trace
);
2270 int write_stream_end_message(struct details_write_ctx
*ctx
,
2271 const bt_message
*msg
)
2274 const bt_stream
*stream
=
2275 bt_message_stream_end_borrow_stream_const(msg
);
2276 const bt_stream_class
*sc
=
2277 bt_stream_borrow_class_const(stream
);
2278 const bt_clock_class
*cc
=
2279 bt_stream_class_borrow_default_clock_class_const(sc
);
2281 if (!ctx
->details_comp
->cfg
.with_data
) {
2287 const bt_clock_snapshot
*cs
;
2288 bt_message_stream_clock_snapshot_state cs_state
=
2289 bt_message_stream_end_borrow_default_clock_snapshot_const(msg
, &cs
);
2291 if (cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
2292 write_time(ctx
, cs
);
2294 write_time_str(ctx
, "Unknown");
2298 /* Write follow tag for message */
2299 ret
= write_message_follow_tag(ctx
, stream
);
2304 /* Write stream properties */
2305 write_obj_type_name(ctx
, "Stream end\n");
2312 int write_packet_beginning_message(struct details_write_ctx
*ctx
,
2313 const bt_message
*msg
)
2316 const bt_packet
*packet
=
2317 bt_message_packet_beginning_borrow_packet_const(msg
);
2318 const bt_stream
*stream
= bt_packet_borrow_stream_const(packet
);
2319 const bt_stream_class
*sc
= bt_stream_borrow_class_const(stream
);
2320 const bt_field
*field
;
2322 if (!ctx
->details_comp
->cfg
.with_data
) {
2327 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(sc
)) {
2329 bt_message_packet_beginning_borrow_default_clock_snapshot_const(
2333 /* Write follow tag for message */
2334 ret
= write_message_follow_tag(ctx
, stream
);
2339 write_obj_type_name(ctx
, "Packet beginning");
2341 if (ctx
->details_comp
->cfg
.compact
) {
2347 field
= bt_packet_borrow_context_field_const(packet
);
2349 g_string_append(ctx
->str
, ":\n");
2351 write_root_field(ctx
, "Context", field
);
2362 int write_discarded_items_message(struct details_write_ctx
*ctx
,
2363 const char *name
, const bt_stream
*stream
,
2364 const bt_clock_snapshot
*beginning_cs
,
2365 const bt_clock_snapshot
*end_cs
, uint64_t count
)
2371 write_time(ctx
, beginning_cs
);
2372 BT_ASSERT_DBG(end_cs
);
2373 write_time(ctx
, end_cs
);
2376 /* Write follow tag for message */
2377 ret
= write_message_follow_tag(ctx
, stream
);
2382 write_obj_type_name(ctx
, "Discarded ");
2383 write_obj_type_name(ctx
, name
);
2386 if (count
== UINT64_C(-1)) {
2391 g_string_append(ctx
->str
, " (");
2392 write_uint_prop_value(ctx
, count
);
2393 g_string_append_printf(ctx
->str
, " %s)\n", name
);
2400 int write_discarded_events_message(struct details_write_ctx
*ctx
,
2401 const bt_message
*msg
)
2404 const bt_stream
*stream
= bt_message_discarded_events_borrow_stream_const(
2406 const bt_stream_class
*sc
= bt_stream_borrow_class_const(stream
);
2407 const bt_clock_snapshot
*beginning_cs
= NULL
;
2408 const bt_clock_snapshot
*end_cs
= NULL
;
2411 if (!ctx
->details_comp
->cfg
.with_data
) {
2415 if (bt_stream_class_discarded_events_have_default_clock_snapshots(sc
)) {
2417 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
2420 bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
2424 if (bt_message_discarded_events_get_count(msg
, &count
) !=
2425 BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
2426 count
= UINT64_C(-1);
2429 ret
= write_discarded_items_message(ctx
, "events", stream
,
2430 beginning_cs
, end_cs
, count
);
2437 int write_discarded_packets_message(struct details_write_ctx
*ctx
,
2438 const bt_message
*msg
)
2441 const bt_stream
*stream
= bt_message_discarded_packets_borrow_stream_const(
2443 const bt_stream_class
*sc
= bt_stream_borrow_class_const(stream
);
2444 const bt_clock_snapshot
*beginning_cs
= NULL
;
2445 const bt_clock_snapshot
*end_cs
= NULL
;
2448 if (!ctx
->details_comp
->cfg
.with_data
) {
2452 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(sc
)) {
2454 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
2457 bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
2461 if (bt_message_discarded_packets_get_count(msg
, &count
) !=
2462 BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
2463 count
= UINT64_C(-1);
2466 ret
= write_discarded_items_message(ctx
, "packets", stream
,
2467 beginning_cs
, end_cs
, count
);
2474 int write_packet_end_message(struct details_write_ctx
*ctx
,
2475 const bt_message
*msg
)
2478 const bt_packet
*packet
=
2479 bt_message_packet_end_borrow_packet_const(msg
);
2480 const bt_stream
*stream
= bt_packet_borrow_stream_const(packet
);
2481 const bt_stream_class
*sc
= bt_stream_borrow_class_const(stream
);
2483 if (!ctx
->details_comp
->cfg
.with_data
) {
2488 if (bt_stream_class_packets_have_end_default_clock_snapshot(sc
)) {
2490 bt_message_packet_end_borrow_default_clock_snapshot_const(
2494 /* Write follow tag for message */
2495 ret
= write_message_follow_tag(ctx
, stream
);
2500 write_obj_type_name(ctx
, "Packet end");
2508 int write_message_iterator_inactivity_message(struct details_write_ctx
*ctx
,
2509 const bt_message
*msg
)
2512 const bt_clock_snapshot
*cs
=
2513 bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(
2517 write_time(ctx
, cs
);
2518 write_obj_type_name(ctx
, "Message iterator inactivity");
2520 if (ctx
->details_comp
->cfg
.compact
) {
2525 /* Write clock class properties */
2526 g_string_append(ctx
->str
, ":\n");
2529 write_prop_name(ctx
, "Clock class");
2530 g_string_append_c(ctx
->str
, ':');
2533 write_clock_class_prop_lines(ctx
,
2534 bt_clock_snapshot_borrow_clock_class_const(cs
));
2541 int details_write_message(struct details_comp
*details_comp
,
2542 const bt_message
*msg
)
2545 struct details_write_ctx ctx
= {
2546 .details_comp
= details_comp
,
2547 .str
= details_comp
->str
,
2551 /* Reset output buffer */
2552 g_string_assign(details_comp
->str
, "");
2554 switch (bt_message_get_type(msg
)) {
2555 case BT_MESSAGE_TYPE_EVENT
:
2556 ret
= write_event_message(&ctx
, msg
);
2558 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
2559 ret
= write_message_iterator_inactivity_message(&ctx
, msg
);
2561 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
2562 ret
= write_stream_beginning_message(&ctx
, msg
);
2564 case BT_MESSAGE_TYPE_STREAM_END
:
2565 ret
= write_stream_end_message(&ctx
, msg
);
2567 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
2568 ret
= write_packet_beginning_message(&ctx
, msg
);
2570 case BT_MESSAGE_TYPE_PACKET_END
:
2571 ret
= write_packet_end_message(&ctx
, msg
);
2573 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
2574 ret
= write_discarded_events_message(&ctx
, msg
);
2576 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
2577 ret
= write_discarded_packets_message(&ctx
, msg
);
2584 * If this component printed at least one character so far, and
2585 * we're not in compact mode, and there's something in the
2586 * output buffer for this message, then prepend a newline to the
2587 * output buffer to visually separate message blocks.
2589 if (details_comp
->printed_something
&& !details_comp
->cfg
.compact
&&
2590 details_comp
->str
->len
> 0) {
2591 /* TODO: Optimize this */
2592 g_string_prepend_c(details_comp
->str
, '\n');