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_value
; /* In cycles. */
53 int print_field(struct pretty_component
*pretty
,
54 const struct 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 struct bt_event
*event
)
83 const struct bt_clock_value
*clock_value
;
85 enum bt_clock_value_status cv_status
;
87 cv_status
= bt_event_borrow_default_clock_value_const(event
, &clock_value
);
88 if (cv_status
!= BT_CLOCK_VALUE_STATUS_KNOWN
|| !clock_value
) {
89 g_string_append(pretty
->string
, "????????????????????");
93 cycles
= bt_clock_value_get_value(clock_value
);
94 g_string_append_printf(pretty
->string
, "%020" PRIu64
, cycles
);
96 if (pretty
->last_cycles_timestamp
!= -1ULL) {
97 pretty
->delta_cycles
= cycles
- pretty
->last_cycles_timestamp
;
99 pretty
->last_cycles_timestamp
= cycles
;
103 void print_timestamp_wall(struct pretty_component
*pretty
,
104 const struct bt_clock_value
*clock_value
)
107 int64_t ts_nsec
= 0; /* add configurable offset */
108 int64_t ts_sec
= 0; /* add configurable offset */
109 uint64_t ts_sec_abs
, ts_nsec_abs
;
113 g_string_append(pretty
->string
, "??:??:??.?????????");
117 ret
= bt_clock_value_get_ns_from_origin(clock_value
, &ts_nsec
);
119 // TODO: log, this is unexpected
120 g_string_append(pretty
->string
, "Error");
124 if (pretty
->last_real_timestamp
!= -1ULL) {
125 pretty
->delta_real_timestamp
= ts_nsec
- pretty
->last_real_timestamp
;
128 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 struct bt_event
*event
, bool *start_line
)
221 bool print_names
= pretty
->options
.print_header_field_names
;
223 const struct bt_stream
*stream
= NULL
;
224 const struct bt_stream_class
*stream_class
= NULL
;
225 const struct bt_trace
*trace
= NULL
;
226 const struct bt_clock_value
*clock_value
= NULL
;
227 enum bt_clock_value_status cv_status
;
229 stream
= bt_event_borrow_stream_const(event
);
235 stream_class
= bt_stream_borrow_class_const(stream
);
240 trace
= bt_stream_class_borrow_trace_const(stream_class
);
246 cv_status
= bt_event_borrow_default_clock_value_const(event
,
248 if (cv_status
!= BT_CLOCK_VALUE_STATUS_KNOWN
|| !clock_value
) {
249 /* No default clock value: skip the timestamp without an error */
254 print_name_equal(pretty
, "timestamp");
256 g_string_append(pretty
->string
, "[");
258 if (pretty
->use_colors
) {
259 g_string_append(pretty
->string
, COLOR_TIMESTAMP
);
261 if (pretty
->options
.print_timestamp_cycles
) {
262 print_timestamp_cycles(pretty
, event
);
265 cv_status
= bt_event_borrow_default_clock_value_const(event
,
267 print_timestamp_wall(pretty
, clock_value
);
269 if (pretty
->use_colors
) {
270 g_string_append(pretty
->string
, COLOR_RST
);
274 g_string_append(pretty
->string
, "] ");
276 if (pretty
->options
.print_delta_field
) {
278 g_string_append(pretty
->string
, ", ");
279 print_name_equal(pretty
, "delta");
281 g_string_append(pretty
->string
, "(");
283 if (pretty
->options
.print_timestamp_cycles
) {
284 if (pretty
->delta_cycles
== -1ULL) {
285 g_string_append(pretty
->string
,
286 "+??????????\?\?) "); /* Not a trigraph. */
288 g_string_append_printf(pretty
->string
,
289 "+%012" PRIu64
, pretty
->delta_cycles
);
292 if (pretty
->delta_real_timestamp
!= -1ULL) {
293 uint64_t delta_sec
, delta_nsec
, delta
;
295 delta
= pretty
->delta_real_timestamp
;
296 delta_sec
= delta
/ NSEC_PER_SEC
;
297 delta_nsec
= delta
% NSEC_PER_SEC
;
298 g_string_append_printf(pretty
->string
,
299 "+%" PRIu64
".%09" PRIu64
,
300 delta_sec
, delta_nsec
);
302 g_string_append(pretty
->string
, "+?.?????????");
306 g_string_append(pretty
->string
, ") ");
309 *start_line
= !print_names
;
316 int print_event_header(struct pretty_component
*pretty
,
317 const struct bt_event
*event
)
319 bool print_names
= pretty
->options
.print_header_field_names
;
321 const struct bt_event_class
*event_class
= NULL
;
322 const struct bt_stream_class
*stream_class
= NULL
;
323 const struct bt_trace
*trace_class
= NULL
;
325 enum bt_property_availability prop_avail
;
327 event_class
= bt_event_borrow_class_const(event
);
332 stream_class
= bt_event_class_borrow_stream_class_const(event_class
);
337 trace_class
= bt_stream_class_borrow_trace_const(stream_class
);
342 ret
= print_event_timestamp(pretty
, event
, &pretty
->start_line
);
346 if (pretty
->options
.print_trace_field
) {
349 name
= bt_trace_get_name(trace_class
);
351 if (!pretty
->start_line
) {
352 g_string_append(pretty
->string
, ", ");
355 print_name_equal(pretty
, "trace");
358 g_string_append(pretty
->string
, name
);
361 g_string_append(pretty
->string
, " ");
365 if (pretty
->options
.print_trace_hostname_field
) {
366 const struct bt_value
*hostname_str
;
368 hostname_str
= bt_trace_borrow_environment_entry_value_by_name_const(
369 trace_class
, "hostname");
373 if (!pretty
->start_line
) {
374 g_string_append(pretty
->string
, ", ");
377 print_name_equal(pretty
, "trace:hostname");
379 str
= bt_value_string_get(hostname_str
);
380 g_string_append(pretty
->string
, str
);
384 if (pretty
->options
.print_trace_domain_field
) {
385 const struct bt_value
*domain_str
;
387 domain_str
= bt_trace_borrow_environment_entry_value_by_name_const(
388 trace_class
, "domain");
392 if (!pretty
->start_line
) {
393 g_string_append(pretty
->string
, ", ");
396 print_name_equal(pretty
, "trace:domain");
397 } else if (dom_print
) {
398 g_string_append(pretty
->string
, ":");
400 str
= bt_value_string_get(domain_str
);
401 g_string_append(pretty
->string
, str
);
405 if (pretty
->options
.print_trace_procname_field
) {
406 const struct bt_value
*procname_str
;
408 procname_str
= bt_trace_borrow_environment_entry_value_by_name_const(
409 trace_class
, "procname");
413 if (!pretty
->start_line
) {
414 g_string_append(pretty
->string
, ", ");
417 print_name_equal(pretty
, "trace:procname");
418 } else if (dom_print
) {
419 g_string_append(pretty
->string
, ":");
421 str
= bt_value_string_get(procname_str
);
422 g_string_append(pretty
->string
, str
);
426 if (pretty
->options
.print_trace_vpid_field
) {
427 const struct bt_value
*vpid_value
;
429 vpid_value
= bt_trace_borrow_environment_entry_value_by_name_const(
430 trace_class
, "vpid");
434 if (!pretty
->start_line
) {
435 g_string_append(pretty
->string
, ", ");
438 print_name_equal(pretty
, "trace:vpid");
439 } else if (dom_print
) {
440 g_string_append(pretty
->string
, ":");
442 value
= bt_value_integer_get(vpid_value
);
443 g_string_append_printf(pretty
->string
,
444 "(%" PRId64
")", value
);
448 if (pretty
->options
.print_loglevel_field
) {
449 static const char *log_level_names
[] = {
450 [ BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
] = "TRACE_EMERG",
451 [ BT_EVENT_CLASS_LOG_LEVEL_ALERT
] = "TRACE_ALERT",
452 [ BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
] = "TRACE_CRIT",
453 [ BT_EVENT_CLASS_LOG_LEVEL_ERROR
] = "TRACE_ERR",
454 [ BT_EVENT_CLASS_LOG_LEVEL_WARNING
] = "TRACE_WARNING",
455 [ BT_EVENT_CLASS_LOG_LEVEL_NOTICE
] = "TRACE_NOTICE",
456 [ BT_EVENT_CLASS_LOG_LEVEL_INFO
] = "TRACE_INFO",
457 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
458 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
459 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
460 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
461 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
462 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
463 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
464 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG
] = "TRACE_DEBUG",
466 enum bt_event_class_log_level log_level
;
467 const char *log_level_str
= NULL
;
469 prop_avail
= bt_event_class_get_log_level(event_class
,
471 if (prop_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
472 log_level_str
= log_level_names
[log_level
];
473 BT_ASSERT(log_level_str
);
475 if (!pretty
->start_line
) {
476 g_string_append(pretty
->string
, ", ");
479 print_name_equal(pretty
, "loglevel");
480 } else if (dom_print
) {
481 g_string_append(pretty
->string
, ":");
484 g_string_append(pretty
->string
, log_level_str
);
485 g_string_append_printf(
486 pretty
->string
, " (%d)", (int) log_level
);
490 if (pretty
->options
.print_emf_field
) {
493 uri_str
= bt_event_class_get_emf_uri(event_class
);
495 if (!pretty
->start_line
) {
496 g_string_append(pretty
->string
, ", ");
499 print_name_equal(pretty
, "model.emf.uri");
500 } else if (dom_print
) {
501 g_string_append(pretty
->string
, ":");
504 g_string_append(pretty
->string
, uri_str
);
508 if (dom_print
&& !print_names
) {
509 g_string_append(pretty
->string
, " ");
511 if (!pretty
->start_line
) {
512 g_string_append(pretty
->string
, ", ");
514 pretty
->start_line
= true;
516 print_name_equal(pretty
, "name");
518 if (pretty
->use_colors
) {
519 g_string_append(pretty
->string
, COLOR_EVENT_NAME
);
521 g_string_append(pretty
->string
, bt_event_class_get_name(event_class
));
522 if (pretty
->use_colors
) {
523 g_string_append(pretty
->string
, COLOR_RST
);
526 g_string_append(pretty
->string
, ": ");
528 g_string_append(pretty
->string
, ", ");
536 int print_integer(struct pretty_component
*pretty
,
537 const struct bt_field
*field
)
540 enum bt_field_class_integer_preferred_display_base base
;
541 const struct bt_field_class
*int_fc
;
546 bool rst_color
= false;
547 enum bt_field_class_type ft_type
;
549 int_fc
= bt_field_borrow_class_const(field
);
551 ft_type
= bt_field_get_class_type(field
);
552 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
553 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
554 v
.u
= bt_field_unsigned_integer_get_value(field
);
556 v
.s
= bt_field_signed_integer_get_value(field
);
559 if (pretty
->use_colors
) {
560 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
564 base
= bt_field_class_integer_get_preferred_display_base(int_fc
);
566 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
570 len
= bt_field_class_integer_get_field_value_range(int_fc
);
571 g_string_append(pretty
->string
, "0b");
572 v
.u
= _bt_piecewise_lshift(v
.u
, 64 - len
);
573 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
574 g_string_append_printf(pretty
->string
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
575 v
.u
= _bt_piecewise_lshift(v
.u
, 1);
579 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
581 if (ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
582 ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
) {
585 len
= bt_field_class_integer_get_field_value_range(
591 /* Round length to the nearest 3-bit */
592 rounded_len
= (((len
- 1) / 3) + 1) * 3;
593 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
597 g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
600 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
601 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
602 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
603 g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
605 g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
608 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
612 len
= bt_field_class_integer_get_field_value_range(int_fc
);
614 /* Round length to the nearest nibble */
615 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
617 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
620 g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
629 g_string_append(pretty
->string
, COLOR_RST
);
635 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
639 g_string_append_c(pretty
->string
, '"');
641 for (i
= 0; i
< strlen(str
); i
++) {
642 /* Escape sequences not recognized by iscntrl(). */
645 g_string_append(pretty
->string
, "\\\\");
648 g_string_append(pretty
->string
, "\\\'");
651 g_string_append(pretty
->string
, "\\\"");
654 g_string_append(pretty
->string
, "\\\?");
658 /* Standard characters. */
659 if (!iscntrl(str
[i
])) {
660 g_string_append_c(pretty
->string
, str
[i
]);
666 g_string_append(pretty
->string
, "\\0");
669 g_string_append(pretty
->string
, "\\a");
672 g_string_append(pretty
->string
, "\\b");
675 g_string_append(pretty
->string
, "\\e");
678 g_string_append(pretty
->string
, "\\f");
681 g_string_append(pretty
->string
, "\\n");
684 g_string_append(pretty
->string
, "\\r");
687 g_string_append(pretty
->string
, "\\t");
690 g_string_append(pretty
->string
, "\\v");
693 /* Unhandled control-sequence, print as hex. */
694 g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
699 g_string_append_c(pretty
->string
, '"');
703 int print_enum(struct pretty_component
*pretty
,
704 const struct bt_field
*field
)
707 const struct bt_field_class
*enumeration_field_class
= NULL
;
708 bt_field_class_enumeration_mapping_label_array label_array
;
709 uint64_t label_count
;
712 enumeration_field_class
= bt_field_borrow_class_const(field
);
713 if (!enumeration_field_class
) {
718 switch (bt_field_get_class_type(field
)) {
719 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
720 ret
= bt_field_unsigned_enumeration_get_mapping_labels(field
,
721 &label_array
, &label_count
);
723 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
724 ret
= bt_field_signed_enumeration_get_mapping_labels(field
,
725 &label_array
, &label_count
);
736 g_string_append(pretty
->string
, "( ");
737 if (label_count
== 0) {
738 if (pretty
->use_colors
) {
739 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
741 g_string_append(pretty
->string
, "<unknown>");
742 if (pretty
->use_colors
) {
743 g_string_append(pretty
->string
, COLOR_RST
);
747 for (i
= 0; i
< label_count
; i
++) {
748 const char *mapping_name
= label_array
[i
];
751 g_string_append(pretty
->string
, ", ");
753 if (pretty
->use_colors
) {
754 g_string_append(pretty
->string
, COLOR_ENUM_MAPPING_NAME
);
756 print_escape_string(pretty
, mapping_name
);
757 if (pretty
->use_colors
) {
758 g_string_append(pretty
->string
, COLOR_RST
);
762 g_string_append(pretty
->string
, " : container = ");
763 ret
= print_integer(pretty
, field
);
767 g_string_append(pretty
->string
, " )");
773 int filter_field_name(struct pretty_component
*pretty
, const char *field_name
,
774 GQuark
*filter_fields
, int filter_array_len
)
777 GQuark field_quark
= g_quark_try_string(field_name
);
779 if (!field_quark
|| pretty
->options
.verbose
) {
783 for (i
= 0; i
< filter_array_len
; i
++) {
784 if (field_quark
== filter_fields
[i
]) {
792 int print_struct_field(struct pretty_component
*pretty
,
793 const struct bt_field
*_struct
,
794 const struct bt_field_class
*struct_class
,
795 uint64_t i
, bool print_names
, uint64_t *nr_printed_fields
,
796 GQuark
*filter_fields
, int filter_array_len
)
799 const char *field_name
;
800 const struct bt_field
*field
= NULL
;
801 const struct bt_field_class
*field_class
= NULL
;;
803 field
= bt_field_structure_borrow_member_field_by_index_const(_struct
, i
);
809 bt_field_class_structure_borrow_member_by_index_const(struct_class
, i
,
810 &field_name
, &field_class
);
812 if (filter_fields
&& !filter_field_name(pretty
, field_name
,
813 filter_fields
, filter_array_len
)) {
818 if (*nr_printed_fields
> 0) {
819 g_string_append(pretty
->string
, ", ");
821 g_string_append(pretty
->string
, " ");
824 print_field_name_equal(pretty
, field_name
);
826 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
827 *nr_printed_fields
+= 1;
834 int print_struct(struct pretty_component
*pretty
,
835 const struct bt_field
*_struct
, bool print_names
,
836 GQuark
*filter_fields
, int filter_array_len
)
839 const struct bt_field_class
*struct_class
= NULL
;
840 uint64_t nr_fields
, i
, nr_printed_fields
;
842 struct_class
= bt_field_borrow_class_const(_struct
);
847 nr_fields
= bt_field_class_structure_get_member_count(struct_class
);
852 g_string_append(pretty
->string
, "{");
854 nr_printed_fields
= 0;
855 for (i
= 0; i
< nr_fields
; i
++) {
856 ret
= print_struct_field(pretty
, _struct
, struct_class
, i
,
857 print_names
, &nr_printed_fields
, filter_fields
,
864 g_string_append(pretty
->string
, " }");
871 int print_array_field(struct pretty_component
*pretty
,
872 const struct bt_field
*array
, uint64_t i
, bool print_names
)
874 const struct bt_field
*field
= NULL
;
877 g_string_append(pretty
->string
, ", ");
879 g_string_append(pretty
->string
, " ");
882 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
885 field
= bt_field_array_borrow_element_field_by_index_const(array
, i
);
887 return print_field(pretty
, field
, print_names
, NULL
, 0);
891 int print_array(struct pretty_component
*pretty
,
892 const struct bt_field
*array
, bool print_names
)
895 const struct bt_field_class
*array_class
= NULL
;
899 array_class
= bt_field_borrow_class_const(array
);
904 len
= bt_field_array_get_length(array
);
905 g_string_append(pretty
->string
, "[");
907 for (i
= 0; i
< len
; i
++) {
908 ret
= print_array_field(pretty
, array
, i
, print_names
);
914 g_string_append(pretty
->string
, " ]");
921 int print_sequence_field(struct pretty_component
*pretty
,
922 const struct bt_field
*seq
, uint64_t i
, bool print_names
)
924 const struct bt_field
*field
= NULL
;
927 g_string_append(pretty
->string
, ", ");
929 g_string_append(pretty
->string
, " ");
932 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
935 field
= bt_field_array_borrow_element_field_by_index_const(seq
, i
);
937 return print_field(pretty
, field
, print_names
, NULL
, 0);
941 int print_sequence(struct pretty_component
*pretty
,
942 const struct bt_field
*seq
, bool print_names
)
948 len
= bt_field_array_get_length(seq
);
954 g_string_append(pretty
->string
, "[");
957 for (i
= 0; i
< len
; i
++) {
958 ret
= print_sequence_field(pretty
, seq
, i
, print_names
);
964 g_string_append(pretty
->string
, " ]");
971 int print_variant(struct pretty_component
*pretty
,
972 const struct bt_field
*variant
, bool print_names
)
975 const struct bt_field
*field
= NULL
;
977 field
= bt_field_variant_borrow_selected_option_field_const(variant
);
979 g_string_append(pretty
->string
, "{ ");
982 // TODO: find tag's name using field path
983 // print_field_name_equal(pretty, tag_choice);
985 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
990 g_string_append(pretty
->string
, " }");
997 int print_field(struct pretty_component
*pretty
,
998 const struct bt_field
*field
, bool print_names
,
999 GQuark
*filter_fields
, int filter_array_len
)
1001 enum bt_field_class_type class_id
;
1003 class_id
= bt_field_get_class_type(field
);
1005 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
1006 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
1007 return print_integer(pretty
, field
);
1008 case BT_FIELD_CLASS_TYPE_REAL
:
1012 v
= bt_field_real_get_value(field
);
1013 if (pretty
->use_colors
) {
1014 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
1016 g_string_append_printf(pretty
->string
, "%g", v
);
1017 if (pretty
->use_colors
) {
1018 g_string_append(pretty
->string
, COLOR_RST
);
1022 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
1023 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
1024 return print_enum(pretty
, field
);
1025 case BT_FIELD_CLASS_TYPE_STRING
:
1029 str
= bt_field_string_get_value(field
);
1034 if (pretty
->use_colors
) {
1035 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1037 print_escape_string(pretty
, str
);
1038 if (pretty
->use_colors
) {
1039 g_string_append(pretty
->string
, COLOR_RST
);
1043 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1044 return print_struct(pretty
, field
, print_names
, filter_fields
,
1046 case BT_FIELD_CLASS_TYPE_VARIANT
:
1047 return print_variant(pretty
, field
, print_names
);
1048 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1049 return print_array(pretty
, field
, print_names
);
1050 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1051 return print_sequence(pretty
, field
, print_names
);
1053 // TODO: log instead
1054 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) class_id
);
1060 int print_stream_packet_context(struct pretty_component
*pretty
,
1061 const struct bt_event
*event
)
1064 const struct bt_packet
*packet
= NULL
;
1065 const struct bt_field
*main_field
= NULL
;
1067 packet
= bt_event_borrow_packet_const(event
);
1072 main_field
= bt_packet_borrow_context_field_const(packet
);
1076 if (!pretty
->start_line
) {
1077 g_string_append(pretty
->string
, ", ");
1079 pretty
->start_line
= false;
1080 if (pretty
->options
.print_scope_field_names
) {
1081 print_name_equal(pretty
, "stream.packet.context");
1083 ret
= print_field(pretty
, main_field
,
1084 pretty
->options
.print_context_field_names
,
1085 stream_packet_context_quarks
,
1086 STREAM_PACKET_CONTEXT_QUARKS_LEN
);
1093 int print_event_header_raw(struct pretty_component
*pretty
,
1094 const struct bt_event
*event
)
1097 const struct bt_field
*main_field
= NULL
;
1099 main_field
= bt_event_borrow_header_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
, "stream.event.header");
1110 ret
= print_field(pretty
, main_field
,
1111 pretty
->options
.print_header_field_names
, NULL
, 0);
1118 int print_stream_event_context(struct pretty_component
*pretty
,
1119 const struct bt_event
*event
)
1122 const struct bt_field
*main_field
= NULL
;
1124 main_field
= bt_event_borrow_common_context_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
, "stream.event.context");
1135 ret
= print_field(pretty
, main_field
,
1136 pretty
->options
.print_context_field_names
, NULL
, 0);
1143 int print_event_context(struct pretty_component
*pretty
,
1144 const struct bt_event
*event
)
1147 const struct bt_field
*main_field
= NULL
;
1149 main_field
= bt_event_borrow_specific_context_field_const(event
);
1153 if (!pretty
->start_line
) {
1154 g_string_append(pretty
->string
, ", ");
1156 pretty
->start_line
= false;
1157 if (pretty
->options
.print_scope_field_names
) {
1158 print_name_equal(pretty
, "event.context");
1160 ret
= print_field(pretty
, main_field
,
1161 pretty
->options
.print_context_field_names
, NULL
, 0);
1168 int print_event_payload(struct pretty_component
*pretty
,
1169 const struct bt_event
*event
)
1172 const struct bt_field
*main_field
= NULL
;
1174 main_field
= bt_event_borrow_payload_field_const(event
);
1178 if (!pretty
->start_line
) {
1179 g_string_append(pretty
->string
, ", ");
1181 pretty
->start_line
= false;
1182 if (pretty
->options
.print_scope_field_names
) {
1183 print_name_equal(pretty
, "event.fields");
1185 ret
= print_field(pretty
, main_field
,
1186 pretty
->options
.print_payload_field_names
, NULL
, 0);
1193 int flush_buf(FILE *stream
, struct pretty_component
*pretty
)
1197 if (pretty
->string
->len
== 0) {
1201 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, stream
) != 1) {
1210 int pretty_print_event(struct pretty_component
*pretty
,
1211 struct bt_notification
*event_notif
)
1214 const struct bt_event
*event
=
1215 bt_notification_event_borrow_event(event_notif
);
1218 pretty
->start_line
= true;
1219 g_string_assign(pretty
->string
, "");
1220 ret
= print_event_header(pretty
, event
);
1225 ret
= print_stream_packet_context(pretty
, event
);
1230 if (pretty
->options
.verbose
) {
1231 ret
= print_event_header_raw(pretty
, event
);
1237 ret
= print_stream_event_context(pretty
, event
);
1242 ret
= print_event_context(pretty
, event
);
1247 ret
= print_event_payload(pretty
, event
);
1252 g_string_append_c(pretty
->string
, '\n');
1253 if (flush_buf(pretty
->out
, pretty
)) {
1263 int print_discarded_elements_msg(
1264 struct pretty_component
*pretty
, const struct bt_packet
*packet
,
1265 uint64_t count
, const char *elem_type
)
1269 const struct bt_stream
*stream
= NULL
;
1270 const struct bt_stream_class
*stream_class
= NULL
;
1271 const struct bt_trace
*trace
= NULL
;
1272 const char *stream_name
;
1273 const char *trace_name
;
1274 const unsigned char *trace_uuid
;
1275 int64_t stream_class_id
;
1277 struct bt_clock_value
*begin_clock_value
= NULL
;
1278 struct bt_clock_value
*end_clock_value
= NULL
;
1282 stream
= bt_packet_borrow_stream_const(packet
);
1284 stream_name
= bt_stream_get_name(stream
);
1286 /* Stream class ID */
1287 stream_class
= bt_stream_borrow_class_const(stream
);
1288 BT_ASSERT(stream_class
);
1289 stream_class_id
= bt_stream_class_get_id(stream_class
);
1292 stream_id
= bt_stream_get_id(stream
);
1295 trace
= bt_stream_class_borrow_trace_const(stream_class
);
1297 trace_name
= bt_trace_get_name(trace
);
1299 trace_name
= "(unknown)";
1303 trace_uuid
= bt_trace_get_uuid(trace
);
1305 /* Beginning and end times */
1306 (void) bt_packet_borrow_previous_packet_default_end_clock_value_const(
1307 packet
, &begin_clock_value
);
1308 (void) bt_packet_borrow_default_end_clock_value_const(packet
,
1311 /* Format message */
1312 g_string_assign(pretty
->string
, "");
1313 g_string_append_printf(pretty
->string
,
1314 "%s%sWARNING%s%s: Tracer discarded %" PRId64
" %s%s ",
1315 bt_common_color_fg_yellow(),
1316 bt_common_color_bold(),
1317 bt_common_color_reset(),
1318 bt_common_color_fg_yellow(),
1319 count
, elem_type
, count
== 1 ? "" : "s");
1321 if (begin_clock_value
&& end_clock_value
) {
1322 g_string_append(pretty
->string
, "between [");
1323 print_timestamp_wall(pretty
, begin_clock_value
);
1324 g_string_append(pretty
->string
, "] and [");
1325 print_timestamp_wall(pretty
, end_clock_value
);
1326 g_string_append(pretty
->string
, "]");
1328 g_string_append(pretty
->string
, "(unknown time range)");
1331 g_string_append_printf(pretty
->string
, " in trace \"%s\" ", trace_name
);
1334 g_string_append_printf(pretty
->string
,
1335 "(UUID: %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x) ",
1353 g_string_append(pretty
->string
, "(no UUID) ");
1356 g_string_append_printf(pretty
->string
,
1357 "within stream \"%s\" (stream class ID: %" PRId64
", ",
1358 stream_name
, stream_class_id
);
1360 if (stream_id
>= 0) {
1361 g_string_append_printf(pretty
->string
,
1362 "stream ID: %" PRId64
, stream_id
);
1364 g_string_append(pretty
->string
, "no stream ID");
1367 g_string_append_printf(pretty
->string
, ").%s\n",
1368 bt_common_color_reset());
1371 * Print to standard error stream to remain backward compatible
1372 * with Babeltrace 1.
1374 if (flush_buf(stderr
, pretty
)) {
1384 int pretty_print_packet(struct pretty_component
*pretty
,
1385 struct bt_notification
*packet_beginning_notif
)
1388 const struct bt_packet
*packet
= bt_notification_packet_begin_borrow_packet_const(
1389 packet_beginning_notif
);
1393 if (bt_packet_get_discarded_event_count(packet
, &count
) ==
1394 BT_PACKET_PROPERTY_AVAILABILITY_AVAILABLE
&&
1396 status
= print_discarded_elements_msg(pretty
, packet
,
1403 if (bt_packet_get_discarded_packet_count(packet
, &count
) ==
1404 BT_PACKET_PROPERTY_AVAILABILITY_AVAILABLE
&&
1406 status
= print_discarded_elements_msg(pretty
, packet
,