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
,
57 GQuark
*filters_fields
, int filter_array_len
);
60 void print_name_equal(struct pretty_component
*pretty
, const char *name
)
62 if (pretty
->use_colors
) {
63 g_string_append(pretty
->string
, COLOR_NAME
);
64 g_string_append(pretty
->string
, name
);
65 g_string_append(pretty
->string
, COLOR_RST
);
67 g_string_append(pretty
->string
, name
);
69 g_string_append(pretty
->string
, " = ");
73 void print_field_name_equal(struct pretty_component
*pretty
, const char *name
)
75 if (pretty
->use_colors
) {
76 g_string_append(pretty
->string
, COLOR_FIELD_NAME
);
77 g_string_append(pretty
->string
, name
);
78 g_string_append(pretty
->string
, COLOR_RST
);
80 g_string_append(pretty
->string
, name
);
82 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 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 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 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 g_string_append(pretty
->string
, "[");
242 if (pretty
->use_colors
) {
243 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 g_string_append(pretty
->string
, COLOR_RST
);
255 g_string_append(pretty
->string
, "] ");
257 if (pretty
->options
.print_delta_field
) {
259 g_string_append(pretty
->string
, ", ");
260 print_name_equal(pretty
, "delta");
262 g_string_append(pretty
->string
, "(");
264 if (pretty
->options
.print_timestamp_cycles
) {
265 if (pretty
->delta_cycles
== -1ULL) {
266 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 g_string_append(pretty
->string
, "+?.?????????");
287 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 g_string_append(pretty
->string
, ", ");
326 print_name_equal(pretty
, "trace");
329 g_string_append(pretty
->string
, name
);
332 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 g_string_append(pretty
->string
, ", ");
348 print_name_equal(pretty
, "trace:hostname");
350 str
= bt_value_string_get(hostname_str
);
351 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 g_string_append(pretty
->string
, ", ");
367 print_name_equal(pretty
, "trace:domain");
368 } else if (dom_print
) {
369 g_string_append(pretty
->string
, ":");
371 str
= bt_value_string_get(domain_str
);
372 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 g_string_append(pretty
->string
, ", ");
388 print_name_equal(pretty
, "trace:procname");
389 } else if (dom_print
) {
390 g_string_append(pretty
->string
, ":");
392 str
= bt_value_string_get(procname_str
);
393 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 g_string_append(pretty
->string
, ", ");
409 print_name_equal(pretty
, "trace:vpid");
410 } else if (dom_print
) {
411 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(log_level_str
);
446 if (!pretty
->start_line
) {
447 g_string_append(pretty
->string
, ", ");
450 print_name_equal(pretty
, "loglevel");
451 } else if (dom_print
) {
452 g_string_append(pretty
->string
, ":");
455 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 g_string_append(pretty
->string
, ", ");
470 print_name_equal(pretty
, "model.emf.uri");
471 } else if (dom_print
) {
472 g_string_append(pretty
->string
, ":");
475 g_string_append(pretty
->string
, uri_str
);
479 if (dom_print
&& !print_names
) {
480 g_string_append(pretty
->string
, " ");
482 if (!pretty
->start_line
) {
483 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 g_string_append(pretty
->string
, COLOR_EVENT_NAME
);
494 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
498 g_string_append(pretty
->string
, ev_name
);
500 g_string_append(pretty
->string
, "<unknown>");
502 if (pretty
->use_colors
) {
503 g_string_append(pretty
->string
, COLOR_RST
);
506 g_string_append(pretty
->string
, ": ");
508 g_string_append(pretty
->string
, ", ");
516 int print_integer(struct pretty_component
*pretty
,
517 const bt_field
*field
)
520 bt_field_class_integer_preferred_display_base base
;
521 const bt_field_class
*int_fc
;
526 bool rst_color
= false;
527 bt_field_class_type ft_type
;
529 int_fc
= bt_field_borrow_class_const(field
);
531 ft_type
= bt_field_get_class_type(field
);
532 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
533 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
534 v
.u
= bt_field_integer_unsigned_get_value(field
);
536 v
.s
= bt_field_integer_signed_get_value(field
);
539 if (pretty
->use_colors
) {
540 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
544 base
= bt_field_class_integer_get_preferred_display_base(int_fc
);
546 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
550 len
= bt_field_class_integer_get_field_value_range(int_fc
);
551 g_string_append(pretty
->string
, "0b");
552 _bt_safe_lshift(v
.u
, 64 - len
);
553 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
554 bt_common_g_string_append_printf(pretty
->string
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
555 _bt_safe_lshift(v
.u
, 1);
559 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
561 if (ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
562 ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
) {
565 len
= bt_field_class_integer_get_field_value_range(
571 /* Round length to the nearest 3-bit */
572 rounded_len
= (((len
- 1) / 3) + 1) * 3;
573 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
577 bt_common_g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
580 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
581 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
582 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
583 bt_common_g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
585 bt_common_g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
588 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
592 len
= bt_field_class_integer_get_field_value_range(int_fc
);
594 /* Round length to the nearest nibble */
595 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
597 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
600 bt_common_g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
609 g_string_append(pretty
->string
, COLOR_RST
);
615 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
619 g_string_append_c(pretty
->string
, '"');
621 for (i
= 0; i
< strlen(str
); i
++) {
622 /* Escape sequences not recognized by iscntrl(). */
625 g_string_append(pretty
->string
, "\\\\");
628 g_string_append(pretty
->string
, "\\\'");
631 g_string_append(pretty
->string
, "\\\"");
634 g_string_append(pretty
->string
, "\\\?");
638 /* Standard characters. */
639 if (!iscntrl(str
[i
])) {
640 g_string_append_c(pretty
->string
, str
[i
]);
646 g_string_append(pretty
->string
, "\\0");
649 g_string_append(pretty
->string
, "\\a");
652 g_string_append(pretty
->string
, "\\b");
655 g_string_append(pretty
->string
, "\\e");
658 g_string_append(pretty
->string
, "\\f");
661 g_string_append(pretty
->string
, "\\n");
664 g_string_append(pretty
->string
, "\\r");
667 g_string_append(pretty
->string
, "\\t");
670 g_string_append(pretty
->string
, "\\v");
673 /* Unhandled control-sequence, print as hex. */
674 bt_common_g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
679 g_string_append_c(pretty
->string
, '"');
683 int print_enum(struct pretty_component
*pretty
,
684 const bt_field
*field
)
687 const bt_field_class
*enumeration_field_class
= NULL
;
688 bt_field_class_enumeration_mapping_label_array label_array
;
689 uint64_t label_count
;
692 enumeration_field_class
= bt_field_borrow_class_const(field
);
693 if (!enumeration_field_class
) {
698 switch (bt_field_get_class_type(field
)) {
699 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
700 ret
= bt_field_enumeration_unsigned_get_mapping_labels(field
,
701 &label_array
, &label_count
);
703 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
704 ret
= bt_field_enumeration_signed_get_mapping_labels(field
,
705 &label_array
, &label_count
);
716 g_string_append(pretty
->string
, "( ");
717 if (label_count
== 0) {
718 if (pretty
->use_colors
) {
719 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
721 g_string_append(pretty
->string
, "<unknown>");
722 if (pretty
->use_colors
) {
723 g_string_append(pretty
->string
, COLOR_RST
);
727 for (i
= 0; i
< label_count
; i
++) {
728 const char *mapping_name
= label_array
[i
];
731 g_string_append(pretty
->string
, ", ");
733 if (pretty
->use_colors
) {
734 g_string_append(pretty
->string
, COLOR_ENUM_MAPPING_NAME
);
736 print_escape_string(pretty
, mapping_name
);
737 if (pretty
->use_colors
) {
738 g_string_append(pretty
->string
, COLOR_RST
);
742 g_string_append(pretty
->string
, " : container = ");
743 ret
= print_integer(pretty
, field
);
747 g_string_append(pretty
->string
, " )");
753 int filter_field_name(struct pretty_component
*pretty
, const char *field_name
,
754 GQuark
*filter_fields
, int filter_array_len
)
757 GQuark field_quark
= g_quark_try_string(field_name
);
759 if (!field_quark
|| pretty
->options
.verbose
) {
763 for (i
= 0; i
< filter_array_len
; i
++) {
764 if (field_quark
== filter_fields
[i
]) {
772 int print_struct_field(struct pretty_component
*pretty
,
773 const bt_field
*_struct
,
774 const bt_field_class
*struct_class
,
775 uint64_t i
, bool print_names
, uint64_t *nr_printed_fields
,
776 GQuark
*filter_fields
, int filter_array_len
)
779 const char *field_name
;
780 const bt_field
*field
= NULL
;
781 const bt_field_class_structure_member
*member
;
783 field
= bt_field_structure_borrow_member_field_by_index_const(_struct
, i
);
789 member
= bt_field_class_structure_borrow_member_by_index_const(
791 field_name
= bt_field_class_structure_member_get_name(member
);
793 if (filter_fields
&& !filter_field_name(pretty
, field_name
,
794 filter_fields
, filter_array_len
)) {
799 if (*nr_printed_fields
> 0) {
800 g_string_append(pretty
->string
, ", ");
802 g_string_append(pretty
->string
, " ");
805 print_field_name_equal(pretty
, field_name
);
807 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
808 *nr_printed_fields
+= 1;
815 int print_struct(struct pretty_component
*pretty
,
816 const bt_field
*_struct
, bool print_names
,
817 GQuark
*filter_fields
, int filter_array_len
)
820 const bt_field_class
*struct_class
= NULL
;
821 uint64_t nr_fields
, i
, nr_printed_fields
;
823 struct_class
= bt_field_borrow_class_const(_struct
);
829 nr_fields
= bt_field_class_structure_get_member_count(struct_class
);
831 g_string_append(pretty
->string
, "{");
833 nr_printed_fields
= 0;
834 for (i
= 0; i
< nr_fields
; i
++) {
835 ret
= print_struct_field(pretty
, _struct
, struct_class
, i
,
836 print_names
, &nr_printed_fields
, filter_fields
,
843 g_string_append(pretty
->string
, " }");
850 int print_array_field(struct pretty_component
*pretty
,
851 const bt_field
*array
, uint64_t i
, bool print_names
)
853 const bt_field
*field
= NULL
;
856 g_string_append(pretty
->string
, ", ");
858 g_string_append(pretty
->string
, " ");
861 bt_common_g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
864 field
= bt_field_array_borrow_element_field_by_index_const(array
, i
);
866 return print_field(pretty
, field
, print_names
, NULL
, 0);
870 int print_array(struct pretty_component
*pretty
,
871 const bt_field
*array
, bool print_names
)
874 const bt_field_class
*array_class
= NULL
;
878 array_class
= bt_field_borrow_class_const(array
);
883 len
= bt_field_array_get_length(array
);
884 g_string_append(pretty
->string
, "[");
886 for (i
= 0; i
< len
; i
++) {
887 ret
= print_array_field(pretty
, array
, i
, print_names
);
893 g_string_append(pretty
->string
, " ]");
900 int print_sequence_field(struct pretty_component
*pretty
,
901 const bt_field
*seq
, uint64_t i
, bool print_names
)
903 const bt_field
*field
= NULL
;
906 g_string_append(pretty
->string
, ", ");
908 g_string_append(pretty
->string
, " ");
911 bt_common_g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
914 field
= bt_field_array_borrow_element_field_by_index_const(seq
, i
);
916 return print_field(pretty
, field
, print_names
, NULL
, 0);
920 int print_sequence(struct pretty_component
*pretty
,
921 const bt_field
*seq
, bool print_names
)
927 len
= bt_field_array_get_length(seq
);
928 g_string_append(pretty
->string
, "[");
931 for (i
= 0; i
< len
; i
++) {
932 ret
= print_sequence_field(pretty
, seq
, i
, print_names
);
938 g_string_append(pretty
->string
, " ]");
945 int print_variant(struct pretty_component
*pretty
,
946 const bt_field
*variant
, bool print_names
)
949 const bt_field
*field
= NULL
;
951 field
= bt_field_variant_borrow_selected_option_field_const(variant
);
953 g_string_append(pretty
->string
, "{ ");
956 // TODO: find tag's name using field path
957 // print_field_name_equal(pretty, tag_choice);
959 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
964 g_string_append(pretty
->string
, " }");
971 int print_field(struct pretty_component
*pretty
,
972 const bt_field
*field
, bool print_names
,
973 GQuark
*filter_fields
, int filter_array_len
)
975 bt_field_class_type class_id
;
977 class_id
= bt_field_get_class_type(field
);
979 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
980 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
981 return print_integer(pretty
, field
);
982 case BT_FIELD_CLASS_TYPE_REAL
:
986 v
= bt_field_real_get_value(field
);
987 if (pretty
->use_colors
) {
988 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
990 bt_common_g_string_append_printf(pretty
->string
, "%g", v
);
991 if (pretty
->use_colors
) {
992 g_string_append(pretty
->string
, COLOR_RST
);
996 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
997 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
998 return print_enum(pretty
, field
);
999 case BT_FIELD_CLASS_TYPE_STRING
:
1003 str
= bt_field_string_get_value(field
);
1008 if (pretty
->use_colors
) {
1009 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1011 print_escape_string(pretty
, str
);
1012 if (pretty
->use_colors
) {
1013 g_string_append(pretty
->string
, COLOR_RST
);
1017 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1018 return print_struct(pretty
, field
, print_names
, filter_fields
,
1020 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
:
1021 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
:
1022 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
:
1023 return print_variant(pretty
, field
, print_names
);
1024 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1025 return print_array(pretty
, field
, print_names
);
1026 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1027 return print_sequence(pretty
, field
, print_names
);
1029 // TODO: log instead
1030 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) class_id
);
1036 int print_stream_packet_context(struct pretty_component
*pretty
,
1037 const bt_event
*event
)
1040 const bt_packet
*packet
= NULL
;
1041 const bt_field
*main_field
= NULL
;
1043 packet
= bt_event_borrow_packet_const(event
);
1047 main_field
= bt_packet_borrow_context_field_const(packet
);
1051 if (!pretty
->start_line
) {
1052 g_string_append(pretty
->string
, ", ");
1054 pretty
->start_line
= false;
1055 if (pretty
->options
.print_scope_field_names
) {
1056 print_name_equal(pretty
, "stream.packet.context");
1058 ret
= print_field(pretty
, main_field
,
1059 pretty
->options
.print_context_field_names
,
1060 stream_packet_context_quarks
,
1061 STREAM_PACKET_CONTEXT_QUARKS_LEN
);
1068 int print_stream_event_context(struct pretty_component
*pretty
,
1069 const bt_event
*event
)
1072 const bt_field
*main_field
= NULL
;
1074 main_field
= bt_event_borrow_common_context_field_const(event
);
1078 if (!pretty
->start_line
) {
1079 g_string_append(pretty
->string
, ", ");
1081 pretty
->start_line
= false;
1082 if (pretty
->options
.print_scope_field_names
) {
1083 print_name_equal(pretty
, "stream.event.context");
1085 ret
= print_field(pretty
, main_field
,
1086 pretty
->options
.print_context_field_names
, NULL
, 0);
1093 int print_event_context(struct pretty_component
*pretty
,
1094 const bt_event
*event
)
1097 const bt_field
*main_field
= NULL
;
1099 main_field
= bt_event_borrow_specific_context_field_const(event
);
1103 if (!pretty
->start_line
) {
1104 g_string_append(pretty
->string
, ", ");
1106 pretty
->start_line
= false;
1107 if (pretty
->options
.print_scope_field_names
) {
1108 print_name_equal(pretty
, "event.context");
1110 ret
= print_field(pretty
, main_field
,
1111 pretty
->options
.print_context_field_names
, NULL
, 0);
1118 int print_event_payload(struct pretty_component
*pretty
,
1119 const bt_event
*event
)
1122 const bt_field
*main_field
= NULL
;
1124 main_field
= bt_event_borrow_payload_field_const(event
);
1128 if (!pretty
->start_line
) {
1129 g_string_append(pretty
->string
, ", ");
1131 pretty
->start_line
= false;
1132 if (pretty
->options
.print_scope_field_names
) {
1133 print_name_equal(pretty
, "event.fields");
1135 ret
= print_field(pretty
, main_field
,
1136 pretty
->options
.print_payload_field_names
, NULL
, 0);
1143 int flush_buf(FILE *stream
, struct pretty_component
*pretty
)
1147 if (pretty
->string
->len
== 0) {
1151 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, stream
) != 1) {
1160 int pretty_print_event(struct pretty_component
*pretty
,
1161 const bt_message
*event_msg
)
1164 const bt_event
*event
=
1165 bt_message_event_borrow_event_const(event_msg
);
1168 pretty
->start_line
= true;
1169 g_string_assign(pretty
->string
, "");
1170 ret
= print_event_header(pretty
, event_msg
);
1175 ret
= print_stream_packet_context(pretty
, event
);
1180 ret
= print_stream_event_context(pretty
, event
);
1185 ret
= print_event_context(pretty
, event
);
1190 ret
= print_event_payload(pretty
, event
);
1195 g_string_append_c(pretty
->string
, '\n');
1196 if (flush_buf(pretty
->out
, pretty
)) {
1206 int print_discarded_elements_msg(struct pretty_component
*pretty
,
1207 const bt_stream
*stream
,
1208 const bt_clock_snapshot
*begin_clock_snapshot
,
1209 const bt_clock_snapshot
*end_clock_snapshot
,
1210 uint64_t count
, const char *elem_type
)
1213 const bt_stream_class
*stream_class
= NULL
;
1214 const bt_trace
*trace
= NULL
;
1215 const char *stream_name
;
1216 const char *trace_name
;
1218 int64_t stream_class_id
;
1220 const char *init_msg
;
1223 stream_name
= bt_stream_get_name(stream
);
1225 stream_name
= "(unknown)";
1228 /* Stream class ID */
1229 stream_class
= bt_stream_borrow_class_const(stream
);
1230 BT_ASSERT(stream_class
);
1231 stream_class_id
= bt_stream_class_get_id(stream_class
);
1234 stream_id
= bt_stream_get_id(stream
);
1237 trace
= bt_stream_borrow_trace_const(stream
);
1239 trace_name
= bt_trace_get_name(trace
);
1241 trace_name
= "(unknown)";
1245 trace_uuid
= bt_trace_get_uuid(trace
);
1247 /* Format message */
1248 g_string_assign(pretty
->string
, "");
1250 if (count
== UINT64_C(-1)) {
1251 init_msg
= "Tracer may have discarded";
1253 init_msg
= "Tracer discarded";
1256 bt_common_g_string_append_printf(pretty
->string
,
1257 "%s%sWARNING%s%s: %s ",
1258 bt_common_color_fg_yellow(),
1259 bt_common_color_bold(),
1260 bt_common_color_reset(),
1261 bt_common_color_fg_yellow(), init_msg
);
1263 if (count
== UINT64_C(-1)) {
1264 bt_common_g_string_append_printf(pretty
->string
, "%ss", elem_type
);
1266 bt_common_g_string_append_printf(pretty
->string
,
1267 "%" PRIu64
" %s%s", count
, elem_type
,
1268 count
== 1 ? "" : "s");
1271 g_string_append_c(pretty
->string
, ' ');
1273 if (begin_clock_snapshot
&& end_clock_snapshot
) {
1274 g_string_append(pretty
->string
, "between [");
1275 print_timestamp_wall(pretty
, begin_clock_snapshot
, false);
1276 g_string_append(pretty
->string
, "] and [");
1277 print_timestamp_wall(pretty
, end_clock_snapshot
, false);
1278 g_string_append(pretty
->string
, "]");
1280 g_string_append(pretty
->string
, "(unknown time range)");
1283 bt_common_g_string_append_printf(pretty
->string
, " in trace \"%s\" ", trace_name
);
1286 bt_common_g_string_append_printf(pretty
->string
,
1287 "(UUID: " BT_UUID_FMT
") ",
1288 BT_UUID_FMT_VALUES(trace_uuid
));
1290 g_string_append(pretty
->string
, "(no UUID) ");
1293 bt_common_g_string_append_printf(pretty
->string
,
1294 "within stream \"%s\" (stream class ID: %" PRIu64
", ",
1295 stream_name
, stream_class_id
);
1297 if (stream_id
>= 0) {
1298 bt_common_g_string_append_printf(pretty
->string
,
1299 "stream ID: %" PRIu64
, stream_id
);
1301 g_string_append(pretty
->string
, "no stream ID");
1304 bt_common_g_string_append_printf(pretty
->string
, ").%s\n",
1305 bt_common_color_reset());
1308 * Print to standard error stream to remain backward compatible
1309 * with Babeltrace 1.
1311 if (flush_buf(stderr
, pretty
)) {
1319 int pretty_print_discarded_items(struct pretty_component
*pretty
,
1320 const bt_message
*msg
)
1322 const bt_clock_snapshot
*begin
= NULL
;
1323 const bt_clock_snapshot
*end
= NULL
;
1324 const bt_stream
*stream
;
1325 const bt_stream_class
*stream_class
;
1326 uint64_t count
= UINT64_C(-1);
1327 const char *elem_type
;
1329 switch (bt_message_get_type(msg
)) {
1330 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1331 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1333 if (bt_message_discarded_events_get_count(msg
, &count
) ==
1334 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1335 count
= UINT64_C(-1);
1338 elem_type
= "event";
1340 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1341 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1343 if (bt_message_discarded_packets_get_count(msg
, &count
) ==
1344 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1345 count
= UINT64_C(-1);
1348 elem_type
= "packet";
1355 stream_class
= bt_stream_borrow_class_const(stream
);
1357 switch (bt_message_get_type(msg
)) {
1358 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1359 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
1361 begin
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1363 end
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1368 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1369 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
1371 begin
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1373 end
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1382 print_discarded_elements_msg(pretty
, stream
, begin
, end
,