2 * SPDX-License-Identifier: MIT
4 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 #include <babeltrace2/babeltrace.h>
9 #include "compat/bitfield.h"
10 #include "common/common.h"
11 #include "common/uuid.h"
12 #include "compat/time.h"
13 #include "common/assert.h"
20 #define NSEC_PER_SEC 1000000000LL
22 static char color_name
[32];
23 static char color_field_name
[32];
24 static char color_rst
[32];
25 static char color_string_value
[32];
26 static char color_number_value
[32];
27 static char color_enum_mapping_name
[32];
28 static char color_unknown
[32];
29 static char color_event_name
[32];
30 static char color_timestamp
[32];
33 int64_t real_timestamp
; /* Relative to UNIX epoch. */
34 uint64_t clock_snapshot
; /* In cycles. */
38 int print_field(struct pretty_component
*pretty
,
39 const bt_field
*field
, bool print_names
);
42 void print_name_equal(struct pretty_component
*pretty
, const char *name
)
44 if (pretty
->use_colors
) {
45 bt_common_g_string_append(pretty
->string
, color_name
);
46 bt_common_g_string_append(pretty
->string
, name
);
47 bt_common_g_string_append(pretty
->string
, color_rst
);
49 bt_common_g_string_append(pretty
->string
, name
);
51 bt_common_g_string_append(pretty
->string
, " = ");
55 void print_field_name_equal(struct pretty_component
*pretty
, const char *name
)
57 if (pretty
->use_colors
) {
58 bt_common_g_string_append(pretty
->string
, color_field_name
);
59 bt_common_g_string_append(pretty
->string
, name
);
60 bt_common_g_string_append(pretty
->string
, color_rst
);
62 bt_common_g_string_append(pretty
->string
, name
);
64 bt_common_g_string_append(pretty
->string
, " = ");
68 void print_timestamp_cycles(struct pretty_component
*pretty
,
69 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
73 cycles
= bt_clock_snapshot_get_value(clock_snapshot
);
74 bt_common_g_string_append_printf(pretty
->string
, "%020" PRIu64
, cycles
);
77 if (pretty
->last_cycles_timestamp
!= -1ULL) {
78 pretty
->delta_cycles
= cycles
- pretty
->last_cycles_timestamp
;
81 pretty
->last_cycles_timestamp
= cycles
;
86 void print_timestamp_wall(struct pretty_component
*pretty
,
87 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
90 int64_t ts_nsec
= 0; /* add configurable offset */
91 int64_t ts_sec
= 0; /* add configurable offset */
92 uint64_t ts_sec_abs
, ts_nsec_abs
;
95 if (!clock_snapshot
) {
96 bt_common_g_string_append(pretty
->string
, "??:??:??.?????????");
100 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, &ts_nsec
);
102 // TODO: log, this is unexpected
103 bt_common_g_string_append(pretty
->string
, "Error");
108 if (pretty
->last_real_timestamp
!= -1ULL) {
109 pretty
->delta_real_timestamp
= ts_nsec
- pretty
->last_real_timestamp
;
112 pretty
->last_real_timestamp
= ts_nsec
;
115 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
116 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
118 if (ts_sec
>= 0 && ts_nsec
>= 0) {
121 ts_nsec_abs
= ts_nsec
;
122 } else if (ts_sec
> 0 && ts_nsec
< 0) {
124 ts_sec_abs
= ts_sec
- 1;
125 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
126 } else if (ts_sec
== 0 && ts_nsec
< 0) {
129 ts_nsec_abs
= -ts_nsec
;
130 } else if (ts_sec
< 0 && ts_nsec
> 0) {
132 ts_sec_abs
= -(ts_sec
+ 1);
133 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
134 } else if (ts_sec
< 0 && ts_nsec
== 0) {
136 ts_sec_abs
= -ts_sec
;
137 ts_nsec_abs
= ts_nsec
;
138 } else { /* (ts_sec < 0 && ts_nsec < 0) */
140 ts_sec_abs
= -ts_sec
;
141 ts_nsec_abs
= -ts_nsec
;
144 if (!pretty
->options
.clock_seconds
) {
146 time_t time_s
= (time_t) ts_sec_abs
;
148 if (is_negative
&& !pretty
->negative_timestamp_warning_done
) {
150 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
151 pretty
->negative_timestamp_warning_done
= true;
155 if (!pretty
->options
.clock_gmt
) {
158 res
= bt_localtime_r(&time_s
, &tm
);
161 fprintf(stderr
, "[warning] Unable to get localtime.\n");
167 res
= bt_gmtime_r(&time_s
, &tm
);
170 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
174 if (pretty
->options
.clock_date
) {
178 /* Print date and time */
179 res
= strftime(timestr
, sizeof(timestr
),
183 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
187 bt_common_g_string_append(pretty
->string
, timestr
);
190 /* Print time in HH:MM:SS.ns */
191 bt_common_g_string_append_printf(pretty
->string
,
192 "%02d:%02d:%02d.%09" PRIu64
, tm
.tm_hour
, tm
.tm_min
,
193 tm
.tm_sec
, ts_nsec_abs
);
197 bt_common_g_string_append_printf(pretty
->string
, "%s%" PRId64
".%09" PRIu64
,
198 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
204 int print_event_timestamp(struct pretty_component
*pretty
,
205 const bt_message
*event_msg
, bool *start_line
)
207 bool print_names
= pretty
->options
.print_header_field_names
;
209 const bt_clock_snapshot
*clock_snapshot
= NULL
;
211 if (!bt_message_event_borrow_stream_class_default_clock_class_const(
213 /* No default clock class: skip the timestamp without an error */
217 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(event_msg
);
220 print_name_equal(pretty
, "timestamp");
222 bt_common_g_string_append(pretty
->string
, "[");
224 if (pretty
->use_colors
) {
225 bt_common_g_string_append(pretty
->string
, color_timestamp
);
227 if (pretty
->options
.print_timestamp_cycles
) {
228 print_timestamp_cycles(pretty
, clock_snapshot
, true);
230 print_timestamp_wall(pretty
, clock_snapshot
, true);
232 if (pretty
->use_colors
) {
233 bt_common_g_string_append(pretty
->string
, color_rst
);
237 bt_common_g_string_append(pretty
->string
, "] ");
239 if (pretty
->options
.print_delta_field
) {
241 bt_common_g_string_append(pretty
->string
, ", ");
242 print_name_equal(pretty
, "delta");
244 bt_common_g_string_append(pretty
->string
, "(");
246 if (pretty
->options
.print_timestamp_cycles
) {
247 if (pretty
->delta_cycles
== -1ULL) {
248 bt_common_g_string_append(pretty
->string
,
249 "+??????????\?\?"); /* Not a trigraph. */
251 bt_common_g_string_append_printf(pretty
->string
,
252 "+%012" PRIu64
, pretty
->delta_cycles
);
255 if (pretty
->delta_real_timestamp
!= -1ULL) {
256 uint64_t delta_sec
, delta_nsec
, delta
;
258 delta
= pretty
->delta_real_timestamp
;
259 delta_sec
= delta
/ NSEC_PER_SEC
;
260 delta_nsec
= delta
% NSEC_PER_SEC
;
261 bt_common_g_string_append_printf(pretty
->string
,
262 "+%" PRIu64
".%09" PRIu64
,
263 delta_sec
, delta_nsec
);
265 bt_common_g_string_append(pretty
->string
, "+?.?????????");
269 bt_common_g_string_append(pretty
->string
, ") ");
272 *start_line
= !print_names
;
279 int print_event_header(struct pretty_component
*pretty
,
280 const bt_message
*event_msg
)
282 bool print_names
= pretty
->options
.print_header_field_names
;
284 const bt_event_class
*event_class
= NULL
;
285 const bt_stream
*stream
= NULL
;
286 const bt_trace
*trace
= NULL
;
287 const bt_event
*event
= bt_message_event_borrow_event_const(event_msg
);
290 bt_property_availability prop_avail
;
292 event_class
= bt_event_borrow_class_const(event
);
293 stream
= bt_event_borrow_stream_const(event
);
294 trace
= bt_stream_borrow_trace_const(stream
);
295 ret
= print_event_timestamp(pretty
, event_msg
, &pretty
->start_line
);
299 if (pretty
->options
.print_trace_field
) {
302 name
= bt_trace_get_name(trace
);
304 if (!pretty
->start_line
) {
305 bt_common_g_string_append(pretty
->string
, ", ");
308 print_name_equal(pretty
, "trace");
311 bt_common_g_string_append(pretty
->string
, name
);
314 bt_common_g_string_append(pretty
->string
, ", ");
318 if (pretty
->options
.print_trace_hostname_field
) {
319 const bt_value
*hostname_str
;
321 hostname_str
= bt_trace_borrow_environment_entry_value_by_name_const(
326 if (!pretty
->start_line
) {
327 bt_common_g_string_append(pretty
->string
, ", ");
330 print_name_equal(pretty
, "trace:hostname");
332 str
= bt_value_string_get(hostname_str
);
333 bt_common_g_string_append(pretty
->string
, str
);
337 if (pretty
->options
.print_trace_domain_field
) {
338 const bt_value
*domain_str
;
340 domain_str
= bt_trace_borrow_environment_entry_value_by_name_const(
345 if (!pretty
->start_line
) {
346 bt_common_g_string_append(pretty
->string
, ", ");
349 print_name_equal(pretty
, "trace:domain");
350 } else if (dom_print
) {
351 bt_common_g_string_append(pretty
->string
, ":");
353 str
= bt_value_string_get(domain_str
);
354 bt_common_g_string_append(pretty
->string
, str
);
358 if (pretty
->options
.print_trace_procname_field
) {
359 const bt_value
*procname_str
;
361 procname_str
= bt_trace_borrow_environment_entry_value_by_name_const(
366 if (!pretty
->start_line
) {
367 bt_common_g_string_append(pretty
->string
, ", ");
370 print_name_equal(pretty
, "trace:procname");
371 } else if (dom_print
) {
372 bt_common_g_string_append(pretty
->string
, ":");
374 str
= bt_value_string_get(procname_str
);
375 bt_common_g_string_append(pretty
->string
, str
);
379 if (pretty
->options
.print_trace_vpid_field
) {
380 const bt_value
*vpid_value
;
382 vpid_value
= bt_trace_borrow_environment_entry_value_by_name_const(
387 if (!pretty
->start_line
) {
388 bt_common_g_string_append(pretty
->string
, ", ");
391 print_name_equal(pretty
, "trace:vpid");
392 } else if (dom_print
) {
393 bt_common_g_string_append(pretty
->string
, ":");
395 value
= bt_value_integer_signed_get(vpid_value
);
396 bt_common_g_string_append_printf(pretty
->string
,
397 "(%" PRId64
")", value
);
401 if (pretty
->options
.print_loglevel_field
) {
402 static const char *log_level_names
[] = {
403 [ BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
] = "TRACE_EMERG",
404 [ BT_EVENT_CLASS_LOG_LEVEL_ALERT
] = "TRACE_ALERT",
405 [ BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
] = "TRACE_CRIT",
406 [ BT_EVENT_CLASS_LOG_LEVEL_ERROR
] = "TRACE_ERR",
407 [ BT_EVENT_CLASS_LOG_LEVEL_WARNING
] = "TRACE_WARNING",
408 [ BT_EVENT_CLASS_LOG_LEVEL_NOTICE
] = "TRACE_NOTICE",
409 [ BT_EVENT_CLASS_LOG_LEVEL_INFO
] = "TRACE_INFO",
410 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
411 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
412 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
413 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
414 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
415 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
416 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
417 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG
] = "TRACE_DEBUG",
419 bt_event_class_log_level log_level
;
420 const char *log_level_str
= NULL
;
422 prop_avail
= bt_event_class_get_log_level(event_class
,
424 if (prop_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
425 log_level_str
= log_level_names
[log_level
];
426 BT_ASSERT_DBG(log_level_str
);
428 if (!pretty
->start_line
) {
429 bt_common_g_string_append(pretty
->string
, ", ");
432 print_name_equal(pretty
, "loglevel");
433 } else if (dom_print
) {
434 bt_common_g_string_append(pretty
->string
, ":");
437 bt_common_g_string_append(pretty
->string
, log_level_str
);
438 bt_common_g_string_append_printf(
439 pretty
->string
, " (%d)", (int) log_level
);
443 if (pretty
->options
.print_emf_field
) {
446 uri_str
= bt_event_class_get_emf_uri(event_class
);
448 if (!pretty
->start_line
) {
449 bt_common_g_string_append(pretty
->string
, ", ");
452 print_name_equal(pretty
, "model.emf.uri");
453 } else if (dom_print
) {
454 bt_common_g_string_append(pretty
->string
, ":");
457 bt_common_g_string_append(pretty
->string
, uri_str
);
461 if (dom_print
&& !print_names
) {
462 bt_common_g_string_append(pretty
->string
, " ");
464 if (!pretty
->start_line
) {
465 bt_common_g_string_append(pretty
->string
, ", ");
467 pretty
->start_line
= true;
469 print_name_equal(pretty
, "name");
471 ev_name
= bt_event_class_get_name(event_class
);
472 if (pretty
->use_colors
) {
474 bt_common_g_string_append(pretty
->string
,
477 bt_common_g_string_append(pretty
->string
,
482 bt_common_g_string_append(pretty
->string
, ev_name
);
484 bt_common_g_string_append(pretty
->string
, "<unknown>");
486 if (pretty
->use_colors
) {
487 bt_common_g_string_append(pretty
->string
, color_rst
);
490 bt_common_g_string_append(pretty
->string
, ": ");
492 bt_common_g_string_append(pretty
->string
, ", ");
500 int print_integer(struct pretty_component
*pretty
,
501 const bt_field
*field
)
504 bt_field_class_integer_preferred_display_base base
;
505 const bt_field_class
*int_fc
;
510 bool rst_color
= false;
511 bt_field_class_type ft_type
;
513 int_fc
= bt_field_borrow_class_const(field
);
514 BT_ASSERT_DBG(int_fc
);
515 ft_type
= bt_field_get_class_type(field
);
516 if (bt_field_class_type_is(ft_type
,
517 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
)) {
518 v
.u
= bt_field_integer_unsigned_get_value(field
);
520 v
.s
= bt_field_integer_signed_get_value(field
);
523 if (pretty
->use_colors
) {
524 bt_common_g_string_append(pretty
->string
, color_number_value
);
528 base
= bt_field_class_integer_get_preferred_display_base(int_fc
);
530 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
534 len
= bt_field_class_integer_get_field_value_range(int_fc
);
535 bt_common_g_string_append(pretty
->string
, "0b");
536 _bt_safe_lshift(v
.u
, 64 - len
);
537 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
538 bt_common_g_string_append_c(pretty
->string
,
539 (v
.u
& (1ULL << 63)) ? '1' : '0');
540 _bt_safe_lshift(v
.u
, 1);
544 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
546 if (bt_field_class_type_is(ft_type
,
547 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
)) {
550 len
= bt_field_class_integer_get_field_value_range(
555 BT_ASSERT_DBG(len
!= 0);
556 /* Round length to the nearest 3-bit */
557 rounded_len
= (((len
- 1) / 3) + 1) * 3;
558 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
562 bt_common_g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
565 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
566 if (bt_field_class_type_is(ft_type
,
567 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
)) {
568 bt_common_g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
570 bt_common_g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
573 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
577 len
= bt_field_class_integer_get_field_value_range(int_fc
);
579 /* Round length to the nearest nibble */
580 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
582 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
585 bt_common_g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
594 bt_common_g_string_append(pretty
->string
, color_rst
);
600 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
604 bt_common_g_string_append_c(pretty
->string
, '"');
606 for (i
= 0; i
< strlen(str
); i
++) {
607 /* Escape sequences not recognized by iscntrl(). */
610 bt_common_g_string_append(pretty
->string
, "\\\\");
613 bt_common_g_string_append(pretty
->string
, "\\\'");
616 bt_common_g_string_append(pretty
->string
, "\\\"");
619 bt_common_g_string_append(pretty
->string
, "\\\?");
623 /* Standard characters. */
624 if (!iscntrl((unsigned char) str
[i
])) {
625 bt_common_g_string_append_c(pretty
->string
, str
[i
]);
631 bt_common_g_string_append(pretty
->string
, "\\0");
634 bt_common_g_string_append(pretty
->string
, "\\a");
637 bt_common_g_string_append(pretty
->string
, "\\b");
640 bt_common_g_string_append(pretty
->string
, "\\e");
643 bt_common_g_string_append(pretty
->string
, "\\f");
646 bt_common_g_string_append(pretty
->string
, "\\n");
649 bt_common_g_string_append(pretty
->string
, "\\r");
652 bt_common_g_string_append(pretty
->string
, "\\t");
655 bt_common_g_string_append(pretty
->string
, "\\v");
658 /* Unhandled control-sequence, print as hex. */
659 bt_common_g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
664 bt_common_g_string_append_c(pretty
->string
, '"');
668 int print_enum(struct pretty_component
*pretty
,
669 const bt_field
*field
)
672 bt_field_class_enumeration_mapping_label_array label_array
;
673 uint64_t label_count
;
676 switch (bt_field_get_class_type(field
)) {
677 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
678 ret
= bt_field_enumeration_unsigned_get_mapping_labels(field
,
679 &label_array
, &label_count
);
681 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
682 ret
= bt_field_enumeration_signed_get_mapping_labels(field
,
683 &label_array
, &label_count
);
694 bt_common_g_string_append(pretty
->string
, "( ");
695 if (label_count
== 0) {
696 if (pretty
->use_colors
) {
697 bt_common_g_string_append(pretty
->string
, color_unknown
);
699 bt_common_g_string_append(pretty
->string
, "<unknown>");
700 if (pretty
->use_colors
) {
701 bt_common_g_string_append(pretty
->string
, color_rst
);
705 for (i
= 0; i
< label_count
; i
++) {
706 const char *mapping_name
= label_array
[i
];
709 bt_common_g_string_append(pretty
->string
, ", ");
711 if (pretty
->use_colors
) {
712 bt_common_g_string_append(pretty
->string
, color_enum_mapping_name
);
714 print_escape_string(pretty
, mapping_name
);
715 if (pretty
->use_colors
) {
716 bt_common_g_string_append(pretty
->string
, color_rst
);
720 bt_common_g_string_append(pretty
->string
, " : container = ");
721 ret
= print_integer(pretty
, field
);
725 bt_common_g_string_append(pretty
->string
, " )");
731 int print_struct_field(struct pretty_component
*pretty
,
732 const bt_field
*_struct
,
733 const bt_field_class
*struct_class
,
734 uint64_t i
, bool print_names
, uint64_t *nr_printed_fields
)
737 const char *field_name
;
738 const bt_field
*field
= NULL
;
739 const bt_field_class_structure_member
*member
;
741 field
= bt_field_structure_borrow_member_field_by_index_const(_struct
, i
);
747 member
= bt_field_class_structure_borrow_member_by_index_const(
749 field_name
= bt_field_class_structure_member_get_name(member
);
751 if (*nr_printed_fields
> 0) {
752 bt_common_g_string_append(pretty
->string
, ", ");
754 bt_common_g_string_append(pretty
->string
, " ");
757 print_field_name_equal(pretty
, field_name
);
759 ret
= print_field(pretty
, field
, print_names
);
760 *nr_printed_fields
+= 1;
767 int print_struct(struct pretty_component
*pretty
,
768 const bt_field
*_struct
, bool print_names
)
771 const bt_field_class
*struct_class
= NULL
;
772 uint64_t nr_fields
, i
, nr_printed_fields
;
774 struct_class
= bt_field_borrow_class_const(_struct
);
775 nr_fields
= bt_field_class_structure_get_member_count(struct_class
);
777 bt_common_g_string_append(pretty
->string
, "{");
779 nr_printed_fields
= 0;
780 for (i
= 0; i
< nr_fields
; i
++) {
781 ret
= print_struct_field(pretty
, _struct
, struct_class
, i
,
782 print_names
, &nr_printed_fields
);
788 bt_common_g_string_append(pretty
->string
, " }");
795 int print_array_field(struct pretty_component
*pretty
,
796 const bt_field
*array
, uint64_t i
, bool print_names
)
798 const bt_field
*field
= NULL
;
801 bt_common_g_string_append(pretty
->string
, ", ");
803 bt_common_g_string_append(pretty
->string
, " ");
806 bt_common_g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
809 field
= bt_field_array_borrow_element_field_by_index_const(array
, i
);
810 BT_ASSERT_DBG(field
);
811 return print_field(pretty
, field
, print_names
);
815 int print_array(struct pretty_component
*pretty
,
816 const bt_field
*array
, bool print_names
)
822 len
= bt_field_array_get_length(array
);
823 bt_common_g_string_append(pretty
->string
, "[");
825 for (i
= 0; i
< len
; i
++) {
826 ret
= print_array_field(pretty
, array
, i
, print_names
);
832 bt_common_g_string_append(pretty
->string
, " ]");
839 int print_sequence_field(struct pretty_component
*pretty
,
840 const bt_field
*seq
, uint64_t i
, bool print_names
)
842 const bt_field
*field
= NULL
;
845 bt_common_g_string_append(pretty
->string
, ", ");
847 bt_common_g_string_append(pretty
->string
, " ");
850 bt_common_g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
853 field
= bt_field_array_borrow_element_field_by_index_const(seq
, i
);
854 BT_ASSERT_DBG(field
);
855 return print_field(pretty
, field
, print_names
);
859 int print_sequence(struct pretty_component
*pretty
,
860 const bt_field
*seq
, bool print_names
)
866 len
= bt_field_array_get_length(seq
);
867 bt_common_g_string_append(pretty
->string
, "[");
870 for (i
= 0; i
< len
; i
++) {
871 ret
= print_sequence_field(pretty
, seq
, i
, print_names
);
877 bt_common_g_string_append(pretty
->string
, " ]");
884 int print_option(struct pretty_component
*pretty
,
885 const bt_field
*option
, bool print_names
)
888 const bt_field
*field
= NULL
;
890 field
= bt_field_option_borrow_field_const(option
);
892 bt_common_g_string_append(pretty
->string
, "{ ");
895 // TODO: find tag's name using field path
896 // print_field_name_equal(pretty, tag_choice);
898 ret
= print_field(pretty
, field
, print_names
);
903 bt_common_g_string_append(pretty
->string
, " }");
905 bt_common_g_string_append(pretty
->string
, "<none>");
913 int print_variant(struct pretty_component
*pretty
,
914 const bt_field
*variant
, bool print_names
)
917 const bt_field
*field
= NULL
;
919 field
= bt_field_variant_borrow_selected_option_field_const(variant
);
920 BT_ASSERT_DBG(field
);
921 bt_common_g_string_append(pretty
->string
, "{ ");
924 // TODO: find tag's name using field path
925 // print_field_name_equal(pretty, tag_choice);
927 ret
= print_field(pretty
, field
, print_names
);
932 bt_common_g_string_append(pretty
->string
, " }");
939 int print_field(struct pretty_component
*pretty
,
940 const bt_field
*field
, bool print_names
)
942 bt_field_class_type class_id
;
944 class_id
= bt_field_get_class_type(field
);
945 if (class_id
== BT_FIELD_CLASS_TYPE_BOOL
) {
949 v
= bt_field_bool_get_value(field
);
950 if (pretty
->use_colors
) {
951 bt_common_g_string_append(pretty
->string
, color_number_value
);
958 bt_common_g_string_append(pretty
->string
, text
);
959 if (pretty
->use_colors
) {
960 bt_common_g_string_append(pretty
->string
, color_rst
);
963 } else if (class_id
== BT_FIELD_CLASS_TYPE_BIT_ARRAY
) {
964 uint64_t v
= bt_field_bit_array_get_value_as_integer(field
);
966 if (pretty
->use_colors
) {
967 bt_common_g_string_append(pretty
->string
,
970 bt_common_g_string_append_printf(pretty
->string
, "0x%" PRIX64
,
972 if (pretty
->use_colors
) {
973 bt_common_g_string_append(pretty
->string
, color_rst
);
976 } else if (bt_field_class_type_is(class_id
,
977 BT_FIELD_CLASS_TYPE_ENUMERATION
)) {
978 return print_enum(pretty
, field
);
979 } else if (bt_field_class_type_is(class_id
,
980 BT_FIELD_CLASS_TYPE_INTEGER
)) {
981 return print_integer(pretty
, field
);
982 } else if (bt_field_class_type_is(class_id
,
983 BT_FIELD_CLASS_TYPE_REAL
)) {
986 if (class_id
== BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
) {
987 v
= bt_field_real_single_precision_get_value(field
);
989 v
= bt_field_real_double_precision_get_value(field
);
992 if (pretty
->use_colors
) {
993 bt_common_g_string_append(pretty
->string
, color_number_value
);
995 bt_common_g_string_append_printf(pretty
->string
, "%g", v
);
996 if (pretty
->use_colors
) {
997 bt_common_g_string_append(pretty
->string
, color_rst
);
1000 } else if (class_id
== BT_FIELD_CLASS_TYPE_STRING
) {
1003 str
= bt_field_string_get_value(field
);
1008 if (pretty
->use_colors
) {
1009 bt_common_g_string_append(pretty
->string
, color_string_value
);
1011 print_escape_string(pretty
, str
);
1012 if (pretty
->use_colors
) {
1013 bt_common_g_string_append(pretty
->string
, color_rst
);
1016 } else if (class_id
== BT_FIELD_CLASS_TYPE_STRUCTURE
) {
1017 return print_struct(pretty
, field
, print_names
);
1018 } else if (bt_field_class_type_is(class_id
,
1019 BT_FIELD_CLASS_TYPE_OPTION
)) {
1020 return print_option(pretty
, field
, print_names
);
1021 } else if (bt_field_class_type_is(class_id
,
1022 BT_FIELD_CLASS_TYPE_VARIANT
)) {
1023 return print_variant(pretty
, field
, print_names
);
1024 } else if (class_id
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
) {
1025 return print_array(pretty
, field
, print_names
);
1026 } else if (bt_field_class_type_is(class_id
,
1027 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
)) {
1028 return print_sequence(pretty
, field
, print_names
);
1030 // TODO: log instead
1031 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) class_id
);
1037 int print_stream_packet_context(struct pretty_component
*pretty
,
1038 const bt_event
*event
)
1041 const bt_packet
*packet
= NULL
;
1042 const bt_field
*main_field
= NULL
;
1044 packet
= bt_event_borrow_packet_const(event
);
1048 main_field
= bt_packet_borrow_context_field_const(packet
);
1052 if (!pretty
->start_line
) {
1053 bt_common_g_string_append(pretty
->string
, ", ");
1055 pretty
->start_line
= false;
1056 if (pretty
->options
.print_scope_field_names
) {
1057 print_name_equal(pretty
, "stream.packet.context");
1059 ret
= print_field(pretty
, main_field
,
1060 pretty
->options
.print_context_field_names
);
1067 int print_stream_event_context(struct pretty_component
*pretty
,
1068 const bt_event
*event
)
1071 const bt_field
*main_field
= NULL
;
1073 main_field
= bt_event_borrow_common_context_field_const(event
);
1077 if (!pretty
->start_line
) {
1078 bt_common_g_string_append(pretty
->string
, ", ");
1080 pretty
->start_line
= false;
1081 if (pretty
->options
.print_scope_field_names
) {
1082 print_name_equal(pretty
, "stream.event.context");
1084 ret
= print_field(pretty
, main_field
,
1085 pretty
->options
.print_context_field_names
);
1092 int print_event_context(struct pretty_component
*pretty
,
1093 const bt_event
*event
)
1096 const bt_field
*main_field
= NULL
;
1098 main_field
= bt_event_borrow_specific_context_field_const(event
);
1102 if (!pretty
->start_line
) {
1103 bt_common_g_string_append(pretty
->string
, ", ");
1105 pretty
->start_line
= false;
1106 if (pretty
->options
.print_scope_field_names
) {
1107 print_name_equal(pretty
, "event.context");
1109 ret
= print_field(pretty
, main_field
,
1110 pretty
->options
.print_context_field_names
);
1117 int print_event_payload(struct pretty_component
*pretty
,
1118 const bt_event
*event
)
1121 const bt_field
*main_field
= NULL
;
1123 main_field
= bt_event_borrow_payload_field_const(event
);
1127 if (!pretty
->start_line
) {
1128 bt_common_g_string_append(pretty
->string
, ", ");
1130 pretty
->start_line
= false;
1131 if (pretty
->options
.print_scope_field_names
) {
1132 print_name_equal(pretty
, "event.fields");
1134 ret
= print_field(pretty
, main_field
,
1135 pretty
->options
.print_payload_field_names
);
1142 int flush_buf(FILE *stream
, struct pretty_component
*pretty
)
1146 if (pretty
->string
->len
== 0) {
1150 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, stream
) != 1) {
1159 int pretty_print_event(struct pretty_component
*pretty
,
1160 const bt_message
*event_msg
)
1163 const bt_event
*event
=
1164 bt_message_event_borrow_event_const(event_msg
);
1166 BT_ASSERT_DBG(event
);
1167 pretty
->start_line
= true;
1168 g_string_assign(pretty
->string
, "");
1169 ret
= print_event_header(pretty
, event_msg
);
1174 ret
= print_stream_packet_context(pretty
, event
);
1179 ret
= print_stream_event_context(pretty
, event
);
1184 ret
= print_event_context(pretty
, event
);
1189 ret
= print_event_payload(pretty
, event
);
1194 bt_common_g_string_append_c(pretty
->string
, '\n');
1195 if (flush_buf(pretty
->out
, pretty
)) {
1205 int print_discarded_elements_msg(struct pretty_component
*pretty
,
1206 const bt_stream
*stream
,
1207 const bt_clock_snapshot
*begin_clock_snapshot
,
1208 const bt_clock_snapshot
*end_clock_snapshot
,
1209 uint64_t count
, const char *elem_type
)
1212 const bt_stream_class
*stream_class
= NULL
;
1213 const bt_trace
*trace
= NULL
;
1214 const char *stream_name
;
1215 const char *trace_name
;
1217 int64_t stream_class_id
;
1219 const char *init_msg
;
1222 stream_name
= bt_stream_get_name(stream
);
1224 stream_name
= "(unknown)";
1227 /* Stream class ID */
1228 stream_class
= bt_stream_borrow_class_const(stream
);
1229 BT_ASSERT(stream_class
);
1230 stream_class_id
= bt_stream_class_get_id(stream_class
);
1233 stream_id
= bt_stream_get_id(stream
);
1236 trace
= bt_stream_borrow_trace_const(stream
);
1238 trace_name
= bt_trace_get_name(trace
);
1240 trace_name
= "(unknown)";
1244 trace_uuid
= bt_trace_get_uuid(trace
);
1246 /* Format message */
1247 g_string_assign(pretty
->string
, "");
1249 if (count
== UINT64_C(-1)) {
1250 init_msg
= "Tracer may have discarded";
1252 init_msg
= "Tracer discarded";
1255 bt_common_g_string_append_printf(pretty
->string
,
1256 "%s%sWARNING%s%s: %s ",
1257 bt_common_color_fg_yellow(),
1258 bt_common_color_bold(),
1259 bt_common_color_reset(),
1260 bt_common_color_fg_yellow(), init_msg
);
1262 if (count
== UINT64_C(-1)) {
1263 bt_common_g_string_append_printf(pretty
->string
, "%ss", elem_type
);
1265 bt_common_g_string_append_printf(pretty
->string
,
1266 "%" PRIu64
" %s%s", count
, elem_type
,
1267 count
== 1 ? "" : "s");
1270 bt_common_g_string_append_c(pretty
->string
, ' ');
1272 if (begin_clock_snapshot
&& end_clock_snapshot
) {
1273 bt_common_g_string_append(pretty
->string
, "between [");
1274 print_timestamp_wall(pretty
, begin_clock_snapshot
, false);
1275 bt_common_g_string_append(pretty
->string
, "] and [");
1276 print_timestamp_wall(pretty
, end_clock_snapshot
, false);
1277 bt_common_g_string_append(pretty
->string
, "]");
1279 bt_common_g_string_append(pretty
->string
, "(unknown time range)");
1282 bt_common_g_string_append_printf(pretty
->string
, " in trace \"%s\" ", trace_name
);
1285 bt_common_g_string_append_printf(pretty
->string
,
1286 "(UUID: " BT_UUID_FMT
") ",
1287 BT_UUID_FMT_VALUES(trace_uuid
));
1289 bt_common_g_string_append(pretty
->string
, "(no UUID) ");
1292 bt_common_g_string_append_printf(pretty
->string
,
1293 "within stream \"%s\" (stream class ID: %" PRIu64
", ",
1294 stream_name
, stream_class_id
);
1296 if (stream_id
>= 0) {
1297 bt_common_g_string_append_printf(pretty
->string
,
1298 "stream ID: %" PRIu64
, stream_id
);
1300 bt_common_g_string_append(pretty
->string
, "no stream ID");
1303 bt_common_g_string_append_printf(pretty
->string
, ").%s\n",
1304 bt_common_color_reset());
1307 * Print to standard error stream to remain backward compatible
1308 * with Babeltrace 1.
1310 if (flush_buf(stderr
, pretty
)) {
1318 int pretty_print_discarded_items(struct pretty_component
*pretty
,
1319 const bt_message
*msg
)
1321 const bt_clock_snapshot
*begin
= NULL
;
1322 const bt_clock_snapshot
*end
= NULL
;
1323 const bt_stream
*stream
;
1324 const bt_stream_class
*stream_class
;
1325 uint64_t count
= UINT64_C(-1);
1326 const char *elem_type
;
1328 switch (bt_message_get_type(msg
)) {
1329 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1330 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1332 if (bt_message_discarded_events_get_count(msg
, &count
) ==
1333 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1334 count
= UINT64_C(-1);
1337 elem_type
= "event";
1339 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1340 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1342 if (bt_message_discarded_packets_get_count(msg
, &count
) ==
1343 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1344 count
= UINT64_C(-1);
1347 elem_type
= "packet";
1354 stream_class
= bt_stream_borrow_class_const(stream
);
1356 switch (bt_message_get_type(msg
)) {
1357 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1358 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
1360 begin
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1362 end
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1367 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1368 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
1370 begin
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1372 end
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1381 print_discarded_elements_msg(pretty
, stream
, begin
, end
,
1387 void pretty_print_init(void)
1389 strcpy(color_name
, bt_common_color_bold());
1390 strcpy(color_field_name
, bt_common_color_fg_cyan());
1391 strcpy(color_rst
, bt_common_color_reset());
1392 strcpy(color_string_value
, bt_common_color_bold());
1393 strcpy(color_number_value
, bt_common_color_bold());
1394 strcpy(color_enum_mapping_name
, bt_common_color_bold());
1395 strcpy(color_unknown
, bt_common_color_bold());
1396 strcat(color_unknown
, bt_common_color_fg_bright_red());
1397 strcpy(color_event_name
, bt_common_color_bold());
1398 strcat(color_event_name
, bt_common_color_fg_bright_magenta());
1399 strcpy(color_timestamp
, bt_common_color_bold());
1400 strcat(color_timestamp
, bt_common_color_fg_bright_yellow());