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"
37 #define NSEC_PER_SEC 1000000000LL
39 #define COLOR_NAME BT_COMMON_COLOR_BOLD
40 #define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN
41 #define COLOR_RST BT_COMMON_COLOR_RESET
42 #define COLOR_STRING_VALUE BT_COMMON_COLOR_BOLD
43 #define COLOR_NUMBER_VALUE BT_COMMON_COLOR_BOLD
44 #define COLOR_ENUM_MAPPING_NAME BT_COMMON_COLOR_BOLD
45 #define COLOR_UNKNOWN BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED
46 #define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA
47 #define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW
50 int64_t real_timestamp
; /* Relative to UNIX epoch. */
51 uint64_t clock_snapshot
; /* In cycles. */
55 int print_field(struct pretty_component
*pretty
,
56 const bt_field
*field
, bool print_names
);
59 void print_name_equal(struct pretty_component
*pretty
, const char *name
)
61 if (pretty
->use_colors
) {
62 bt_common_g_string_append(pretty
->string
, COLOR_NAME
);
63 bt_common_g_string_append(pretty
->string
, name
);
64 bt_common_g_string_append(pretty
->string
, COLOR_RST
);
66 bt_common_g_string_append(pretty
->string
, name
);
68 bt_common_g_string_append(pretty
->string
, " = ");
72 void print_field_name_equal(struct pretty_component
*pretty
, const char *name
)
74 if (pretty
->use_colors
) {
75 bt_common_g_string_append(pretty
->string
, COLOR_FIELD_NAME
);
76 bt_common_g_string_append(pretty
->string
, name
);
77 bt_common_g_string_append(pretty
->string
, COLOR_RST
);
79 bt_common_g_string_append(pretty
->string
, name
);
81 bt_common_g_string_append(pretty
->string
, " = ");
85 void print_timestamp_cycles(struct pretty_component
*pretty
,
86 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
90 cycles
= bt_clock_snapshot_get_value(clock_snapshot
);
91 bt_common_g_string_append_printf(pretty
->string
, "%020" PRIu64
, cycles
);
94 if (pretty
->last_cycles_timestamp
!= -1ULL) {
95 pretty
->delta_cycles
= cycles
- pretty
->last_cycles_timestamp
;
98 pretty
->last_cycles_timestamp
= cycles
;
103 void print_timestamp_wall(struct pretty_component
*pretty
,
104 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
107 int64_t ts_nsec
= 0; /* add configurable offset */
108 int64_t ts_sec
= 0; /* add configurable offset */
109 uint64_t ts_sec_abs
, ts_nsec_abs
;
112 if (!clock_snapshot
) {
113 bt_common_g_string_append(pretty
->string
, "??:??:??.?????????");
117 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, &ts_nsec
);
119 // TODO: log, this is unexpected
120 bt_common_g_string_append(pretty
->string
, "Error");
125 if (pretty
->last_real_timestamp
!= -1ULL) {
126 pretty
->delta_real_timestamp
= ts_nsec
- pretty
->last_real_timestamp
;
129 pretty
->last_real_timestamp
= ts_nsec
;
132 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
133 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
135 if (ts_sec
>= 0 && ts_nsec
>= 0) {
138 ts_nsec_abs
= ts_nsec
;
139 } else if (ts_sec
> 0 && ts_nsec
< 0) {
141 ts_sec_abs
= ts_sec
- 1;
142 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
143 } else if (ts_sec
== 0 && ts_nsec
< 0) {
146 ts_nsec_abs
= -ts_nsec
;
147 } else if (ts_sec
< 0 && ts_nsec
> 0) {
149 ts_sec_abs
= -(ts_sec
+ 1);
150 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
151 } else if (ts_sec
< 0 && ts_nsec
== 0) {
153 ts_sec_abs
= -ts_sec
;
154 ts_nsec_abs
= ts_nsec
;
155 } else { /* (ts_sec < 0 && ts_nsec < 0) */
157 ts_sec_abs
= -ts_sec
;
158 ts_nsec_abs
= -ts_nsec
;
161 if (!pretty
->options
.clock_seconds
) {
163 time_t time_s
= (time_t) ts_sec_abs
;
165 if (is_negative
&& !pretty
->negative_timestamp_warning_done
) {
167 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
168 pretty
->negative_timestamp_warning_done
= true;
172 if (!pretty
->options
.clock_gmt
) {
175 res
= bt_localtime_r(&time_s
, &tm
);
178 fprintf(stderr
, "[warning] Unable to get localtime.\n");
184 res
= bt_gmtime_r(&time_s
, &tm
);
187 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
191 if (pretty
->options
.clock_date
) {
195 /* Print date and time */
196 res
= strftime(timestr
, sizeof(timestr
),
200 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
204 bt_common_g_string_append(pretty
->string
, timestr
);
207 /* Print time in HH:MM:SS.ns */
208 bt_common_g_string_append_printf(pretty
->string
,
209 "%02d:%02d:%02d.%09" PRIu64
, tm
.tm_hour
, tm
.tm_min
,
210 tm
.tm_sec
, ts_nsec_abs
);
214 bt_common_g_string_append_printf(pretty
->string
, "%s%" PRId64
".%09" PRIu64
,
215 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
221 int print_event_timestamp(struct pretty_component
*pretty
,
222 const bt_message
*event_msg
, bool *start_line
)
224 bool print_names
= pretty
->options
.print_header_field_names
;
226 const bt_clock_snapshot
*clock_snapshot
= NULL
;
228 if (!bt_message_event_borrow_stream_class_default_clock_class_const(
230 /* No default clock class: skip the timestamp without an error */
234 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(event_msg
);
237 print_name_equal(pretty
, "timestamp");
239 bt_common_g_string_append(pretty
->string
, "[");
241 if (pretty
->use_colors
) {
242 bt_common_g_string_append(pretty
->string
, COLOR_TIMESTAMP
);
244 if (pretty
->options
.print_timestamp_cycles
) {
245 print_timestamp_cycles(pretty
, clock_snapshot
, true);
247 print_timestamp_wall(pretty
, clock_snapshot
, true);
249 if (pretty
->use_colors
) {
250 bt_common_g_string_append(pretty
->string
, COLOR_RST
);
254 bt_common_g_string_append(pretty
->string
, "] ");
256 if (pretty
->options
.print_delta_field
) {
258 bt_common_g_string_append(pretty
->string
, ", ");
259 print_name_equal(pretty
, "delta");
261 bt_common_g_string_append(pretty
->string
, "(");
263 if (pretty
->options
.print_timestamp_cycles
) {
264 if (pretty
->delta_cycles
== -1ULL) {
265 bt_common_g_string_append(pretty
->string
,
266 "+??????????\?\?"); /* Not a trigraph. */
268 bt_common_g_string_append_printf(pretty
->string
,
269 "+%012" PRIu64
, pretty
->delta_cycles
);
272 if (pretty
->delta_real_timestamp
!= -1ULL) {
273 uint64_t delta_sec
, delta_nsec
, delta
;
275 delta
= pretty
->delta_real_timestamp
;
276 delta_sec
= delta
/ NSEC_PER_SEC
;
277 delta_nsec
= delta
% NSEC_PER_SEC
;
278 bt_common_g_string_append_printf(pretty
->string
,
279 "+%" PRIu64
".%09" PRIu64
,
280 delta_sec
, delta_nsec
);
282 bt_common_g_string_append(pretty
->string
, "+?.?????????");
286 bt_common_g_string_append(pretty
->string
, ") ");
289 *start_line
= !print_names
;
296 int print_event_header(struct pretty_component
*pretty
,
297 const bt_message
*event_msg
)
299 bool print_names
= pretty
->options
.print_header_field_names
;
301 const bt_event_class
*event_class
= NULL
;
302 const bt_stream
*stream
= NULL
;
303 const bt_trace
*trace
= NULL
;
304 const bt_event
*event
= bt_message_event_borrow_event_const(event_msg
);
307 bt_property_availability prop_avail
;
309 event_class
= bt_event_borrow_class_const(event
);
310 stream
= bt_event_borrow_stream_const(event
);
311 trace
= bt_stream_borrow_trace_const(stream
);
312 ret
= print_event_timestamp(pretty
, event_msg
, &pretty
->start_line
);
316 if (pretty
->options
.print_trace_field
) {
319 name
= bt_trace_get_name(trace
);
321 if (!pretty
->start_line
) {
322 bt_common_g_string_append(pretty
->string
, ", ");
325 print_name_equal(pretty
, "trace");
328 bt_common_g_string_append(pretty
->string
, name
);
331 bt_common_g_string_append(pretty
->string
, ", ");
335 if (pretty
->options
.print_trace_hostname_field
) {
336 const bt_value
*hostname_str
;
338 hostname_str
= bt_trace_borrow_environment_entry_value_by_name_const(
343 if (!pretty
->start_line
) {
344 bt_common_g_string_append(pretty
->string
, ", ");
347 print_name_equal(pretty
, "trace:hostname");
349 str
= bt_value_string_get(hostname_str
);
350 bt_common_g_string_append(pretty
->string
, str
);
354 if (pretty
->options
.print_trace_domain_field
) {
355 const bt_value
*domain_str
;
357 domain_str
= bt_trace_borrow_environment_entry_value_by_name_const(
362 if (!pretty
->start_line
) {
363 bt_common_g_string_append(pretty
->string
, ", ");
366 print_name_equal(pretty
, "trace:domain");
367 } else if (dom_print
) {
368 bt_common_g_string_append(pretty
->string
, ":");
370 str
= bt_value_string_get(domain_str
);
371 bt_common_g_string_append(pretty
->string
, str
);
375 if (pretty
->options
.print_trace_procname_field
) {
376 const bt_value
*procname_str
;
378 procname_str
= bt_trace_borrow_environment_entry_value_by_name_const(
383 if (!pretty
->start_line
) {
384 bt_common_g_string_append(pretty
->string
, ", ");
387 print_name_equal(pretty
, "trace:procname");
388 } else if (dom_print
) {
389 bt_common_g_string_append(pretty
->string
, ":");
391 str
= bt_value_string_get(procname_str
);
392 bt_common_g_string_append(pretty
->string
, str
);
396 if (pretty
->options
.print_trace_vpid_field
) {
397 const bt_value
*vpid_value
;
399 vpid_value
= bt_trace_borrow_environment_entry_value_by_name_const(
404 if (!pretty
->start_line
) {
405 bt_common_g_string_append(pretty
->string
, ", ");
408 print_name_equal(pretty
, "trace:vpid");
409 } else if (dom_print
) {
410 bt_common_g_string_append(pretty
->string
, ":");
412 value
= bt_value_integer_signed_get(vpid_value
);
413 bt_common_g_string_append_printf(pretty
->string
,
414 "(%" PRId64
")", value
);
418 if (pretty
->options
.print_loglevel_field
) {
419 static const char *log_level_names
[] = {
420 [ BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
] = "TRACE_EMERG",
421 [ BT_EVENT_CLASS_LOG_LEVEL_ALERT
] = "TRACE_ALERT",
422 [ BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
] = "TRACE_CRIT",
423 [ BT_EVENT_CLASS_LOG_LEVEL_ERROR
] = "TRACE_ERR",
424 [ BT_EVENT_CLASS_LOG_LEVEL_WARNING
] = "TRACE_WARNING",
425 [ BT_EVENT_CLASS_LOG_LEVEL_NOTICE
] = "TRACE_NOTICE",
426 [ BT_EVENT_CLASS_LOG_LEVEL_INFO
] = "TRACE_INFO",
427 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
428 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
429 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
430 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
431 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
432 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
433 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
434 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG
] = "TRACE_DEBUG",
436 bt_event_class_log_level log_level
;
437 const char *log_level_str
= NULL
;
439 prop_avail
= bt_event_class_get_log_level(event_class
,
441 if (prop_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
442 log_level_str
= log_level_names
[log_level
];
443 BT_ASSERT(log_level_str
);
445 if (!pretty
->start_line
) {
446 bt_common_g_string_append(pretty
->string
, ", ");
449 print_name_equal(pretty
, "loglevel");
450 } else if (dom_print
) {
451 bt_common_g_string_append(pretty
->string
, ":");
454 bt_common_g_string_append(pretty
->string
, log_level_str
);
455 bt_common_g_string_append_printf(
456 pretty
->string
, " (%d)", (int) log_level
);
460 if (pretty
->options
.print_emf_field
) {
463 uri_str
= bt_event_class_get_emf_uri(event_class
);
465 if (!pretty
->start_line
) {
466 bt_common_g_string_append(pretty
->string
, ", ");
469 print_name_equal(pretty
, "model.emf.uri");
470 } else if (dom_print
) {
471 bt_common_g_string_append(pretty
->string
, ":");
474 bt_common_g_string_append(pretty
->string
, uri_str
);
478 if (dom_print
&& !print_names
) {
479 bt_common_g_string_append(pretty
->string
, " ");
481 if (!pretty
->start_line
) {
482 bt_common_g_string_append(pretty
->string
, ", ");
484 pretty
->start_line
= true;
486 print_name_equal(pretty
, "name");
488 ev_name
= bt_event_class_get_name(event_class
);
489 if (pretty
->use_colors
) {
491 bt_common_g_string_append(pretty
->string
,
494 bt_common_g_string_append(pretty
->string
,
499 bt_common_g_string_append(pretty
->string
, ev_name
);
501 bt_common_g_string_append(pretty
->string
, "<unknown>");
503 if (pretty
->use_colors
) {
504 bt_common_g_string_append(pretty
->string
, COLOR_RST
);
507 bt_common_g_string_append(pretty
->string
, ": ");
509 bt_common_g_string_append(pretty
->string
, ", ");
517 int print_integer(struct pretty_component
*pretty
,
518 const bt_field
*field
)
521 bt_field_class_integer_preferred_display_base base
;
522 const bt_field_class
*int_fc
;
527 bool rst_color
= false;
528 bt_field_class_type ft_type
;
530 int_fc
= bt_field_borrow_class_const(field
);
532 ft_type
= bt_field_get_class_type(field
);
533 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
534 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
535 v
.u
= bt_field_integer_unsigned_get_value(field
);
537 v
.s
= bt_field_integer_signed_get_value(field
);
540 if (pretty
->use_colors
) {
541 bt_common_g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
545 base
= bt_field_class_integer_get_preferred_display_base(int_fc
);
547 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
551 len
= bt_field_class_integer_get_field_value_range(int_fc
);
552 bt_common_g_string_append(pretty
->string
, "0b");
553 _bt_safe_lshift(v
.u
, 64 - len
);
554 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
555 bt_common_g_string_append_printf(pretty
->string
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
556 _bt_safe_lshift(v
.u
, 1);
560 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
562 if (ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
563 ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
) {
566 len
= bt_field_class_integer_get_field_value_range(
572 /* Round length to the nearest 3-bit */
573 rounded_len
= (((len
- 1) / 3) + 1) * 3;
574 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
578 bt_common_g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
581 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
582 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
583 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
584 bt_common_g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
586 bt_common_g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
589 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
593 len
= bt_field_class_integer_get_field_value_range(int_fc
);
595 /* Round length to the nearest nibble */
596 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
598 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
601 bt_common_g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
610 bt_common_g_string_append(pretty
->string
, COLOR_RST
);
616 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
620 bt_common_g_string_append_c(pretty
->string
, '"');
622 for (i
= 0; i
< strlen(str
); i
++) {
623 /* Escape sequences not recognized by iscntrl(). */
626 bt_common_g_string_append(pretty
->string
, "\\\\");
629 bt_common_g_string_append(pretty
->string
, "\\\'");
632 bt_common_g_string_append(pretty
->string
, "\\\"");
635 bt_common_g_string_append(pretty
->string
, "\\\?");
639 /* Standard characters. */
640 if (!iscntrl(str
[i
])) {
641 bt_common_g_string_append_c(pretty
->string
, str
[i
]);
647 bt_common_g_string_append(pretty
->string
, "\\0");
650 bt_common_g_string_append(pretty
->string
, "\\a");
653 bt_common_g_string_append(pretty
->string
, "\\b");
656 bt_common_g_string_append(pretty
->string
, "\\e");
659 bt_common_g_string_append(pretty
->string
, "\\f");
662 bt_common_g_string_append(pretty
->string
, "\\n");
665 bt_common_g_string_append(pretty
->string
, "\\r");
668 bt_common_g_string_append(pretty
->string
, "\\t");
671 bt_common_g_string_append(pretty
->string
, "\\v");
674 /* Unhandled control-sequence, print as hex. */
675 bt_common_g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
680 bt_common_g_string_append_c(pretty
->string
, '"');
684 int print_enum(struct pretty_component
*pretty
,
685 const bt_field
*field
)
688 const bt_field_class
*enumeration_field_class
= NULL
;
689 bt_field_class_enumeration_mapping_label_array label_array
;
690 uint64_t label_count
;
693 enumeration_field_class
= bt_field_borrow_class_const(field
);
694 if (!enumeration_field_class
) {
699 switch (bt_field_get_class_type(field
)) {
700 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
701 ret
= bt_field_enumeration_unsigned_get_mapping_labels(field
,
702 &label_array
, &label_count
);
704 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
705 ret
= bt_field_enumeration_signed_get_mapping_labels(field
,
706 &label_array
, &label_count
);
717 bt_common_g_string_append(pretty
->string
, "( ");
718 if (label_count
== 0) {
719 if (pretty
->use_colors
) {
720 bt_common_g_string_append(pretty
->string
, COLOR_UNKNOWN
);
722 bt_common_g_string_append(pretty
->string
, "<unknown>");
723 if (pretty
->use_colors
) {
724 bt_common_g_string_append(pretty
->string
, COLOR_RST
);
728 for (i
= 0; i
< label_count
; i
++) {
729 const char *mapping_name
= label_array
[i
];
732 bt_common_g_string_append(pretty
->string
, ", ");
734 if (pretty
->use_colors
) {
735 bt_common_g_string_append(pretty
->string
, COLOR_ENUM_MAPPING_NAME
);
737 print_escape_string(pretty
, mapping_name
);
738 if (pretty
->use_colors
) {
739 bt_common_g_string_append(pretty
->string
, COLOR_RST
);
743 bt_common_g_string_append(pretty
->string
, " : container = ");
744 ret
= print_integer(pretty
, field
);
748 bt_common_g_string_append(pretty
->string
, " )");
754 int print_struct_field(struct pretty_component
*pretty
,
755 const bt_field
*_struct
,
756 const bt_field_class
*struct_class
,
757 uint64_t i
, bool print_names
, uint64_t *nr_printed_fields
)
760 const char *field_name
;
761 const bt_field
*field
= NULL
;
762 const bt_field_class_structure_member
*member
;
764 field
= bt_field_structure_borrow_member_field_by_index_const(_struct
, i
);
770 member
= bt_field_class_structure_borrow_member_by_index_const(
772 field_name
= bt_field_class_structure_member_get_name(member
);
774 if (*nr_printed_fields
> 0) {
775 bt_common_g_string_append(pretty
->string
, ", ");
777 bt_common_g_string_append(pretty
->string
, " ");
780 print_field_name_equal(pretty
, field_name
);
782 ret
= print_field(pretty
, field
, print_names
);
783 *nr_printed_fields
+= 1;
790 int print_struct(struct pretty_component
*pretty
,
791 const bt_field
*_struct
, bool print_names
)
794 const bt_field_class
*struct_class
= NULL
;
795 uint64_t nr_fields
, i
, nr_printed_fields
;
797 struct_class
= bt_field_borrow_class_const(_struct
);
803 nr_fields
= bt_field_class_structure_get_member_count(struct_class
);
805 bt_common_g_string_append(pretty
->string
, "{");
807 nr_printed_fields
= 0;
808 for (i
= 0; i
< nr_fields
; i
++) {
809 ret
= print_struct_field(pretty
, _struct
, struct_class
, i
,
810 print_names
, &nr_printed_fields
);
816 bt_common_g_string_append(pretty
->string
, " }");
823 int print_array_field(struct pretty_component
*pretty
,
824 const bt_field
*array
, uint64_t i
, bool print_names
)
826 const bt_field
*field
= NULL
;
829 bt_common_g_string_append(pretty
->string
, ", ");
831 bt_common_g_string_append(pretty
->string
, " ");
834 bt_common_g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
837 field
= bt_field_array_borrow_element_field_by_index_const(array
, i
);
839 return print_field(pretty
, field
, print_names
);
843 int print_array(struct pretty_component
*pretty
,
844 const bt_field
*array
, bool print_names
)
847 const bt_field_class
*array_class
= NULL
;
851 array_class
= bt_field_borrow_class_const(array
);
856 len
= bt_field_array_get_length(array
);
857 bt_common_g_string_append(pretty
->string
, "[");
859 for (i
= 0; i
< len
; i
++) {
860 ret
= print_array_field(pretty
, array
, i
, print_names
);
866 bt_common_g_string_append(pretty
->string
, " ]");
873 int print_sequence_field(struct pretty_component
*pretty
,
874 const bt_field
*seq
, uint64_t i
, bool print_names
)
876 const bt_field
*field
= NULL
;
879 bt_common_g_string_append(pretty
->string
, ", ");
881 bt_common_g_string_append(pretty
->string
, " ");
884 bt_common_g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
887 field
= bt_field_array_borrow_element_field_by_index_const(seq
, i
);
889 return print_field(pretty
, field
, print_names
);
893 int print_sequence(struct pretty_component
*pretty
,
894 const bt_field
*seq
, bool print_names
)
900 len
= bt_field_array_get_length(seq
);
901 bt_common_g_string_append(pretty
->string
, "[");
904 for (i
= 0; i
< len
; i
++) {
905 ret
= print_sequence_field(pretty
, seq
, i
, print_names
);
911 bt_common_g_string_append(pretty
->string
, " ]");
918 int print_variant(struct pretty_component
*pretty
,
919 const bt_field
*variant
, bool print_names
)
922 const bt_field
*field
= NULL
;
924 field
= bt_field_variant_borrow_selected_option_field_const(variant
);
926 bt_common_g_string_append(pretty
->string
, "{ ");
929 // TODO: find tag's name using field path
930 // print_field_name_equal(pretty, tag_choice);
932 ret
= print_field(pretty
, field
, print_names
);
937 bt_common_g_string_append(pretty
->string
, " }");
944 int print_field(struct pretty_component
*pretty
,
945 const bt_field
*field
, bool print_names
)
947 bt_field_class_type class_id
;
949 class_id
= bt_field_get_class_type(field
);
951 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
952 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
953 return print_integer(pretty
, field
);
954 case BT_FIELD_CLASS_TYPE_REAL
:
958 v
= bt_field_real_get_value(field
);
959 if (pretty
->use_colors
) {
960 bt_common_g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
962 bt_common_g_string_append_printf(pretty
->string
, "%g", v
);
963 if (pretty
->use_colors
) {
964 bt_common_g_string_append(pretty
->string
, COLOR_RST
);
968 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
969 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
970 return print_enum(pretty
, field
);
971 case BT_FIELD_CLASS_TYPE_STRING
:
975 str
= bt_field_string_get_value(field
);
980 if (pretty
->use_colors
) {
981 bt_common_g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
983 print_escape_string(pretty
, str
);
984 if (pretty
->use_colors
) {
985 bt_common_g_string_append(pretty
->string
, COLOR_RST
);
989 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
990 return print_struct(pretty
, field
, print_names
);
991 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
:
992 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
:
993 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
:
994 return print_variant(pretty
, field
, print_names
);
995 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
996 return print_array(pretty
, field
, print_names
);
997 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
998 return print_sequence(pretty
, field
, print_names
);
1000 // TODO: log instead
1001 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) class_id
);
1007 int print_stream_packet_context(struct pretty_component
*pretty
,
1008 const bt_event
*event
)
1011 const bt_packet
*packet
= NULL
;
1012 const bt_field
*main_field
= NULL
;
1014 packet
= bt_event_borrow_packet_const(event
);
1018 main_field
= bt_packet_borrow_context_field_const(packet
);
1022 if (!pretty
->start_line
) {
1023 bt_common_g_string_append(pretty
->string
, ", ");
1025 pretty
->start_line
= false;
1026 if (pretty
->options
.print_scope_field_names
) {
1027 print_name_equal(pretty
, "stream.packet.context");
1029 ret
= print_field(pretty
, main_field
,
1030 pretty
->options
.print_context_field_names
);
1037 int print_stream_event_context(struct pretty_component
*pretty
,
1038 const bt_event
*event
)
1041 const bt_field
*main_field
= NULL
;
1043 main_field
= bt_event_borrow_common_context_field_const(event
);
1047 if (!pretty
->start_line
) {
1048 bt_common_g_string_append(pretty
->string
, ", ");
1050 pretty
->start_line
= false;
1051 if (pretty
->options
.print_scope_field_names
) {
1052 print_name_equal(pretty
, "stream.event.context");
1054 ret
= print_field(pretty
, main_field
,
1055 pretty
->options
.print_context_field_names
);
1062 int print_event_context(struct pretty_component
*pretty
,
1063 const bt_event
*event
)
1066 const bt_field
*main_field
= NULL
;
1068 main_field
= bt_event_borrow_specific_context_field_const(event
);
1072 if (!pretty
->start_line
) {
1073 bt_common_g_string_append(pretty
->string
, ", ");
1075 pretty
->start_line
= false;
1076 if (pretty
->options
.print_scope_field_names
) {
1077 print_name_equal(pretty
, "event.context");
1079 ret
= print_field(pretty
, main_field
,
1080 pretty
->options
.print_context_field_names
);
1087 int print_event_payload(struct pretty_component
*pretty
,
1088 const bt_event
*event
)
1091 const bt_field
*main_field
= NULL
;
1093 main_field
= bt_event_borrow_payload_field_const(event
);
1097 if (!pretty
->start_line
) {
1098 bt_common_g_string_append(pretty
->string
, ", ");
1100 pretty
->start_line
= false;
1101 if (pretty
->options
.print_scope_field_names
) {
1102 print_name_equal(pretty
, "event.fields");
1104 ret
= print_field(pretty
, main_field
,
1105 pretty
->options
.print_payload_field_names
);
1112 int flush_buf(FILE *stream
, struct pretty_component
*pretty
)
1116 if (pretty
->string
->len
== 0) {
1120 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, stream
) != 1) {
1129 int pretty_print_event(struct pretty_component
*pretty
,
1130 const bt_message
*event_msg
)
1133 const bt_event
*event
=
1134 bt_message_event_borrow_event_const(event_msg
);
1137 pretty
->start_line
= true;
1138 g_string_assign(pretty
->string
, "");
1139 ret
= print_event_header(pretty
, event_msg
);
1144 ret
= print_stream_packet_context(pretty
, event
);
1149 ret
= print_stream_event_context(pretty
, event
);
1154 ret
= print_event_context(pretty
, event
);
1159 ret
= print_event_payload(pretty
, event
);
1164 bt_common_g_string_append_c(pretty
->string
, '\n');
1165 if (flush_buf(pretty
->out
, pretty
)) {
1175 int print_discarded_elements_msg(struct pretty_component
*pretty
,
1176 const bt_stream
*stream
,
1177 const bt_clock_snapshot
*begin_clock_snapshot
,
1178 const bt_clock_snapshot
*end_clock_snapshot
,
1179 uint64_t count
, const char *elem_type
)
1182 const bt_stream_class
*stream_class
= NULL
;
1183 const bt_trace
*trace
= NULL
;
1184 const char *stream_name
;
1185 const char *trace_name
;
1187 int64_t stream_class_id
;
1189 const char *init_msg
;
1192 stream_name
= bt_stream_get_name(stream
);
1194 stream_name
= "(unknown)";
1197 /* Stream class ID */
1198 stream_class
= bt_stream_borrow_class_const(stream
);
1199 BT_ASSERT(stream_class
);
1200 stream_class_id
= bt_stream_class_get_id(stream_class
);
1203 stream_id
= bt_stream_get_id(stream
);
1206 trace
= bt_stream_borrow_trace_const(stream
);
1208 trace_name
= bt_trace_get_name(trace
);
1210 trace_name
= "(unknown)";
1214 trace_uuid
= bt_trace_get_uuid(trace
);
1216 /* Format message */
1217 g_string_assign(pretty
->string
, "");
1219 if (count
== UINT64_C(-1)) {
1220 init_msg
= "Tracer may have discarded";
1222 init_msg
= "Tracer discarded";
1225 bt_common_g_string_append_printf(pretty
->string
,
1226 "%s%sWARNING%s%s: %s ",
1227 bt_common_color_fg_yellow(),
1228 bt_common_color_bold(),
1229 bt_common_color_reset(),
1230 bt_common_color_fg_yellow(), init_msg
);
1232 if (count
== UINT64_C(-1)) {
1233 bt_common_g_string_append_printf(pretty
->string
, "%ss", elem_type
);
1235 bt_common_g_string_append_printf(pretty
->string
,
1236 "%" PRIu64
" %s%s", count
, elem_type
,
1237 count
== 1 ? "" : "s");
1240 bt_common_g_string_append_c(pretty
->string
, ' ');
1242 if (begin_clock_snapshot
&& end_clock_snapshot
) {
1243 bt_common_g_string_append(pretty
->string
, "between [");
1244 print_timestamp_wall(pretty
, begin_clock_snapshot
, false);
1245 bt_common_g_string_append(pretty
->string
, "] and [");
1246 print_timestamp_wall(pretty
, end_clock_snapshot
, false);
1247 bt_common_g_string_append(pretty
->string
, "]");
1249 bt_common_g_string_append(pretty
->string
, "(unknown time range)");
1252 bt_common_g_string_append_printf(pretty
->string
, " in trace \"%s\" ", trace_name
);
1255 bt_common_g_string_append_printf(pretty
->string
,
1256 "(UUID: " BT_UUID_FMT
") ",
1257 BT_UUID_FMT_VALUES(trace_uuid
));
1259 bt_common_g_string_append(pretty
->string
, "(no UUID) ");
1262 bt_common_g_string_append_printf(pretty
->string
,
1263 "within stream \"%s\" (stream class ID: %" PRIu64
", ",
1264 stream_name
, stream_class_id
);
1266 if (stream_id
>= 0) {
1267 bt_common_g_string_append_printf(pretty
->string
,
1268 "stream ID: %" PRIu64
, stream_id
);
1270 bt_common_g_string_append(pretty
->string
, "no stream ID");
1273 bt_common_g_string_append_printf(pretty
->string
, ").%s\n",
1274 bt_common_color_reset());
1277 * Print to standard error stream to remain backward compatible
1278 * with Babeltrace 1.
1280 if (flush_buf(stderr
, pretty
)) {
1288 int pretty_print_discarded_items(struct pretty_component
*pretty
,
1289 const bt_message
*msg
)
1291 const bt_clock_snapshot
*begin
= NULL
;
1292 const bt_clock_snapshot
*end
= NULL
;
1293 const bt_stream
*stream
;
1294 const bt_stream_class
*stream_class
;
1295 uint64_t count
= UINT64_C(-1);
1296 const char *elem_type
;
1298 switch (bt_message_get_type(msg
)) {
1299 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1300 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1302 if (bt_message_discarded_events_get_count(msg
, &count
) ==
1303 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1304 count
= UINT64_C(-1);
1307 elem_type
= "event";
1309 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1310 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1312 if (bt_message_discarded_packets_get_count(msg
, &count
) ==
1313 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1314 count
= UINT64_C(-1);
1317 elem_type
= "packet";
1324 stream_class
= bt_stream_borrow_class_const(stream
);
1326 switch (bt_message_get_type(msg
)) {
1327 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1328 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
1330 begin
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1332 end
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1337 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1338 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
1340 begin
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1342 end
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1351 print_discarded_elements_msg(pretty
, stream
, begin
, end
,