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 <babeltrace/babeltrace.h>
27 #include <babeltrace/bitfield-internal.h>
28 #include <babeltrace/common-internal.h>
29 #include <babeltrace/compat/time-internal.h>
30 #include <babeltrace/assert-internal.h>
35 #define NSEC_PER_SEC 1000000000LL
37 #define COLOR_NAME BT_COMMON_COLOR_BOLD
38 #define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN
39 #define COLOR_RST BT_COMMON_COLOR_RESET
40 #define COLOR_STRING_VALUE BT_COMMON_COLOR_BOLD
41 #define COLOR_NUMBER_VALUE BT_COMMON_COLOR_BOLD
42 #define COLOR_ENUM_MAPPING_NAME BT_COMMON_COLOR_BOLD
43 #define COLOR_UNKNOWN BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED
44 #define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA
45 #define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW
48 int64_t real_timestamp
; /* Relative to UNIX epoch. */
49 uint64_t clock_snapshot
; /* In cycles. */
53 int print_field(struct pretty_component
*pretty
,
54 const bt_field
*field
, bool print_names
,
55 GQuark
*filters_fields
, int filter_array_len
);
58 void print_name_equal(struct pretty_component
*pretty
, const char *name
)
60 if (pretty
->use_colors
) {
61 g_string_append_printf(pretty
->string
, "%s%s%s = ", COLOR_NAME
,
64 g_string_append_printf(pretty
->string
, "%s = ", name
);
69 void print_field_name_equal(struct pretty_component
*pretty
, const char *name
)
71 if (pretty
->use_colors
) {
72 g_string_append_printf(pretty
->string
, "%s%s%s = ",
73 COLOR_FIELD_NAME
, name
, COLOR_RST
);
75 g_string_append_printf(pretty
->string
, "%s = ", name
);
80 void print_timestamp_cycles(struct pretty_component
*pretty
,
81 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
85 cycles
= bt_clock_snapshot_get_value(clock_snapshot
);
86 g_string_append_printf(pretty
->string
, "%020" PRIu64
, cycles
);
89 if (pretty
->last_cycles_timestamp
!= -1ULL) {
90 pretty
->delta_cycles
= cycles
- pretty
->last_cycles_timestamp
;
93 pretty
->last_cycles_timestamp
= cycles
;
98 void print_timestamp_wall(struct pretty_component
*pretty
,
99 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
102 int64_t ts_nsec
= 0; /* add configurable offset */
103 int64_t ts_sec
= 0; /* add configurable offset */
104 uint64_t ts_sec_abs
, ts_nsec_abs
;
107 if (!clock_snapshot
) {
108 g_string_append(pretty
->string
, "??:??:??.?????????");
112 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, &ts_nsec
);
114 // TODO: log, this is unexpected
115 g_string_append(pretty
->string
, "Error");
120 if (pretty
->last_real_timestamp
!= -1ULL) {
121 pretty
->delta_real_timestamp
= ts_nsec
- pretty
->last_real_timestamp
;
124 pretty
->last_real_timestamp
= ts_nsec
;
127 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
128 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
130 if (ts_sec
>= 0 && ts_nsec
>= 0) {
133 ts_nsec_abs
= ts_nsec
;
134 } else if (ts_sec
> 0 && ts_nsec
< 0) {
136 ts_sec_abs
= ts_sec
- 1;
137 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
138 } else if (ts_sec
== 0 && ts_nsec
< 0) {
141 ts_nsec_abs
= -ts_nsec
;
142 } else if (ts_sec
< 0 && ts_nsec
> 0) {
144 ts_sec_abs
= -(ts_sec
+ 1);
145 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
146 } else if (ts_sec
< 0 && ts_nsec
== 0) {
148 ts_sec_abs
= -ts_sec
;
149 ts_nsec_abs
= ts_nsec
;
150 } else { /* (ts_sec < 0 && ts_nsec < 0) */
152 ts_sec_abs
= -ts_sec
;
153 ts_nsec_abs
= -ts_nsec
;
156 if (!pretty
->options
.clock_seconds
) {
158 time_t time_s
= (time_t) ts_sec_abs
;
160 if (is_negative
&& !pretty
->negative_timestamp_warning_done
) {
162 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
163 pretty
->negative_timestamp_warning_done
= true;
167 if (!pretty
->options
.clock_gmt
) {
170 res
= bt_localtime_r(&time_s
, &tm
);
173 fprintf(stderr
, "[warning] Unable to get localtime.\n");
179 res
= bt_gmtime_r(&time_s
, &tm
);
182 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
186 if (pretty
->options
.clock_date
) {
190 /* Print date and time */
191 res
= strftime(timestr
, sizeof(timestr
),
195 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
199 g_string_append(pretty
->string
, timestr
);
202 /* Print time in HH:MM:SS.ns */
203 g_string_append_printf(pretty
->string
,
204 "%02d:%02d:%02d.%09" PRIu64
, tm
.tm_hour
, tm
.tm_min
,
205 tm
.tm_sec
, ts_nsec_abs
);
209 g_string_append_printf(pretty
->string
, "%s%" PRId64
".%09" PRIu64
,
210 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
216 int print_event_timestamp(struct pretty_component
*pretty
,
217 const bt_message
*event_msg
, bool *start_line
)
219 bool print_names
= pretty
->options
.print_header_field_names
;
221 const bt_clock_snapshot
*clock_snapshot
= NULL
;
223 if (!bt_message_event_borrow_stream_class_default_clock_class_const(
225 /* No default clock class: skip the timestamp without an error */
229 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(event_msg
);
232 print_name_equal(pretty
, "timestamp");
234 g_string_append(pretty
->string
, "[");
236 if (pretty
->use_colors
) {
237 g_string_append(pretty
->string
, COLOR_TIMESTAMP
);
239 if (pretty
->options
.print_timestamp_cycles
) {
240 print_timestamp_cycles(pretty
, clock_snapshot
, true);
242 print_timestamp_wall(pretty
, clock_snapshot
, true);
244 if (pretty
->use_colors
) {
245 g_string_append(pretty
->string
, COLOR_RST
);
249 g_string_append(pretty
->string
, "] ");
251 if (pretty
->options
.print_delta_field
) {
253 g_string_append(pretty
->string
, ", ");
254 print_name_equal(pretty
, "delta");
256 g_string_append(pretty
->string
, "(");
258 if (pretty
->options
.print_timestamp_cycles
) {
259 if (pretty
->delta_cycles
== -1ULL) {
260 g_string_append(pretty
->string
,
261 "+??????????\?\?"); /* Not a trigraph. */
263 g_string_append_printf(pretty
->string
,
264 "+%012" PRIu64
, pretty
->delta_cycles
);
267 if (pretty
->delta_real_timestamp
!= -1ULL) {
268 uint64_t delta_sec
, delta_nsec
, delta
;
270 delta
= pretty
->delta_real_timestamp
;
271 delta_sec
= delta
/ NSEC_PER_SEC
;
272 delta_nsec
= delta
% NSEC_PER_SEC
;
273 g_string_append_printf(pretty
->string
,
274 "+%" PRIu64
".%09" PRIu64
,
275 delta_sec
, delta_nsec
);
277 g_string_append(pretty
->string
, "+?.?????????");
281 g_string_append(pretty
->string
, ") ");
284 *start_line
= !print_names
;
291 int print_event_header(struct pretty_component
*pretty
,
292 const bt_message
*event_msg
)
294 bool print_names
= pretty
->options
.print_header_field_names
;
296 const bt_event_class
*event_class
= NULL
;
297 const bt_stream_class
*stream_class
= NULL
;
298 const bt_trace_class
*trace_class
= NULL
;
299 const bt_packet
*packet
= NULL
;
300 const bt_stream
*stream
= NULL
;
301 const bt_trace
*trace
= NULL
;
302 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_class
= bt_event_class_borrow_stream_class_const(event_class
);
308 trace_class
= bt_stream_class_borrow_trace_class_const(stream_class
);
309 packet
= bt_event_borrow_packet_const(event
);
310 stream
= bt_packet_borrow_stream_const(packet
);
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 g_string_append(pretty
->string
, ", ");
325 print_name_equal(pretty
, "trace");
328 g_string_append(pretty
->string
, name
);
331 g_string_append(pretty
->string
, ", ");
335 if (pretty
->options
.print_trace_hostname_field
) {
336 const bt_value
*hostname_str
;
338 hostname_str
= bt_trace_class_borrow_environment_entry_value_by_name_const(
339 trace_class
, "hostname");
343 if (!pretty
->start_line
) {
344 g_string_append(pretty
->string
, ", ");
347 print_name_equal(pretty
, "trace:hostname");
349 str
= bt_value_string_get(hostname_str
);
350 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_class_borrow_environment_entry_value_by_name_const(
358 trace_class
, "domain");
362 if (!pretty
->start_line
) {
363 g_string_append(pretty
->string
, ", ");
366 print_name_equal(pretty
, "trace:domain");
367 } else if (dom_print
) {
368 g_string_append(pretty
->string
, ":");
370 str
= bt_value_string_get(domain_str
);
371 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_class_borrow_environment_entry_value_by_name_const(
379 trace_class
, "procname");
383 if (!pretty
->start_line
) {
384 g_string_append(pretty
->string
, ", ");
387 print_name_equal(pretty
, "trace:procname");
388 } else if (dom_print
) {
389 g_string_append(pretty
->string
, ":");
391 str
= bt_value_string_get(procname_str
);
392 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_class_borrow_environment_entry_value_by_name_const(
400 trace_class
, "vpid");
404 if (!pretty
->start_line
) {
405 g_string_append(pretty
->string
, ", ");
408 print_name_equal(pretty
, "trace:vpid");
409 } else if (dom_print
) {
410 g_string_append(pretty
->string
, ":");
412 value
= bt_value_signed_integer_get(vpid_value
);
413 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 g_string_append(pretty
->string
, ", ");
449 print_name_equal(pretty
, "loglevel");
450 } else if (dom_print
) {
451 g_string_append(pretty
->string
, ":");
454 g_string_append(pretty
->string
, log_level_str
);
455 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 g_string_append(pretty
->string
, ", ");
469 print_name_equal(pretty
, "model.emf.uri");
470 } else if (dom_print
) {
471 g_string_append(pretty
->string
, ":");
474 g_string_append(pretty
->string
, uri_str
);
478 if (dom_print
&& !print_names
) {
479 g_string_append(pretty
->string
, " ");
481 if (!pretty
->start_line
) {
482 g_string_append(pretty
->string
, ", ");
484 pretty
->start_line
= true;
486 print_name_equal(pretty
, "name");
488 if (pretty
->use_colors
) {
489 g_string_append(pretty
->string
, COLOR_EVENT_NAME
);
491 g_string_append(pretty
->string
, bt_event_class_get_name(event_class
));
492 if (pretty
->use_colors
) {
493 g_string_append(pretty
->string
, COLOR_RST
);
496 g_string_append(pretty
->string
, ": ");
498 g_string_append(pretty
->string
, ", ");
506 int print_integer(struct pretty_component
*pretty
,
507 const bt_field
*field
)
510 bt_field_class_integer_preferred_display_base base
;
511 const bt_field_class
*int_fc
;
516 bool rst_color
= false;
517 bt_field_class_type ft_type
;
519 int_fc
= bt_field_borrow_class_const(field
);
521 ft_type
= bt_field_get_class_type(field
);
522 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
523 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
524 v
.u
= bt_field_unsigned_integer_get_value(field
);
526 v
.s
= bt_field_signed_integer_get_value(field
);
529 if (pretty
->use_colors
) {
530 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
534 base
= bt_field_class_integer_get_preferred_display_base(int_fc
);
536 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
540 len
= bt_field_class_integer_get_field_value_range(int_fc
);
541 g_string_append(pretty
->string
, "0b");
542 _bt_safe_lshift(v
.u
, 64 - len
);
543 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
544 g_string_append_printf(pretty
->string
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
545 _bt_safe_lshift(v
.u
, 1);
549 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
551 if (ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
552 ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
) {
555 len
= bt_field_class_integer_get_field_value_range(
561 /* Round length to the nearest 3-bit */
562 rounded_len
= (((len
- 1) / 3) + 1) * 3;
563 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
567 g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
570 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
571 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
572 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
573 g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
575 g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
578 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
582 len
= bt_field_class_integer_get_field_value_range(int_fc
);
584 /* Round length to the nearest nibble */
585 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
587 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
590 g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
599 g_string_append(pretty
->string
, COLOR_RST
);
605 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
609 g_string_append_c(pretty
->string
, '"');
611 for (i
= 0; i
< strlen(str
); i
++) {
612 /* Escape sequences not recognized by iscntrl(). */
615 g_string_append(pretty
->string
, "\\\\");
618 g_string_append(pretty
->string
, "\\\'");
621 g_string_append(pretty
->string
, "\\\"");
624 g_string_append(pretty
->string
, "\\\?");
628 /* Standard characters. */
629 if (!iscntrl(str
[i
])) {
630 g_string_append_c(pretty
->string
, str
[i
]);
636 g_string_append(pretty
->string
, "\\0");
639 g_string_append(pretty
->string
, "\\a");
642 g_string_append(pretty
->string
, "\\b");
645 g_string_append(pretty
->string
, "\\e");
648 g_string_append(pretty
->string
, "\\f");
651 g_string_append(pretty
->string
, "\\n");
654 g_string_append(pretty
->string
, "\\r");
657 g_string_append(pretty
->string
, "\\t");
660 g_string_append(pretty
->string
, "\\v");
663 /* Unhandled control-sequence, print as hex. */
664 g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
669 g_string_append_c(pretty
->string
, '"');
673 int print_enum(struct pretty_component
*pretty
,
674 const bt_field
*field
)
677 const bt_field_class
*enumeration_field_class
= NULL
;
678 bt_field_class_enumeration_mapping_label_array label_array
;
679 uint64_t label_count
;
682 enumeration_field_class
= bt_field_borrow_class_const(field
);
683 if (!enumeration_field_class
) {
688 switch (bt_field_get_class_type(field
)) {
689 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
690 ret
= bt_field_unsigned_enumeration_get_mapping_labels(field
,
691 &label_array
, &label_count
);
693 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
694 ret
= bt_field_signed_enumeration_get_mapping_labels(field
,
695 &label_array
, &label_count
);
706 g_string_append(pretty
->string
, "( ");
707 if (label_count
== 0) {
708 if (pretty
->use_colors
) {
709 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
711 g_string_append(pretty
->string
, "<unknown>");
712 if (pretty
->use_colors
) {
713 g_string_append(pretty
->string
, COLOR_RST
);
717 for (i
= 0; i
< label_count
; i
++) {
718 const char *mapping_name
= label_array
[i
];
721 g_string_append(pretty
->string
, ", ");
723 if (pretty
->use_colors
) {
724 g_string_append(pretty
->string
, COLOR_ENUM_MAPPING_NAME
);
726 print_escape_string(pretty
, mapping_name
);
727 if (pretty
->use_colors
) {
728 g_string_append(pretty
->string
, COLOR_RST
);
732 g_string_append(pretty
->string
, " : container = ");
733 ret
= print_integer(pretty
, field
);
737 g_string_append(pretty
->string
, " )");
743 int filter_field_name(struct pretty_component
*pretty
, const char *field_name
,
744 GQuark
*filter_fields
, int filter_array_len
)
747 GQuark field_quark
= g_quark_try_string(field_name
);
749 if (!field_quark
|| pretty
->options
.verbose
) {
753 for (i
= 0; i
< filter_array_len
; i
++) {
754 if (field_quark
== filter_fields
[i
]) {
762 int print_struct_field(struct pretty_component
*pretty
,
763 const bt_field
*_struct
,
764 const bt_field_class
*struct_class
,
765 uint64_t i
, bool print_names
, uint64_t *nr_printed_fields
,
766 GQuark
*filter_fields
, int filter_array_len
)
769 const char *field_name
;
770 const bt_field
*field
= NULL
;
771 const bt_field_class_structure_member
*member
;
773 field
= bt_field_structure_borrow_member_field_by_index_const(_struct
, i
);
779 member
= bt_field_class_structure_borrow_member_by_index_const(
781 field_name
= bt_field_class_structure_member_get_name(member
);
783 if (filter_fields
&& !filter_field_name(pretty
, field_name
,
784 filter_fields
, filter_array_len
)) {
789 if (*nr_printed_fields
> 0) {
790 g_string_append(pretty
->string
, ", ");
792 g_string_append(pretty
->string
, " ");
795 print_field_name_equal(pretty
, field_name
);
797 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
798 *nr_printed_fields
+= 1;
805 int print_struct(struct pretty_component
*pretty
,
806 const bt_field
*_struct
, bool print_names
,
807 GQuark
*filter_fields
, int filter_array_len
)
810 const bt_field_class
*struct_class
= NULL
;
811 uint64_t nr_fields
, i
, nr_printed_fields
;
813 struct_class
= bt_field_borrow_class_const(_struct
);
819 nr_fields
= bt_field_class_structure_get_member_count(struct_class
);
821 g_string_append(pretty
->string
, "{");
823 nr_printed_fields
= 0;
824 for (i
= 0; i
< nr_fields
; i
++) {
825 ret
= print_struct_field(pretty
, _struct
, struct_class
, i
,
826 print_names
, &nr_printed_fields
, filter_fields
,
833 g_string_append(pretty
->string
, " }");
840 int print_array_field(struct pretty_component
*pretty
,
841 const bt_field
*array
, uint64_t i
, bool print_names
)
843 const bt_field
*field
= NULL
;
846 g_string_append(pretty
->string
, ", ");
848 g_string_append(pretty
->string
, " ");
851 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
854 field
= bt_field_array_borrow_element_field_by_index_const(array
, i
);
856 return print_field(pretty
, field
, print_names
, NULL
, 0);
860 int print_array(struct pretty_component
*pretty
,
861 const bt_field
*array
, bool print_names
)
864 const bt_field_class
*array_class
= NULL
;
868 array_class
= bt_field_borrow_class_const(array
);
873 len
= bt_field_array_get_length(array
);
874 g_string_append(pretty
->string
, "[");
876 for (i
= 0; i
< len
; i
++) {
877 ret
= print_array_field(pretty
, array
, i
, print_names
);
883 g_string_append(pretty
->string
, " ]");
890 int print_sequence_field(struct pretty_component
*pretty
,
891 const bt_field
*seq
, uint64_t i
, bool print_names
)
893 const bt_field
*field
= NULL
;
896 g_string_append(pretty
->string
, ", ");
898 g_string_append(pretty
->string
, " ");
901 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
904 field
= bt_field_array_borrow_element_field_by_index_const(seq
, i
);
906 return print_field(pretty
, field
, print_names
, NULL
, 0);
910 int print_sequence(struct pretty_component
*pretty
,
911 const bt_field
*seq
, bool print_names
)
917 len
= bt_field_array_get_length(seq
);
918 g_string_append(pretty
->string
, "[");
921 for (i
= 0; i
< len
; i
++) {
922 ret
= print_sequence_field(pretty
, seq
, i
, print_names
);
928 g_string_append(pretty
->string
, " ]");
935 int print_variant(struct pretty_component
*pretty
,
936 const bt_field
*variant
, bool print_names
)
939 const bt_field
*field
= NULL
;
941 field
= bt_field_variant_borrow_selected_option_field_const(variant
);
943 g_string_append(pretty
->string
, "{ ");
946 // TODO: find tag's name using field path
947 // print_field_name_equal(pretty, tag_choice);
949 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
954 g_string_append(pretty
->string
, " }");
961 int print_field(struct pretty_component
*pretty
,
962 const bt_field
*field
, bool print_names
,
963 GQuark
*filter_fields
, int filter_array_len
)
965 bt_field_class_type class_id
;
967 class_id
= bt_field_get_class_type(field
);
969 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
970 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
971 return print_integer(pretty
, field
);
972 case BT_FIELD_CLASS_TYPE_REAL
:
976 v
= bt_field_real_get_value(field
);
977 if (pretty
->use_colors
) {
978 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
980 g_string_append_printf(pretty
->string
, "%g", v
);
981 if (pretty
->use_colors
) {
982 g_string_append(pretty
->string
, COLOR_RST
);
986 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
987 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
988 return print_enum(pretty
, field
);
989 case BT_FIELD_CLASS_TYPE_STRING
:
993 str
= bt_field_string_get_value(field
);
998 if (pretty
->use_colors
) {
999 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1001 print_escape_string(pretty
, str
);
1002 if (pretty
->use_colors
) {
1003 g_string_append(pretty
->string
, COLOR_RST
);
1007 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1008 return print_struct(pretty
, field
, print_names
, filter_fields
,
1010 case BT_FIELD_CLASS_TYPE_VARIANT
:
1011 return print_variant(pretty
, field
, print_names
);
1012 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1013 return print_array(pretty
, field
, print_names
);
1014 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1015 return print_sequence(pretty
, field
, print_names
);
1017 // TODO: log instead
1018 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) class_id
);
1024 int print_stream_packet_context(struct pretty_component
*pretty
,
1025 const bt_event
*event
)
1028 const bt_packet
*packet
= NULL
;
1029 const bt_field
*main_field
= NULL
;
1031 packet
= bt_event_borrow_packet_const(event
);
1036 main_field
= bt_packet_borrow_context_field_const(packet
);
1040 if (!pretty
->start_line
) {
1041 g_string_append(pretty
->string
, ", ");
1043 pretty
->start_line
= false;
1044 if (pretty
->options
.print_scope_field_names
) {
1045 print_name_equal(pretty
, "stream.packet.context");
1047 ret
= print_field(pretty
, main_field
,
1048 pretty
->options
.print_context_field_names
,
1049 stream_packet_context_quarks
,
1050 STREAM_PACKET_CONTEXT_QUARKS_LEN
);
1057 int print_stream_event_context(struct pretty_component
*pretty
,
1058 const bt_event
*event
)
1061 const bt_field
*main_field
= NULL
;
1063 main_field
= bt_event_borrow_common_context_field_const(event
);
1067 if (!pretty
->start_line
) {
1068 g_string_append(pretty
->string
, ", ");
1070 pretty
->start_line
= false;
1071 if (pretty
->options
.print_scope_field_names
) {
1072 print_name_equal(pretty
, "stream.event.context");
1074 ret
= print_field(pretty
, main_field
,
1075 pretty
->options
.print_context_field_names
, NULL
, 0);
1082 int print_event_context(struct pretty_component
*pretty
,
1083 const bt_event
*event
)
1086 const bt_field
*main_field
= NULL
;
1088 main_field
= bt_event_borrow_specific_context_field_const(event
);
1092 if (!pretty
->start_line
) {
1093 g_string_append(pretty
->string
, ", ");
1095 pretty
->start_line
= false;
1096 if (pretty
->options
.print_scope_field_names
) {
1097 print_name_equal(pretty
, "event.context");
1099 ret
= print_field(pretty
, main_field
,
1100 pretty
->options
.print_context_field_names
, NULL
, 0);
1107 int print_event_payload(struct pretty_component
*pretty
,
1108 const bt_event
*event
)
1111 const bt_field
*main_field
= NULL
;
1113 main_field
= bt_event_borrow_payload_field_const(event
);
1117 if (!pretty
->start_line
) {
1118 g_string_append(pretty
->string
, ", ");
1120 pretty
->start_line
= false;
1121 if (pretty
->options
.print_scope_field_names
) {
1122 print_name_equal(pretty
, "event.fields");
1124 ret
= print_field(pretty
, main_field
,
1125 pretty
->options
.print_payload_field_names
, NULL
, 0);
1132 int flush_buf(FILE *stream
, struct pretty_component
*pretty
)
1136 if (pretty
->string
->len
== 0) {
1140 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, stream
) != 1) {
1149 int pretty_print_event(struct pretty_component
*pretty
,
1150 const bt_message
*event_msg
)
1153 const bt_event
*event
=
1154 bt_message_event_borrow_event_const(event_msg
);
1157 pretty
->start_line
= true;
1158 g_string_assign(pretty
->string
, "");
1159 ret
= print_event_header(pretty
, event_msg
);
1164 ret
= print_stream_packet_context(pretty
, event
);
1169 ret
= print_stream_event_context(pretty
, event
);
1174 ret
= print_event_context(pretty
, event
);
1179 ret
= print_event_payload(pretty
, event
);
1184 g_string_append_c(pretty
->string
, '\n');
1185 if (flush_buf(pretty
->out
, pretty
)) {
1195 int print_discarded_elements_msg(struct pretty_component
*pretty
,
1196 const bt_stream
*stream
,
1197 const bt_clock_snapshot
*begin_clock_snapshot
,
1198 const bt_clock_snapshot
*end_clock_snapshot
,
1199 uint64_t count
, const char *elem_type
)
1202 const bt_stream_class
*stream_class
= NULL
;
1203 const bt_trace
*trace
= NULL
;
1204 const char *stream_name
;
1205 const char *trace_name
;
1207 int64_t stream_class_id
;
1209 const char *init_msg
;
1212 stream_name
= bt_stream_get_name(stream
);
1214 stream_name
= "(unknown)";
1217 /* Stream class ID */
1218 stream_class
= bt_stream_borrow_class_const(stream
);
1219 BT_ASSERT(stream_class
);
1220 stream_class_id
= bt_stream_class_get_id(stream_class
);
1223 stream_id
= bt_stream_get_id(stream
);
1226 trace
= bt_stream_borrow_trace_const(stream
);
1228 trace_name
= bt_trace_get_name(trace
);
1230 trace_name
= "(unknown)";
1234 trace_uuid
= bt_trace_class_get_uuid(
1235 bt_trace_borrow_class_const(trace
));
1237 /* Format message */
1238 g_string_assign(pretty
->string
, "");
1240 if (count
== UINT64_C(-1)) {
1241 init_msg
= "Tracer may have discarded";
1243 init_msg
= "Tracer discarded";
1246 g_string_append_printf(pretty
->string
,
1247 "%s%sWARNING%s%s: %s ",
1248 bt_common_color_fg_yellow(),
1249 bt_common_color_bold(),
1250 bt_common_color_reset(),
1251 bt_common_color_fg_yellow(), init_msg
);
1253 if (count
== UINT64_C(-1)) {
1254 g_string_append_printf(pretty
->string
, "%ss", elem_type
);
1256 g_string_append_printf(pretty
->string
,
1257 "%" PRIu64
" %s%s", count
, elem_type
,
1258 count
== 1 ? "" : "s");
1261 if (begin_clock_snapshot
&& end_clock_snapshot
) {
1262 g_string_append(pretty
->string
, " between [");
1263 print_timestamp_wall(pretty
, begin_clock_snapshot
, false);
1264 g_string_append(pretty
->string
, "] and [");
1265 print_timestamp_wall(pretty
, end_clock_snapshot
, false);
1266 g_string_append(pretty
->string
, "]");
1268 g_string_append(pretty
->string
, "(unknown time range)");
1271 g_string_append_printf(pretty
->string
, " in trace \"%s\" ", trace_name
);
1274 g_string_append_printf(pretty
->string
,
1275 "(UUID: %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x) ",
1293 g_string_append(pretty
->string
, "(no UUID) ");
1296 g_string_append_printf(pretty
->string
,
1297 "within stream \"%s\" (stream class ID: %" PRIu64
", ",
1298 stream_name
, stream_class_id
);
1300 if (stream_id
>= 0) {
1301 g_string_append_printf(pretty
->string
,
1302 "stream ID: %" PRIu64
, stream_id
);
1304 g_string_append(pretty
->string
, "no stream ID");
1307 g_string_append_printf(pretty
->string
, ").%s\n",
1308 bt_common_color_reset());
1311 * Print to standard error stream to remain backward compatible
1312 * with Babeltrace 1.
1314 if (flush_buf(stderr
, pretty
)) {
1322 int pretty_print_discarded_items(struct pretty_component
*pretty
,
1323 const bt_message
*msg
)
1325 const bt_clock_snapshot
*begin
= NULL
;
1326 const bt_clock_snapshot
*end
= NULL
;
1327 const bt_stream
*stream
;
1328 const bt_stream_class
*stream_class
;
1329 uint64_t count
= UINT64_C(-1);
1330 const char *elem_type
;
1332 switch (bt_message_get_type(msg
)) {
1333 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1334 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1336 if (bt_message_discarded_events_get_count(msg
, &count
) ==
1337 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1338 count
= UINT64_C(-1);
1341 elem_type
= "event";
1343 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1344 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1346 if (bt_message_discarded_packets_get_count(msg
, &count
) ==
1347 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1348 count
= UINT64_C(-1);
1351 elem_type
= "packet";
1358 stream_class
= bt_stream_borrow_class_const(stream
);
1360 if (bt_stream_class_borrow_default_clock_class_const(stream_class
)) {
1361 switch (bt_message_get_type(msg
)) {
1362 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1363 begin
= bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const(
1365 end
= bt_message_discarded_events_borrow_default_end_clock_snapshot_const(
1368 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1369 begin
= bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const(
1371 end
= bt_message_discarded_packets_borrow_default_end_clock_snapshot_const(
1379 print_discarded_elements_msg(pretty
, stream
, begin
, end
,