4 * Babeltrace CTF Text Output Plugin Event Printing
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <babeltrace/babeltrace.h>
31 #include <babeltrace/bitfield-internal.h>
32 #include <babeltrace/common-internal.h>
33 #include <babeltrace/compat/time-internal.h>
34 #include <babeltrace/assert-internal.h>
39 #define NSEC_PER_SEC 1000000000LL
41 #define COLOR_NAME BT_COMMON_COLOR_BOLD
42 #define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN
43 #define COLOR_RST BT_COMMON_COLOR_RESET
44 #define COLOR_STRING_VALUE BT_COMMON_COLOR_BOLD
45 #define COLOR_NUMBER_VALUE BT_COMMON_COLOR_BOLD
46 #define COLOR_ENUM_MAPPING_NAME BT_COMMON_COLOR_BOLD
47 #define COLOR_UNKNOWN BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED
48 #define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA
49 #define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW
52 int64_t real_timestamp
; /* Relative to UNIX epoch. */
53 uint64_t clock_value
; /* In cycles. */
57 enum bt_component_status
print_field(struct pretty_component
*pretty
,
58 struct bt_field
*field
, bool print_names
,
59 GQuark
*filters_fields
, int filter_array_len
);
62 void print_name_equal(struct pretty_component
*pretty
, const char *name
)
64 if (pretty
->use_colors
) {
65 g_string_append_printf(pretty
->string
, "%s%s%s = ", COLOR_NAME
,
68 g_string_append_printf(pretty
->string
, "%s = ", name
);
73 void print_field_name_equal(struct pretty_component
*pretty
, const char *name
)
75 if (pretty
->use_colors
) {
76 g_string_append_printf(pretty
->string
, "%s%s%s = ",
77 COLOR_FIELD_NAME
, name
, COLOR_RST
);
79 g_string_append_printf(pretty
->string
, "%s = ", name
);
84 void print_timestamp_cycles(struct pretty_component
*pretty
,
85 struct bt_event
*event
)
87 struct bt_clock_value
*clock_value
;
89 enum bt_clock_value_status cv_status
;
91 cv_status
= bt_event_borrow_default_clock_value(event
, &clock_value
);
92 if (cv_status
!= BT_CLOCK_VALUE_STATUS_KNOWN
|| !clock_value
) {
93 g_string_append(pretty
->string
, "????????????????????");
97 cycles
= bt_clock_value_get_value(clock_value
);
98 g_string_append_printf(pretty
->string
, "%020" PRIu64
, cycles
);
100 if (pretty
->last_cycles_timestamp
!= -1ULL) {
101 pretty
->delta_cycles
= cycles
- pretty
->last_cycles_timestamp
;
103 pretty
->last_cycles_timestamp
= cycles
;
107 void print_timestamp_wall(struct pretty_component
*pretty
,
108 struct bt_clock_value
*clock_value
)
111 int64_t ts_nsec
= 0; /* add configurable offset */
112 int64_t ts_sec
= 0; /* add configurable offset */
113 uint64_t ts_sec_abs
, ts_nsec_abs
;
117 g_string_append(pretty
->string
, "??:??:??.?????????");
121 ret
= bt_clock_value_get_ns_from_origin(clock_value
, &ts_nsec
);
123 // TODO: log, this is unexpected
124 g_string_append(pretty
->string
, "Error");
128 if (pretty
->last_real_timestamp
!= -1ULL) {
129 pretty
->delta_real_timestamp
= ts_nsec
- pretty
->last_real_timestamp
;
132 pretty
->last_real_timestamp
= ts_nsec
;
133 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
134 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
136 if (ts_sec
>= 0 && ts_nsec
>= 0) {
139 ts_nsec_abs
= ts_nsec
;
140 } else if (ts_sec
> 0 && ts_nsec
< 0) {
142 ts_sec_abs
= ts_sec
- 1;
143 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
144 } else if (ts_sec
== 0 && ts_nsec
< 0) {
147 ts_nsec_abs
= -ts_nsec
;
148 } else if (ts_sec
< 0 && ts_nsec
> 0) {
150 ts_sec_abs
= -(ts_sec
+ 1);
151 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
152 } else if (ts_sec
< 0 && ts_nsec
== 0) {
154 ts_sec_abs
= -ts_sec
;
155 ts_nsec_abs
= ts_nsec
;
156 } else { /* (ts_sec < 0 && ts_nsec < 0) */
158 ts_sec_abs
= -ts_sec
;
159 ts_nsec_abs
= -ts_nsec
;
162 if (!pretty
->options
.clock_seconds
) {
164 time_t time_s
= (time_t) ts_sec_abs
;
166 if (is_negative
&& !pretty
->negative_timestamp_warning_done
) {
168 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
169 pretty
->negative_timestamp_warning_done
= true;
173 if (!pretty
->options
.clock_gmt
) {
176 res
= bt_localtime_r(&time_s
, &tm
);
179 fprintf(stderr
, "[warning] Unable to get localtime.\n");
185 res
= bt_gmtime_r(&time_s
, &tm
);
188 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
192 if (pretty
->options
.clock_date
) {
196 /* Print date and time */
197 res
= strftime(timestr
, sizeof(timestr
),
201 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
205 g_string_append(pretty
->string
, timestr
);
208 /* Print time in HH:MM:SS.ns */
209 g_string_append_printf(pretty
->string
,
210 "%02d:%02d:%02d.%09" PRIu64
, tm
.tm_hour
, tm
.tm_min
,
211 tm
.tm_sec
, ts_nsec_abs
);
215 g_string_append_printf(pretty
->string
, "%s%" PRId64
".%09" PRIu64
,
216 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
222 enum bt_component_status
print_event_timestamp(struct pretty_component
*pretty
,
223 struct bt_event
*event
, bool *start_line
)
225 bool print_names
= pretty
->options
.print_header_field_names
;
226 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
227 struct bt_stream
*stream
= NULL
;
228 struct bt_stream_class
*stream_class
= NULL
;
229 struct bt_trace
*trace
= NULL
;
230 struct bt_clock_value
*clock_value
= NULL
;
231 enum bt_clock_value_status cv_status
;
233 stream
= bt_event_borrow_stream(event
);
235 ret
= BT_COMPONENT_STATUS_ERROR
;
239 stream_class
= bt_stream_borrow_class(stream
);
241 ret
= BT_COMPONENT_STATUS_ERROR
;
244 trace
= bt_stream_class_borrow_trace(stream_class
);
246 ret
= BT_COMPONENT_STATUS_ERROR
;
250 cv_status
= bt_event_borrow_default_clock_value(event
, &clock_value
);
251 if (cv_status
!= BT_CLOCK_VALUE_STATUS_KNOWN
|| !clock_value
) {
252 /* No default clock value: skip the timestamp without an error */
257 print_name_equal(pretty
, "timestamp");
259 g_string_append(pretty
->string
, "[");
261 if (pretty
->use_colors
) {
262 g_string_append(pretty
->string
, COLOR_TIMESTAMP
);
264 if (pretty
->options
.print_timestamp_cycles
) {
265 print_timestamp_cycles(pretty
, event
);
268 cv_status
= bt_event_borrow_default_clock_value(event
,
270 print_timestamp_wall(pretty
, clock_value
);
272 if (pretty
->use_colors
) {
273 g_string_append(pretty
->string
, COLOR_RST
);
277 g_string_append(pretty
->string
, "] ");
279 if (pretty
->options
.print_delta_field
) {
281 g_string_append(pretty
->string
, ", ");
282 print_name_equal(pretty
, "delta");
284 g_string_append(pretty
->string
, "(");
286 if (pretty
->options
.print_timestamp_cycles
) {
287 if (pretty
->delta_cycles
== -1ULL) {
288 g_string_append(pretty
->string
,
289 "+??????????\?\?) "); /* Not a trigraph. */
291 g_string_append_printf(pretty
->string
,
292 "+%012" PRIu64
, pretty
->delta_cycles
);
295 if (pretty
->delta_real_timestamp
!= -1ULL) {
296 uint64_t delta_sec
, delta_nsec
, delta
;
298 delta
= pretty
->delta_real_timestamp
;
299 delta_sec
= delta
/ NSEC_PER_SEC
;
300 delta_nsec
= delta
% NSEC_PER_SEC
;
301 g_string_append_printf(pretty
->string
,
302 "+%" PRIu64
".%09" PRIu64
,
303 delta_sec
, delta_nsec
);
305 g_string_append(pretty
->string
, "+?.?????????");
309 g_string_append(pretty
->string
, ") ");
312 *start_line
= !print_names
;
319 enum bt_component_status
print_event_header(struct pretty_component
*pretty
,
320 struct bt_event
*event
)
322 bool print_names
= pretty
->options
.print_header_field_names
;
323 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
324 struct bt_event_class
*event_class
= NULL
;
325 struct bt_stream_class
*stream_class
= NULL
;
326 struct bt_trace
*trace_class
= NULL
;
328 enum bt_property_availability prop_avail
;
330 event_class
= bt_event_borrow_class(event
);
332 ret
= BT_COMPONENT_STATUS_ERROR
;
335 stream_class
= bt_event_class_borrow_stream_class(event_class
);
337 ret
= BT_COMPONENT_STATUS_ERROR
;
340 trace_class
= bt_stream_class_borrow_trace(stream_class
);
342 ret
= BT_COMPONENT_STATUS_ERROR
;
345 ret
= print_event_timestamp(pretty
, event
, &pretty
->start_line
);
346 if (ret
!= BT_COMPONENT_STATUS_OK
) {
349 if (pretty
->options
.print_trace_field
) {
352 name
= bt_trace_get_name(trace_class
);
354 if (!pretty
->start_line
) {
355 g_string_append(pretty
->string
, ", ");
358 print_name_equal(pretty
, "trace");
361 g_string_append(pretty
->string
, name
);
364 g_string_append(pretty
->string
, " ");
368 if (pretty
->options
.print_trace_hostname_field
) {
369 struct bt_value
*hostname_str
;
371 hostname_str
= bt_trace_borrow_environment_entry_value_by_name(
372 trace_class
, "hostname");
376 if (!pretty
->start_line
) {
377 g_string_append(pretty
->string
, ", ");
380 print_name_equal(pretty
, "trace:hostname");
382 str
= bt_value_string_get(hostname_str
);
383 g_string_append(pretty
->string
, str
);
387 if (pretty
->options
.print_trace_domain_field
) {
388 struct bt_value
*domain_str
;
390 domain_str
= bt_trace_borrow_environment_entry_value_by_name(
391 trace_class
, "domain");
395 if (!pretty
->start_line
) {
396 g_string_append(pretty
->string
, ", ");
399 print_name_equal(pretty
, "trace:domain");
400 } else if (dom_print
) {
401 g_string_append(pretty
->string
, ":");
403 str
= bt_value_string_get(domain_str
);
404 g_string_append(pretty
->string
, str
);
408 if (pretty
->options
.print_trace_procname_field
) {
409 struct bt_value
*procname_str
;
411 procname_str
= bt_trace_borrow_environment_entry_value_by_name(
412 trace_class
, "procname");
416 if (!pretty
->start_line
) {
417 g_string_append(pretty
->string
, ", ");
420 print_name_equal(pretty
, "trace:procname");
421 } else if (dom_print
) {
422 g_string_append(pretty
->string
, ":");
424 str
= bt_value_string_get(procname_str
);
425 g_string_append(pretty
->string
, str
);
429 if (pretty
->options
.print_trace_vpid_field
) {
430 struct bt_value
*vpid_value
;
432 vpid_value
= bt_trace_borrow_environment_entry_value_by_name(
433 trace_class
, "vpid");
437 if (!pretty
->start_line
) {
438 g_string_append(pretty
->string
, ", ");
441 print_name_equal(pretty
, "trace:vpid");
442 } else if (dom_print
) {
443 g_string_append(pretty
->string
, ":");
445 value
= bt_value_integer_get(vpid_value
);
446 g_string_append_printf(pretty
->string
,
447 "(%" PRId64
")", value
);
451 if (pretty
->options
.print_loglevel_field
) {
452 static const char *log_level_names
[] = {
453 [ BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
] = "TRACE_EMERG",
454 [ BT_EVENT_CLASS_LOG_LEVEL_ALERT
] = "TRACE_ALERT",
455 [ BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
] = "TRACE_CRIT",
456 [ BT_EVENT_CLASS_LOG_LEVEL_ERROR
] = "TRACE_ERR",
457 [ BT_EVENT_CLASS_LOG_LEVEL_WARNING
] = "TRACE_WARNING",
458 [ BT_EVENT_CLASS_LOG_LEVEL_NOTICE
] = "TRACE_NOTICE",
459 [ BT_EVENT_CLASS_LOG_LEVEL_INFO
] = "TRACE_INFO",
460 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
461 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
462 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
463 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
464 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
465 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
466 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
467 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG
] = "TRACE_DEBUG",
469 enum bt_event_class_log_level log_level
;
470 const char *log_level_str
= NULL
;
472 prop_avail
= bt_event_class_get_log_level(event_class
,
474 if (prop_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
475 log_level_str
= log_level_names
[log_level
];
476 BT_ASSERT(log_level_str
);
478 if (!pretty
->start_line
) {
479 g_string_append(pretty
->string
, ", ");
482 print_name_equal(pretty
, "loglevel");
483 } else if (dom_print
) {
484 g_string_append(pretty
->string
, ":");
487 g_string_append(pretty
->string
, log_level_str
);
488 g_string_append_printf(
489 pretty
->string
, " (%d)", (int) log_level
);
493 if (pretty
->options
.print_emf_field
) {
496 uri_str
= bt_event_class_get_emf_uri(event_class
);
498 if (!pretty
->start_line
) {
499 g_string_append(pretty
->string
, ", ");
502 print_name_equal(pretty
, "model.emf.uri");
503 } else if (dom_print
) {
504 g_string_append(pretty
->string
, ":");
507 g_string_append(pretty
->string
, uri_str
);
511 if (dom_print
&& !print_names
) {
512 g_string_append(pretty
->string
, " ");
514 if (!pretty
->start_line
) {
515 g_string_append(pretty
->string
, ", ");
517 pretty
->start_line
= true;
519 print_name_equal(pretty
, "name");
521 if (pretty
->use_colors
) {
522 g_string_append(pretty
->string
, COLOR_EVENT_NAME
);
524 g_string_append(pretty
->string
, bt_event_class_get_name(event_class
));
525 if (pretty
->use_colors
) {
526 g_string_append(pretty
->string
, COLOR_RST
);
529 g_string_append(pretty
->string
, ": ");
531 g_string_append(pretty
->string
, ", ");
539 enum bt_component_status
print_integer(struct pretty_component
*pretty
,
540 struct bt_field
*field
)
542 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
543 enum bt_field_class_integer_preferred_display_base base
;
544 struct bt_field_class
*int_fc
;
549 bool rst_color
= false;
550 enum bt_field_class_type ft_type
;
552 int_fc
= bt_field_borrow_class(field
);
554 ft_type
= bt_field_get_class_type(field
);
555 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
556 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
557 v
.u
= bt_field_unsigned_integer_get_value(field
);
559 v
.s
= bt_field_signed_integer_get_value(field
);
562 if (pretty
->use_colors
) {
563 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
567 base
= bt_field_class_integer_get_preferred_display_base(int_fc
);
569 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
573 len
= bt_field_class_integer_get_field_value_range(int_fc
);
574 g_string_append(pretty
->string
, "0b");
575 v
.u
= _bt_piecewise_lshift(v
.u
, 64 - len
);
576 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
577 g_string_append_printf(pretty
->string
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
578 v
.u
= _bt_piecewise_lshift(v
.u
, 1);
582 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
584 if (ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
585 ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
) {
588 len
= bt_field_class_integer_get_field_value_range(
594 /* Round length to the nearest 3-bit */
595 rounded_len
= (((len
- 1) / 3) + 1) * 3;
596 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
600 g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
603 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
604 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
605 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
606 g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
608 g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
611 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
615 len
= bt_field_class_integer_get_field_value_range(int_fc
);
617 /* Round length to the nearest nibble */
618 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
620 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
623 g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
627 ret
= BT_COMPONENT_STATUS_ERROR
;
632 g_string_append(pretty
->string
, COLOR_RST
);
638 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
642 g_string_append_c(pretty
->string
, '"');
644 for (i
= 0; i
< strlen(str
); i
++) {
645 /* Escape sequences not recognized by iscntrl(). */
648 g_string_append(pretty
->string
, "\\\\");
651 g_string_append(pretty
->string
, "\\\'");
654 g_string_append(pretty
->string
, "\\\"");
657 g_string_append(pretty
->string
, "\\\?");
661 /* Standard characters. */
662 if (!iscntrl(str
[i
])) {
663 g_string_append_c(pretty
->string
, str
[i
]);
669 g_string_append(pretty
->string
, "\\0");
672 g_string_append(pretty
->string
, "\\a");
675 g_string_append(pretty
->string
, "\\b");
678 g_string_append(pretty
->string
, "\\e");
681 g_string_append(pretty
->string
, "\\f");
684 g_string_append(pretty
->string
, "\\n");
687 g_string_append(pretty
->string
, "\\r");
690 g_string_append(pretty
->string
, "\\t");
693 g_string_append(pretty
->string
, "\\v");
696 /* Unhandled control-sequence, print as hex. */
697 g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
702 g_string_append_c(pretty
->string
, '"');
706 enum bt_component_status
print_enum(struct pretty_component
*pretty
,
707 struct bt_field
*field
)
709 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
710 struct bt_field_class
*enumeration_field_class
= NULL
;
711 bt_field_class_enumeration_mapping_label_array label_array
;
712 uint64_t label_count
;
715 enumeration_field_class
= bt_field_borrow_class(field
);
716 if (!enumeration_field_class
) {
717 ret
= BT_COMPONENT_STATUS_ERROR
;
721 switch (bt_field_get_class_type(field
)) {
722 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
723 ret
= bt_field_unsigned_enumeration_get_mapping_labels(field
,
724 &label_array
, &label_count
);
726 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
727 ret
= bt_field_signed_enumeration_get_mapping_labels(field
,
728 &label_array
, &label_count
);
735 ret
= BT_COMPONENT_STATUS_ERROR
;
739 g_string_append(pretty
->string
, "( ");
740 if (label_count
== 0) {
741 if (pretty
->use_colors
) {
742 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
744 g_string_append(pretty
->string
, "<unknown>");
745 if (pretty
->use_colors
) {
746 g_string_append(pretty
->string
, COLOR_RST
);
750 for (i
= 0; i
< label_count
; i
++) {
751 const char *mapping_name
= label_array
[i
];
754 g_string_append(pretty
->string
, ", ");
756 if (pretty
->use_colors
) {
757 g_string_append(pretty
->string
, COLOR_ENUM_MAPPING_NAME
);
759 print_escape_string(pretty
, mapping_name
);
760 if (pretty
->use_colors
) {
761 g_string_append(pretty
->string
, COLOR_RST
);
765 g_string_append(pretty
->string
, " : container = ");
766 ret
= print_integer(pretty
, field
);
767 if (ret
!= BT_COMPONENT_STATUS_OK
) {
770 g_string_append(pretty
->string
, " )");
776 int filter_field_name(struct pretty_component
*pretty
, const char *field_name
,
777 GQuark
*filter_fields
, int filter_array_len
)
780 GQuark field_quark
= g_quark_try_string(field_name
);
782 if (!field_quark
|| pretty
->options
.verbose
) {
786 for (i
= 0; i
< filter_array_len
; i
++) {
787 if (field_quark
== filter_fields
[i
]) {
795 enum bt_component_status
print_struct_field(struct pretty_component
*pretty
,
796 struct bt_field
*_struct
,
797 struct bt_field_class
*struct_class
,
798 uint64_t i
, bool print_names
, uint64_t *nr_printed_fields
,
799 GQuark
*filter_fields
, int filter_array_len
)
801 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
802 const char *field_name
;
803 struct bt_field
*field
= NULL
;
804 struct bt_field_class
*field_class
= NULL
;;
806 field
= bt_field_structure_borrow_member_field_by_index(_struct
, i
);
808 ret
= BT_COMPONENT_STATUS_ERROR
;
812 bt_field_class_structure_borrow_member_by_index(struct_class
, i
,
813 &field_name
, &field_class
);
815 if (filter_fields
&& !filter_field_name(pretty
, field_name
,
816 filter_fields
, filter_array_len
)) {
817 ret
= BT_COMPONENT_STATUS_OK
;
821 if (*nr_printed_fields
> 0) {
822 g_string_append(pretty
->string
, ", ");
824 g_string_append(pretty
->string
, " ");
827 print_field_name_equal(pretty
, field_name
);
829 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
830 *nr_printed_fields
+= 1;
837 enum bt_component_status
print_struct(struct pretty_component
*pretty
,
838 struct bt_field
*_struct
, bool print_names
,
839 GQuark
*filter_fields
, int filter_array_len
)
841 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
842 struct bt_field_class
*struct_class
= NULL
;
843 uint64_t nr_fields
, i
, nr_printed_fields
;
845 struct_class
= bt_field_borrow_class(_struct
);
847 ret
= BT_COMPONENT_STATUS_ERROR
;
850 nr_fields
= bt_field_class_structure_get_member_count(struct_class
);
852 ret
= BT_COMPONENT_STATUS_ERROR
;
855 g_string_append(pretty
->string
, "{");
857 nr_printed_fields
= 0;
858 for (i
= 0; i
< nr_fields
; i
++) {
859 ret
= print_struct_field(pretty
, _struct
, struct_class
, i
,
860 print_names
, &nr_printed_fields
, filter_fields
,
862 if (ret
!= BT_COMPONENT_STATUS_OK
) {
867 g_string_append(pretty
->string
, " }");
874 enum bt_component_status
print_array_field(struct pretty_component
*pretty
,
875 struct bt_field
*array
, uint64_t i
, bool print_names
)
877 struct bt_field
*field
= NULL
;
880 g_string_append(pretty
->string
, ", ");
882 g_string_append(pretty
->string
, " ");
885 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
888 field
= bt_field_array_borrow_element_field_by_index(array
, i
);
890 return print_field(pretty
, field
, print_names
, NULL
, 0);
894 enum bt_component_status
print_array(struct pretty_component
*pretty
,
895 struct bt_field
*array
, bool print_names
)
897 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
898 struct bt_field_class
*array_class
= NULL
;
902 array_class
= bt_field_borrow_class(array
);
904 ret
= BT_COMPONENT_STATUS_ERROR
;
907 len
= bt_field_array_get_length(array
);
908 g_string_append(pretty
->string
, "[");
910 for (i
= 0; i
< len
; i
++) {
911 ret
= print_array_field(pretty
, array
, i
, print_names
);
912 if (ret
!= BT_COMPONENT_STATUS_OK
) {
917 g_string_append(pretty
->string
, " ]");
924 enum bt_component_status
print_sequence_field(struct pretty_component
*pretty
,
925 struct bt_field
*seq
, uint64_t i
, bool print_names
)
927 struct bt_field
*field
= NULL
;
930 g_string_append(pretty
->string
, ", ");
932 g_string_append(pretty
->string
, " ");
935 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
938 field
= bt_field_array_borrow_element_field_by_index(seq
, i
);
940 return print_field(pretty
, field
, print_names
, NULL
, 0);
944 enum bt_component_status
print_sequence(struct pretty_component
*pretty
,
945 struct bt_field
*seq
, bool print_names
)
947 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
951 len
= bt_field_array_get_length(seq
);
953 ret
= BT_COMPONENT_STATUS_ERROR
;
957 g_string_append(pretty
->string
, "[");
960 for (i
= 0; i
< len
; i
++) {
961 ret
= print_sequence_field(pretty
, seq
, i
, print_names
);
962 if (ret
!= BT_COMPONENT_STATUS_OK
) {
967 g_string_append(pretty
->string
, " ]");
974 enum bt_component_status
print_variant(struct pretty_component
*pretty
,
975 struct bt_field
*variant
, bool print_names
)
977 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
978 struct bt_field
*field
= NULL
;
980 field
= bt_field_variant_borrow_selected_option_field(variant
);
982 g_string_append(pretty
->string
, "{ ");
985 // TODO: find tag's name using field path
986 // print_field_name_equal(pretty, tag_choice);
988 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
989 if (ret
!= BT_COMPONENT_STATUS_OK
) {
993 g_string_append(pretty
->string
, " }");
1000 enum bt_component_status
print_field(struct pretty_component
*pretty
,
1001 struct bt_field
*field
, bool print_names
,
1002 GQuark
*filter_fields
, int filter_array_len
)
1004 enum bt_field_class_type class_id
;
1006 class_id
= bt_field_get_class_type(field
);
1008 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
1009 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
1010 return print_integer(pretty
, field
);
1011 case BT_FIELD_CLASS_TYPE_REAL
:
1015 v
= bt_field_real_get_value(field
);
1016 if (pretty
->use_colors
) {
1017 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
1019 g_string_append_printf(pretty
->string
, "%g", v
);
1020 if (pretty
->use_colors
) {
1021 g_string_append(pretty
->string
, COLOR_RST
);
1023 return BT_COMPONENT_STATUS_OK
;
1025 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
1026 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
1027 return print_enum(pretty
, field
);
1028 case BT_FIELD_CLASS_TYPE_STRING
:
1032 str
= bt_field_string_get_value(field
);
1034 return BT_COMPONENT_STATUS_ERROR
;
1037 if (pretty
->use_colors
) {
1038 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1040 print_escape_string(pretty
, str
);
1041 if (pretty
->use_colors
) {
1042 g_string_append(pretty
->string
, COLOR_RST
);
1044 return BT_COMPONENT_STATUS_OK
;
1046 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1047 return print_struct(pretty
, field
, print_names
, filter_fields
,
1049 case BT_FIELD_CLASS_TYPE_VARIANT
:
1050 return print_variant(pretty
, field
, print_names
);
1051 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1052 return print_array(pretty
, field
, print_names
);
1053 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1054 return print_sequence(pretty
, field
, print_names
);
1056 // TODO: log instead
1057 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) class_id
);
1058 return BT_COMPONENT_STATUS_ERROR
;
1063 enum bt_component_status
print_stream_packet_context(struct pretty_component
*pretty
,
1064 struct bt_event
*event
)
1066 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1067 struct bt_packet
*packet
= NULL
;
1068 struct bt_field
*main_field
= NULL
;
1070 packet
= bt_event_borrow_packet(event
);
1072 ret
= BT_COMPONENT_STATUS_ERROR
;
1075 main_field
= bt_packet_borrow_context_field(packet
);
1079 if (!pretty
->start_line
) {
1080 g_string_append(pretty
->string
, ", ");
1082 pretty
->start_line
= false;
1083 if (pretty
->options
.print_scope_field_names
) {
1084 print_name_equal(pretty
, "stream.packet.context");
1086 ret
= print_field(pretty
, main_field
,
1087 pretty
->options
.print_context_field_names
,
1088 stream_packet_context_quarks
,
1089 STREAM_PACKET_CONTEXT_QUARKS_LEN
);
1096 enum bt_component_status
print_event_header_raw(struct pretty_component
*pretty
,
1097 struct bt_event
*event
)
1099 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1100 struct bt_field
*main_field
= NULL
;
1102 main_field
= bt_event_borrow_header_field(event
);
1106 if (!pretty
->start_line
) {
1107 g_string_append(pretty
->string
, ", ");
1109 pretty
->start_line
= false;
1110 if (pretty
->options
.print_scope_field_names
) {
1111 print_name_equal(pretty
, "stream.event.header");
1113 ret
= print_field(pretty
, main_field
,
1114 pretty
->options
.print_header_field_names
, NULL
, 0);
1121 enum bt_component_status
print_stream_event_context(struct pretty_component
*pretty
,
1122 struct bt_event
*event
)
1124 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1125 struct bt_field
*main_field
= NULL
;
1127 main_field
= bt_event_borrow_common_context_field(event
);
1131 if (!pretty
->start_line
) {
1132 g_string_append(pretty
->string
, ", ");
1134 pretty
->start_line
= false;
1135 if (pretty
->options
.print_scope_field_names
) {
1136 print_name_equal(pretty
, "stream.event.context");
1138 ret
= print_field(pretty
, main_field
,
1139 pretty
->options
.print_context_field_names
, NULL
, 0);
1146 enum bt_component_status
print_event_context(struct pretty_component
*pretty
,
1147 struct bt_event
*event
)
1149 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1150 struct bt_field
*main_field
= NULL
;
1152 main_field
= bt_event_borrow_specific_context_field(event
);
1156 if (!pretty
->start_line
) {
1157 g_string_append(pretty
->string
, ", ");
1159 pretty
->start_line
= false;
1160 if (pretty
->options
.print_scope_field_names
) {
1161 print_name_equal(pretty
, "event.context");
1163 ret
= print_field(pretty
, main_field
,
1164 pretty
->options
.print_context_field_names
, NULL
, 0);
1171 enum bt_component_status
print_event_payload(struct pretty_component
*pretty
,
1172 struct bt_event
*event
)
1174 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1175 struct bt_field
*main_field
= NULL
;
1177 main_field
= bt_event_borrow_payload_field(event
);
1181 if (!pretty
->start_line
) {
1182 g_string_append(pretty
->string
, ", ");
1184 pretty
->start_line
= false;
1185 if (pretty
->options
.print_scope_field_names
) {
1186 print_name_equal(pretty
, "event.fields");
1188 ret
= print_field(pretty
, main_field
,
1189 pretty
->options
.print_payload_field_names
, NULL
, 0);
1196 int flush_buf(FILE *stream
, struct pretty_component
*pretty
)
1200 if (pretty
->string
->len
== 0) {
1204 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, stream
) != 1) {
1213 enum bt_component_status
pretty_print_event(struct pretty_component
*pretty
,
1214 struct bt_notification
*event_notif
)
1216 enum bt_component_status ret
;
1217 struct bt_event
*event
=
1218 bt_notification_event_borrow_event(event_notif
);
1221 pretty
->start_line
= true;
1222 g_string_assign(pretty
->string
, "");
1223 ret
= print_event_header(pretty
, event
);
1224 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1228 ret
= print_stream_packet_context(pretty
, event
);
1229 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1233 if (pretty
->options
.verbose
) {
1234 ret
= print_event_header_raw(pretty
, event
);
1235 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1240 ret
= print_stream_event_context(pretty
, event
);
1241 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1245 ret
= print_event_context(pretty
, event
);
1246 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1250 ret
= print_event_payload(pretty
, event
);
1251 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1255 g_string_append_c(pretty
->string
, '\n');
1256 if (flush_buf(pretty
->out
, pretty
)) {
1257 ret
= BT_COMPONENT_STATUS_ERROR
;
1266 enum bt_component_status
print_discarded_elements_msg(
1267 struct pretty_component
*pretty
, struct bt_packet
*packet
,
1268 uint64_t count
, const char *elem_type
)
1271 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1272 struct bt_stream
*stream
= NULL
;
1273 struct bt_stream_class
*stream_class
= NULL
;
1274 struct bt_trace
*trace
= NULL
;
1275 const char *stream_name
;
1276 const char *trace_name
;
1277 const unsigned char *trace_uuid
;
1278 int64_t stream_class_id
;
1280 struct bt_clock_value
*begin_clock_value
= NULL
;
1281 struct bt_clock_value
*end_clock_value
= NULL
;
1285 stream
= bt_packet_borrow_stream(packet
);
1287 stream_name
= bt_stream_get_name(stream
);
1289 /* Stream class ID */
1290 stream_class
= bt_stream_borrow_class(stream
);
1291 BT_ASSERT(stream_class
);
1292 stream_class_id
= bt_stream_class_get_id(stream_class
);
1295 stream_id
= bt_stream_get_id(stream
);
1298 trace
= bt_stream_class_borrow_trace(stream_class
);
1300 trace_name
= bt_trace_get_name(trace
);
1302 trace_name
= "(unknown)";
1306 trace_uuid
= bt_trace_get_uuid(trace
);
1308 /* Beginning and end times */
1309 (void) bt_packet_borrow_previous_packet_default_end_clock_value(
1310 packet
, &begin_clock_value
);
1311 (void) bt_packet_borrow_default_end_clock_value(packet
,
1314 /* Format message */
1315 g_string_assign(pretty
->string
, "");
1316 g_string_append_printf(pretty
->string
,
1317 "%s%sWARNING%s%s: Tracer discarded %" PRId64
" %s%s ",
1318 bt_common_color_fg_yellow(),
1319 bt_common_color_bold(),
1320 bt_common_color_reset(),
1321 bt_common_color_fg_yellow(),
1322 count
, elem_type
, count
== 1 ? "" : "s");
1324 if (begin_clock_value
&& end_clock_value
) {
1325 g_string_append(pretty
->string
, "between [");
1326 print_timestamp_wall(pretty
, begin_clock_value
);
1327 g_string_append(pretty
->string
, "] and [");
1328 print_timestamp_wall(pretty
, end_clock_value
);
1329 g_string_append(pretty
->string
, "]");
1331 g_string_append(pretty
->string
, "(unknown time range)");
1334 g_string_append_printf(pretty
->string
, " in trace \"%s\" ", trace_name
);
1337 g_string_append_printf(pretty
->string
,
1338 "(UUID: %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x) ",
1356 g_string_append(pretty
->string
, "(no UUID) ");
1359 g_string_append_printf(pretty
->string
,
1360 "within stream \"%s\" (stream class ID: %" PRId64
", ",
1361 stream_name
, stream_class_id
);
1363 if (stream_id
>= 0) {
1364 g_string_append_printf(pretty
->string
,
1365 "stream ID: %" PRId64
, stream_id
);
1367 g_string_append(pretty
->string
, "no stream ID");
1370 g_string_append_printf(pretty
->string
, ").%s\n",
1371 bt_common_color_reset());
1374 * Print to standard error stream to remain backward compatible
1375 * with Babeltrace 1.
1377 if (flush_buf(stderr
, pretty
)) {
1378 ret
= BT_COMPONENT_STATUS_ERROR
;
1387 enum bt_component_status
pretty_print_packet(struct pretty_component
*pretty
,
1388 struct bt_notification
*packet_beginning_notif
)
1391 struct bt_packet
*packet
= bt_notification_packet_begin_borrow_packet(
1392 packet_beginning_notif
);
1394 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
1396 if (bt_packet_get_discarded_event_count(packet
, &count
) ==
1397 BT_PACKET_PROPERTY_AVAILABILITY_AVAILABLE
&&
1399 status
= print_discarded_elements_msg(pretty
, packet
,
1401 if (status
!= BT_COMPONENT_STATUS_OK
) {
1406 if (bt_packet_get_discarded_packet_count(packet
, &count
) ==
1407 BT_PACKET_PROPERTY_AVAILABILITY_AVAILABLE
&&
1409 status
= print_discarded_elements_msg(pretty
, packet
,
1411 if (status
!= BT_COMPONENT_STATUS_OK
) {