sink.text.details: add `with-data` initialization parameter
[babeltrace.git] / src / plugins / text / details / write.c
CommitLineData
55478183
PP
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
55478183 23#include <babeltrace2/babeltrace.h>
350ad6c1
PP
24#include <stdio.h>
25#include <string.h>
55478183
PP
26
27#include "common/assert.h"
28#include "common/common.h"
6162e6b7 29#include "common/uuid.h"
55478183
PP
30#include "details.h"
31#include "write.h"
32#include "obj-lifetime-mgmt.h"
33#include "colors.h"
34
35static inline
36const char *plural(uint64_t value)
37{
38 return value == 1 ? "" : "s";
39}
40
41static inline
42void incr_indent_by(struct details_write_ctx *ctx, unsigned int value)
43{
44 BT_ASSERT(ctx);
45 ctx->indent_level += value;
46}
47
48static inline
49void incr_indent(struct details_write_ctx *ctx)
50{
51 incr_indent_by(ctx, 2);
52}
53
54static inline
55void decr_indent_by(struct details_write_ctx *ctx, unsigned int value)
56{
57 BT_ASSERT(ctx);
58 BT_ASSERT(ctx->indent_level >= value);
59 ctx->indent_level -= value;
60}
61
62static inline
63void decr_indent(struct details_write_ctx *ctx)
64{
65 decr_indent_by(ctx, 2);
66}
67
68static inline
69void 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
114static inline
115void 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
167static inline
168void write_nl(struct details_write_ctx *ctx)
169{
170 BT_ASSERT(ctx);
171 g_string_append_c(ctx->str, '\n');
172}
173
174static inline
175void write_sp(struct details_write_ctx *ctx)
176{
177 BT_ASSERT(ctx);
178 g_string_append_c(ctx->str, ' ');
179}
180
181static inline
182void write_indent(struct details_write_ctx *ctx)
183{
184 uint64_t i;
185
186 BT_ASSERT(ctx);
187
188 for (i = 0; i < ctx->indent_level; i++) {
189 write_sp(ctx);
190 }
191}
192
193static inline
194void 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
201static inline
48be9a91
PP
202void write_array_index(struct details_write_ctx *ctx, uint64_t index,
203 const char *color)
55478183
PP
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:",
48be9a91 210 color, buf, color_reset(ctx));
55478183
PP
211}
212
213static inline
214void 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
220static inline
221void 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
48be9a91
PP
227static inline
228void 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
55478183
PP
235static inline
236void 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
25510d56
PP
242static inline
243void 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
55478183
PP
250static inline
251void write_uint_str_prop_value(struct details_write_ctx *ctx, const char *value)
252{
253 write_str_prop_value(ctx, value);
254}
255
256static inline
257void 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
265static inline
266void 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
274static inline
275void 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
281static inline
282void write_str_prop_line(struct details_write_ctx *ctx, const char *prop_name,
283 const char *prop_value)
284{
285 BT_ASSERT(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
293static inline
294void 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
304static inline
305void 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
315static inline
316void write_int_str_prop_value(struct details_write_ctx *ctx, const char *value)
317{
318 write_str_prop_value(ctx, value);
319}
320
321static inline
dacaa55d 322void write_bool_prop_value(struct details_write_ctx *ctx, bt_bool prop_value)
55478183
PP
323{
324 const char *str;
325
dacaa55d 326 g_string_append(ctx->str, color_bold(ctx));
55478183
PP
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
dacaa55d
PP
336 g_string_append_printf(ctx->str, "%s%s", str, color_reset(ctx));
337}
338
339static inline
340void 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);
55478183
PP
348}
349
350static inline
351void write_uuid_prop_line(struct details_write_ctx *ctx, const char *prop_name,
352 bt_uuid uuid)
353{
354 BT_ASSERT(uuid);
355 write_indent(ctx);
356 write_prop_name(ctx, prop_name);
357 g_string_append_printf(ctx->str,
6162e6b7 358 ": %s" BT_UUID_FMT "%s\n",
55478183 359 color_bold(ctx),
6162e6b7 360 BT_UUID_FMT_VALUES(uuid),
55478183
PP
361 color_reset(ctx));
362}
363
48be9a91
PP
364static
365gint compare_strings(const char **a, const char **b)
366{
367 return strcmp(*a, *b);
368}
369
370static
371bt_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(keys);
377 g_ptr_array_add(keys, (void *) key);
378 return BT_TRUE;
379}
380
381static
382void 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(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 {
393729a6 427 uint64_t length = bt_value_array_get_length(value);
48be9a91
PP
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(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
491static
492void write_user_attributes(struct details_write_ctx *ctx,
493 const bt_value *user_attrs, bool write_newline, bool *written)
494{
495 BT_ASSERT(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
55478183
PP
510static
511void 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
45c51519 541struct int_range {
55478183
PP
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
553struct enum_field_class_mapping {
554 /* Weak */
555 const char *label;
556
45c51519 557 /* Array of `struct int_range` */
55478183
PP
558 GArray *ranges;
559};
560
561static
562gint 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
568static
45c51519 569gint compare_int_ranges_signed(struct int_range *a, struct int_range *b)
55478183
PP
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
587static
45c51519 588gint compare_int_ranges_unsigned(struct int_range *a, struct int_range *b)
55478183
PP
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
45c51519
PP
605static
606GArray *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
653end:
654 return ranges;
655}
656
55478183
PP
657static
658void 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
668static
45c51519
PP
669struct int_range *int_range_at(GArray *ranges, uint64_t index)
670{
671 return &g_array_index(ranges, struct int_range, index);
672}
673
674static
675void write_int_range(struct details_write_ctx *ctx,
676 struct int_range *range, bool is_signed)
55478183
PP
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
45c51519
PP
686 if (range->lower.u != range->upper.u) {
687 g_string_append(ctx->str, ", ");
55478183 688
45c51519
PP
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 }
55478183
PP
694 }
695
696 g_string_append(ctx->str, "]");
697}
698
699static
700void 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(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;
45c51519 719 const void *fc_range_set;
55478183
PP
720 struct enum_field_class_mapping *mapping = g_new0(
721 struct enum_field_class_mapping, 1);
722
723 BT_ASSERT(mapping);
55478183
PP
724
725 if (is_signed) {
9c08c816 726 fc_mapping = bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
55478183 727 fc, i);
9c08c816 728 fc_range_set = bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
45c51519 729 fc_mapping);
55478183 730 } else {
9c08c816 731 fc_mapping = bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
55478183 732 fc, i);
9c08c816 733 fc_range_set = bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
45c51519 734 fc_mapping);
55478183
PP
735 }
736
737 mapping->label = bt_field_class_enumeration_mapping_get_label(
9c08c816 738 bt_field_class_enumeration_signed_mapping_as_mapping_const(
55478183 739 fc_mapping));
45c51519
PP
740 mapping->ranges = range_set_to_int_ranges(fc_range_set,
741 is_signed);
742 BT_ASSERT(mapping->ranges);
55478183
PP
743 g_ptr_array_add(mappings, mapping);
744 }
745
45c51519 746 /* Sort mappings (ranges are already sorted within mappings) */
55478183
PP
747 g_ptr_array_sort(mappings,
748 (GCompareFunc) compare_enum_field_class_mappings);
749
55478183
PP
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);
48be9a91 755 write_prop_name_line(ctx, mapping->label);
55478183 756
55478183 757 for (range_i = 0; range_i < mapping->ranges->len; range_i++) {
45c51519
PP
758 write_sp(ctx);
759 write_int_range(ctx,
760 int_range_at(mapping->ranges, range_i),
761 is_signed);
55478183 762 }
55478183
PP
763 }
764
765 g_ptr_array_free(mappings, TRUE);
766}
767
768static
769void 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)) {
e7ceb9df 777 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT:
55478183
PP
778 write_str_prop_value(ctx, "Packet context");
779 break;
e7ceb9df 780 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT:
55478183
PP
781 write_str_prop_value(ctx, "Event common context");
782 break;
e7ceb9df 783 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT:
55478183
PP
784 write_str_prop_value(ctx, "Event specific context");
785 break;
e7ceb9df 786 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD:
55478183
PP
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
819static
45c51519
PP
820void write_field_class(struct details_write_ctx *ctx, const bt_field_class *fc);
821
822static
823void write_variant_field_class_option(struct details_write_ctx *ctx,
824 const bt_field_class *fc, uint64_t index)
55478183 825{
55478183 826 bt_field_class_type fc_type = bt_field_class_get_type(fc);
45c51519
PP
827 const bt_field_class_variant_option *option =
828 bt_field_class_variant_borrow_option_by_index_const(
829 fc, index);
48be9a91 830 const void *orig_ranges = NULL;
45c51519
PP
831 GArray *int_ranges = NULL;
832 bool is_signed;
48be9a91
PP
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);
55478183 838
45c51519
PP
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_SELECTOR) {
9c08c816
PP
844 const bt_field_class_variant_with_selector_unsigned_option *spec_opt =
845 bt_field_class_variant_with_selector_unsigned_borrow_option_by_index_const(
45c51519
PP
846 fc, index);
847
848 orig_ranges =
9c08c816 849 bt_field_class_variant_with_selector_unsigned_option_borrow_ranges_const(
45c51519
PP
850 spec_opt);
851 is_signed = false;
48be9a91 852 } else if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR) {
9c08c816
PP
853 const bt_field_class_variant_with_selector_signed_option *spec_opt =
854 bt_field_class_variant_with_selector_signed_borrow_option_by_index_const(
45c51519
PP
855 fc, index);
856
857 orig_ranges =
9c08c816 858 bt_field_class_variant_with_selector_signed_option_borrow_ranges_const(
45c51519
PP
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(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 {
55478183
PP
878 write_sp(ctx);
879 }
880
48be9a91
PP
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);
45c51519 886
48be9a91
PP
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);
45c51519 892
48be9a91
PP
893 /* User attributes */
894 write_user_attributes(ctx, user_attrs,
895 false, NULL);
45c51519 896
48be9a91 897 decr_indent(ctx);
45c51519
PP
898 }
899
48be9a91
PP
900 if (int_ranges) {
901 g_array_free(int_ranges, TRUE);
45c51519
PP
902 }
903}
904
905static
906void 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);
48be9a91
PP
911 const bt_value *user_attrs;
912 bool wrote_user_attrs = false;
45c51519 913
55478183
PP
914 /* Write field class's type */
915 switch (fc_type) {
dacaa55d
PP
916 case BT_FIELD_CLASS_TYPE_BOOL:
917 type = "Boolean";
918 break;
9684703d
PP
919 case BT_FIELD_CLASS_TYPE_BIT_ARRAY:
920 type = "Bit array";
921 break;
55478183
PP
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_REAL:
935 type = "Real";
936 break;
937 case BT_FIELD_CLASS_TYPE_STRING:
938 type = "String";
939 break;
940 case BT_FIELD_CLASS_TYPE_STRUCTURE:
941 type = "Structure";
942 break;
943 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
944 type = "Static array";
945 break;
946 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
947 type = "Dynamic array";
948 break;
25510d56
PP
949 case BT_FIELD_CLASS_TYPE_OPTION:
950 type = "Option";
951 break;
45c51519
PP
952 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
953 type = "Variant (no selector)";
954 break;
955 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
956 type = "Variant (unsigned selector)";
957 break;
958 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
959 type = "Variant (signed selector)";
55478183
PP
960 break;
961 default:
962 abort();
963 }
964
965 g_string_append_printf(ctx->str, "%s%s%s",
966 color_fg_blue(ctx), type, color_reset(ctx));
967
48be9a91 968 /* Write field class's single-line properties */
55478183
PP
969 switch (fc_type) {
970 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
971 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
972 write_sp(ctx);
973 write_int_field_class_props(ctx, fc, true);
974 break;
975 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
976 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
977 {
978 uint64_t mapping_count =
979 bt_field_class_enumeration_get_mapping_count(fc);
980
981 write_sp(ctx);
982 write_int_field_class_props(ctx, fc, false);
983 g_string_append(ctx->str, ", ");
984 write_uint_prop_value(ctx, mapping_count);
985 g_string_append_printf(ctx->str, " mapping%s)",
986 plural(mapping_count));
55478183
PP
987 break;
988 }
989 case BT_FIELD_CLASS_TYPE_REAL:
990 if (bt_field_class_real_is_single_precision(fc)) {
991 g_string_append(ctx->str, " (Single precision)");
992 } else {
993 g_string_append(ctx->str, " (Double precision)");
994 }
995
996 break;
997 case BT_FIELD_CLASS_TYPE_STRUCTURE:
998 {
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));
55478183
PP
1006 break;
1007 }
1008 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1009 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
1010 if (fc_type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY) {
1011 g_string_append(ctx->str, " (Length ");
1012 write_uint_prop_value(ctx,
9c08c816 1013 bt_field_class_array_static_get_length(fc));
55478183
PP
1014 g_string_append_c(ctx->str, ')');
1015 } else {
1016 const bt_field_path *length_field_path =
9c08c816 1017 bt_field_class_array_dynamic_borrow_length_field_path_const(
55478183
PP
1018 fc);
1019
1020 if (length_field_path) {
1021 g_string_append(ctx->str, " (Length field path ");
1022 write_field_path(ctx, length_field_path);
1023 g_string_append_c(ctx->str, ')');
1024 }
1025 }
1026
55478183 1027 break;
25510d56
PP
1028 case BT_FIELD_CLASS_TYPE_OPTION:
1029 {
1030 const bt_field_path *selector_field_path =
1031 bt_field_class_option_borrow_selector_field_path_const(fc);
1032
1033 if (selector_field_path) {
1034 g_string_append(ctx->str, " (Selector field path ");
1035 write_field_path(ctx, selector_field_path);
1036 g_string_append_c(ctx->str, ')');
1037 }
1038
48be9a91
PP
1039 break;
1040 }
1041 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
1042 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
1043 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
1044 {
1045 uint64_t option_count =
1046 bt_field_class_variant_get_option_count(fc);
1047 const bt_field_path *sel_field_path = NULL;
1048
1049 if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR ||
1050 fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR) {
1051 sel_field_path =
1052 bt_field_class_variant_with_selector_borrow_selector_field_path_const(
1053 fc);
1054 BT_ASSERT(sel_field_path);
1055 }
1056
1057 g_string_append(ctx->str, " (");
1058 write_uint_prop_value(ctx, option_count);
1059 g_string_append_printf(ctx->str, " option%s",
1060 plural(option_count));
1061
1062 if (sel_field_path) {
1063 g_string_append(ctx->str, ", Selector field path ");
1064 write_field_path(ctx, sel_field_path);
1065 }
1066
1067 g_string_append_c(ctx->str, ')');
1068 break;
1069 }
1070 default:
1071 break;
1072 }
1073
1074 incr_indent(ctx);
1075 user_attrs = bt_field_class_borrow_user_attributes_const(fc);
1076 if (!bt_value_map_is_empty(user_attrs)) {
1077 g_string_append(ctx->str, ":\n");
1078 write_user_attributes(ctx, user_attrs, false, NULL);
1079 wrote_user_attrs = true;
1080 }
1081
1082 /* Write field class's complex properties */
1083 switch (fc_type) {
1084 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
1085 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
1086 {
1087 uint64_t mapping_count =
1088 bt_field_class_enumeration_get_mapping_count(fc);
1089
1090 if (mapping_count > 0) {
1091 if (wrote_user_attrs) {
1092 write_nl(ctx);
1093 write_indent(ctx);
1094 write_prop_name(ctx, "Mappings");
1095 g_string_append_c(ctx->str, ':');
1096 incr_indent(ctx);
1097 } else {
1098 /* Each mapping starts with its own newline */
1099 g_string_append_c(ctx->str, ':');
1100 }
1101
1102 write_enum_field_class_mappings(ctx, fc);
1103
1104 if (wrote_user_attrs) {
1105 decr_indent(ctx);
1106 }
1107 }
1108
1109 break;
1110 }
1111 case BT_FIELD_CLASS_TYPE_STRUCTURE:
1112 {
1113 uint64_t member_count =
1114 bt_field_class_structure_get_member_count(fc);
1115
1116 if (member_count > 0) {
1117 if (wrote_user_attrs) {
1118 write_nl(ctx);
1119 write_indent(ctx);
1120 write_prop_name(ctx, "Members");
1121 g_string_append_c(ctx->str, ':');
1122 incr_indent(ctx);
1123 } else {
1124 /* Each member starts with its own newline */
1125 g_string_append_c(ctx->str, ':');
1126 }
1127
1128 for (i = 0; i < member_count; i++) {
1129 const bt_field_class_structure_member *member =
1130 bt_field_class_structure_borrow_member_by_index_const(
1131 fc, i);
1132 const bt_value *user_attrs;
1133 const bt_field_class *member_fc =
1134 bt_field_class_structure_member_borrow_field_class_const(member);
1135
1136 write_nl(ctx);
1137 write_compound_member_name(ctx,
1138 bt_field_class_structure_member_get_name(member));
1139 user_attrs = bt_field_class_structure_member_borrow_user_attributes_const(
1140 member);
1141
1142 if (bt_value_map_is_empty(user_attrs)) {
1143 write_sp(ctx);
1144 write_field_class(ctx, member_fc);
1145 } else {
1146 write_nl(ctx);
1147 incr_indent(ctx);
1148
1149 /* Field class */
1150 write_prop_name_line(ctx, "Field class");
1151 write_sp(ctx);
1152 write_field_class(ctx, member_fc);
1153 write_nl(ctx);
1154
1155 /* User attributes */
1156 write_user_attributes(ctx, user_attrs,
1157 false, NULL);
1158
1159 decr_indent(ctx);
1160 }
1161 }
1162
1163 if (wrote_user_attrs) {
1164 decr_indent(ctx);
1165 }
1166 }
1167
1168 break;
1169 }
1170 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1171 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
1172 if (wrote_user_attrs) {
1173 write_nl(ctx);
1174 } else {
1175 g_string_append(ctx->str, ":\n");
1176 }
1177
1178 write_prop_name_line(ctx, "Element");
1179 write_sp(ctx);
1180 write_field_class(ctx,
1181 bt_field_class_array_borrow_element_field_class_const(fc));
1182 break;
1183 case BT_FIELD_CLASS_TYPE_OPTION:
1184 if (wrote_user_attrs) {
1185 write_nl(ctx);
1186 } else {
1187 g_string_append(ctx->str, ":\n");
1188 }
1189
1190 write_prop_name_line(ctx, "Content");
25510d56
PP
1191 write_sp(ctx);
1192 write_field_class(ctx,
1193 bt_field_class_option_borrow_field_class_const(fc));
25510d56 1194 break;
45c51519
PP
1195 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
1196 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
1197 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
48be9a91
PP
1198 {
1199 uint64_t option_count =
1200 bt_field_class_variant_get_option_count(fc);
1201
1202 if (option_count > 0) {
1203 if (wrote_user_attrs) {
1204 write_nl(ctx);
1205 write_indent(ctx);
1206 write_prop_name(ctx, "Options");
1207 g_string_append_c(ctx->str, ':');
1208 incr_indent(ctx);
1209 } else {
1210 /* Each option starts with its own newline */
1211 g_string_append_c(ctx->str, ':');
1212 }
1213
1214 for (i = 0; i < option_count; i++) {
1215 write_variant_field_class_option(ctx, fc, i);
1216 }
1217
1218 if (wrote_user_attrs) {
1219 decr_indent(ctx);
1220 }
1221 }
1222
55478183 1223 break;
48be9a91 1224 }
55478183
PP
1225 default:
1226 break;
1227 }
48be9a91
PP
1228
1229 decr_indent(ctx);
55478183
PP
1230}
1231
1232static
1233void write_root_field_class(struct details_write_ctx *ctx, const char *name,
1234 const bt_field_class *fc)
1235{
1236 BT_ASSERT(name);
1237 BT_ASSERT(fc);
1238 write_indent(ctx);
1239 write_prop_name(ctx, name);
1240 g_string_append(ctx->str, ": ");
45c51519 1241 write_field_class(ctx, fc);
55478183
PP
1242 write_nl(ctx);
1243}
1244
1245static
1246void write_event_class(struct details_write_ctx *ctx, const bt_event_class *ec)
1247{
1248 const char *name = bt_event_class_get_name(ec);
1249 const char *emf_uri;
1250 const bt_field_class *fc;
1251 bt_event_class_log_level log_level;
1252
1253 write_indent(ctx);
1254 write_obj_type_name(ctx, "Event class");
1255
1256 /* Write name and ID */
1257 if (name) {
1258 g_string_append_printf(ctx->str, " `%s%s%s`",
1259 color_fg_green(ctx), name, color_reset(ctx));
1260 }
1261
1262 g_string_append(ctx->str, " (ID ");
1263 write_uint_prop_value(ctx, bt_event_class_get_id(ec));
1264 g_string_append(ctx->str, "):\n");
1265
1266 /* Write properties */
1267 incr_indent(ctx);
1268
48be9a91
PP
1269 /* Write user attributes */
1270 write_user_attributes(ctx,
1271 bt_event_class_borrow_user_attributes_const(ec), true, NULL);
1272
55478183
PP
1273 /* Write log level */
1274 if (bt_event_class_get_log_level(ec, &log_level) ==
1275 BT_PROPERTY_AVAILABILITY_AVAILABLE) {
1276 const char *ll_str = NULL;
1277
1278 switch (log_level) {
1279 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY:
1280 ll_str = "Emergency";
1281 break;
1282 case BT_EVENT_CLASS_LOG_LEVEL_ALERT:
1283 ll_str = "Alert";
1284 break;
1285 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL:
1286 ll_str = "Critical";
1287 break;
1288 case BT_EVENT_CLASS_LOG_LEVEL_ERROR:
1289 ll_str = "Error";
1290 break;
1291 case BT_EVENT_CLASS_LOG_LEVEL_WARNING:
1292 ll_str = "Warning";
1293 break;
1294 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE:
1295 ll_str = "Notice";
1296 break;
1297 case BT_EVENT_CLASS_LOG_LEVEL_INFO:
1298 ll_str = "Info";
1299 break;
1300 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM:
1301 ll_str = "Debug (system)";
1302 break;
1303 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM:
1304 ll_str = "Debug (program)";
1305 break;
1306 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS:
1307 ll_str = "Debug (process)";
1308 break;
1309 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE:
1310 ll_str = "Debug (module)";
1311 break;
1312 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT:
1313 ll_str = "Debug (unit)";
1314 break;
1315 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION:
1316 ll_str = "Debug (function)";
1317 break;
1318 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE:
1319 ll_str = "Debug (line)";
1320 break;
1321 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG:
1322 ll_str = "Debug";
1323 break;
1324 default:
1325 abort();
1326 }
1327
1328 write_str_prop_line(ctx, "Log level", ll_str);
1329 }
1330
1331 /* Write EMF URI */
1332 emf_uri = bt_event_class_get_emf_uri(ec);
1333 if (emf_uri) {
1334 write_str_prop_line(ctx, "EMF URI", emf_uri);
1335 }
1336
1337 /* Write specific context field class */
1338 fc = bt_event_class_borrow_specific_context_field_class_const(ec);
1339 if (fc) {
1340 write_root_field_class(ctx, "Specific context field class", fc);
1341 }
1342
1343 /* Write payload field class */
1344 fc = bt_event_class_borrow_payload_field_class_const(ec);
1345 if (fc) {
1346 write_root_field_class(ctx, "Payload field class", fc);
1347 }
1348
1349 decr_indent(ctx);
1350}
1351
1352static
1353void write_clock_class_prop_lines(struct details_write_ctx *ctx,
1354 const bt_clock_class *cc)
1355{
1356 int64_t offset_seconds;
1357 uint64_t offset_cycles;
1358 const char *str;
1359
1360 str = bt_clock_class_get_name(cc);
1361 if (str) {
1362 write_str_prop_line(ctx, "Name", str);
1363 }
1364
48be9a91
PP
1365 write_user_attributes(ctx,
1366 bt_clock_class_borrow_user_attributes_const(cc), true, NULL);
55478183
PP
1367 str = bt_clock_class_get_description(cc);
1368 if (str) {
1369 write_str_prop_line(ctx, "Description", str);
1370 }
1371
1372 write_uint_prop_line(ctx, "Frequency (Hz)",
1373 bt_clock_class_get_frequency(cc));
1374 write_uint_prop_line(ctx, "Precision (cycles)",
1375 bt_clock_class_get_precision(cc));
1376 bt_clock_class_get_offset(cc, &offset_seconds, &offset_cycles);
1377 write_int_prop_line(ctx, "Offset (s)", offset_seconds);
1378 write_uint_prop_line(ctx, "Offset (cycles)", offset_cycles);
1379 write_bool_prop_line(ctx, "Origin is Unix epoch",
1380 bt_clock_class_origin_is_unix_epoch(cc));
1381
1382 if (ctx->details_comp->cfg.with_uuid) {
1383 bt_uuid uuid = bt_clock_class_get_uuid(cc);
1384
1385 if (uuid) {
1386 write_uuid_prop_line(ctx, "UUID", uuid);
1387 }
1388 }
1389}
1390
1391static
1392gint compare_event_classes(const bt_event_class **a, const bt_event_class **b)
1393{
1394 uint64_t id_a = bt_event_class_get_id(*a);
1395 uint64_t id_b = bt_event_class_get_id(*b);
1396
1397 if (id_a < id_b) {
1398 return -1;
1399 } else if (id_a > id_b) {
1400 return 1;
1401 } else {
1402 return 0;
1403 }
1404}
1405
1406static
1407void write_stream_class(struct details_write_ctx *ctx,
1408 const bt_stream_class *sc)
1409{
1410 const bt_field_class *fc;
1411 GPtrArray *event_classes = g_ptr_array_new();
1412 uint64_t i;
1413
1414 write_indent(ctx);
1415 write_obj_type_name(ctx, "Stream class");
1416
1417 /* Write name and ID */
1418 if (ctx->details_comp->cfg.with_stream_class_name) {
1419 const char *name = bt_stream_class_get_name(sc);
1420
1421 if (name) {
1422 g_string_append(ctx->str, " `");
1423 write_str_prop_value(ctx, name);
1424 g_string_append(ctx->str, "`");
1425 }
1426 }
1427
1428 g_string_append(ctx->str, " (ID ");
1429 write_uint_prop_value(ctx, bt_stream_class_get_id(sc));
1430 g_string_append(ctx->str, "):\n");
1431
1432 /* Write properties */
1433 incr_indent(ctx);
1434
48be9a91
PP
1435 /* Write user attributes */
1436 write_user_attributes(ctx,
1437 bt_stream_class_borrow_user_attributes_const(sc), true, NULL);
1438
55478183
PP
1439 /* Write configuration */
1440 write_bool_prop_line(ctx,
26fc5aed
PP
1441 "Supports packets", bt_stream_class_supports_packets(sc));
1442
1443 if (bt_stream_class_supports_packets(sc)) {
1444 write_bool_prop_line(ctx,
1445 "Packets have beginning default clock snapshot",
1446 bt_stream_class_packets_have_beginning_default_clock_snapshot(sc));
1447 write_bool_prop_line(ctx,
1448 "Packets have end default clock snapshot",
1449 bt_stream_class_packets_have_end_default_clock_snapshot(sc));
1450 }
1451
55478183
PP
1452 write_bool_prop_line(ctx,
1453 "Supports discarded events",
1454 bt_stream_class_supports_discarded_events(sc));
a3c75374
PP
1455
1456 if (bt_stream_class_supports_discarded_events(sc)) {
1457 write_bool_prop_line(ctx,
1458 "Discarded events have default clock snapshots",
1459 bt_stream_class_discarded_events_have_default_clock_snapshots(sc));
1460 }
1461
55478183
PP
1462 write_bool_prop_line(ctx,
1463 "Supports discarded packets",
1464 bt_stream_class_supports_discarded_packets(sc));
a3c75374
PP
1465
1466 if (bt_stream_class_supports_discarded_packets(sc)) {
1467 write_bool_prop_line(ctx,
1468 "Discarded packets have default clock snapshots",
1469 bt_stream_class_discarded_packets_have_default_clock_snapshots(sc));
1470 }
55478183
PP
1471
1472 /* Write default clock class */
1473 if (bt_stream_class_borrow_default_clock_class_const(sc)) {
1474 write_indent(ctx);
1475 write_prop_name(ctx, "Default clock class");
1476 g_string_append_c(ctx->str, ':');
1477 write_nl(ctx);
1478 incr_indent(ctx);
1479 write_clock_class_prop_lines(ctx,
1480 bt_stream_class_borrow_default_clock_class_const(sc));
1481 decr_indent(ctx);
1482 }
1483
1484 fc = bt_stream_class_borrow_packet_context_field_class_const(sc);
1485 if (fc) {
1486 write_root_field_class(ctx, "Packet context field class", fc);
1487 }
1488
1489 fc = bt_stream_class_borrow_event_common_context_field_class_const(sc);
1490 if (fc) {
1491 write_root_field_class(ctx, "Event common context field class",
1492 fc);
1493 }
1494
1495 for (i = 0; i < bt_stream_class_get_event_class_count(sc); i++) {
1496 g_ptr_array_add(event_classes,
1497 (gpointer) bt_stream_class_borrow_event_class_by_index_const(
1498 sc, i));
1499 }
1500
1501 g_ptr_array_sort(event_classes, (GCompareFunc) compare_event_classes);
1502
1503 for (i = 0; i < event_classes->len; i++) {
1504 write_event_class(ctx, event_classes->pdata[i]);
1505 }
1506
1507 decr_indent(ctx);
1508 g_ptr_array_free(event_classes, TRUE);
1509}
1510
1511static
1512gint compare_stream_classes(const bt_stream_class **a, const bt_stream_class **b)
1513{
1514 uint64_t id_a = bt_stream_class_get_id(*a);
1515 uint64_t id_b = bt_stream_class_get_id(*b);
1516
1517 if (id_a < id_b) {
1518 return -1;
1519 } else if (id_a > id_b) {
1520 return 1;
1521 } else {
1522 return 0;
1523 }
1524}
1525
55478183
PP
1526static
1527void write_trace_class(struct details_write_ctx *ctx, const bt_trace_class *tc)
1528{
1529 GPtrArray *stream_classes = g_ptr_array_new();
55478183
PP
1530 uint64_t i;
1531 bool printed_prop = false;
1532
1533 write_indent(ctx);
1534 write_obj_type_name(ctx, "Trace class");
1535
48be9a91 1536
55478183
PP
1537 for (i = 0; i < bt_trace_class_get_stream_class_count(tc); i++) {
1538 g_ptr_array_add(stream_classes,
1539 (gpointer) bt_trace_class_borrow_stream_class_by_index_const(
1540 tc, i));
1541 }
1542
1543 g_ptr_array_sort(stream_classes, (GCompareFunc) compare_stream_classes);
1544
1545 if (stream_classes->len > 0) {
1546 if (!printed_prop) {
1547 g_string_append(ctx->str, ":\n");
1548 printed_prop = true;
1549 }
1550 }
1551
335a2da5
PP
1552 incr_indent(ctx);
1553
48be9a91
PP
1554 /* Write user attributes */
1555 write_user_attributes(ctx,
1556 bt_trace_class_borrow_user_attributes_const(tc), true,
1557 &printed_prop);
1558
1559 /* Write stream classes */
55478183
PP
1560 for (i = 0; i < stream_classes->len; i++) {
1561 write_stream_class(ctx, stream_classes->pdata[i]);
1562 }
1563
55478183
PP
1564 if (!printed_prop) {
1565 write_nl(ctx);
1566 }
1567
335a2da5 1568 decr_indent(ctx);
55478183 1569 g_ptr_array_free(stream_classes, TRUE);
55478183
PP
1570}
1571
1572static
1573int try_write_meta(struct details_write_ctx *ctx, const bt_trace_class *tc,
1574 const bt_stream_class *sc, const bt_event_class *ec)
1575{
1576 int ret = 0;
1577
1578 BT_ASSERT(tc);
1579
1580 if (details_need_to_write_trace_class(ctx, tc)) {
1581 uint64_t sc_i;
1582
1583 if (ctx->details_comp->cfg.compact &&
1584 ctx->details_comp->printed_something) {
1585 /*
1586 * There are no empty line between messages in
1587 * compact mode, so write one here to decouple
1588 * the trace class from the next message.
1589 */
1590 write_nl(ctx);
1591 }
1592
1593 /*
1594 * write_trace_class() also writes all its stream
1595 * classes their event classes, so we don't need to
1596 * rewrite `sc`.
1597 */
1598 write_trace_class(ctx, tc);
55478183
PP
1599
1600 /*
1601 * Mark this trace class as written, as well as all
1602 * its stream classes and their event classes.
1603 */
1604 ret = details_did_write_trace_class(ctx, tc);
1605 if (ret) {
1606 goto end;
1607 }
1608
1609 for (sc_i = 0; sc_i < bt_trace_class_get_stream_class_count(tc);
1610 sc_i++) {
1611 uint64_t ec_i;
1612 const bt_stream_class *tc_sc =
1613 bt_trace_class_borrow_stream_class_by_index_const(
1614 tc, sc_i);
1615
1616 details_did_write_meta_object(ctx, tc, tc_sc);
1617
1618 for (ec_i = 0; ec_i <
1619 bt_stream_class_get_event_class_count(tc_sc);
1620 ec_i++) {
1621 details_did_write_meta_object(ctx, tc,
1622 bt_stream_class_borrow_event_class_by_index_const(
1623 tc_sc, ec_i));
1624 }
1625 }
1626
1627 goto end;
1628 }
1629
1630 if (sc && details_need_to_write_meta_object(ctx, tc, sc)) {
1631 uint64_t ec_i;
1632
1633 BT_ASSERT(tc);
1634
1635 if (ctx->details_comp->cfg.compact &&
1636 ctx->details_comp->printed_something) {
1637 /*
1638 * There are no empty line between messages in
1639 * compact mode, so write one here to decouple
1640 * the stream class from the next message.
1641 */
1642 write_nl(ctx);
1643 }
1644
1645 /*
1646 * write_stream_class() also writes all its event
1647 * classes, so we don't need to rewrite `ec`.
1648 */
1649 write_stream_class(ctx, sc);
55478183
PP
1650
1651 /*
1652 * Mark this stream class as written, as well as all its
1653 * event classes.
1654 */
1655 details_did_write_meta_object(ctx, tc, sc);
1656
1657 for (ec_i = 0; ec_i <
1658 bt_stream_class_get_event_class_count(sc);
1659 ec_i++) {
1660 details_did_write_meta_object(ctx, tc,
1661 bt_stream_class_borrow_event_class_by_index_const(
1662 sc, ec_i));
1663 }
1664
1665 goto end;
1666 }
1667
1668 if (ec && details_need_to_write_meta_object(ctx, tc, ec)) {
1669 BT_ASSERT(sc);
1670
1671 if (ctx->details_comp->cfg.compact &&
1672 ctx->details_comp->printed_something) {
1673 /*
1674 * There are no empty line between messages in
1675 * compact mode, so write one here to decouple
1676 * the event class from the next message.
1677 */
1678 write_nl(ctx);
1679 }
1680
1681 write_event_class(ctx, ec);
55478183
PP
1682 details_did_write_meta_object(ctx, tc, ec);
1683 goto end;
1684 }
1685
1686end:
1687 return ret;
1688}
1689
1690static
1691void write_time_str(struct details_write_ctx *ctx, const char *str)
1692{
1693 if (!ctx->details_comp->cfg.with_time) {
1694 goto end;
1695 }
1696
1697 g_string_append_printf(ctx->str, "[%s%s%s%s]",
1698 color_bold(ctx), color_fg_blue(ctx), str, color_reset(ctx));
1699
1700 if (ctx->details_comp->cfg.compact) {
1701 write_sp(ctx);
1702 } else {
1703 write_nl(ctx);
1704 }
1705
1706end:
1707 return;
1708}
1709
1710static
1711void write_time(struct details_write_ctx *ctx, const bt_clock_snapshot *cs)
1712{
d24d5663 1713 bt_clock_snapshot_get_ns_from_origin_status cs_status;
55478183
PP
1714 int64_t ns_from_origin;
1715 char buf[32];
1716
1717 if (!ctx->details_comp->cfg.with_time) {
1718 goto end;
1719 }
1720
1721 format_uint(buf, bt_clock_snapshot_get_value(cs), 10);
1722 g_string_append_printf(ctx->str, "[%s%s%s%s%s",
1723 color_bold(ctx), color_fg_blue(ctx), buf,
1724 color_reset(ctx),
1725 ctx->details_comp->cfg.compact ? "" : " cycles");
d24d5663
PP
1726 cs_status = bt_clock_snapshot_get_ns_from_origin(cs, &ns_from_origin);
1727 if (cs_status == BT_CLOCK_SNAPSHOT_GET_NS_FROM_ORIGIN_STATUS_OK) {
55478183
PP
1728 format_int(buf, ns_from_origin, 10);
1729 g_string_append_printf(ctx->str, "%s %s%s%s%s%s",
1730 ctx->details_comp->cfg.compact ? "" : ",",
1731 color_bold(ctx), color_fg_blue(ctx), buf,
1732 color_reset(ctx),
1733 ctx->details_comp->cfg.compact ? "" : " ns from origin");
1734 }
1735
1736 g_string_append(ctx->str, "]");
1737
1738 if (ctx->details_comp->cfg.compact) {
1739 write_sp(ctx);
1740 } else {
1741 write_nl(ctx);
1742 }
1743
1744end:
1745 return;
1746}
1747
1748static
1749int write_message_follow_tag(struct details_write_ctx *ctx,
1750 const bt_stream *stream)
1751{
1752 int ret;
1753 uint64_t unique_trace_id;
1754 const bt_stream_class *sc = bt_stream_borrow_class_const(stream);
1755 const bt_trace *trace = bt_stream_borrow_trace_const(stream);
1756
1757 ret = details_trace_unique_id(ctx, trace, &unique_trace_id);
1758 if (ret) {
1759 goto end;
1760 }
1761
1762 if (ctx->details_comp->cfg.compact) {
1763 g_string_append_printf(ctx->str,
1764 "%s{%s%" PRIu64 " %" PRIu64 " %" PRIu64 "%s%s}%s ",
1765 color_fg_cyan(ctx), color_bold(ctx),
1766 unique_trace_id, bt_stream_class_get_id(sc),
1767 bt_stream_get_id(stream),
1768 color_reset(ctx), color_fg_cyan(ctx), color_reset(ctx));
1769 } else {
1770 g_string_append_printf(ctx->str,
1771 "%s{Trace %s%" PRIu64 "%s%s, Stream class ID %s%" PRIu64 "%s%s, Stream ID %s%" PRIu64 "%s%s}%s\n",
1772 color_fg_cyan(ctx),
1773 color_bold(ctx), unique_trace_id,
1774 color_reset(ctx), color_fg_cyan(ctx),
1775 color_bold(ctx), bt_stream_class_get_id(sc),
1776 color_reset(ctx), color_fg_cyan(ctx),
1777 color_bold(ctx), bt_stream_get_id(stream),
1778 color_reset(ctx), color_fg_cyan(ctx),
1779 color_reset(ctx));
1780 }
1781
1782end:
1783 return ret;
1784}
1785
1786static
1787void write_field(struct details_write_ctx *ctx, const bt_field *field,
1788 const char *name)
1789{
1790 uint64_t i;
1791 bt_field_class_type fc_type = bt_field_get_class_type(field);
1792 const bt_field_class *fc;
1793 char buf[64];
1794
1795 /* Write field's name */
1796 if (name) {
1797 write_compound_member_name(ctx, name);
1798 }
1799
1800 /* Write field's value */
1801 switch (fc_type) {
dacaa55d
PP
1802 case BT_FIELD_CLASS_TYPE_BOOL:
1803 write_sp(ctx);
1804 write_bool_prop_value(ctx, bt_field_bool_get_value(field));
1805 break;
9684703d
PP
1806 case BT_FIELD_CLASS_TYPE_BIT_ARRAY:
1807 format_uint(buf, bt_field_bit_array_get_value_as_integer(field),
1808 16);
1809 write_sp(ctx);
1810 write_uint_str_prop_value(ctx, buf);
1811 break;
55478183
PP
1812 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
1813 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
1814 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
1815 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
1816 {
1817 unsigned int fmt_base;
1818 bt_field_class_integer_preferred_display_base base;
1819
1820 fc = bt_field_borrow_class_const(field);
1821 base = bt_field_class_integer_get_preferred_display_base(fc);
1822
1823 switch (base) {
1824 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
1825 fmt_base = 10;
1826 break;
1827 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
1828 fmt_base = 8;
1829 break;
1830 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
1831 fmt_base = 2;
1832 break;
1833 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
1834 fmt_base = 16;
1835 break;
1836 default:
1837 abort();
1838 }
1839
1840 if (fc_type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
1841 fc_type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION) {
1842 format_uint(buf,
9c08c816 1843 bt_field_integer_unsigned_get_value(field),
55478183
PP
1844 fmt_base);
1845 write_sp(ctx);
1846 write_uint_str_prop_value(ctx, buf);
1847 } else {
1848 format_int(buf,
9c08c816 1849 bt_field_integer_signed_get_value(field),
55478183
PP
1850 fmt_base);
1851 write_sp(ctx);
1852 write_int_str_prop_value(ctx, buf);
1853 }
1854
1855 break;
1856 }
1857 case BT_FIELD_CLASS_TYPE_REAL:
1858 write_sp(ctx);
1859 write_float_prop_value(ctx, bt_field_real_get_value(field));
1860 break;
1861 case BT_FIELD_CLASS_TYPE_STRING:
1862 write_sp(ctx);
1863 write_str_prop_value(ctx, bt_field_string_get_value(field));
1864 break;
1865 case BT_FIELD_CLASS_TYPE_STRUCTURE:
1866 {
1867 uint64_t member_count;
1868
1869 fc = bt_field_borrow_class_const(field);
1870 member_count = bt_field_class_structure_get_member_count(fc);
1871
1872 if (member_count > 0) {
1873 incr_indent(ctx);
1874
1875 for (i = 0; i < member_count; i++) {
1876 const bt_field_class_structure_member *member =
1877 bt_field_class_structure_borrow_member_by_index_const(
1878 fc, i);
1879 const bt_field *member_field =
1880 bt_field_structure_borrow_member_field_by_index_const(
1881 field, i);
1882
1883 write_nl(ctx);
1884 write_field(ctx, member_field,
1885 bt_field_class_structure_member_get_name(member));
1886 }
1887
1888 decr_indent(ctx);
1889 } else {
8f512475
PP
1890 write_sp(ctx);
1891 write_none_prop_value(ctx, "Empty");
55478183
PP
1892 }
1893
1894 break;
1895 }
1896 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1897 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
1898 {
1899 uint64_t length = bt_field_array_get_length(field);
1900
1901 if (length == 0) {
8f512475
PP
1902 write_sp(ctx);
1903 write_none_prop_value(ctx, "Empty");
55478183
PP
1904 } else {
1905 g_string_append(ctx->str, " Length ");
1906 write_uint_prop_value(ctx, length);
1907 g_string_append_c(ctx->str, ':');
1908 }
1909
1910 incr_indent(ctx);
1911
1912 for (i = 0; i < length; i++) {
1913 const bt_field *elem_field =
1914 bt_field_array_borrow_element_field_by_index_const(
1915 field, i);
1916
1917 write_nl(ctx);
48be9a91 1918 write_array_index(ctx, i, color_fg_cyan(ctx));
55478183
PP
1919 write_field(ctx, elem_field, NULL);
1920 }
1921
1922 decr_indent(ctx);
1923 break;
1924 }
25510d56
PP
1925 case BT_FIELD_CLASS_TYPE_OPTION:
1926 {
1927 const bt_field *content_field =
1928 bt_field_option_borrow_field_const(field);
1929
1930 if (!content_field) {
1931 write_sp(ctx);
1932 write_none_prop_value(ctx, "None");
1933 } else {
1934 write_field(ctx, content_field, NULL);
1935 }
1936
1937 break;
1938 }
45c51519
PP
1939 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
1940 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
1941 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
55478183
PP
1942 write_field(ctx,
1943 bt_field_variant_borrow_selected_option_field_const(
1944 field), NULL);
1945 break;
1946 default:
1947 abort();
1948 }
1949}
1950
1951static
1952void write_root_field(struct details_write_ctx *ctx, const char *name,
1953 const bt_field *field)
1954{
1955 BT_ASSERT(name);
1956 BT_ASSERT(field);
1957 write_indent(ctx);
1958 write_prop_name(ctx, name);
1959 g_string_append(ctx->str, ":");
1960 write_field(ctx, field, NULL);
1961 write_nl(ctx);
1962}
1963
1964static
1965int write_event_message(struct details_write_ctx *ctx,
1966 const bt_message *msg)
1967{
1968 int ret = 0;
1969 const bt_event *event = bt_message_event_borrow_event_const(msg);
1970 const bt_stream *stream = bt_event_borrow_stream_const(event);
1971 const bt_event_class *ec = bt_event_borrow_class_const(event);
1972 const bt_stream_class *sc = bt_event_class_borrow_stream_class_const(ec);
1973 const bt_trace_class *tc = bt_stream_class_borrow_trace_class_const(sc);
1974 const char *ec_name;
1975 const bt_field *field;
1976
1977 ret = try_write_meta(ctx, tc, sc, ec);
1978 if (ret) {
1979 goto end;
1980 }
1981
cc413248
PP
1982 if (!ctx->details_comp->cfg.with_data) {
1983 goto end;
1984 }
1985
1986 if (ctx->str->len > 0) {
1987 /*
1988 * Output buffer contains metadata: separate blocks with
1989 * newline.
1990 */
1991 write_nl(ctx);
1992 }
1993
55478183
PP
1994 /* Write time */
1995 if (bt_stream_class_borrow_default_clock_class_const(sc)) {
1996 write_time(ctx,
1997 bt_message_event_borrow_default_clock_snapshot_const(
1998 msg));
1999 }
2000
2001 /* Write follow tag for message */
2002 ret = write_message_follow_tag(ctx, stream);
2003 if (ret) {
2004 goto end;
2005 }
2006
2007 /* Write object's basic properties */
2008 write_obj_type_name(ctx, "Event");
2009 ec_name = bt_event_class_get_name(ec);
2010 if (ec_name) {
2011 g_string_append_printf(ctx->str, " `%s%s%s`",
2012 color_fg_green(ctx), ec_name, color_reset(ctx));
2013 }
2014
2015 g_string_append(ctx->str, " (");
2016
2017 if (!ctx->details_comp->cfg.compact) {
2018 g_string_append(ctx->str, "Class ID ");
2019 }
2020
2021 write_uint_prop_value(ctx, bt_event_class_get_id(ec));
2022 g_string_append(ctx->str, ")");
2023
2024 if (ctx->details_comp->cfg.compact) {
2025 write_nl(ctx);
2026 goto end;
2027 }
2028
2029 /* Write fields */
2030 g_string_append(ctx->str, ":\n");
2031 incr_indent(ctx);
2032 field = bt_event_borrow_common_context_field_const(event);
2033 if (field) {
2034 write_root_field(ctx, "Common context", field);
2035 }
2036
2037 field = bt_event_borrow_specific_context_field_const(event);
2038 if (field) {
2039 write_root_field(ctx, "Specific context", field);
2040 }
2041
2042 field = bt_event_borrow_payload_field_const(event);
2043 if (field) {
2044 write_root_field(ctx, "Payload", field);
2045 }
2046
2047 decr_indent(ctx);
2048
2049end:
55478183
PP
2050 return ret;
2051}
2052
2053static
2054gint compare_streams(const bt_stream **a, const bt_stream **b)
2055{
2056 uint64_t id_a = bt_stream_get_id(*a);
2057 uint64_t id_b = bt_stream_get_id(*b);
2058
2059 if (id_a < id_b) {
2060 return -1;
2061 } else if (id_a > id_b) {
2062 return 1;
2063 } else {
2064 const bt_stream_class *a_sc = bt_stream_borrow_class_const(*a);
2065 const bt_stream_class *b_sc = bt_stream_borrow_class_const(*b);
2066 uint64_t a_sc_id = bt_stream_class_get_id(a_sc);
2067 uint64_t b_sc_id = bt_stream_class_get_id(b_sc);
2068
2069 if (a_sc_id < b_sc_id) {
2070 return -1;
2071 } else if (a_sc_id > b_sc_id) {
2072 return 1;
2073 } else {
2074 return 0;
2075 }
2076 }
2077}
2078
2079static
2080void write_trace(struct details_write_ctx *ctx, const bt_trace *trace)
2081{
2082 const char *name;
55478183
PP
2083 GPtrArray *streams = g_ptr_array_new();
2084 uint64_t i;
2085 bool printed_prop = false;
335a2da5
PP
2086 GPtrArray *env_names = g_ptr_array_new();
2087 uint64_t env_count;
55478183
PP
2088
2089 write_indent(ctx);
2090 write_obj_type_name(ctx, "Trace");
2091
2092 /* Write name */
2093 if (ctx->details_comp->cfg.with_trace_name) {
2094 name = bt_trace_get_name(trace);
2095 if (name) {
2096 g_string_append(ctx->str, " `");
2097 write_str_prop_value(ctx, name);
2098 g_string_append(ctx->str, "`");
2099 }
2100 }
2101
2102 /* Write properties */
2103 incr_indent(ctx);
2104
335a2da5
PP
2105 /* Write UUID */
2106 if (ctx->details_comp->cfg.with_uuid) {
2107 bt_uuid uuid = bt_trace_get_uuid(trace);
2108
2109 if (uuid) {
55478183
PP
2110 if (!printed_prop) {
2111 g_string_append(ctx->str, ":\n");
2112 printed_prop = true;
2113 }
2114
335a2da5 2115 write_uuid_prop_line(ctx, "UUID", uuid);
55478183
PP
2116 }
2117 }
2118
335a2da5
PP
2119 /* Write environment */
2120 env_count = bt_trace_get_environment_entry_count(trace);
2121 if (env_count > 0) {
2122 if (!printed_prop) {
2123 g_string_append(ctx->str, ":\n");
2124 printed_prop = true;
2125 }
55478183 2126
335a2da5
PP
2127 write_indent(ctx);
2128 write_prop_name(ctx, "Environment");
2129 g_string_append(ctx->str, " (");
2130 write_uint_prop_value(ctx, env_count);
2131 g_string_append_printf(ctx->str, " entr%s):",
2132 env_count == 1 ? "y" : "ies");
2133 write_nl(ctx);
2134 incr_indent(ctx);
2135
2136 for (i = 0; i < env_count; i++) {
2137 const char *name;
2138 const bt_value *value;
2139
2140 bt_trace_borrow_environment_entry_by_index_const(
2141 trace, i, &name, &value);
2142 g_ptr_array_add(env_names, (gpointer) name);
2143 }
2144
2145 g_ptr_array_sort(env_names, (GCompareFunc) compare_strings);
2146
2147 for (i = 0; i < env_names->len; i++) {
2148 const char *name = env_names->pdata[i];
2149 const bt_value *value =
2150 bt_trace_borrow_environment_entry_value_by_name_const(
2151 trace, name);
2152
2153 BT_ASSERT(value);
2154 write_compound_member_name(ctx, name);
2155 write_sp(ctx);
2156
2157 if (bt_value_get_type(value) ==
2158 BT_VALUE_TYPE_SIGNED_INTEGER) {
2159 write_int_prop_value(ctx,
9c08c816 2160 bt_value_integer_signed_get(value));
335a2da5
PP
2161 } else if (bt_value_get_type(value) ==
2162 BT_VALUE_TYPE_STRING) {
2163 write_str_prop_value(ctx,
2164 bt_value_string_get(value));
2165 } else {
2166 abort();
55478183
PP
2167 }
2168
335a2da5 2169 write_nl(ctx);
55478183 2170 }
335a2da5
PP
2171
2172 decr_indent(ctx);
55478183
PP
2173 }
2174
2175 for (i = 0; i < bt_trace_get_stream_count(trace); i++) {
2176 g_ptr_array_add(streams,
2177 (gpointer) bt_trace_borrow_stream_by_index_const(
2178 trace, i));
2179 }
2180
2181 g_ptr_array_sort(streams, (GCompareFunc) compare_streams);
2182
2183 if (streams->len > 0 && !printed_prop) {
2184 g_string_append(ctx->str, ":\n");
2185 printed_prop = true;
2186 }
2187
2188 for (i = 0; i < streams->len; i++) {
2189 const bt_stream *stream = streams->pdata[i];
2190
2191 write_indent(ctx);
2192 write_obj_type_name(ctx, "Stream");
2193 g_string_append(ctx->str, " (ID ");
2194 write_uint_prop_value(ctx, bt_stream_get_id(stream));
2195 g_string_append(ctx->str, ", Class ID ");
2196 write_uint_prop_value(ctx, bt_stream_class_get_id(
2197 bt_stream_borrow_class_const(stream)));
2198 g_string_append(ctx->str, ")");
2199 write_nl(ctx);
2200 }
2201
2202 decr_indent(ctx);
2203
2204 if (!printed_prop) {
2205 write_nl(ctx);
2206 }
2207
2208 g_ptr_array_free(streams, TRUE);
335a2da5 2209 g_ptr_array_free(env_names, TRUE);
55478183
PP
2210}
2211
2212static
2213int write_stream_beginning_message(struct details_write_ctx *ctx,
2214 const bt_message *msg)
2215{
2216 int ret = 0;
2217 const bt_stream *stream =
2218 bt_message_stream_beginning_borrow_stream_const(msg);
2219 const bt_trace *trace = bt_stream_borrow_trace_const(stream);
2220 const bt_stream_class *sc = bt_stream_borrow_class_const(stream);
188edac1 2221 const bt_clock_class *cc = bt_stream_class_borrow_default_clock_class_const(sc);
55478183
PP
2222 const bt_trace_class *tc = bt_stream_class_borrow_trace_class_const(sc);
2223 const char *name;
2224
2225 ret = try_write_meta(ctx, tc, sc, NULL);
2226 if (ret) {
2227 goto end;
2228 }
2229
cc413248
PP
2230 if (!ctx->details_comp->cfg.with_data) {
2231 goto end;
2232 }
2233
2234 if (ctx->str->len > 0) {
2235 /*
2236 * Output buffer contains metadata: separate blocks with
2237 * newline.
2238 */
2239 write_nl(ctx);
2240 }
2241
188edac1
SM
2242 /* Write time */
2243 if (cc) {
2244 const bt_clock_snapshot *cs;
2245 bt_message_stream_clock_snapshot_state cs_state =
2246 bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg, &cs);
2247
2248 if (cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
2249 write_time(ctx, cs);
2250 } else {
2251 write_time_str(ctx, "Unknown");
2252 }
2253 }
2254
55478183
PP
2255 /* Write follow tag for message */
2256 ret = write_message_follow_tag(ctx, stream);
2257 if (ret) {
2258 goto end;
2259 }
2260
2261 /* Write stream properties */
2262 write_obj_type_name(ctx, "Stream beginning");
2263
2264 if (ctx->details_comp->cfg.compact) {
2265 write_nl(ctx);
2266 goto end;
2267 }
2268
2269 g_string_append(ctx->str, ":\n");
2270 incr_indent(ctx);
2271
2272 if (ctx->details_comp->cfg.with_stream_name) {
2273 name = bt_stream_get_name(stream);
2274 if (name) {
2275 write_str_prop_line(ctx, "Name", name);
2276 }
2277 }
2278
2279 if (ctx->details_comp->cfg.with_stream_class_name) {
2280 name = bt_stream_class_get_name(sc);
2281 if (name) {
2282 write_str_prop_line(ctx, "Class name", name);
2283 }
2284 }
2285
2286 write_trace(ctx, trace);
2287 decr_indent(ctx);
2288
2289end:
2290 return ret;
2291}
2292
2293static
2294int write_stream_end_message(struct details_write_ctx *ctx,
2295 const bt_message *msg)
2296{
2297 int ret = 0;
2298 const bt_stream *stream =
2299 bt_message_stream_end_borrow_stream_const(msg);
188edac1
SM
2300 const bt_stream_class *sc =
2301 bt_stream_borrow_class_const(stream);
2302 const bt_clock_class *cc =
2303 bt_stream_class_borrow_default_clock_class_const(sc);
55478183 2304
cc413248
PP
2305 if (!ctx->details_comp->cfg.with_data) {
2306 goto end;
2307 }
2308
55478183 2309 /* Write time */
188edac1
SM
2310 if (cc) {
2311 const bt_clock_snapshot *cs;
2312 bt_message_stream_clock_snapshot_state cs_state =
2313 bt_message_stream_end_borrow_default_clock_snapshot_const(msg, &cs);
55478183 2314
188edac1
SM
2315 if (cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
2316 write_time(ctx, cs);
2317 } else {
2318 write_time_str(ctx, "Unknown");
2319 }
55478183
PP
2320 }
2321
2322 /* Write follow tag for message */
2323 ret = write_message_follow_tag(ctx, stream);
2324 if (ret) {
2325 goto end;
2326 }
2327
188edac1
SM
2328 /* Write stream properties */
2329 write_obj_type_name(ctx, "Stream end\n");
55478183
PP
2330
2331end:
2332 return ret;
2333}
2334
2335static
2336int write_packet_beginning_message(struct details_write_ctx *ctx,
2337 const bt_message *msg)
2338{
2339 int ret = 0;
2340 const bt_packet *packet =
2341 bt_message_packet_beginning_borrow_packet_const(msg);
2342 const bt_stream *stream = bt_packet_borrow_stream_const(packet);
2343 const bt_stream_class *sc = bt_stream_borrow_class_const(stream);
2344 const bt_field *field;
2345
cc413248
PP
2346 if (!ctx->details_comp->cfg.with_data) {
2347 goto end;
2348 }
2349
55478183
PP
2350 /* Write time */
2351 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(sc)) {
2352 write_time(ctx,
2353 bt_message_packet_beginning_borrow_default_clock_snapshot_const(
2354 msg));
2355 }
2356
2357 /* Write follow tag for message */
2358 ret = write_message_follow_tag(ctx, stream);
2359 if (ret) {
2360 goto end;
2361 }
2362
2363 write_obj_type_name(ctx, "Packet beginning");
2364
2365 if (ctx->details_comp->cfg.compact) {
2366 write_nl(ctx);
2367 goto end;
2368 }
2369
2370 /* Write field */
55478183
PP
2371 field = bt_packet_borrow_context_field_const(packet);
2372 if (field) {
340be5dd
PP
2373 g_string_append(ctx->str, ":\n");
2374 incr_indent(ctx);
55478183 2375 write_root_field(ctx, "Context", field);
340be5dd
PP
2376 decr_indent(ctx);
2377 } else {
2378 write_nl(ctx);
55478183
PP
2379 }
2380
55478183
PP
2381end:
2382 return ret;
2383}
2384
2385static
2386int write_discarded_items_message(struct details_write_ctx *ctx,
2387 const char *name, const bt_stream *stream,
2388 const bt_clock_snapshot *beginning_cs,
2389 const bt_clock_snapshot *end_cs, uint64_t count)
2390{
2391 int ret = 0;
2392
2393 /* Write times */
2394 if (beginning_cs) {
2395 write_time(ctx, beginning_cs);
2396 BT_ASSERT(end_cs);
2397 write_time(ctx, end_cs);
2398 }
2399
2400 /* Write follow tag for message */
2401 ret = write_message_follow_tag(ctx, stream);
2402 if (ret) {
2403 goto end;
2404 }
2405
2406 write_obj_type_name(ctx, "Discarded ");
2407 write_obj_type_name(ctx, name);
2408
2409 /* Write count */
2410 if (count == UINT64_C(-1)) {
2411 write_nl(ctx);
2412 goto end;
2413 }
2414
2415 g_string_append(ctx->str, " (");
2416 write_uint_prop_value(ctx, count);
2417 g_string_append_printf(ctx->str, " %s)\n", name);
2418
2419end:
2420 return ret;
2421}
2422
2423static
2424int write_discarded_events_message(struct details_write_ctx *ctx,
2425 const bt_message *msg)
2426{
cc413248 2427 int ret = 0;
55478183
PP
2428 const bt_stream *stream = bt_message_discarded_events_borrow_stream_const(
2429 msg);
2430 const bt_stream_class *sc = bt_stream_borrow_class_const(stream);
2431 const bt_clock_snapshot *beginning_cs = NULL;
2432 const bt_clock_snapshot *end_cs = NULL;
2433 uint64_t count;
2434
cc413248
PP
2435 if (!ctx->details_comp->cfg.with_data) {
2436 goto end;
2437 }
2438
55478183
PP
2439 if (bt_stream_class_discarded_events_have_default_clock_snapshots(sc)) {
2440 beginning_cs =
2441 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
2442 msg);
2443 end_cs =
2444 bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
2445 msg);
2446 }
2447
2448 if (bt_message_discarded_events_get_count(msg, &count) !=
2449 BT_PROPERTY_AVAILABILITY_AVAILABLE) {
2450 count = UINT64_C(-1);
2451 }
2452
cc413248 2453 ret = write_discarded_items_message(ctx, "events", stream,
55478183 2454 beginning_cs, end_cs, count);
cc413248
PP
2455
2456end:
2457 return ret;
55478183
PP
2458}
2459
2460static
2461int write_discarded_packets_message(struct details_write_ctx *ctx,
2462 const bt_message *msg)
2463{
cc413248 2464 int ret = 0;
55478183
PP
2465 const bt_stream *stream = bt_message_discarded_packets_borrow_stream_const(
2466 msg);
2467 const bt_stream_class *sc = bt_stream_borrow_class_const(stream);
2468 const bt_clock_snapshot *beginning_cs = NULL;
2469 const bt_clock_snapshot *end_cs = NULL;
2470 uint64_t count;
2471
cc413248
PP
2472 if (!ctx->details_comp->cfg.with_data) {
2473 goto end;
2474 }
2475
55478183
PP
2476 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(sc)) {
2477 beginning_cs =
2478 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
2479 msg);
2480 end_cs =
2481 bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
2482 msg);
2483 }
2484
2485 if (bt_message_discarded_packets_get_count(msg, &count) !=
2486 BT_PROPERTY_AVAILABILITY_AVAILABLE) {
2487 count = UINT64_C(-1);
2488 }
2489
cc413248 2490 ret = write_discarded_items_message(ctx, "packets", stream,
55478183 2491 beginning_cs, end_cs, count);
cc413248
PP
2492
2493end:
2494 return ret;
55478183
PP
2495}
2496
2497static
2498int write_packet_end_message(struct details_write_ctx *ctx,
2499 const bt_message *msg)
2500{
2501 int ret = 0;
2502 const bt_packet *packet =
2503 bt_message_packet_end_borrow_packet_const(msg);
2504 const bt_stream *stream = bt_packet_borrow_stream_const(packet);
2505 const bt_stream_class *sc = bt_stream_borrow_class_const(stream);
2506
cc413248
PP
2507 if (!ctx->details_comp->cfg.with_data) {
2508 goto end;
2509 }
2510
55478183
PP
2511 /* Write time */
2512 if (bt_stream_class_packets_have_end_default_clock_snapshot(sc)) {
2513 write_time(ctx,
2514 bt_message_packet_end_borrow_default_clock_snapshot_const(
2515 msg));
2516 }
2517
2518 /* Write follow tag for message */
2519 ret = write_message_follow_tag(ctx, stream);
2520 if (ret) {
2521 goto end;
2522 }
2523
2524 write_obj_type_name(ctx, "Packet end");
2525 write_nl(ctx);
2526
2527end:
2528 return ret;
2529}
2530
2531static
2532int write_message_iterator_inactivity_message(struct details_write_ctx *ctx,
2533 const bt_message *msg)
2534{
2535 int ret = 0;
2536 const bt_clock_snapshot *cs =
2537 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
2538 msg);
2539
2540 /* Write time */
2541 write_time(ctx, cs);
2542 write_obj_type_name(ctx, "Message iterator inactivity");
2543
2544 if (ctx->details_comp->cfg.compact) {
2545 write_nl(ctx);
2546 goto end;
2547 }
2548
2549 /* Write clock class properties */
2550 g_string_append(ctx->str, ":\n");
2551 incr_indent(ctx);
2552 write_indent(ctx);
2553 write_prop_name(ctx, "Clock class");
2554 g_string_append_c(ctx->str, ':');
2555 write_nl(ctx);
2556 incr_indent(ctx);
2557 write_clock_class_prop_lines(ctx,
2558 bt_clock_snapshot_borrow_clock_class_const(cs));
2559 decr_indent(ctx);
2560
2561end:
2562 return ret;
2563}
2564
2565BT_HIDDEN
2566int details_write_message(struct details_comp *details_comp,
2567 const bt_message *msg)
2568{
2569 int ret = 0;
2570 struct details_write_ctx ctx = {
2571 .details_comp = details_comp,
2572 .str = details_comp->str,
2573 .indent_level = 0,
2574 };
2575
2576 /* Reset output buffer */
2577 g_string_assign(details_comp->str, "");
2578
55478183
PP
2579 switch (bt_message_get_type(msg)) {
2580 case BT_MESSAGE_TYPE_EVENT:
2581 ret = write_event_message(&ctx, msg);
2582 break;
2583 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
2584 ret = write_message_iterator_inactivity_message(&ctx, msg);
2585 break;
2586 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
2587 ret = write_stream_beginning_message(&ctx, msg);
2588 break;
2589 case BT_MESSAGE_TYPE_STREAM_END:
2590 ret = write_stream_end_message(&ctx, msg);
2591 break;
2592 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
2593 ret = write_packet_beginning_message(&ctx, msg);
2594 break;
2595 case BT_MESSAGE_TYPE_PACKET_END:
2596 ret = write_packet_end_message(&ctx, msg);
2597 break;
55478183
PP
2598 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
2599 ret = write_discarded_events_message(&ctx, msg);
2600 break;
2601 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
2602 ret = write_discarded_packets_message(&ctx, msg);
2603 break;
2604 default:
2605 abort();
2606 }
2607
cc413248
PP
2608 /*
2609 * If this component printed at least one character so far, and
2610 * we're not in compact mode, and there's something in the
2611 * output buffer for this message, then prepend a newline to the
2612 * output buffer to visually separate message blocks.
2613 */
2614 if (details_comp->printed_something && !details_comp->cfg.compact &&
2615 details_comp->str->len > 0) {
2616 /* TODO: Optimize this */
2617 g_string_prepend_c(details_comp->str, '\n');
2618 }
2619
55478183
PP
2620 return ret;
2621}
This page took 0.151491 seconds and 4 git commands to generate.