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_printf(pretty
->string
, "%s%s%s = ", COLOR_NAME
,
66 g_string_append_printf(pretty
->string
, "%s = ", name
);
71 void print_field_name_equal(struct pretty_component
*pretty
, const char *name
)
73 if (pretty
->use_colors
) {
74 g_string_append_printf(pretty
->string
, "%s%s%s = ",
75 COLOR_FIELD_NAME
, name
, COLOR_RST
);
77 g_string_append_printf(pretty
->string
, "%s = ", name
);
82 void print_timestamp_cycles(struct pretty_component
*pretty
,
83 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
87 cycles
= bt_clock_snapshot_get_value(clock_snapshot
);
88 g_string_append_printf(pretty
->string
, "%020" PRIu64
, cycles
);
91 if (pretty
->last_cycles_timestamp
!= -1ULL) {
92 pretty
->delta_cycles
= cycles
- pretty
->last_cycles_timestamp
;
95 pretty
->last_cycles_timestamp
= cycles
;
100 void print_timestamp_wall(struct pretty_component
*pretty
,
101 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
104 int64_t ts_nsec
= 0; /* add configurable offset */
105 int64_t ts_sec
= 0; /* add configurable offset */
106 uint64_t ts_sec_abs
, ts_nsec_abs
;
109 if (!clock_snapshot
) {
110 g_string_append(pretty
->string
, "??:??:??.?????????");
114 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, &ts_nsec
);
116 // TODO: log, this is unexpected
117 g_string_append(pretty
->string
, "Error");
122 if (pretty
->last_real_timestamp
!= -1ULL) {
123 pretty
->delta_real_timestamp
= ts_nsec
- pretty
->last_real_timestamp
;
126 pretty
->last_real_timestamp
= ts_nsec
;
129 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
130 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
132 if (ts_sec
>= 0 && ts_nsec
>= 0) {
135 ts_nsec_abs
= ts_nsec
;
136 } else if (ts_sec
> 0 && ts_nsec
< 0) {
138 ts_sec_abs
= ts_sec
- 1;
139 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
140 } else if (ts_sec
== 0 && ts_nsec
< 0) {
143 ts_nsec_abs
= -ts_nsec
;
144 } else if (ts_sec
< 0 && ts_nsec
> 0) {
146 ts_sec_abs
= -(ts_sec
+ 1);
147 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
148 } else if (ts_sec
< 0 && ts_nsec
== 0) {
150 ts_sec_abs
= -ts_sec
;
151 ts_nsec_abs
= ts_nsec
;
152 } else { /* (ts_sec < 0 && ts_nsec < 0) */
154 ts_sec_abs
= -ts_sec
;
155 ts_nsec_abs
= -ts_nsec
;
158 if (!pretty
->options
.clock_seconds
) {
160 time_t time_s
= (time_t) ts_sec_abs
;
162 if (is_negative
&& !pretty
->negative_timestamp_warning_done
) {
164 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
165 pretty
->negative_timestamp_warning_done
= true;
169 if (!pretty
->options
.clock_gmt
) {
172 res
= bt_localtime_r(&time_s
, &tm
);
175 fprintf(stderr
, "[warning] Unable to get localtime.\n");
181 res
= bt_gmtime_r(&time_s
, &tm
);
184 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
188 if (pretty
->options
.clock_date
) {
192 /* Print date and time */
193 res
= strftime(timestr
, sizeof(timestr
),
197 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
201 g_string_append(pretty
->string
, timestr
);
204 /* Print time in HH:MM:SS.ns */
205 g_string_append_printf(pretty
->string
,
206 "%02d:%02d:%02d.%09" PRIu64
, tm
.tm_hour
, tm
.tm_min
,
207 tm
.tm_sec
, ts_nsec_abs
);
211 g_string_append_printf(pretty
->string
, "%s%" PRId64
".%09" PRIu64
,
212 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
218 int print_event_timestamp(struct pretty_component
*pretty
,
219 const bt_message
*event_msg
, bool *start_line
)
221 bool print_names
= pretty
->options
.print_header_field_names
;
223 const bt_clock_snapshot
*clock_snapshot
= NULL
;
225 if (!bt_message_event_borrow_stream_class_default_clock_class_const(
227 /* No default clock class: skip the timestamp without an error */
231 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(event_msg
);
234 print_name_equal(pretty
, "timestamp");
236 g_string_append(pretty
->string
, "[");
238 if (pretty
->use_colors
) {
239 g_string_append(pretty
->string
, COLOR_TIMESTAMP
);
241 if (pretty
->options
.print_timestamp_cycles
) {
242 print_timestamp_cycles(pretty
, clock_snapshot
, true);
244 print_timestamp_wall(pretty
, clock_snapshot
, true);
246 if (pretty
->use_colors
) {
247 g_string_append(pretty
->string
, COLOR_RST
);
251 g_string_append(pretty
->string
, "] ");
253 if (pretty
->options
.print_delta_field
) {
255 g_string_append(pretty
->string
, ", ");
256 print_name_equal(pretty
, "delta");
258 g_string_append(pretty
->string
, "(");
260 if (pretty
->options
.print_timestamp_cycles
) {
261 if (pretty
->delta_cycles
== -1ULL) {
262 g_string_append(pretty
->string
,
263 "+??????????\?\?"); /* Not a trigraph. */
265 g_string_append_printf(pretty
->string
,
266 "+%012" PRIu64
, pretty
->delta_cycles
);
269 if (pretty
->delta_real_timestamp
!= -1ULL) {
270 uint64_t delta_sec
, delta_nsec
, delta
;
272 delta
= pretty
->delta_real_timestamp
;
273 delta_sec
= delta
/ NSEC_PER_SEC
;
274 delta_nsec
= delta
% NSEC_PER_SEC
;
275 g_string_append_printf(pretty
->string
,
276 "+%" PRIu64
".%09" PRIu64
,
277 delta_sec
, delta_nsec
);
279 g_string_append(pretty
->string
, "+?.?????????");
283 g_string_append(pretty
->string
, ") ");
286 *start_line
= !print_names
;
293 int print_event_header(struct pretty_component
*pretty
,
294 const bt_message
*event_msg
)
296 bool print_names
= pretty
->options
.print_header_field_names
;
298 const bt_event_class
*event_class
= NULL
;
299 const bt_stream
*stream
= NULL
;
300 const bt_trace
*trace
= NULL
;
301 const bt_event
*event
= bt_message_event_borrow_event_const(event_msg
);
304 bt_property_availability prop_avail
;
306 event_class
= bt_event_borrow_class_const(event
);
307 stream
= bt_event_borrow_stream_const(event
);
308 trace
= bt_stream_borrow_trace_const(stream
);
309 ret
= print_event_timestamp(pretty
, event_msg
, &pretty
->start_line
);
313 if (pretty
->options
.print_trace_field
) {
316 name
= bt_trace_get_name(trace
);
318 if (!pretty
->start_line
) {
319 g_string_append(pretty
->string
, ", ");
322 print_name_equal(pretty
, "trace");
325 g_string_append(pretty
->string
, name
);
328 g_string_append(pretty
->string
, ", ");
332 if (pretty
->options
.print_trace_hostname_field
) {
333 const bt_value
*hostname_str
;
335 hostname_str
= bt_trace_borrow_environment_entry_value_by_name_const(
340 if (!pretty
->start_line
) {
341 g_string_append(pretty
->string
, ", ");
344 print_name_equal(pretty
, "trace:hostname");
346 str
= bt_value_string_get(hostname_str
);
347 g_string_append(pretty
->string
, str
);
351 if (pretty
->options
.print_trace_domain_field
) {
352 const bt_value
*domain_str
;
354 domain_str
= bt_trace_borrow_environment_entry_value_by_name_const(
359 if (!pretty
->start_line
) {
360 g_string_append(pretty
->string
, ", ");
363 print_name_equal(pretty
, "trace:domain");
364 } else if (dom_print
) {
365 g_string_append(pretty
->string
, ":");
367 str
= bt_value_string_get(domain_str
);
368 g_string_append(pretty
->string
, str
);
372 if (pretty
->options
.print_trace_procname_field
) {
373 const bt_value
*procname_str
;
375 procname_str
= bt_trace_borrow_environment_entry_value_by_name_const(
380 if (!pretty
->start_line
) {
381 g_string_append(pretty
->string
, ", ");
384 print_name_equal(pretty
, "trace:procname");
385 } else if (dom_print
) {
386 g_string_append(pretty
->string
, ":");
388 str
= bt_value_string_get(procname_str
);
389 g_string_append(pretty
->string
, str
);
393 if (pretty
->options
.print_trace_vpid_field
) {
394 const bt_value
*vpid_value
;
396 vpid_value
= bt_trace_borrow_environment_entry_value_by_name_const(
401 if (!pretty
->start_line
) {
402 g_string_append(pretty
->string
, ", ");
405 print_name_equal(pretty
, "trace:vpid");
406 } else if (dom_print
) {
407 g_string_append(pretty
->string
, ":");
409 value
= bt_value_signed_integer_get(vpid_value
);
410 g_string_append_printf(pretty
->string
,
411 "(%" PRId64
")", value
);
415 if (pretty
->options
.print_loglevel_field
) {
416 static const char *log_level_names
[] = {
417 [ BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
] = "TRACE_EMERG",
418 [ BT_EVENT_CLASS_LOG_LEVEL_ALERT
] = "TRACE_ALERT",
419 [ BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
] = "TRACE_CRIT",
420 [ BT_EVENT_CLASS_LOG_LEVEL_ERROR
] = "TRACE_ERR",
421 [ BT_EVENT_CLASS_LOG_LEVEL_WARNING
] = "TRACE_WARNING",
422 [ BT_EVENT_CLASS_LOG_LEVEL_NOTICE
] = "TRACE_NOTICE",
423 [ BT_EVENT_CLASS_LOG_LEVEL_INFO
] = "TRACE_INFO",
424 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
425 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
426 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
427 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
428 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
429 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
430 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
431 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG
] = "TRACE_DEBUG",
433 bt_event_class_log_level log_level
;
434 const char *log_level_str
= NULL
;
436 prop_avail
= bt_event_class_get_log_level(event_class
,
438 if (prop_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
439 log_level_str
= log_level_names
[log_level
];
440 BT_ASSERT(log_level_str
);
442 if (!pretty
->start_line
) {
443 g_string_append(pretty
->string
, ", ");
446 print_name_equal(pretty
, "loglevel");
447 } else if (dom_print
) {
448 g_string_append(pretty
->string
, ":");
451 g_string_append(pretty
->string
, log_level_str
);
452 g_string_append_printf(
453 pretty
->string
, " (%d)", (int) log_level
);
457 if (pretty
->options
.print_emf_field
) {
460 uri_str
= bt_event_class_get_emf_uri(event_class
);
462 if (!pretty
->start_line
) {
463 g_string_append(pretty
->string
, ", ");
466 print_name_equal(pretty
, "model.emf.uri");
467 } else if (dom_print
) {
468 g_string_append(pretty
->string
, ":");
471 g_string_append(pretty
->string
, uri_str
);
475 if (dom_print
&& !print_names
) {
476 g_string_append(pretty
->string
, " ");
478 if (!pretty
->start_line
) {
479 g_string_append(pretty
->string
, ", ");
481 pretty
->start_line
= true;
483 print_name_equal(pretty
, "name");
485 ev_name
= bt_event_class_get_name(event_class
);
486 if (pretty
->use_colors
) {
488 g_string_append(pretty
->string
, COLOR_EVENT_NAME
);
490 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
494 g_string_append(pretty
->string
, ev_name
);
496 g_string_append(pretty
->string
, "<unknown>");
498 if (pretty
->use_colors
) {
499 g_string_append(pretty
->string
, COLOR_RST
);
502 g_string_append(pretty
->string
, ": ");
504 g_string_append(pretty
->string
, ", ");
512 int print_integer(struct pretty_component
*pretty
,
513 const bt_field
*field
)
516 bt_field_class_integer_preferred_display_base base
;
517 const bt_field_class
*int_fc
;
522 bool rst_color
= false;
523 bt_field_class_type ft_type
;
525 int_fc
= bt_field_borrow_class_const(field
);
527 ft_type
= bt_field_get_class_type(field
);
528 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
529 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
530 v
.u
= bt_field_unsigned_integer_get_value(field
);
532 v
.s
= bt_field_signed_integer_get_value(field
);
535 if (pretty
->use_colors
) {
536 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
540 base
= bt_field_class_integer_get_preferred_display_base(int_fc
);
542 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
546 len
= bt_field_class_integer_get_field_value_range(int_fc
);
547 g_string_append(pretty
->string
, "0b");
548 _bt_safe_lshift(v
.u
, 64 - len
);
549 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
550 g_string_append_printf(pretty
->string
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
551 _bt_safe_lshift(v
.u
, 1);
555 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
557 if (ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
558 ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
) {
561 len
= bt_field_class_integer_get_field_value_range(
567 /* Round length to the nearest 3-bit */
568 rounded_len
= (((len
- 1) / 3) + 1) * 3;
569 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
573 g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
576 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
577 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
578 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
579 g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
581 g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
584 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
588 len
= bt_field_class_integer_get_field_value_range(int_fc
);
590 /* Round length to the nearest nibble */
591 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
593 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
596 g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
605 g_string_append(pretty
->string
, COLOR_RST
);
611 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
615 g_string_append_c(pretty
->string
, '"');
617 for (i
= 0; i
< strlen(str
); i
++) {
618 /* Escape sequences not recognized by iscntrl(). */
621 g_string_append(pretty
->string
, "\\\\");
624 g_string_append(pretty
->string
, "\\\'");
627 g_string_append(pretty
->string
, "\\\"");
630 g_string_append(pretty
->string
, "\\\?");
634 /* Standard characters. */
635 if (!iscntrl(str
[i
])) {
636 g_string_append_c(pretty
->string
, str
[i
]);
642 g_string_append(pretty
->string
, "\\0");
645 g_string_append(pretty
->string
, "\\a");
648 g_string_append(pretty
->string
, "\\b");
651 g_string_append(pretty
->string
, "\\e");
654 g_string_append(pretty
->string
, "\\f");
657 g_string_append(pretty
->string
, "\\n");
660 g_string_append(pretty
->string
, "\\r");
663 g_string_append(pretty
->string
, "\\t");
666 g_string_append(pretty
->string
, "\\v");
669 /* Unhandled control-sequence, print as hex. */
670 g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
675 g_string_append_c(pretty
->string
, '"');
679 int print_enum(struct pretty_component
*pretty
,
680 const bt_field
*field
)
683 const bt_field_class
*enumeration_field_class
= NULL
;
684 bt_field_class_enumeration_mapping_label_array label_array
;
685 uint64_t label_count
;
688 enumeration_field_class
= bt_field_borrow_class_const(field
);
689 if (!enumeration_field_class
) {
694 switch (bt_field_get_class_type(field
)) {
695 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
696 ret
= bt_field_unsigned_enumeration_get_mapping_labels(field
,
697 &label_array
, &label_count
);
699 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
700 ret
= bt_field_signed_enumeration_get_mapping_labels(field
,
701 &label_array
, &label_count
);
712 g_string_append(pretty
->string
, "( ");
713 if (label_count
== 0) {
714 if (pretty
->use_colors
) {
715 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
717 g_string_append(pretty
->string
, "<unknown>");
718 if (pretty
->use_colors
) {
719 g_string_append(pretty
->string
, COLOR_RST
);
723 for (i
= 0; i
< label_count
; i
++) {
724 const char *mapping_name
= label_array
[i
];
727 g_string_append(pretty
->string
, ", ");
729 if (pretty
->use_colors
) {
730 g_string_append(pretty
->string
, COLOR_ENUM_MAPPING_NAME
);
732 print_escape_string(pretty
, mapping_name
);
733 if (pretty
->use_colors
) {
734 g_string_append(pretty
->string
, COLOR_RST
);
738 g_string_append(pretty
->string
, " : container = ");
739 ret
= print_integer(pretty
, field
);
743 g_string_append(pretty
->string
, " )");
749 int filter_field_name(struct pretty_component
*pretty
, const char *field_name
,
750 GQuark
*filter_fields
, int filter_array_len
)
753 GQuark field_quark
= g_quark_try_string(field_name
);
755 if (!field_quark
|| pretty
->options
.verbose
) {
759 for (i
= 0; i
< filter_array_len
; i
++) {
760 if (field_quark
== filter_fields
[i
]) {
768 int print_struct_field(struct pretty_component
*pretty
,
769 const bt_field
*_struct
,
770 const bt_field_class
*struct_class
,
771 uint64_t i
, bool print_names
, uint64_t *nr_printed_fields
,
772 GQuark
*filter_fields
, int filter_array_len
)
775 const char *field_name
;
776 const bt_field
*field
= NULL
;
777 const bt_field_class_structure_member
*member
;
779 field
= bt_field_structure_borrow_member_field_by_index_const(_struct
, i
);
785 member
= bt_field_class_structure_borrow_member_by_index_const(
787 field_name
= bt_field_class_structure_member_get_name(member
);
789 if (filter_fields
&& !filter_field_name(pretty
, field_name
,
790 filter_fields
, filter_array_len
)) {
795 if (*nr_printed_fields
> 0) {
796 g_string_append(pretty
->string
, ", ");
798 g_string_append(pretty
->string
, " ");
801 print_field_name_equal(pretty
, field_name
);
803 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
804 *nr_printed_fields
+= 1;
811 int print_struct(struct pretty_component
*pretty
,
812 const bt_field
*_struct
, bool print_names
,
813 GQuark
*filter_fields
, int filter_array_len
)
816 const bt_field_class
*struct_class
= NULL
;
817 uint64_t nr_fields
, i
, nr_printed_fields
;
819 struct_class
= bt_field_borrow_class_const(_struct
);
825 nr_fields
= bt_field_class_structure_get_member_count(struct_class
);
827 g_string_append(pretty
->string
, "{");
829 nr_printed_fields
= 0;
830 for (i
= 0; i
< nr_fields
; i
++) {
831 ret
= print_struct_field(pretty
, _struct
, struct_class
, i
,
832 print_names
, &nr_printed_fields
, filter_fields
,
839 g_string_append(pretty
->string
, " }");
846 int print_array_field(struct pretty_component
*pretty
,
847 const bt_field
*array
, uint64_t i
, bool print_names
)
849 const bt_field
*field
= NULL
;
852 g_string_append(pretty
->string
, ", ");
854 g_string_append(pretty
->string
, " ");
857 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
860 field
= bt_field_array_borrow_element_field_by_index_const(array
, i
);
862 return print_field(pretty
, field
, print_names
, NULL
, 0);
866 int print_array(struct pretty_component
*pretty
,
867 const bt_field
*array
, bool print_names
)
870 const bt_field_class
*array_class
= NULL
;
874 array_class
= bt_field_borrow_class_const(array
);
879 len
= bt_field_array_get_length(array
);
880 g_string_append(pretty
->string
, "[");
882 for (i
= 0; i
< len
; i
++) {
883 ret
= print_array_field(pretty
, array
, i
, print_names
);
889 g_string_append(pretty
->string
, " ]");
896 int print_sequence_field(struct pretty_component
*pretty
,
897 const bt_field
*seq
, uint64_t i
, bool print_names
)
899 const bt_field
*field
= NULL
;
902 g_string_append(pretty
->string
, ", ");
904 g_string_append(pretty
->string
, " ");
907 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
910 field
= bt_field_array_borrow_element_field_by_index_const(seq
, i
);
912 return print_field(pretty
, field
, print_names
, NULL
, 0);
916 int print_sequence(struct pretty_component
*pretty
,
917 const bt_field
*seq
, bool print_names
)
923 len
= bt_field_array_get_length(seq
);
924 g_string_append(pretty
->string
, "[");
927 for (i
= 0; i
< len
; i
++) {
928 ret
= print_sequence_field(pretty
, seq
, i
, print_names
);
934 g_string_append(pretty
->string
, " ]");
941 int print_variant(struct pretty_component
*pretty
,
942 const bt_field
*variant
, bool print_names
)
945 const bt_field
*field
= NULL
;
947 field
= bt_field_variant_borrow_selected_option_field_const(variant
);
949 g_string_append(pretty
->string
, "{ ");
952 // TODO: find tag's name using field path
953 // print_field_name_equal(pretty, tag_choice);
955 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
960 g_string_append(pretty
->string
, " }");
967 int print_field(struct pretty_component
*pretty
,
968 const bt_field
*field
, bool print_names
,
969 GQuark
*filter_fields
, int filter_array_len
)
971 bt_field_class_type class_id
;
973 class_id
= bt_field_get_class_type(field
);
975 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
976 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
977 return print_integer(pretty
, field
);
978 case BT_FIELD_CLASS_TYPE_REAL
:
982 v
= bt_field_real_get_value(field
);
983 if (pretty
->use_colors
) {
984 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
986 g_string_append_printf(pretty
->string
, "%g", v
);
987 if (pretty
->use_colors
) {
988 g_string_append(pretty
->string
, COLOR_RST
);
992 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
993 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
994 return print_enum(pretty
, field
);
995 case BT_FIELD_CLASS_TYPE_STRING
:
999 str
= bt_field_string_get_value(field
);
1004 if (pretty
->use_colors
) {
1005 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1007 print_escape_string(pretty
, str
);
1008 if (pretty
->use_colors
) {
1009 g_string_append(pretty
->string
, COLOR_RST
);
1013 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1014 return print_struct(pretty
, field
, print_names
, filter_fields
,
1016 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
:
1017 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
:
1018 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
:
1019 return print_variant(pretty
, field
, print_names
);
1020 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1021 return print_array(pretty
, field
, print_names
);
1022 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1023 return print_sequence(pretty
, field
, print_names
);
1025 // TODO: log instead
1026 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) class_id
);
1032 int print_stream_packet_context(struct pretty_component
*pretty
,
1033 const bt_event
*event
)
1036 const bt_packet
*packet
= NULL
;
1037 const bt_field
*main_field
= NULL
;
1039 packet
= bt_event_borrow_packet_const(event
);
1043 main_field
= bt_packet_borrow_context_field_const(packet
);
1047 if (!pretty
->start_line
) {
1048 g_string_append(pretty
->string
, ", ");
1050 pretty
->start_line
= false;
1051 if (pretty
->options
.print_scope_field_names
) {
1052 print_name_equal(pretty
, "stream.packet.context");
1054 ret
= print_field(pretty
, main_field
,
1055 pretty
->options
.print_context_field_names
,
1056 stream_packet_context_quarks
,
1057 STREAM_PACKET_CONTEXT_QUARKS_LEN
);
1064 int print_stream_event_context(struct pretty_component
*pretty
,
1065 const bt_event
*event
)
1068 const bt_field
*main_field
= NULL
;
1070 main_field
= bt_event_borrow_common_context_field_const(event
);
1074 if (!pretty
->start_line
) {
1075 g_string_append(pretty
->string
, ", ");
1077 pretty
->start_line
= false;
1078 if (pretty
->options
.print_scope_field_names
) {
1079 print_name_equal(pretty
, "stream.event.context");
1081 ret
= print_field(pretty
, main_field
,
1082 pretty
->options
.print_context_field_names
, NULL
, 0);
1089 int print_event_context(struct pretty_component
*pretty
,
1090 const bt_event
*event
)
1093 const bt_field
*main_field
= NULL
;
1095 main_field
= bt_event_borrow_specific_context_field_const(event
);
1099 if (!pretty
->start_line
) {
1100 g_string_append(pretty
->string
, ", ");
1102 pretty
->start_line
= false;
1103 if (pretty
->options
.print_scope_field_names
) {
1104 print_name_equal(pretty
, "event.context");
1106 ret
= print_field(pretty
, main_field
,
1107 pretty
->options
.print_context_field_names
, NULL
, 0);
1114 int print_event_payload(struct pretty_component
*pretty
,
1115 const bt_event
*event
)
1118 const bt_field
*main_field
= NULL
;
1120 main_field
= bt_event_borrow_payload_field_const(event
);
1124 if (!pretty
->start_line
) {
1125 g_string_append(pretty
->string
, ", ");
1127 pretty
->start_line
= false;
1128 if (pretty
->options
.print_scope_field_names
) {
1129 print_name_equal(pretty
, "event.fields");
1131 ret
= print_field(pretty
, main_field
,
1132 pretty
->options
.print_payload_field_names
, NULL
, 0);
1139 int flush_buf(FILE *stream
, struct pretty_component
*pretty
)
1143 if (pretty
->string
->len
== 0) {
1147 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, stream
) != 1) {
1156 int pretty_print_event(struct pretty_component
*pretty
,
1157 const bt_message
*event_msg
)
1160 const bt_event
*event
=
1161 bt_message_event_borrow_event_const(event_msg
);
1164 pretty
->start_line
= true;
1165 g_string_assign(pretty
->string
, "");
1166 ret
= print_event_header(pretty
, event_msg
);
1171 ret
= print_stream_packet_context(pretty
, event
);
1176 ret
= print_stream_event_context(pretty
, event
);
1181 ret
= print_event_context(pretty
, event
);
1186 ret
= print_event_payload(pretty
, event
);
1191 g_string_append_c(pretty
->string
, '\n');
1192 if (flush_buf(pretty
->out
, pretty
)) {
1202 int print_discarded_elements_msg(struct pretty_component
*pretty
,
1203 const bt_stream
*stream
,
1204 const bt_clock_snapshot
*begin_clock_snapshot
,
1205 const bt_clock_snapshot
*end_clock_snapshot
,
1206 uint64_t count
, const char *elem_type
)
1209 const bt_stream_class
*stream_class
= NULL
;
1210 const bt_trace
*trace
= NULL
;
1211 const char *stream_name
;
1212 const char *trace_name
;
1214 int64_t stream_class_id
;
1216 const char *init_msg
;
1219 stream_name
= bt_stream_get_name(stream
);
1221 stream_name
= "(unknown)";
1224 /* Stream class ID */
1225 stream_class
= bt_stream_borrow_class_const(stream
);
1226 BT_ASSERT(stream_class
);
1227 stream_class_id
= bt_stream_class_get_id(stream_class
);
1230 stream_id
= bt_stream_get_id(stream
);
1233 trace
= bt_stream_borrow_trace_const(stream
);
1235 trace_name
= bt_trace_get_name(trace
);
1237 trace_name
= "(unknown)";
1241 trace_uuid
= bt_trace_get_uuid(trace
);
1243 /* Format message */
1244 g_string_assign(pretty
->string
, "");
1246 if (count
== UINT64_C(-1)) {
1247 init_msg
= "Tracer may have discarded";
1249 init_msg
= "Tracer discarded";
1252 g_string_append_printf(pretty
->string
,
1253 "%s%sWARNING%s%s: %s ",
1254 bt_common_color_fg_yellow(),
1255 bt_common_color_bold(),
1256 bt_common_color_reset(),
1257 bt_common_color_fg_yellow(), init_msg
);
1259 if (count
== UINT64_C(-1)) {
1260 g_string_append_printf(pretty
->string
, "%ss", elem_type
);
1262 g_string_append_printf(pretty
->string
,
1263 "%" PRIu64
" %s%s", count
, elem_type
,
1264 count
== 1 ? "" : "s");
1267 g_string_append_c(pretty
->string
, ' ');
1269 if (begin_clock_snapshot
&& end_clock_snapshot
) {
1270 g_string_append(pretty
->string
, "between [");
1271 print_timestamp_wall(pretty
, begin_clock_snapshot
, false);
1272 g_string_append(pretty
->string
, "] and [");
1273 print_timestamp_wall(pretty
, end_clock_snapshot
, false);
1274 g_string_append(pretty
->string
, "]");
1276 g_string_append(pretty
->string
, "(unknown time range)");
1279 g_string_append_printf(pretty
->string
, " in trace \"%s\" ", trace_name
);
1282 g_string_append_printf(pretty
->string
,
1283 "(UUID: " BT_UUID_FMT
") ",
1284 BT_UUID_FMT_VALUES(trace_uuid
));
1286 g_string_append(pretty
->string
, "(no UUID) ");
1289 g_string_append_printf(pretty
->string
,
1290 "within stream \"%s\" (stream class ID: %" PRIu64
", ",
1291 stream_name
, stream_class_id
);
1293 if (stream_id
>= 0) {
1294 g_string_append_printf(pretty
->string
,
1295 "stream ID: %" PRIu64
, stream_id
);
1297 g_string_append(pretty
->string
, "no stream ID");
1300 g_string_append_printf(pretty
->string
, ").%s\n",
1301 bt_common_color_reset());
1304 * Print to standard error stream to remain backward compatible
1305 * with Babeltrace 1.
1307 if (flush_buf(stderr
, pretty
)) {
1315 int pretty_print_discarded_items(struct pretty_component
*pretty
,
1316 const bt_message
*msg
)
1318 const bt_clock_snapshot
*begin
= NULL
;
1319 const bt_clock_snapshot
*end
= NULL
;
1320 const bt_stream
*stream
;
1321 const bt_stream_class
*stream_class
;
1322 uint64_t count
= UINT64_C(-1);
1323 const char *elem_type
;
1325 switch (bt_message_get_type(msg
)) {
1326 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1327 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1329 if (bt_message_discarded_events_get_count(msg
, &count
) ==
1330 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1331 count
= UINT64_C(-1);
1334 elem_type
= "event";
1336 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1337 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1339 if (bt_message_discarded_packets_get_count(msg
, &count
) ==
1340 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1341 count
= UINT64_C(-1);
1344 elem_type
= "packet";
1351 stream_class
= bt_stream_borrow_class_const(stream
);
1353 switch (bt_message_get_type(msg
)) {
1354 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1355 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
1357 begin
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1359 end
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1364 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1365 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
1367 begin
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1369 end
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1378 print_discarded_elements_msg(pretty
, stream
, begin
, end
,