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 "compat/time.h"
30 #include "common/assert.h"
36 #define NSEC_PER_SEC 1000000000LL
38 #define COLOR_NAME BT_COMMON_COLOR_BOLD
39 #define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN
40 #define COLOR_RST BT_COMMON_COLOR_RESET
41 #define COLOR_STRING_VALUE BT_COMMON_COLOR_BOLD
42 #define COLOR_NUMBER_VALUE BT_COMMON_COLOR_BOLD
43 #define COLOR_ENUM_MAPPING_NAME BT_COMMON_COLOR_BOLD
44 #define COLOR_UNKNOWN BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED
45 #define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA
46 #define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW
49 int64_t real_timestamp
; /* Relative to UNIX epoch. */
50 uint64_t clock_snapshot
; /* In cycles. */
54 int print_field(struct pretty_component
*pretty
,
55 const bt_field
*field
, bool print_names
,
56 GQuark
*filters_fields
, int filter_array_len
);
59 void print_name_equal(struct pretty_component
*pretty
, const char *name
)
61 if (pretty
->use_colors
) {
62 g_string_append_printf(pretty
->string
, "%s%s%s = ", COLOR_NAME
,
65 g_string_append_printf(pretty
->string
, "%s = ", name
);
70 void print_field_name_equal(struct pretty_component
*pretty
, const char *name
)
72 if (pretty
->use_colors
) {
73 g_string_append_printf(pretty
->string
, "%s%s%s = ",
74 COLOR_FIELD_NAME
, name
, COLOR_RST
);
76 g_string_append_printf(pretty
->string
, "%s = ", name
);
81 void print_timestamp_cycles(struct pretty_component
*pretty
,
82 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
86 cycles
= bt_clock_snapshot_get_value(clock_snapshot
);
87 g_string_append_printf(pretty
->string
, "%020" PRIu64
, cycles
);
90 if (pretty
->last_cycles_timestamp
!= -1ULL) {
91 pretty
->delta_cycles
= cycles
- pretty
->last_cycles_timestamp
;
94 pretty
->last_cycles_timestamp
= cycles
;
99 void print_timestamp_wall(struct pretty_component
*pretty
,
100 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
103 int64_t ts_nsec
= 0; /* add configurable offset */
104 int64_t ts_sec
= 0; /* add configurable offset */
105 uint64_t ts_sec_abs
, ts_nsec_abs
;
108 if (!clock_snapshot
) {
109 g_string_append(pretty
->string
, "??:??:??.?????????");
113 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, &ts_nsec
);
115 // TODO: log, this is unexpected
116 g_string_append(pretty
->string
, "Error");
121 if (pretty
->last_real_timestamp
!= -1ULL) {
122 pretty
->delta_real_timestamp
= ts_nsec
- pretty
->last_real_timestamp
;
125 pretty
->last_real_timestamp
= ts_nsec
;
128 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
129 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
131 if (ts_sec
>= 0 && ts_nsec
>= 0) {
134 ts_nsec_abs
= ts_nsec
;
135 } else if (ts_sec
> 0 && ts_nsec
< 0) {
137 ts_sec_abs
= ts_sec
- 1;
138 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
139 } else if (ts_sec
== 0 && ts_nsec
< 0) {
142 ts_nsec_abs
= -ts_nsec
;
143 } else if (ts_sec
< 0 && ts_nsec
> 0) {
145 ts_sec_abs
= -(ts_sec
+ 1);
146 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
147 } else if (ts_sec
< 0 && ts_nsec
== 0) {
149 ts_sec_abs
= -ts_sec
;
150 ts_nsec_abs
= ts_nsec
;
151 } else { /* (ts_sec < 0 && ts_nsec < 0) */
153 ts_sec_abs
= -ts_sec
;
154 ts_nsec_abs
= -ts_nsec
;
157 if (!pretty
->options
.clock_seconds
) {
159 time_t time_s
= (time_t) ts_sec_abs
;
161 if (is_negative
&& !pretty
->negative_timestamp_warning_done
) {
163 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
164 pretty
->negative_timestamp_warning_done
= true;
168 if (!pretty
->options
.clock_gmt
) {
171 res
= bt_localtime_r(&time_s
, &tm
);
174 fprintf(stderr
, "[warning] Unable to get localtime.\n");
180 res
= bt_gmtime_r(&time_s
, &tm
);
183 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
187 if (pretty
->options
.clock_date
) {
191 /* Print date and time */
192 res
= strftime(timestr
, sizeof(timestr
),
196 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
200 g_string_append(pretty
->string
, timestr
);
203 /* Print time in HH:MM:SS.ns */
204 g_string_append_printf(pretty
->string
,
205 "%02d:%02d:%02d.%09" PRIu64
, tm
.tm_hour
, tm
.tm_min
,
206 tm
.tm_sec
, ts_nsec_abs
);
210 g_string_append_printf(pretty
->string
, "%s%" PRId64
".%09" PRIu64
,
211 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
217 int print_event_timestamp(struct pretty_component
*pretty
,
218 const bt_message
*event_msg
, bool *start_line
)
220 bool print_names
= pretty
->options
.print_header_field_names
;
222 const bt_clock_snapshot
*clock_snapshot
= NULL
;
224 if (!bt_message_event_borrow_stream_class_default_clock_class_const(
226 /* No default clock class: skip the timestamp without an error */
230 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(event_msg
);
233 print_name_equal(pretty
, "timestamp");
235 g_string_append(pretty
->string
, "[");
237 if (pretty
->use_colors
) {
238 g_string_append(pretty
->string
, COLOR_TIMESTAMP
);
240 if (pretty
->options
.print_timestamp_cycles
) {
241 print_timestamp_cycles(pretty
, clock_snapshot
, true);
243 print_timestamp_wall(pretty
, clock_snapshot
, true);
245 if (pretty
->use_colors
) {
246 g_string_append(pretty
->string
, COLOR_RST
);
250 g_string_append(pretty
->string
, "] ");
252 if (pretty
->options
.print_delta_field
) {
254 g_string_append(pretty
->string
, ", ");
255 print_name_equal(pretty
, "delta");
257 g_string_append(pretty
->string
, "(");
259 if (pretty
->options
.print_timestamp_cycles
) {
260 if (pretty
->delta_cycles
== -1ULL) {
261 g_string_append(pretty
->string
,
262 "+??????????\?\?"); /* Not a trigraph. */
264 g_string_append_printf(pretty
->string
,
265 "+%012" PRIu64
, pretty
->delta_cycles
);
268 if (pretty
->delta_real_timestamp
!= -1ULL) {
269 uint64_t delta_sec
, delta_nsec
, delta
;
271 delta
= pretty
->delta_real_timestamp
;
272 delta_sec
= delta
/ NSEC_PER_SEC
;
273 delta_nsec
= delta
% NSEC_PER_SEC
;
274 g_string_append_printf(pretty
->string
,
275 "+%" PRIu64
".%09" PRIu64
,
276 delta_sec
, delta_nsec
);
278 g_string_append(pretty
->string
, "+?.?????????");
282 g_string_append(pretty
->string
, ") ");
285 *start_line
= !print_names
;
292 int print_event_header(struct pretty_component
*pretty
,
293 const bt_message
*event_msg
)
295 bool print_names
= pretty
->options
.print_header_field_names
;
297 const bt_event_class
*event_class
= NULL
;
298 const bt_stream_class
*stream_class
= NULL
;
299 const bt_trace_class
*trace_class
= NULL
;
300 const bt_packet
*packet
= NULL
;
301 const bt_stream
*stream
= NULL
;
302 const bt_trace
*trace
= NULL
;
303 const bt_event
*event
= bt_message_event_borrow_event_const(event_msg
);
306 bt_property_availability prop_avail
;
308 event_class
= bt_event_borrow_class_const(event
);
309 stream_class
= bt_event_class_borrow_stream_class_const(event_class
);
310 trace_class
= bt_stream_class_borrow_trace_class_const(stream_class
);
311 packet
= bt_event_borrow_packet_const(event
);
312 stream
= bt_packet_borrow_stream_const(packet
);
313 trace
= bt_stream_borrow_trace_const(stream
);
314 ret
= print_event_timestamp(pretty
, event_msg
, &pretty
->start_line
);
318 if (pretty
->options
.print_trace_field
) {
321 name
= bt_trace_get_name(trace
);
323 if (!pretty
->start_line
) {
324 g_string_append(pretty
->string
, ", ");
327 print_name_equal(pretty
, "trace");
330 g_string_append(pretty
->string
, name
);
333 g_string_append(pretty
->string
, ", ");
337 if (pretty
->options
.print_trace_hostname_field
) {
338 const bt_value
*hostname_str
;
340 hostname_str
= bt_trace_class_borrow_environment_entry_value_by_name_const(
341 trace_class
, "hostname");
345 if (!pretty
->start_line
) {
346 g_string_append(pretty
->string
, ", ");
349 print_name_equal(pretty
, "trace:hostname");
351 str
= bt_value_string_get(hostname_str
);
352 g_string_append(pretty
->string
, str
);
356 if (pretty
->options
.print_trace_domain_field
) {
357 const bt_value
*domain_str
;
359 domain_str
= bt_trace_class_borrow_environment_entry_value_by_name_const(
360 trace_class
, "domain");
364 if (!pretty
->start_line
) {
365 g_string_append(pretty
->string
, ", ");
368 print_name_equal(pretty
, "trace:domain");
369 } else if (dom_print
) {
370 g_string_append(pretty
->string
, ":");
372 str
= bt_value_string_get(domain_str
);
373 g_string_append(pretty
->string
, str
);
377 if (pretty
->options
.print_trace_procname_field
) {
378 const bt_value
*procname_str
;
380 procname_str
= bt_trace_class_borrow_environment_entry_value_by_name_const(
381 trace_class
, "procname");
385 if (!pretty
->start_line
) {
386 g_string_append(pretty
->string
, ", ");
389 print_name_equal(pretty
, "trace:procname");
390 } else if (dom_print
) {
391 g_string_append(pretty
->string
, ":");
393 str
= bt_value_string_get(procname_str
);
394 g_string_append(pretty
->string
, str
);
398 if (pretty
->options
.print_trace_vpid_field
) {
399 const bt_value
*vpid_value
;
401 vpid_value
= bt_trace_class_borrow_environment_entry_value_by_name_const(
402 trace_class
, "vpid");
406 if (!pretty
->start_line
) {
407 g_string_append(pretty
->string
, ", ");
410 print_name_equal(pretty
, "trace:vpid");
411 } else if (dom_print
) {
412 g_string_append(pretty
->string
, ":");
414 value
= bt_value_signed_integer_get(vpid_value
);
415 g_string_append_printf(pretty
->string
,
416 "(%" PRId64
")", value
);
420 if (pretty
->options
.print_loglevel_field
) {
421 static const char *log_level_names
[] = {
422 [ BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
] = "TRACE_EMERG",
423 [ BT_EVENT_CLASS_LOG_LEVEL_ALERT
] = "TRACE_ALERT",
424 [ BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
] = "TRACE_CRIT",
425 [ BT_EVENT_CLASS_LOG_LEVEL_ERROR
] = "TRACE_ERR",
426 [ BT_EVENT_CLASS_LOG_LEVEL_WARNING
] = "TRACE_WARNING",
427 [ BT_EVENT_CLASS_LOG_LEVEL_NOTICE
] = "TRACE_NOTICE",
428 [ BT_EVENT_CLASS_LOG_LEVEL_INFO
] = "TRACE_INFO",
429 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
430 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
431 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
432 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
433 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
434 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
435 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
436 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG
] = "TRACE_DEBUG",
438 bt_event_class_log_level log_level
;
439 const char *log_level_str
= NULL
;
441 prop_avail
= bt_event_class_get_log_level(event_class
,
443 if (prop_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
444 log_level_str
= log_level_names
[log_level
];
445 BT_ASSERT(log_level_str
);
447 if (!pretty
->start_line
) {
448 g_string_append(pretty
->string
, ", ");
451 print_name_equal(pretty
, "loglevel");
452 } else if (dom_print
) {
453 g_string_append(pretty
->string
, ":");
456 g_string_append(pretty
->string
, log_level_str
);
457 g_string_append_printf(
458 pretty
->string
, " (%d)", (int) log_level
);
462 if (pretty
->options
.print_emf_field
) {
465 uri_str
= bt_event_class_get_emf_uri(event_class
);
467 if (!pretty
->start_line
) {
468 g_string_append(pretty
->string
, ", ");
471 print_name_equal(pretty
, "model.emf.uri");
472 } else if (dom_print
) {
473 g_string_append(pretty
->string
, ":");
476 g_string_append(pretty
->string
, uri_str
);
480 if (dom_print
&& !print_names
) {
481 g_string_append(pretty
->string
, " ");
483 if (!pretty
->start_line
) {
484 g_string_append(pretty
->string
, ", ");
486 pretty
->start_line
= true;
488 print_name_equal(pretty
, "name");
490 ev_name
= bt_event_class_get_name(event_class
);
491 if (pretty
->use_colors
) {
493 g_string_append(pretty
->string
, COLOR_EVENT_NAME
);
495 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
499 g_string_append(pretty
->string
, ev_name
);
501 g_string_append(pretty
->string
, "<unknown>");
503 if (pretty
->use_colors
) {
504 g_string_append(pretty
->string
, COLOR_RST
);
507 g_string_append(pretty
->string
, ": ");
509 g_string_append(pretty
->string
, ", ");
517 int print_integer(struct pretty_component
*pretty
,
518 const bt_field
*field
)
521 bt_field_class_integer_preferred_display_base base
;
522 const bt_field_class
*int_fc
;
527 bool rst_color
= false;
528 bt_field_class_type ft_type
;
530 int_fc
= bt_field_borrow_class_const(field
);
532 ft_type
= bt_field_get_class_type(field
);
533 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
534 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
535 v
.u
= bt_field_unsigned_integer_get_value(field
);
537 v
.s
= bt_field_signed_integer_get_value(field
);
540 if (pretty
->use_colors
) {
541 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
545 base
= bt_field_class_integer_get_preferred_display_base(int_fc
);
547 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
551 len
= bt_field_class_integer_get_field_value_range(int_fc
);
552 g_string_append(pretty
->string
, "0b");
553 _bt_safe_lshift(v
.u
, 64 - len
);
554 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
555 g_string_append_printf(pretty
->string
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
556 _bt_safe_lshift(v
.u
, 1);
560 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
562 if (ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
563 ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
) {
566 len
= bt_field_class_integer_get_field_value_range(
572 /* Round length to the nearest 3-bit */
573 rounded_len
= (((len
- 1) / 3) + 1) * 3;
574 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
578 g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
581 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
582 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
583 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
584 g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
586 g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
589 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
593 len
= bt_field_class_integer_get_field_value_range(int_fc
);
595 /* Round length to the nearest nibble */
596 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
598 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
601 g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
610 g_string_append(pretty
->string
, COLOR_RST
);
616 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
620 g_string_append_c(pretty
->string
, '"');
622 for (i
= 0; i
< strlen(str
); i
++) {
623 /* Escape sequences not recognized by iscntrl(). */
626 g_string_append(pretty
->string
, "\\\\");
629 g_string_append(pretty
->string
, "\\\'");
632 g_string_append(pretty
->string
, "\\\"");
635 g_string_append(pretty
->string
, "\\\?");
639 /* Standard characters. */
640 if (!iscntrl(str
[i
])) {
641 g_string_append_c(pretty
->string
, str
[i
]);
647 g_string_append(pretty
->string
, "\\0");
650 g_string_append(pretty
->string
, "\\a");
653 g_string_append(pretty
->string
, "\\b");
656 g_string_append(pretty
->string
, "\\e");
659 g_string_append(pretty
->string
, "\\f");
662 g_string_append(pretty
->string
, "\\n");
665 g_string_append(pretty
->string
, "\\r");
668 g_string_append(pretty
->string
, "\\t");
671 g_string_append(pretty
->string
, "\\v");
674 /* Unhandled control-sequence, print as hex. */
675 g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
680 g_string_append_c(pretty
->string
, '"');
684 int print_enum(struct pretty_component
*pretty
,
685 const bt_field
*field
)
688 const bt_field_class
*enumeration_field_class
= NULL
;
689 bt_field_class_enumeration_mapping_label_array label_array
;
690 uint64_t label_count
;
693 enumeration_field_class
= bt_field_borrow_class_const(field
);
694 if (!enumeration_field_class
) {
699 switch (bt_field_get_class_type(field
)) {
700 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
701 ret
= bt_field_unsigned_enumeration_get_mapping_labels(field
,
702 &label_array
, &label_count
);
704 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
705 ret
= bt_field_signed_enumeration_get_mapping_labels(field
,
706 &label_array
, &label_count
);
717 g_string_append(pretty
->string
, "( ");
718 if (label_count
== 0) {
719 if (pretty
->use_colors
) {
720 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
722 g_string_append(pretty
->string
, "<unknown>");
723 if (pretty
->use_colors
) {
724 g_string_append(pretty
->string
, COLOR_RST
);
728 for (i
= 0; i
< label_count
; i
++) {
729 const char *mapping_name
= label_array
[i
];
732 g_string_append(pretty
->string
, ", ");
734 if (pretty
->use_colors
) {
735 g_string_append(pretty
->string
, COLOR_ENUM_MAPPING_NAME
);
737 print_escape_string(pretty
, mapping_name
);
738 if (pretty
->use_colors
) {
739 g_string_append(pretty
->string
, COLOR_RST
);
743 g_string_append(pretty
->string
, " : container = ");
744 ret
= print_integer(pretty
, field
);
748 g_string_append(pretty
->string
, " )");
754 int filter_field_name(struct pretty_component
*pretty
, const char *field_name
,
755 GQuark
*filter_fields
, int filter_array_len
)
758 GQuark field_quark
= g_quark_try_string(field_name
);
760 if (!field_quark
|| pretty
->options
.verbose
) {
764 for (i
= 0; i
< filter_array_len
; i
++) {
765 if (field_quark
== filter_fields
[i
]) {
773 int print_struct_field(struct pretty_component
*pretty
,
774 const bt_field
*_struct
,
775 const bt_field_class
*struct_class
,
776 uint64_t i
, bool print_names
, uint64_t *nr_printed_fields
,
777 GQuark
*filter_fields
, int filter_array_len
)
780 const char *field_name
;
781 const bt_field
*field
= NULL
;
782 const bt_field_class_structure_member
*member
;
784 field
= bt_field_structure_borrow_member_field_by_index_const(_struct
, i
);
790 member
= bt_field_class_structure_borrow_member_by_index_const(
792 field_name
= bt_field_class_structure_member_get_name(member
);
794 if (filter_fields
&& !filter_field_name(pretty
, field_name
,
795 filter_fields
, filter_array_len
)) {
800 if (*nr_printed_fields
> 0) {
801 g_string_append(pretty
->string
, ", ");
803 g_string_append(pretty
->string
, " ");
806 print_field_name_equal(pretty
, field_name
);
808 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
809 *nr_printed_fields
+= 1;
816 int print_struct(struct pretty_component
*pretty
,
817 const bt_field
*_struct
, bool print_names
,
818 GQuark
*filter_fields
, int filter_array_len
)
821 const bt_field_class
*struct_class
= NULL
;
822 uint64_t nr_fields
, i
, nr_printed_fields
;
824 struct_class
= bt_field_borrow_class_const(_struct
);
830 nr_fields
= bt_field_class_structure_get_member_count(struct_class
);
832 g_string_append(pretty
->string
, "{");
834 nr_printed_fields
= 0;
835 for (i
= 0; i
< nr_fields
; i
++) {
836 ret
= print_struct_field(pretty
, _struct
, struct_class
, i
,
837 print_names
, &nr_printed_fields
, filter_fields
,
844 g_string_append(pretty
->string
, " }");
851 int print_array_field(struct pretty_component
*pretty
,
852 const bt_field
*array
, uint64_t i
, bool print_names
)
854 const bt_field
*field
= NULL
;
857 g_string_append(pretty
->string
, ", ");
859 g_string_append(pretty
->string
, " ");
862 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
865 field
= bt_field_array_borrow_element_field_by_index_const(array
, i
);
867 return print_field(pretty
, field
, print_names
, NULL
, 0);
871 int print_array(struct pretty_component
*pretty
,
872 const bt_field
*array
, bool print_names
)
875 const bt_field_class
*array_class
= NULL
;
879 array_class
= bt_field_borrow_class_const(array
);
884 len
= bt_field_array_get_length(array
);
885 g_string_append(pretty
->string
, "[");
887 for (i
= 0; i
< len
; i
++) {
888 ret
= print_array_field(pretty
, array
, i
, print_names
);
894 g_string_append(pretty
->string
, " ]");
901 int print_sequence_field(struct pretty_component
*pretty
,
902 const bt_field
*seq
, uint64_t i
, bool print_names
)
904 const bt_field
*field
= NULL
;
907 g_string_append(pretty
->string
, ", ");
909 g_string_append(pretty
->string
, " ");
912 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
915 field
= bt_field_array_borrow_element_field_by_index_const(seq
, i
);
917 return print_field(pretty
, field
, print_names
, NULL
, 0);
921 int print_sequence(struct pretty_component
*pretty
,
922 const bt_field
*seq
, bool print_names
)
928 len
= bt_field_array_get_length(seq
);
929 g_string_append(pretty
->string
, "[");
932 for (i
= 0; i
< len
; i
++) {
933 ret
= print_sequence_field(pretty
, seq
, i
, print_names
);
939 g_string_append(pretty
->string
, " ]");
946 int print_variant(struct pretty_component
*pretty
,
947 const bt_field
*variant
, bool print_names
)
950 const bt_field
*field
= NULL
;
952 field
= bt_field_variant_borrow_selected_option_field_const(variant
);
954 g_string_append(pretty
->string
, "{ ");
957 // TODO: find tag's name using field path
958 // print_field_name_equal(pretty, tag_choice);
960 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
965 g_string_append(pretty
->string
, " }");
972 int print_field(struct pretty_component
*pretty
,
973 const bt_field
*field
, bool print_names
,
974 GQuark
*filter_fields
, int filter_array_len
)
976 bt_field_class_type class_id
;
978 class_id
= bt_field_get_class_type(field
);
980 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
981 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
982 return print_integer(pretty
, field
);
983 case BT_FIELD_CLASS_TYPE_REAL
:
987 v
= bt_field_real_get_value(field
);
988 if (pretty
->use_colors
) {
989 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
991 g_string_append_printf(pretty
->string
, "%g", v
);
992 if (pretty
->use_colors
) {
993 g_string_append(pretty
->string
, COLOR_RST
);
997 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
998 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
999 return print_enum(pretty
, field
);
1000 case BT_FIELD_CLASS_TYPE_STRING
:
1004 str
= bt_field_string_get_value(field
);
1009 if (pretty
->use_colors
) {
1010 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1012 print_escape_string(pretty
, str
);
1013 if (pretty
->use_colors
) {
1014 g_string_append(pretty
->string
, COLOR_RST
);
1018 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1019 return print_struct(pretty
, field
, print_names
, filter_fields
,
1021 case BT_FIELD_CLASS_TYPE_VARIANT
:
1022 return print_variant(pretty
, field
, print_names
);
1023 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1024 return print_array(pretty
, field
, print_names
);
1025 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1026 return print_sequence(pretty
, field
, print_names
);
1028 // TODO: log instead
1029 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) class_id
);
1035 int print_stream_packet_context(struct pretty_component
*pretty
,
1036 const bt_event
*event
)
1039 const bt_packet
*packet
= NULL
;
1040 const bt_field
*main_field
= NULL
;
1042 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_class_get_uuid(
1246 bt_trace_borrow_class_const(trace
));
1248 /* Format message */
1249 g_string_assign(pretty
->string
, "");
1251 if (count
== UINT64_C(-1)) {
1252 init_msg
= "Tracer may have discarded";
1254 init_msg
= "Tracer discarded";
1257 g_string_append_printf(pretty
->string
,
1258 "%s%sWARNING%s%s: %s ",
1259 bt_common_color_fg_yellow(),
1260 bt_common_color_bold(),
1261 bt_common_color_reset(),
1262 bt_common_color_fg_yellow(), init_msg
);
1264 if (count
== UINT64_C(-1)) {
1265 g_string_append_printf(pretty
->string
, "%ss", elem_type
);
1267 g_string_append_printf(pretty
->string
,
1268 "%" PRIu64
" %s%s", count
, elem_type
,
1269 count
== 1 ? "" : "s");
1272 g_string_append_c(pretty
->string
, ' ');
1274 if (begin_clock_snapshot
&& end_clock_snapshot
) {
1275 g_string_append(pretty
->string
, "between [");
1276 print_timestamp_wall(pretty
, begin_clock_snapshot
, false);
1277 g_string_append(pretty
->string
, "] and [");
1278 print_timestamp_wall(pretty
, end_clock_snapshot
, false);
1279 g_string_append(pretty
->string
, "]");
1281 g_string_append(pretty
->string
, "(unknown time range)");
1284 g_string_append_printf(pretty
->string
, " in trace \"%s\" ", trace_name
);
1287 g_string_append_printf(pretty
->string
,
1288 "(UUID: %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x) ",
1306 g_string_append(pretty
->string
, "(no UUID) ");
1309 g_string_append_printf(pretty
->string
,
1310 "within stream \"%s\" (stream class ID: %" PRIu64
", ",
1311 stream_name
, stream_class_id
);
1313 if (stream_id
>= 0) {
1314 g_string_append_printf(pretty
->string
,
1315 "stream ID: %" PRIu64
, stream_id
);
1317 g_string_append(pretty
->string
, "no stream ID");
1320 g_string_append_printf(pretty
->string
, ").%s\n",
1321 bt_common_color_reset());
1324 * Print to standard error stream to remain backward compatible
1325 * with Babeltrace 1.
1327 if (flush_buf(stderr
, pretty
)) {
1335 int pretty_print_discarded_items(struct pretty_component
*pretty
,
1336 const bt_message
*msg
)
1338 const bt_clock_snapshot
*begin
= NULL
;
1339 const bt_clock_snapshot
*end
= NULL
;
1340 const bt_stream
*stream
;
1341 const bt_stream_class
*stream_class
;
1342 uint64_t count
= UINT64_C(-1);
1343 const char *elem_type
;
1345 switch (bt_message_get_type(msg
)) {
1346 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1347 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1349 if (bt_message_discarded_events_get_count(msg
, &count
) ==
1350 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1351 count
= UINT64_C(-1);
1354 elem_type
= "event";
1356 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1357 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1359 if (bt_message_discarded_packets_get_count(msg
, &count
) ==
1360 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1361 count
= UINT64_C(-1);
1364 elem_type
= "packet";
1371 stream_class
= bt_stream_borrow_class_const(stream
);
1373 switch (bt_message_get_type(msg
)) {
1374 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1375 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
1377 begin
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1379 end
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1384 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1385 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
1387 begin
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1389 end
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1398 print_discarded_elements_msg(pretty
, stream
, begin
, end
,