2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include <babeltrace2/babeltrace.h>
27 #include "compat/bitfield.h"
28 #include "common/common.h"
29 #include "common/uuid.h"
30 #include "compat/time.h"
31 #include "common/assert.h"
38 #define NSEC_PER_SEC 1000000000LL
40 static char color_name
[32];
41 static char color_field_name
[32];
42 static char color_rst
[32];
43 static char color_string_value
[32];
44 static char color_number_value
[32];
45 static char color_enum_mapping_name
[32];
46 static char color_unknown
[32];
47 static char color_event_name
[32];
48 static char color_timestamp
[32];
51 int64_t real_timestamp
; /* Relative to UNIX epoch. */
52 uint64_t clock_snapshot
; /* In cycles. */
56 int print_field(struct pretty_component
*pretty
,
57 const bt_field
*field
, bool print_names
);
60 void print_name_equal(struct pretty_component
*pretty
, const char *name
)
62 if (pretty
->use_colors
) {
63 bt_common_g_string_append(pretty
->string
, color_name
);
64 bt_common_g_string_append(pretty
->string
, name
);
65 bt_common_g_string_append(pretty
->string
, color_rst
);
67 bt_common_g_string_append(pretty
->string
, name
);
69 bt_common_g_string_append(pretty
->string
, " = ");
73 void print_field_name_equal(struct pretty_component
*pretty
, const char *name
)
75 if (pretty
->use_colors
) {
76 bt_common_g_string_append(pretty
->string
, color_field_name
);
77 bt_common_g_string_append(pretty
->string
, name
);
78 bt_common_g_string_append(pretty
->string
, color_rst
);
80 bt_common_g_string_append(pretty
->string
, name
);
82 bt_common_g_string_append(pretty
->string
, " = ");
86 void print_timestamp_cycles(struct pretty_component
*pretty
,
87 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
91 cycles
= bt_clock_snapshot_get_value(clock_snapshot
);
92 bt_common_g_string_append_printf(pretty
->string
, "%020" PRIu64
, cycles
);
95 if (pretty
->last_cycles_timestamp
!= -1ULL) {
96 pretty
->delta_cycles
= cycles
- pretty
->last_cycles_timestamp
;
99 pretty
->last_cycles_timestamp
= cycles
;
104 void print_timestamp_wall(struct pretty_component
*pretty
,
105 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
108 int64_t ts_nsec
= 0; /* add configurable offset */
109 int64_t ts_sec
= 0; /* add configurable offset */
110 uint64_t ts_sec_abs
, ts_nsec_abs
;
113 if (!clock_snapshot
) {
114 bt_common_g_string_append(pretty
->string
, "??:??:??.?????????");
118 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, &ts_nsec
);
120 // TODO: log, this is unexpected
121 bt_common_g_string_append(pretty
->string
, "Error");
126 if (pretty
->last_real_timestamp
!= -1ULL) {
127 pretty
->delta_real_timestamp
= ts_nsec
- pretty
->last_real_timestamp
;
130 pretty
->last_real_timestamp
= ts_nsec
;
133 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
134 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
136 if (ts_sec
>= 0 && ts_nsec
>= 0) {
139 ts_nsec_abs
= ts_nsec
;
140 } else if (ts_sec
> 0 && ts_nsec
< 0) {
142 ts_sec_abs
= ts_sec
- 1;
143 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
144 } else if (ts_sec
== 0 && ts_nsec
< 0) {
147 ts_nsec_abs
= -ts_nsec
;
148 } else if (ts_sec
< 0 && ts_nsec
> 0) {
150 ts_sec_abs
= -(ts_sec
+ 1);
151 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
152 } else if (ts_sec
< 0 && ts_nsec
== 0) {
154 ts_sec_abs
= -ts_sec
;
155 ts_nsec_abs
= ts_nsec
;
156 } else { /* (ts_sec < 0 && ts_nsec < 0) */
158 ts_sec_abs
= -ts_sec
;
159 ts_nsec_abs
= -ts_nsec
;
162 if (!pretty
->options
.clock_seconds
) {
164 time_t time_s
= (time_t) ts_sec_abs
;
166 if (is_negative
&& !pretty
->negative_timestamp_warning_done
) {
168 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
169 pretty
->negative_timestamp_warning_done
= true;
173 if (!pretty
->options
.clock_gmt
) {
176 res
= bt_localtime_r(&time_s
, &tm
);
179 fprintf(stderr
, "[warning] Unable to get localtime.\n");
185 res
= bt_gmtime_r(&time_s
, &tm
);
188 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
192 if (pretty
->options
.clock_date
) {
196 /* Print date and time */
197 res
= strftime(timestr
, sizeof(timestr
),
201 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
205 bt_common_g_string_append(pretty
->string
, timestr
);
208 /* Print time in HH:MM:SS.ns */
209 bt_common_g_string_append_printf(pretty
->string
,
210 "%02d:%02d:%02d.%09" PRIu64
, tm
.tm_hour
, tm
.tm_min
,
211 tm
.tm_sec
, ts_nsec_abs
);
215 bt_common_g_string_append_printf(pretty
->string
, "%s%" PRId64
".%09" PRIu64
,
216 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
222 int print_event_timestamp(struct pretty_component
*pretty
,
223 const bt_message
*event_msg
, bool *start_line
)
225 bool print_names
= pretty
->options
.print_header_field_names
;
227 const bt_clock_snapshot
*clock_snapshot
= NULL
;
229 if (!bt_message_event_borrow_stream_class_default_clock_class_const(
231 /* No default clock class: skip the timestamp without an error */
235 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(event_msg
);
238 print_name_equal(pretty
, "timestamp");
240 bt_common_g_string_append(pretty
->string
, "[");
242 if (pretty
->use_colors
) {
243 bt_common_g_string_append(pretty
->string
, color_timestamp
);
245 if (pretty
->options
.print_timestamp_cycles
) {
246 print_timestamp_cycles(pretty
, clock_snapshot
, true);
248 print_timestamp_wall(pretty
, clock_snapshot
, true);
250 if (pretty
->use_colors
) {
251 bt_common_g_string_append(pretty
->string
, color_rst
);
255 bt_common_g_string_append(pretty
->string
, "] ");
257 if (pretty
->options
.print_delta_field
) {
259 bt_common_g_string_append(pretty
->string
, ", ");
260 print_name_equal(pretty
, "delta");
262 bt_common_g_string_append(pretty
->string
, "(");
264 if (pretty
->options
.print_timestamp_cycles
) {
265 if (pretty
->delta_cycles
== -1ULL) {
266 bt_common_g_string_append(pretty
->string
,
267 "+??????????\?\?"); /* Not a trigraph. */
269 bt_common_g_string_append_printf(pretty
->string
,
270 "+%012" PRIu64
, pretty
->delta_cycles
);
273 if (pretty
->delta_real_timestamp
!= -1ULL) {
274 uint64_t delta_sec
, delta_nsec
, delta
;
276 delta
= pretty
->delta_real_timestamp
;
277 delta_sec
= delta
/ NSEC_PER_SEC
;
278 delta_nsec
= delta
% NSEC_PER_SEC
;
279 bt_common_g_string_append_printf(pretty
->string
,
280 "+%" PRIu64
".%09" PRIu64
,
281 delta_sec
, delta_nsec
);
283 bt_common_g_string_append(pretty
->string
, "+?.?????????");
287 bt_common_g_string_append(pretty
->string
, ") ");
290 *start_line
= !print_names
;
297 int print_event_header(struct pretty_component
*pretty
,
298 const bt_message
*event_msg
)
300 bool print_names
= pretty
->options
.print_header_field_names
;
302 const bt_event_class
*event_class
= NULL
;
303 const bt_stream
*stream
= NULL
;
304 const bt_trace
*trace
= NULL
;
305 const bt_event
*event
= bt_message_event_borrow_event_const(event_msg
);
308 bt_property_availability prop_avail
;
310 event_class
= bt_event_borrow_class_const(event
);
311 stream
= bt_event_borrow_stream_const(event
);
312 trace
= bt_stream_borrow_trace_const(stream
);
313 ret
= print_event_timestamp(pretty
, event_msg
, &pretty
->start_line
);
317 if (pretty
->options
.print_trace_field
) {
320 name
= bt_trace_get_name(trace
);
322 if (!pretty
->start_line
) {
323 bt_common_g_string_append(pretty
->string
, ", ");
326 print_name_equal(pretty
, "trace");
329 bt_common_g_string_append(pretty
->string
, name
);
332 bt_common_g_string_append(pretty
->string
, ", ");
336 if (pretty
->options
.print_trace_hostname_field
) {
337 const bt_value
*hostname_str
;
339 hostname_str
= bt_trace_borrow_environment_entry_value_by_name_const(
344 if (!pretty
->start_line
) {
345 bt_common_g_string_append(pretty
->string
, ", ");
348 print_name_equal(pretty
, "trace:hostname");
350 str
= bt_value_string_get(hostname_str
);
351 bt_common_g_string_append(pretty
->string
, str
);
355 if (pretty
->options
.print_trace_domain_field
) {
356 const bt_value
*domain_str
;
358 domain_str
= bt_trace_borrow_environment_entry_value_by_name_const(
363 if (!pretty
->start_line
) {
364 bt_common_g_string_append(pretty
->string
, ", ");
367 print_name_equal(pretty
, "trace:domain");
368 } else if (dom_print
) {
369 bt_common_g_string_append(pretty
->string
, ":");
371 str
= bt_value_string_get(domain_str
);
372 bt_common_g_string_append(pretty
->string
, str
);
376 if (pretty
->options
.print_trace_procname_field
) {
377 const bt_value
*procname_str
;
379 procname_str
= bt_trace_borrow_environment_entry_value_by_name_const(
384 if (!pretty
->start_line
) {
385 bt_common_g_string_append(pretty
->string
, ", ");
388 print_name_equal(pretty
, "trace:procname");
389 } else if (dom_print
) {
390 bt_common_g_string_append(pretty
->string
, ":");
392 str
= bt_value_string_get(procname_str
);
393 bt_common_g_string_append(pretty
->string
, str
);
397 if (pretty
->options
.print_trace_vpid_field
) {
398 const bt_value
*vpid_value
;
400 vpid_value
= bt_trace_borrow_environment_entry_value_by_name_const(
405 if (!pretty
->start_line
) {
406 bt_common_g_string_append(pretty
->string
, ", ");
409 print_name_equal(pretty
, "trace:vpid");
410 } else if (dom_print
) {
411 bt_common_g_string_append(pretty
->string
, ":");
413 value
= bt_value_integer_signed_get(vpid_value
);
414 bt_common_g_string_append_printf(pretty
->string
,
415 "(%" PRId64
")", value
);
419 if (pretty
->options
.print_loglevel_field
) {
420 static const char *log_level_names
[] = {
421 [ BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
] = "TRACE_EMERG",
422 [ BT_EVENT_CLASS_LOG_LEVEL_ALERT
] = "TRACE_ALERT",
423 [ BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
] = "TRACE_CRIT",
424 [ BT_EVENT_CLASS_LOG_LEVEL_ERROR
] = "TRACE_ERR",
425 [ BT_EVENT_CLASS_LOG_LEVEL_WARNING
] = "TRACE_WARNING",
426 [ BT_EVENT_CLASS_LOG_LEVEL_NOTICE
] = "TRACE_NOTICE",
427 [ BT_EVENT_CLASS_LOG_LEVEL_INFO
] = "TRACE_INFO",
428 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
429 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
430 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
431 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
432 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
433 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
434 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
435 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG
] = "TRACE_DEBUG",
437 bt_event_class_log_level log_level
;
438 const char *log_level_str
= NULL
;
440 prop_avail
= bt_event_class_get_log_level(event_class
,
442 if (prop_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
443 log_level_str
= log_level_names
[log_level
];
444 BT_ASSERT_DBG(log_level_str
);
446 if (!pretty
->start_line
) {
447 bt_common_g_string_append(pretty
->string
, ", ");
450 print_name_equal(pretty
, "loglevel");
451 } else if (dom_print
) {
452 bt_common_g_string_append(pretty
->string
, ":");
455 bt_common_g_string_append(pretty
->string
, log_level_str
);
456 bt_common_g_string_append_printf(
457 pretty
->string
, " (%d)", (int) log_level
);
461 if (pretty
->options
.print_emf_field
) {
464 uri_str
= bt_event_class_get_emf_uri(event_class
);
466 if (!pretty
->start_line
) {
467 bt_common_g_string_append(pretty
->string
, ", ");
470 print_name_equal(pretty
, "model.emf.uri");
471 } else if (dom_print
) {
472 bt_common_g_string_append(pretty
->string
, ":");
475 bt_common_g_string_append(pretty
->string
, uri_str
);
479 if (dom_print
&& !print_names
) {
480 bt_common_g_string_append(pretty
->string
, " ");
482 if (!pretty
->start_line
) {
483 bt_common_g_string_append(pretty
->string
, ", ");
485 pretty
->start_line
= true;
487 print_name_equal(pretty
, "name");
489 ev_name
= bt_event_class_get_name(event_class
);
490 if (pretty
->use_colors
) {
492 bt_common_g_string_append(pretty
->string
,
495 bt_common_g_string_append(pretty
->string
,
500 bt_common_g_string_append(pretty
->string
, ev_name
);
502 bt_common_g_string_append(pretty
->string
, "<unknown>");
504 if (pretty
->use_colors
) {
505 bt_common_g_string_append(pretty
->string
, color_rst
);
508 bt_common_g_string_append(pretty
->string
, ": ");
510 bt_common_g_string_append(pretty
->string
, ", ");
518 int print_integer(struct pretty_component
*pretty
,
519 const bt_field
*field
)
522 bt_field_class_integer_preferred_display_base base
;
523 const bt_field_class
*int_fc
;
528 bool rst_color
= false;
529 bt_field_class_type ft_type
;
531 int_fc
= bt_field_borrow_class_const(field
);
532 BT_ASSERT_DBG(int_fc
);
533 ft_type
= bt_field_get_class_type(field
);
534 if (bt_field_class_type_is(ft_type
,
535 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
)) {
536 v
.u
= bt_field_integer_unsigned_get_value(field
);
538 v
.s
= bt_field_integer_signed_get_value(field
);
541 if (pretty
->use_colors
) {
542 bt_common_g_string_append(pretty
->string
, color_number_value
);
546 base
= bt_field_class_integer_get_preferred_display_base(int_fc
);
548 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
552 len
= bt_field_class_integer_get_field_value_range(int_fc
);
553 bt_common_g_string_append(pretty
->string
, "0b");
554 _bt_safe_lshift(v
.u
, 64 - len
);
555 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
556 bt_common_g_string_append_c(pretty
->string
,
557 (v
.u
& (1ULL << 63)) ? '1' : '0');
558 _bt_safe_lshift(v
.u
, 1);
562 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
564 if (bt_field_class_type_is(ft_type
,
565 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
)) {
568 len
= bt_field_class_integer_get_field_value_range(
573 BT_ASSERT_DBG(len
!= 0);
574 /* Round length to the nearest 3-bit */
575 rounded_len
= (((len
- 1) / 3) + 1) * 3;
576 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
580 bt_common_g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
583 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
584 if (bt_field_class_type_is(ft_type
,
585 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
)) {
586 bt_common_g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
588 bt_common_g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
591 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
595 len
= bt_field_class_integer_get_field_value_range(int_fc
);
597 /* Round length to the nearest nibble */
598 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
600 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
603 bt_common_g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
612 bt_common_g_string_append(pretty
->string
, color_rst
);
618 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
622 bt_common_g_string_append_c(pretty
->string
, '"');
624 for (i
= 0; i
< strlen(str
); i
++) {
625 /* Escape sequences not recognized by iscntrl(). */
628 bt_common_g_string_append(pretty
->string
, "\\\\");
631 bt_common_g_string_append(pretty
->string
, "\\\'");
634 bt_common_g_string_append(pretty
->string
, "\\\"");
637 bt_common_g_string_append(pretty
->string
, "\\\?");
641 /* Standard characters. */
642 if (!iscntrl(str
[i
])) {
643 bt_common_g_string_append_c(pretty
->string
, str
[i
]);
649 bt_common_g_string_append(pretty
->string
, "\\0");
652 bt_common_g_string_append(pretty
->string
, "\\a");
655 bt_common_g_string_append(pretty
->string
, "\\b");
658 bt_common_g_string_append(pretty
->string
, "\\e");
661 bt_common_g_string_append(pretty
->string
, "\\f");
664 bt_common_g_string_append(pretty
->string
, "\\n");
667 bt_common_g_string_append(pretty
->string
, "\\r");
670 bt_common_g_string_append(pretty
->string
, "\\t");
673 bt_common_g_string_append(pretty
->string
, "\\v");
676 /* Unhandled control-sequence, print as hex. */
677 bt_common_g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
682 bt_common_g_string_append_c(pretty
->string
, '"');
686 int print_enum(struct pretty_component
*pretty
,
687 const bt_field
*field
)
690 bt_field_class_enumeration_mapping_label_array label_array
;
691 uint64_t label_count
;
694 switch (bt_field_get_class_type(field
)) {
695 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
696 ret
= bt_field_enumeration_unsigned_get_mapping_labels(field
,
697 &label_array
, &label_count
);
699 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
700 ret
= bt_field_enumeration_signed_get_mapping_labels(field
,
701 &label_array
, &label_count
);
712 bt_common_g_string_append(pretty
->string
, "( ");
713 if (label_count
== 0) {
714 if (pretty
->use_colors
) {
715 bt_common_g_string_append(pretty
->string
, color_unknown
);
717 bt_common_g_string_append(pretty
->string
, "<unknown>");
718 if (pretty
->use_colors
) {
719 bt_common_g_string_append(pretty
->string
, color_rst
);
723 for (i
= 0; i
< label_count
; i
++) {
724 const char *mapping_name
= label_array
[i
];
727 bt_common_g_string_append(pretty
->string
, ", ");
729 if (pretty
->use_colors
) {
730 bt_common_g_string_append(pretty
->string
, color_enum_mapping_name
);
732 print_escape_string(pretty
, mapping_name
);
733 if (pretty
->use_colors
) {
734 bt_common_g_string_append(pretty
->string
, color_rst
);
738 bt_common_g_string_append(pretty
->string
, " : container = ");
739 ret
= print_integer(pretty
, field
);
743 bt_common_g_string_append(pretty
->string
, " )");
749 int print_struct_field(struct pretty_component
*pretty
,
750 const bt_field
*_struct
,
751 const bt_field_class
*struct_class
,
752 uint64_t i
, bool print_names
, uint64_t *nr_printed_fields
)
755 const char *field_name
;
756 const bt_field
*field
= NULL
;
757 const bt_field_class_structure_member
*member
;
759 field
= bt_field_structure_borrow_member_field_by_index_const(_struct
, i
);
765 member
= bt_field_class_structure_borrow_member_by_index_const(
767 field_name
= bt_field_class_structure_member_get_name(member
);
769 if (*nr_printed_fields
> 0) {
770 bt_common_g_string_append(pretty
->string
, ", ");
772 bt_common_g_string_append(pretty
->string
, " ");
775 print_field_name_equal(pretty
, field_name
);
777 ret
= print_field(pretty
, field
, print_names
);
778 *nr_printed_fields
+= 1;
785 int print_struct(struct pretty_component
*pretty
,
786 const bt_field
*_struct
, bool print_names
)
789 const bt_field_class
*struct_class
= NULL
;
790 uint64_t nr_fields
, i
, nr_printed_fields
;
792 struct_class
= bt_field_borrow_class_const(_struct
);
793 nr_fields
= bt_field_class_structure_get_member_count(struct_class
);
795 bt_common_g_string_append(pretty
->string
, "{");
797 nr_printed_fields
= 0;
798 for (i
= 0; i
< nr_fields
; i
++) {
799 ret
= print_struct_field(pretty
, _struct
, struct_class
, i
,
800 print_names
, &nr_printed_fields
);
806 bt_common_g_string_append(pretty
->string
, " }");
813 int print_array_field(struct pretty_component
*pretty
,
814 const bt_field
*array
, uint64_t i
, bool print_names
)
816 const bt_field
*field
= NULL
;
819 bt_common_g_string_append(pretty
->string
, ", ");
821 bt_common_g_string_append(pretty
->string
, " ");
824 bt_common_g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
827 field
= bt_field_array_borrow_element_field_by_index_const(array
, i
);
828 BT_ASSERT_DBG(field
);
829 return print_field(pretty
, field
, print_names
);
833 int print_array(struct pretty_component
*pretty
,
834 const bt_field
*array
, bool print_names
)
840 len
= bt_field_array_get_length(array
);
841 bt_common_g_string_append(pretty
->string
, "[");
843 for (i
= 0; i
< len
; i
++) {
844 ret
= print_array_field(pretty
, array
, i
, print_names
);
850 bt_common_g_string_append(pretty
->string
, " ]");
857 int print_sequence_field(struct pretty_component
*pretty
,
858 const bt_field
*seq
, uint64_t i
, bool print_names
)
860 const bt_field
*field
= NULL
;
863 bt_common_g_string_append(pretty
->string
, ", ");
865 bt_common_g_string_append(pretty
->string
, " ");
868 bt_common_g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
871 field
= bt_field_array_borrow_element_field_by_index_const(seq
, i
);
872 BT_ASSERT_DBG(field
);
873 return print_field(pretty
, field
, print_names
);
877 int print_sequence(struct pretty_component
*pretty
,
878 const bt_field
*seq
, bool print_names
)
884 len
= bt_field_array_get_length(seq
);
885 bt_common_g_string_append(pretty
->string
, "[");
888 for (i
= 0; i
< len
; i
++) {
889 ret
= print_sequence_field(pretty
, seq
, i
, print_names
);
895 bt_common_g_string_append(pretty
->string
, " ]");
902 int print_option(struct pretty_component
*pretty
,
903 const bt_field
*option
, bool print_names
)
906 const bt_field
*field
= NULL
;
908 field
= bt_field_option_borrow_field_const(option
);
910 bt_common_g_string_append(pretty
->string
, "{ ");
913 // TODO: find tag's name using field path
914 // print_field_name_equal(pretty, tag_choice);
916 ret
= print_field(pretty
, field
, print_names
);
921 bt_common_g_string_append(pretty
->string
, " }");
923 bt_common_g_string_append(pretty
->string
, "<none>");
931 int print_variant(struct pretty_component
*pretty
,
932 const bt_field
*variant
, bool print_names
)
935 const bt_field
*field
= NULL
;
937 field
= bt_field_variant_borrow_selected_option_field_const(variant
);
938 BT_ASSERT_DBG(field
);
939 bt_common_g_string_append(pretty
->string
, "{ ");
942 // TODO: find tag's name using field path
943 // print_field_name_equal(pretty, tag_choice);
945 ret
= print_field(pretty
, field
, print_names
);
950 bt_common_g_string_append(pretty
->string
, " }");
957 int print_field(struct pretty_component
*pretty
,
958 const bt_field
*field
, bool print_names
)
960 bt_field_class_type class_id
;
962 class_id
= bt_field_get_class_type(field
);
963 if (class_id
== BT_FIELD_CLASS_TYPE_BOOL
) {
967 v
= bt_field_bool_get_value(field
);
968 if (pretty
->use_colors
) {
969 bt_common_g_string_append(pretty
->string
, color_number_value
);
976 bt_common_g_string_append(pretty
->string
, text
);
977 if (pretty
->use_colors
) {
978 bt_common_g_string_append(pretty
->string
, color_rst
);
981 } else if (class_id
== BT_FIELD_CLASS_TYPE_BIT_ARRAY
) {
982 uint64_t v
= bt_field_bit_array_get_value_as_integer(field
);
984 if (pretty
->use_colors
) {
985 bt_common_g_string_append(pretty
->string
,
988 bt_common_g_string_append_printf(pretty
->string
, "0x%" PRIX64
,
990 if (pretty
->use_colors
) {
991 bt_common_g_string_append(pretty
->string
, color_rst
);
994 } else if (bt_field_class_type_is(class_id
,
995 BT_FIELD_CLASS_TYPE_ENUMERATION
)) {
996 return print_enum(pretty
, field
);
997 } else if (bt_field_class_type_is(class_id
,
998 BT_FIELD_CLASS_TYPE_INTEGER
)) {
999 return print_integer(pretty
, field
);
1000 } else if (bt_field_class_type_is(class_id
,
1001 BT_FIELD_CLASS_TYPE_REAL
)) {
1004 if (class_id
== BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
) {
1005 v
= bt_field_real_single_precision_get_value(field
);
1007 v
= bt_field_real_double_precision_get_value(field
);
1010 if (pretty
->use_colors
) {
1011 bt_common_g_string_append(pretty
->string
, color_number_value
);
1013 bt_common_g_string_append_printf(pretty
->string
, "%g", v
);
1014 if (pretty
->use_colors
) {
1015 bt_common_g_string_append(pretty
->string
, color_rst
);
1018 } else if (class_id
== BT_FIELD_CLASS_TYPE_STRING
) {
1021 str
= bt_field_string_get_value(field
);
1026 if (pretty
->use_colors
) {
1027 bt_common_g_string_append(pretty
->string
, color_string_value
);
1029 print_escape_string(pretty
, str
);
1030 if (pretty
->use_colors
) {
1031 bt_common_g_string_append(pretty
->string
, color_rst
);
1034 } else if (class_id
== BT_FIELD_CLASS_TYPE_STRUCTURE
) {
1035 return print_struct(pretty
, field
, print_names
);
1036 } else if (bt_field_class_type_is(class_id
,
1037 BT_FIELD_CLASS_TYPE_OPTION
)) {
1038 return print_option(pretty
, field
, print_names
);
1039 } else if (bt_field_class_type_is(class_id
,
1040 BT_FIELD_CLASS_TYPE_VARIANT
)) {
1041 return print_variant(pretty
, field
, print_names
);
1042 } else if (class_id
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
) {
1043 return print_array(pretty
, field
, print_names
);
1044 } else if (bt_field_class_type_is(class_id
,
1045 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
)) {
1046 return print_sequence(pretty
, field
, print_names
);
1048 // TODO: log instead
1049 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) class_id
);
1055 int print_stream_packet_context(struct pretty_component
*pretty
,
1056 const bt_event
*event
)
1059 const bt_packet
*packet
= NULL
;
1060 const bt_field
*main_field
= NULL
;
1062 packet
= bt_event_borrow_packet_const(event
);
1066 main_field
= bt_packet_borrow_context_field_const(packet
);
1070 if (!pretty
->start_line
) {
1071 bt_common_g_string_append(pretty
->string
, ", ");
1073 pretty
->start_line
= false;
1074 if (pretty
->options
.print_scope_field_names
) {
1075 print_name_equal(pretty
, "stream.packet.context");
1077 ret
= print_field(pretty
, main_field
,
1078 pretty
->options
.print_context_field_names
);
1085 int print_stream_event_context(struct pretty_component
*pretty
,
1086 const bt_event
*event
)
1089 const bt_field
*main_field
= NULL
;
1091 main_field
= bt_event_borrow_common_context_field_const(event
);
1095 if (!pretty
->start_line
) {
1096 bt_common_g_string_append(pretty
->string
, ", ");
1098 pretty
->start_line
= false;
1099 if (pretty
->options
.print_scope_field_names
) {
1100 print_name_equal(pretty
, "stream.event.context");
1102 ret
= print_field(pretty
, main_field
,
1103 pretty
->options
.print_context_field_names
);
1110 int print_event_context(struct pretty_component
*pretty
,
1111 const bt_event
*event
)
1114 const bt_field
*main_field
= NULL
;
1116 main_field
= bt_event_borrow_specific_context_field_const(event
);
1120 if (!pretty
->start_line
) {
1121 bt_common_g_string_append(pretty
->string
, ", ");
1123 pretty
->start_line
= false;
1124 if (pretty
->options
.print_scope_field_names
) {
1125 print_name_equal(pretty
, "event.context");
1127 ret
= print_field(pretty
, main_field
,
1128 pretty
->options
.print_context_field_names
);
1135 int print_event_payload(struct pretty_component
*pretty
,
1136 const bt_event
*event
)
1139 const bt_field
*main_field
= NULL
;
1141 main_field
= bt_event_borrow_payload_field_const(event
);
1145 if (!pretty
->start_line
) {
1146 bt_common_g_string_append(pretty
->string
, ", ");
1148 pretty
->start_line
= false;
1149 if (pretty
->options
.print_scope_field_names
) {
1150 print_name_equal(pretty
, "event.fields");
1152 ret
= print_field(pretty
, main_field
,
1153 pretty
->options
.print_payload_field_names
);
1160 int flush_buf(FILE *stream
, struct pretty_component
*pretty
)
1164 if (pretty
->string
->len
== 0) {
1168 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, stream
) != 1) {
1177 int pretty_print_event(struct pretty_component
*pretty
,
1178 const bt_message
*event_msg
)
1181 const bt_event
*event
=
1182 bt_message_event_borrow_event_const(event_msg
);
1184 BT_ASSERT_DBG(event
);
1185 pretty
->start_line
= true;
1186 g_string_assign(pretty
->string
, "");
1187 ret
= print_event_header(pretty
, event_msg
);
1192 ret
= print_stream_packet_context(pretty
, event
);
1197 ret
= print_stream_event_context(pretty
, event
);
1202 ret
= print_event_context(pretty
, event
);
1207 ret
= print_event_payload(pretty
, event
);
1212 bt_common_g_string_append_c(pretty
->string
, '\n');
1213 if (flush_buf(pretty
->out
, pretty
)) {
1223 int print_discarded_elements_msg(struct pretty_component
*pretty
,
1224 const bt_stream
*stream
,
1225 const bt_clock_snapshot
*begin_clock_snapshot
,
1226 const bt_clock_snapshot
*end_clock_snapshot
,
1227 uint64_t count
, const char *elem_type
)
1230 const bt_stream_class
*stream_class
= NULL
;
1231 const bt_trace
*trace
= NULL
;
1232 const char *stream_name
;
1233 const char *trace_name
;
1235 int64_t stream_class_id
;
1237 const char *init_msg
;
1240 stream_name
= bt_stream_get_name(stream
);
1242 stream_name
= "(unknown)";
1245 /* Stream class ID */
1246 stream_class
= bt_stream_borrow_class_const(stream
);
1247 BT_ASSERT(stream_class
);
1248 stream_class_id
= bt_stream_class_get_id(stream_class
);
1251 stream_id
= bt_stream_get_id(stream
);
1254 trace
= bt_stream_borrow_trace_const(stream
);
1256 trace_name
= bt_trace_get_name(trace
);
1258 trace_name
= "(unknown)";
1262 trace_uuid
= bt_trace_get_uuid(trace
);
1264 /* Format message */
1265 g_string_assign(pretty
->string
, "");
1267 if (count
== UINT64_C(-1)) {
1268 init_msg
= "Tracer may have discarded";
1270 init_msg
= "Tracer discarded";
1273 bt_common_g_string_append_printf(pretty
->string
,
1274 "%s%sWARNING%s%s: %s ",
1275 bt_common_color_fg_yellow(),
1276 bt_common_color_bold(),
1277 bt_common_color_reset(),
1278 bt_common_color_fg_yellow(), init_msg
);
1280 if (count
== UINT64_C(-1)) {
1281 bt_common_g_string_append_printf(pretty
->string
, "%ss", elem_type
);
1283 bt_common_g_string_append_printf(pretty
->string
,
1284 "%" PRIu64
" %s%s", count
, elem_type
,
1285 count
== 1 ? "" : "s");
1288 bt_common_g_string_append_c(pretty
->string
, ' ');
1290 if (begin_clock_snapshot
&& end_clock_snapshot
) {
1291 bt_common_g_string_append(pretty
->string
, "between [");
1292 print_timestamp_wall(pretty
, begin_clock_snapshot
, false);
1293 bt_common_g_string_append(pretty
->string
, "] and [");
1294 print_timestamp_wall(pretty
, end_clock_snapshot
, false);
1295 bt_common_g_string_append(pretty
->string
, "]");
1297 bt_common_g_string_append(pretty
->string
, "(unknown time range)");
1300 bt_common_g_string_append_printf(pretty
->string
, " in trace \"%s\" ", trace_name
);
1303 bt_common_g_string_append_printf(pretty
->string
,
1304 "(UUID: " BT_UUID_FMT
") ",
1305 BT_UUID_FMT_VALUES(trace_uuid
));
1307 bt_common_g_string_append(pretty
->string
, "(no UUID) ");
1310 bt_common_g_string_append_printf(pretty
->string
,
1311 "within stream \"%s\" (stream class ID: %" PRIu64
", ",
1312 stream_name
, stream_class_id
);
1314 if (stream_id
>= 0) {
1315 bt_common_g_string_append_printf(pretty
->string
,
1316 "stream ID: %" PRIu64
, stream_id
);
1318 bt_common_g_string_append(pretty
->string
, "no stream ID");
1321 bt_common_g_string_append_printf(pretty
->string
, ").%s\n",
1322 bt_common_color_reset());
1325 * Print to standard error stream to remain backward compatible
1326 * with Babeltrace 1.
1328 if (flush_buf(stderr
, pretty
)) {
1336 int pretty_print_discarded_items(struct pretty_component
*pretty
,
1337 const bt_message
*msg
)
1339 const bt_clock_snapshot
*begin
= NULL
;
1340 const bt_clock_snapshot
*end
= NULL
;
1341 const bt_stream
*stream
;
1342 const bt_stream_class
*stream_class
;
1343 uint64_t count
= UINT64_C(-1);
1344 const char *elem_type
;
1346 switch (bt_message_get_type(msg
)) {
1347 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1348 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1350 if (bt_message_discarded_events_get_count(msg
, &count
) ==
1351 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1352 count
= UINT64_C(-1);
1355 elem_type
= "event";
1357 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1358 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1360 if (bt_message_discarded_packets_get_count(msg
, &count
) ==
1361 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1362 count
= UINT64_C(-1);
1365 elem_type
= "packet";
1372 stream_class
= bt_stream_borrow_class_const(stream
);
1374 switch (bt_message_get_type(msg
)) {
1375 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1376 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
1378 begin
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1380 end
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1385 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1386 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
1388 begin
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1390 end
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1399 print_discarded_elements_msg(pretty
, stream
, begin
, end
,
1405 void pretty_print_init(void)
1407 strcpy(color_name
, bt_common_color_bold());
1408 strcpy(color_field_name
, bt_common_color_fg_cyan());
1409 strcpy(color_rst
, bt_common_color_reset());
1410 strcpy(color_string_value
, bt_common_color_bold());
1411 strcpy(color_number_value
, bt_common_color_bold());
1412 strcpy(color_enum_mapping_name
, bt_common_color_bold());
1413 strcpy(color_unknown
, bt_common_color_bold());
1414 strcat(color_unknown
, bt_common_color_fg_bright_red());
1415 strcpy(color_event_name
, bt_common_color_bold());
1416 strcat(color_event_name
, bt_common_color_fg_bright_magenta());
1417 strcpy(color_timestamp
, bt_common_color_bold());
1418 strcat(color_timestamp
, bt_common_color_fg_bright_yellow());