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_clock_class
*clock_class
,
86 struct bt_event
*event
)
89 struct bt_clock_value
*clock_value
;
92 clock_value
= bt_event_borrow_clock_value(event
, clock_class
);
94 g_string_append(pretty
->string
, "????????????????????");
98 ret
= bt_clock_value_get_value(clock_value
, &cycles
);
100 // TODO: log, this is unexpected
101 g_string_append(pretty
->string
, "Error");
105 g_string_append_printf(pretty
->string
, "%020" PRIu64
, cycles
);
107 if (pretty
->last_cycles_timestamp
!= -1ULL) {
108 pretty
->delta_cycles
= cycles
- pretty
->last_cycles_timestamp
;
110 pretty
->last_cycles_timestamp
= cycles
;
114 void print_timestamp_wall(struct pretty_component
*pretty
,
115 struct bt_clock_value
*clock_value
)
118 int64_t ts_nsec
= 0; /* add configurable offset */
119 int64_t ts_sec
= 0; /* add configurable offset */
120 uint64_t ts_sec_abs
, ts_nsec_abs
;
124 g_string_append(pretty
->string
, "??:??:??.?????????");
128 ret
= bt_clock_value_get_value_ns_from_epoch(clock_value
, &ts_nsec
);
130 // TODO: log, this is unexpected
131 g_string_append(pretty
->string
, "Error");
135 if (pretty
->last_real_timestamp
!= -1ULL) {
136 pretty
->delta_real_timestamp
= ts_nsec
- pretty
->last_real_timestamp
;
139 pretty
->last_real_timestamp
= ts_nsec
;
140 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
141 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
143 if (ts_sec
>= 0 && ts_nsec
>= 0) {
146 ts_nsec_abs
= ts_nsec
;
147 } else if (ts_sec
> 0 && ts_nsec
< 0) {
149 ts_sec_abs
= ts_sec
- 1;
150 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
151 } else if (ts_sec
== 0 && ts_nsec
< 0) {
154 ts_nsec_abs
= -ts_nsec
;
155 } else if (ts_sec
< 0 && ts_nsec
> 0) {
157 ts_sec_abs
= -(ts_sec
+ 1);
158 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
159 } else if (ts_sec
< 0 && ts_nsec
== 0) {
161 ts_sec_abs
= -ts_sec
;
162 ts_nsec_abs
= ts_nsec
;
163 } else { /* (ts_sec < 0 && ts_nsec < 0) */
165 ts_sec_abs
= -ts_sec
;
166 ts_nsec_abs
= -ts_nsec
;
169 if (!pretty
->options
.clock_seconds
) {
171 time_t time_s
= (time_t) ts_sec_abs
;
173 if (is_negative
&& !pretty
->negative_timestamp_warning_done
) {
175 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
176 pretty
->negative_timestamp_warning_done
= true;
180 if (!pretty
->options
.clock_gmt
) {
183 res
= bt_localtime_r(&time_s
, &tm
);
186 fprintf(stderr
, "[warning] Unable to get localtime.\n");
192 res
= bt_gmtime_r(&time_s
, &tm
);
195 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
199 if (pretty
->options
.clock_date
) {
203 /* Print date and time */
204 res
= strftime(timestr
, sizeof(timestr
),
208 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
212 g_string_append(pretty
->string
, timestr
);
215 /* Print time in HH:MM:SS.ns */
216 g_string_append_printf(pretty
->string
,
217 "%02d:%02d:%02d.%09" PRIu64
, tm
.tm_hour
, tm
.tm_min
,
218 tm
.tm_sec
, ts_nsec_abs
);
222 g_string_append_printf(pretty
->string
, "%s%" PRId64
".%09" PRIu64
,
223 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
229 enum bt_component_status
print_event_timestamp(struct pretty_component
*pretty
,
230 struct bt_event
*event
,
231 struct bt_clock_class_priority_map
*cc_prio_map
,
234 bool print_names
= pretty
->options
.print_header_field_names
;
235 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
236 struct bt_stream
*stream
= NULL
;
237 struct bt_stream_class
*stream_class
= NULL
;
238 struct bt_trace
*trace
= NULL
;
239 struct bt_clock_class
*clock_class
= NULL
;
241 stream
= bt_event_borrow_stream(event
);
243 ret
= BT_COMPONENT_STATUS_ERROR
;
247 stream_class
= bt_stream_borrow_class(stream
);
249 ret
= BT_COMPONENT_STATUS_ERROR
;
252 trace
= bt_stream_class_borrow_trace(stream_class
);
254 ret
= BT_COMPONENT_STATUS_ERROR
;
258 if (bt_clock_class_priority_map_get_clock_class_count(cc_prio_map
) == 0) {
259 /* No clock class: skip the timestamp without an error */
264 bt_clock_class_priority_map_borrow_highest_priority_clock_class(
267 ret
= BT_COMPONENT_STATUS_ERROR
;
272 print_name_equal(pretty
, "timestamp");
274 g_string_append(pretty
->string
, "[");
276 if (pretty
->use_colors
) {
277 g_string_append(pretty
->string
, COLOR_TIMESTAMP
);
279 if (pretty
->options
.print_timestamp_cycles
) {
280 print_timestamp_cycles(pretty
, clock_class
, event
);
282 struct bt_clock_value
*clock_value
=
283 bt_event_borrow_clock_value(event
, clock_class
);
285 print_timestamp_wall(pretty
, clock_value
);
287 if (pretty
->use_colors
) {
288 g_string_append(pretty
->string
, COLOR_RST
);
292 g_string_append(pretty
->string
, "] ");
294 if (pretty
->options
.print_delta_field
) {
296 g_string_append(pretty
->string
, ", ");
297 print_name_equal(pretty
, "delta");
299 g_string_append(pretty
->string
, "(");
301 if (pretty
->options
.print_timestamp_cycles
) {
302 if (pretty
->delta_cycles
== -1ULL) {
303 g_string_append(pretty
->string
,
304 "+??????????\?\?) "); /* Not a trigraph. */
306 g_string_append_printf(pretty
->string
,
307 "+%012" PRIu64
, pretty
->delta_cycles
);
310 if (pretty
->delta_real_timestamp
!= -1ULL) {
311 uint64_t delta_sec
, delta_nsec
, delta
;
313 delta
= pretty
->delta_real_timestamp
;
314 delta_sec
= delta
/ NSEC_PER_SEC
;
315 delta_nsec
= delta
% NSEC_PER_SEC
;
316 g_string_append_printf(pretty
->string
,
317 "+%" PRIu64
".%09" PRIu64
,
318 delta_sec
, delta_nsec
);
320 g_string_append(pretty
->string
, "+?.?????????");
324 g_string_append(pretty
->string
, ") ");
327 *start_line
= !print_names
;
334 enum bt_component_status
print_event_header(struct pretty_component
*pretty
,
335 struct bt_event
*event
,
336 struct bt_clock_class_priority_map
*cc_prio_map
)
338 bool print_names
= pretty
->options
.print_header_field_names
;
339 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
340 struct bt_event_class
*event_class
= NULL
;
341 struct bt_stream_class
*stream_class
= NULL
;
342 struct bt_trace
*trace_class
= NULL
;
345 event_class
= bt_event_borrow_class(event
);
347 ret
= BT_COMPONENT_STATUS_ERROR
;
350 stream_class
= bt_event_class_borrow_stream_class(event_class
);
352 ret
= BT_COMPONENT_STATUS_ERROR
;
355 trace_class
= bt_stream_class_borrow_trace(stream_class
);
357 ret
= BT_COMPONENT_STATUS_ERROR
;
360 ret
= print_event_timestamp(pretty
, event
, cc_prio_map
,
361 &pretty
->start_line
);
362 if (ret
!= BT_COMPONENT_STATUS_OK
) {
365 if (pretty
->options
.print_trace_field
) {
368 name
= bt_trace_get_name(trace_class
);
370 if (!pretty
->start_line
) {
371 g_string_append(pretty
->string
, ", ");
374 print_name_equal(pretty
, "trace");
377 g_string_append(pretty
->string
, name
);
380 g_string_append(pretty
->string
, " ");
384 if (pretty
->options
.print_trace_hostname_field
) {
385 struct bt_value
*hostname_str
;
387 hostname_str
= bt_trace_borrow_environment_field_value_by_name(
388 trace_class
, "hostname");
392 if (!pretty
->start_line
) {
393 g_string_append(pretty
->string
, ", ");
396 print_name_equal(pretty
, "trace:hostname");
398 if (bt_value_string_get(hostname_str
, &str
)
399 == BT_VALUE_STATUS_OK
) {
400 g_string_append(pretty
->string
, str
);
405 if (pretty
->options
.print_trace_domain_field
) {
406 struct bt_value
*domain_str
;
408 domain_str
= bt_trace_borrow_environment_field_value_by_name(
409 trace_class
, "domain");
413 if (!pretty
->start_line
) {
414 g_string_append(pretty
->string
, ", ");
417 print_name_equal(pretty
, "trace:domain");
418 } else if (dom_print
) {
419 g_string_append(pretty
->string
, ":");
421 if (bt_value_string_get(domain_str
, &str
)
422 == BT_VALUE_STATUS_OK
) {
423 g_string_append(pretty
->string
, str
);
428 if (pretty
->options
.print_trace_procname_field
) {
429 struct bt_value
*procname_str
;
431 procname_str
= bt_trace_borrow_environment_field_value_by_name(
432 trace_class
, "procname");
436 if (!pretty
->start_line
) {
437 g_string_append(pretty
->string
, ", ");
440 print_name_equal(pretty
, "trace:procname");
441 } else if (dom_print
) {
442 g_string_append(pretty
->string
, ":");
444 if (bt_value_string_get(procname_str
, &str
)
445 == BT_VALUE_STATUS_OK
) {
446 g_string_append(pretty
->string
, str
);
452 if (pretty
->options
.print_trace_vpid_field
) {
453 struct bt_value
*vpid_value
;
455 vpid_value
= bt_trace_borrow_environment_field_value_by_name(
456 trace_class
, "vpid");
460 if (!pretty
->start_line
) {
461 g_string_append(pretty
->string
, ", ");
464 print_name_equal(pretty
, "trace:vpid");
465 } else if (dom_print
) {
466 g_string_append(pretty
->string
, ":");
468 if (bt_value_integer_get(vpid_value
, &value
)
469 == BT_VALUE_STATUS_OK
) {
470 g_string_append_printf(pretty
->string
, "(%" PRId64
")", value
);
476 if (pretty
->options
.print_loglevel_field
) {
477 static const char *log_level_names
[] = {
478 [ BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
] = "TRACE_EMERG",
479 [ BT_EVENT_CLASS_LOG_LEVEL_ALERT
] = "TRACE_ALERT",
480 [ BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
] = "TRACE_CRIT",
481 [ BT_EVENT_CLASS_LOG_LEVEL_ERROR
] = "TRACE_ERR",
482 [ BT_EVENT_CLASS_LOG_LEVEL_WARNING
] = "TRACE_WARNING",
483 [ BT_EVENT_CLASS_LOG_LEVEL_NOTICE
] = "TRACE_NOTICE",
484 [ BT_EVENT_CLASS_LOG_LEVEL_INFO
] = "TRACE_INFO",
485 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
486 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
487 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
488 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
489 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
490 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
491 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
492 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG
] = "TRACE_DEBUG",
494 enum bt_event_class_log_level log_level
;
495 const char *log_level_str
= NULL
;
497 log_level
= bt_event_class_get_log_level(event_class
);
498 BT_ASSERT(log_level
!= BT_EVENT_CLASS_LOG_LEVEL_UNKNOWN
);
499 if (log_level
!= BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
) {
500 log_level_str
= log_level_names
[log_level
];
504 if (!pretty
->start_line
) {
505 g_string_append(pretty
->string
, ", ");
508 print_name_equal(pretty
, "loglevel");
509 } else if (dom_print
) {
510 g_string_append(pretty
->string
, ":");
513 g_string_append(pretty
->string
, log_level_str
);
514 g_string_append_printf(
515 pretty
->string
, " (%d)", (int) log_level
);
519 if (pretty
->options
.print_emf_field
) {
522 uri_str
= bt_event_class_get_emf_uri(event_class
);
524 if (!pretty
->start_line
) {
525 g_string_append(pretty
->string
, ", ");
528 print_name_equal(pretty
, "model.emf.uri");
529 } else if (dom_print
) {
530 g_string_append(pretty
->string
, ":");
533 g_string_append(pretty
->string
, uri_str
);
537 if (dom_print
&& !print_names
) {
538 g_string_append(pretty
->string
, " ");
540 if (!pretty
->start_line
) {
541 g_string_append(pretty
->string
, ", ");
543 pretty
->start_line
= true;
545 print_name_equal(pretty
, "name");
547 if (pretty
->use_colors
) {
548 g_string_append(pretty
->string
, COLOR_EVENT_NAME
);
550 g_string_append(pretty
->string
, bt_event_class_get_name(event_class
));
551 if (pretty
->use_colors
) {
552 g_string_append(pretty
->string
, COLOR_RST
);
555 g_string_append(pretty
->string
, ": ");
557 g_string_append(pretty
->string
, ", ");
565 enum bt_component_status
print_integer(struct pretty_component
*pretty
,
566 struct bt_field
*field
)
568 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
569 struct bt_field_type
*field_type
= NULL
;
570 enum bt_integer_base base
;
571 enum bt_string_encoding encoding
;
573 struct bt_field_type
*int_ft
;
578 bool rst_color
= false;
579 enum bt_field_type_id ft_id
;
581 field_type
= bt_field_borrow_type(field
);
583 ret
= BT_COMPONENT_STATUS_ERROR
;
587 ft_id
= bt_field_get_type_id(field
);
590 case BT_FIELD_TYPE_ID_INTEGER
:
593 case BT_FIELD_TYPE_ID_ENUM
:
594 int_ft
= bt_field_type_enumeration_borrow_container_field_type(
601 signedness
= bt_field_type_integer_is_signed(int_ft
);
602 if (signedness
< 0) {
603 ret
= BT_COMPONENT_STATUS_ERROR
;
607 ret
= bt_field_integer_unsigned_get_value(field
, &v
.u
);
609 ret
= bt_field_integer_signed_get_value(field
, &v
.s
);
613 ret
= BT_COMPONENT_STATUS_ERROR
;
617 encoding
= bt_field_type_integer_get_encoding(int_ft
);
619 case BT_STRING_ENCODING_UTF8
:
620 case BT_STRING_ENCODING_ASCII
:
621 g_string_append_c(pretty
->tmp_string
, (int) v
.u
);
623 case BT_STRING_ENCODING_NONE
:
624 case BT_STRING_ENCODING_UNKNOWN
:
627 ret
= BT_COMPONENT_STATUS_ERROR
;
631 if (pretty
->use_colors
) {
632 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
636 base
= bt_field_type_integer_get_base(int_ft
);
638 case BT_INTEGER_BASE_BINARY
:
642 len
= bt_field_type_integer_get_size(int_ft
);
644 ret
= BT_COMPONENT_STATUS_ERROR
;
647 g_string_append(pretty
->string
, "0b");
648 v
.u
= _bt_piecewise_lshift(v
.u
, 64 - len
);
649 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
650 g_string_append_printf(pretty
->string
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
651 v
.u
= _bt_piecewise_lshift(v
.u
, 1);
655 case BT_INTEGER_BASE_OCTAL
:
660 len
= bt_field_type_integer_get_size(int_ft
);
662 ret
= BT_COMPONENT_STATUS_ERROR
;
669 /* Round length to the nearest 3-bit */
670 rounded_len
= (((len
- 1) / 3) + 1) * 3;
671 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
675 g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
678 case BT_INTEGER_BASE_DECIMAL
:
679 case BT_INTEGER_BASE_UNSPECIFIED
:
681 g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
683 g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
686 case BT_INTEGER_BASE_HEXADECIMAL
:
690 len
= bt_field_type_integer_get_size(int_ft
);
692 ret
= BT_COMPONENT_STATUS_ERROR
;
696 /* Round length to the nearest nibble */
697 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
699 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
702 g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
706 ret
= BT_COMPONENT_STATUS_ERROR
;
711 g_string_append(pretty
->string
, COLOR_RST
);
717 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
721 g_string_append_c(pretty
->string
, '"');
723 for (i
= 0; i
< strlen(str
); i
++) {
724 /* Escape sequences not recognized by iscntrl(). */
727 g_string_append(pretty
->string
, "\\\\");
730 g_string_append(pretty
->string
, "\\\'");
733 g_string_append(pretty
->string
, "\\\"");
736 g_string_append(pretty
->string
, "\\\?");
740 /* Standard characters. */
741 if (!iscntrl(str
[i
])) {
742 g_string_append_c(pretty
->string
, str
[i
]);
748 g_string_append(pretty
->string
, "\\0");
751 g_string_append(pretty
->string
, "\\a");
754 g_string_append(pretty
->string
, "\\b");
757 g_string_append(pretty
->string
, "\\e");
760 g_string_append(pretty
->string
, "\\f");
763 g_string_append(pretty
->string
, "\\n");
766 g_string_append(pretty
->string
, "\\r");
769 g_string_append(pretty
->string
, "\\t");
772 g_string_append(pretty
->string
, "\\v");
775 /* Unhandled control-sequence, print as hex. */
776 g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
781 g_string_append_c(pretty
->string
, '"');
785 enum bt_component_status
print_enum(struct pretty_component
*pretty
,
786 struct bt_field
*field
)
788 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
789 struct bt_field_type
*enumeration_field_type
= NULL
;
790 struct bt_field_type
*container_field_type
= NULL
;
791 struct bt_field_type_enumeration_mapping_iterator
*iter
= NULL
;
794 enumeration_field_type
= bt_field_borrow_type(field
);
795 if (!enumeration_field_type
) {
796 ret
= BT_COMPONENT_STATUS_ERROR
;
799 container_field_type
=
800 bt_field_type_enumeration_borrow_container_field_type(
801 enumeration_field_type
);
802 if (!container_field_type
) {
803 ret
= BT_COMPONENT_STATUS_ERROR
;
806 iter
= bt_field_enumeration_get_mappings(field
);
808 ret
= BT_COMPONENT_STATUS_ERROR
;
811 g_string_append(pretty
->string
, "( ");
812 ret
= bt_field_type_enumeration_mapping_iterator_next(iter
);
814 if (pretty
->use_colors
) {
815 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
817 g_string_append(pretty
->string
, "<unknown>");
818 if (pretty
->use_colors
) {
819 g_string_append(pretty
->string
, COLOR_RST
);
824 const char *mapping_name
;
826 if (bt_field_type_enumeration_mapping_iterator_signed_get(
827 iter
, &mapping_name
, NULL
, NULL
) < 0) {
828 ret
= BT_COMPONENT_STATUS_ERROR
;
832 g_string_append(pretty
->string
, ", ");
833 if (pretty
->use_colors
) {
834 g_string_append(pretty
->string
, COLOR_ENUM_MAPPING_NAME
);
836 print_escape_string(pretty
, mapping_name
);
837 if (pretty
->use_colors
) {
838 g_string_append(pretty
->string
, COLOR_RST
);
840 if (bt_field_type_enumeration_mapping_iterator_next(iter
) < 0) {
845 g_string_append(pretty
->string
, " : container = ");
846 ret
= print_integer(pretty
, field
);
847 if (ret
!= BT_COMPONENT_STATUS_OK
) {
850 g_string_append(pretty
->string
, " )");
857 int filter_field_name(struct pretty_component
*pretty
, const char *field_name
,
858 GQuark
*filter_fields
, int filter_array_len
)
861 GQuark field_quark
= g_quark_try_string(field_name
);
863 if (!field_quark
|| pretty
->options
.verbose
) {
867 for (i
= 0; i
< filter_array_len
; i
++) {
868 if (field_quark
== filter_fields
[i
]) {
876 enum bt_component_status
print_struct_field(struct pretty_component
*pretty
,
877 struct bt_field
*_struct
,
878 struct bt_field_type
*struct_type
,
879 int i
, bool print_names
, int *nr_printed_fields
,
880 GQuark
*filter_fields
, int filter_array_len
)
882 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
883 const char *field_name
;
884 struct bt_field
*field
= NULL
;
885 struct bt_field_type
*field_type
= NULL
;;
887 field
= bt_field_structure_borrow_field_by_index(_struct
, i
);
889 ret
= BT_COMPONENT_STATUS_ERROR
;
892 if (bt_field_type_structure_borrow_field_by_index(struct_type
,
893 &field_name
, &field_type
, i
) < 0) {
894 ret
= BT_COMPONENT_STATUS_ERROR
;
898 if (filter_fields
&& !filter_field_name(pretty
, field_name
,
899 filter_fields
, filter_array_len
)) {
900 ret
= BT_COMPONENT_STATUS_OK
;
904 if (*nr_printed_fields
> 0) {
905 g_string_append(pretty
->string
, ", ");
907 g_string_append(pretty
->string
, " ");
910 print_field_name_equal(pretty
, field_name
);
912 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
913 *nr_printed_fields
+= 1;
920 enum bt_component_status
print_struct(struct pretty_component
*pretty
,
921 struct bt_field
*_struct
, bool print_names
,
922 GQuark
*filter_fields
, int filter_array_len
)
924 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
925 struct bt_field_type
*struct_type
= NULL
;
926 int nr_fields
, i
, nr_printed_fields
;
928 struct_type
= bt_field_borrow_type(_struct
);
930 ret
= BT_COMPONENT_STATUS_ERROR
;
933 nr_fields
= bt_field_type_structure_get_field_count(struct_type
);
935 ret
= BT_COMPONENT_STATUS_ERROR
;
938 g_string_append(pretty
->string
, "{");
940 nr_printed_fields
= 0;
941 for (i
= 0; i
< nr_fields
; i
++) {
942 ret
= print_struct_field(pretty
, _struct
, struct_type
, i
,
943 print_names
, &nr_printed_fields
, filter_fields
,
945 if (ret
!= BT_COMPONENT_STATUS_OK
) {
950 g_string_append(pretty
->string
, " }");
957 enum bt_component_status
print_array_field(struct pretty_component
*pretty
,
958 struct bt_field
*array
, uint64_t i
,
959 bool is_string
, bool print_names
)
961 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
962 struct bt_field
*field
= NULL
;
966 g_string_append(pretty
->string
, ", ");
968 g_string_append(pretty
->string
, " ");
971 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
974 field
= bt_field_array_borrow_field(array
, i
);
976 ret
= BT_COMPONENT_STATUS_ERROR
;
979 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
986 enum bt_component_status
print_array(struct pretty_component
*pretty
,
987 struct bt_field
*array
, bool print_names
)
989 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
990 struct bt_field_type
*array_type
= NULL
, *field_type
= NULL
;
991 enum bt_field_type_id type_id
;
994 bool is_string
= false;
996 array_type
= bt_field_borrow_type(array
);
998 ret
= BT_COMPONENT_STATUS_ERROR
;
1001 field_type
= bt_field_type_array_borrow_element_field_type(array_type
);
1003 ret
= BT_COMPONENT_STATUS_ERROR
;
1006 len
= bt_field_type_array_get_length(array_type
);
1008 ret
= BT_COMPONENT_STATUS_ERROR
;
1011 type_id
= bt_field_type_get_type_id(field_type
);
1012 if (type_id
== BT_FIELD_TYPE_ID_INTEGER
) {
1013 enum bt_string_encoding encoding
;
1015 encoding
= bt_field_type_integer_get_encoding(field_type
);
1016 if (encoding
== BT_STRING_ENCODING_UTF8
1017 || encoding
== BT_STRING_ENCODING_ASCII
) {
1018 int integer_len
, integer_alignment
;
1020 integer_len
= bt_field_type_integer_get_size(field_type
);
1021 if (integer_len
< 0) {
1022 return BT_COMPONENT_STATUS_ERROR
;
1024 integer_alignment
= bt_field_type_get_alignment(field_type
);
1025 if (integer_alignment
< 0) {
1026 return BT_COMPONENT_STATUS_ERROR
;
1028 if (integer_len
== CHAR_BIT
1029 && integer_alignment
== CHAR_BIT
) {
1036 g_string_assign(pretty
->tmp_string
, "");
1038 g_string_append(pretty
->string
, "[");
1042 for (i
= 0; i
< len
; i
++) {
1043 ret
= print_array_field(pretty
, array
, i
, is_string
, print_names
);
1044 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1051 if (pretty
->use_colors
) {
1052 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1054 print_escape_string(pretty
, pretty
->tmp_string
->str
);
1055 if (pretty
->use_colors
) {
1056 g_string_append(pretty
->string
, COLOR_RST
);
1059 g_string_append(pretty
->string
, " ]");
1067 enum bt_component_status
print_sequence_field(struct pretty_component
*pretty
,
1068 struct bt_field
*seq
, uint64_t i
,
1069 bool is_string
, bool print_names
)
1071 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1072 struct bt_field
*field
= NULL
;
1076 g_string_append(pretty
->string
, ", ");
1078 g_string_append(pretty
->string
, " ");
1081 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
1084 field
= bt_field_sequence_borrow_field(seq
, i
);
1086 ret
= BT_COMPONENT_STATUS_ERROR
;
1089 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
1096 enum bt_component_status
print_sequence(struct pretty_component
*pretty
,
1097 struct bt_field
*seq
, bool print_names
)
1099 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1100 struct bt_field_type
*seq_type
= NULL
, *field_type
= NULL
;
1101 enum bt_field_type_id type_id
;
1104 bool is_string
= false;
1106 seq_type
= bt_field_borrow_type(seq
);
1108 ret
= BT_COMPONENT_STATUS_ERROR
;
1111 len
= bt_field_sequence_get_length(seq
);
1113 ret
= BT_COMPONENT_STATUS_ERROR
;
1116 field_type
= bt_field_type_sequence_borrow_element_field_type(seq_type
);
1118 ret
= BT_COMPONENT_STATUS_ERROR
;
1121 type_id
= bt_field_type_get_type_id(field_type
);
1122 if (type_id
== BT_FIELD_TYPE_ID_INTEGER
) {
1123 enum bt_string_encoding encoding
;
1125 encoding
= bt_field_type_integer_get_encoding(field_type
);
1126 if (encoding
== BT_STRING_ENCODING_UTF8
1127 || encoding
== BT_STRING_ENCODING_ASCII
) {
1128 int integer_len
, integer_alignment
;
1130 integer_len
= bt_field_type_integer_get_size(field_type
);
1131 if (integer_len
< 0) {
1132 ret
= BT_COMPONENT_STATUS_ERROR
;
1135 integer_alignment
= bt_field_type_get_alignment(field_type
);
1136 if (integer_alignment
< 0) {
1137 ret
= BT_COMPONENT_STATUS_ERROR
;
1140 if (integer_len
== CHAR_BIT
1141 && integer_alignment
== CHAR_BIT
) {
1148 g_string_assign(pretty
->tmp_string
, "");
1150 g_string_append(pretty
->string
, "[");
1154 for (i
= 0; i
< len
; i
++) {
1155 ret
= print_sequence_field(pretty
, seq
, i
,
1156 is_string
, print_names
);
1157 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1164 if (pretty
->use_colors
) {
1165 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1167 print_escape_string(pretty
, pretty
->tmp_string
->str
);
1168 if (pretty
->use_colors
) {
1169 g_string_append(pretty
->string
, COLOR_RST
);
1172 g_string_append(pretty
->string
, " ]");
1180 enum bt_component_status
print_variant(struct pretty_component
*pretty
,
1181 struct bt_field
*variant
, bool print_names
)
1183 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1184 struct bt_field
*field
= NULL
;
1186 field
= bt_field_variant_borrow_current_field(variant
);
1188 ret
= BT_COMPONENT_STATUS_ERROR
;
1191 g_string_append(pretty
->string
, "{ ");
1195 struct bt_field_type
*var_ft
;
1196 struct bt_field_type
*tag_ft
;
1197 struct bt_field_type
*container_ft
;
1198 const char *tag_choice
;
1200 struct bt_field_type_enumeration_mapping_iterator
*iter
;
1202 var_ft
= bt_field_borrow_type(variant
);
1203 tag_ft
= bt_field_type_variant_borrow_tag_field_type(
1206 bt_field_type_enumeration_borrow_container_field_type(
1208 is_signed
= bt_field_type_integer_is_signed(container_ft
);
1213 iret
= bt_field_variant_get_tag_signed(variant
, &tag
);
1215 ret
= BT_COMPONENT_STATUS_ERROR
;
1219 iter
= bt_field_type_enumeration_signed_find_mappings_by_value(
1224 iret
= bt_field_variant_get_tag_unsigned(variant
, &tag
);
1226 ret
= BT_COMPONENT_STATUS_ERROR
;
1230 iter
= bt_field_type_enumeration_unsigned_find_mappings_by_value(
1235 ret
= BT_COMPONENT_STATUS_ERROR
;
1239 iret
= bt_field_type_enumeration_mapping_iterator_next(
1242 ret
= BT_COMPONENT_STATUS_ERROR
;
1247 bt_field_type_enumeration_mapping_iterator_signed_get(
1248 iter
, &tag_choice
, NULL
, NULL
);
1251 ret
= BT_COMPONENT_STATUS_ERROR
;
1254 print_field_name_equal(pretty
, tag_choice
);
1257 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
1258 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1262 g_string_append(pretty
->string
, " }");
1269 enum bt_component_status
print_field(struct pretty_component
*pretty
,
1270 struct bt_field
*field
, bool print_names
,
1271 GQuark
*filter_fields
, int filter_array_len
)
1273 enum bt_field_type_id type_id
;
1275 type_id
= bt_field_get_type_id(field
);
1277 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
1278 return print_integer(pretty
, field
);
1279 case BT_CTF_FIELD_TYPE_ID_FLOAT
:
1283 if (bt_field_floating_point_get_value(field
, &v
)) {
1284 return BT_COMPONENT_STATUS_ERROR
;
1286 if (pretty
->use_colors
) {
1287 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
1289 g_string_append_printf(pretty
->string
, "%g", v
);
1290 if (pretty
->use_colors
) {
1291 g_string_append(pretty
->string
, COLOR_RST
);
1293 return BT_COMPONENT_STATUS_OK
;
1295 case BT_CTF_FIELD_TYPE_ID_ENUM
:
1296 return print_enum(pretty
, field
);
1297 case BT_CTF_FIELD_TYPE_ID_STRING
:
1301 str
= bt_field_string_get_value(field
);
1303 return BT_COMPONENT_STATUS_ERROR
;
1306 if (pretty
->use_colors
) {
1307 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1309 print_escape_string(pretty
, str
);
1310 if (pretty
->use_colors
) {
1311 g_string_append(pretty
->string
, COLOR_RST
);
1313 return BT_COMPONENT_STATUS_OK
;
1315 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1316 return print_struct(pretty
, field
, print_names
, filter_fields
,
1318 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1319 return print_variant(pretty
, field
, print_names
);
1320 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1321 return print_array(pretty
, field
, print_names
);
1322 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1323 return print_sequence(pretty
, field
, print_names
);
1325 // TODO: log instead
1326 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) type_id
);
1327 return BT_COMPONENT_STATUS_ERROR
;
1332 enum bt_component_status
print_stream_packet_context(struct pretty_component
*pretty
,
1333 struct bt_event
*event
)
1335 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1336 struct bt_packet
*packet
= NULL
;
1337 struct bt_field
*main_field
= NULL
;
1339 packet
= bt_event_borrow_packet(event
);
1341 ret
= BT_COMPONENT_STATUS_ERROR
;
1344 main_field
= bt_packet_borrow_context(packet
);
1348 if (!pretty
->start_line
) {
1349 g_string_append(pretty
->string
, ", ");
1351 pretty
->start_line
= false;
1352 if (pretty
->options
.print_scope_field_names
) {
1353 print_name_equal(pretty
, "stream.packet.context");
1355 ret
= print_field(pretty
, main_field
,
1356 pretty
->options
.print_context_field_names
,
1357 stream_packet_context_quarks
,
1358 STREAM_PACKET_CONTEXT_QUARKS_LEN
);
1365 enum bt_component_status
print_event_header_raw(struct pretty_component
*pretty
,
1366 struct bt_event
*event
)
1368 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1369 struct bt_field
*main_field
= NULL
;
1371 main_field
= bt_event_borrow_header(event
);
1375 if (!pretty
->start_line
) {
1376 g_string_append(pretty
->string
, ", ");
1378 pretty
->start_line
= false;
1379 if (pretty
->options
.print_scope_field_names
) {
1380 print_name_equal(pretty
, "stream.event.header");
1382 ret
= print_field(pretty
, main_field
,
1383 pretty
->options
.print_header_field_names
, NULL
, 0);
1390 enum bt_component_status
print_stream_event_context(struct pretty_component
*pretty
,
1391 struct bt_event
*event
)
1393 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1394 struct bt_field
*main_field
= NULL
;
1396 main_field
= bt_event_borrow_stream_event_context(event
);
1400 if (!pretty
->start_line
) {
1401 g_string_append(pretty
->string
, ", ");
1403 pretty
->start_line
= false;
1404 if (pretty
->options
.print_scope_field_names
) {
1405 print_name_equal(pretty
, "stream.event.context");
1407 ret
= print_field(pretty
, main_field
,
1408 pretty
->options
.print_context_field_names
, NULL
, 0);
1415 enum bt_component_status
print_event_context(struct pretty_component
*pretty
,
1416 struct bt_event
*event
)
1418 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1419 struct bt_field
*main_field
= NULL
;
1421 main_field
= bt_event_borrow_context(event
);
1425 if (!pretty
->start_line
) {
1426 g_string_append(pretty
->string
, ", ");
1428 pretty
->start_line
= false;
1429 if (pretty
->options
.print_scope_field_names
) {
1430 print_name_equal(pretty
, "event.context");
1432 ret
= print_field(pretty
, main_field
,
1433 pretty
->options
.print_context_field_names
, NULL
, 0);
1440 enum bt_component_status
print_event_payload(struct pretty_component
*pretty
,
1441 struct bt_event
*event
)
1443 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1444 struct bt_field
*main_field
= NULL
;
1446 main_field
= bt_event_borrow_payload(event
);
1450 if (!pretty
->start_line
) {
1451 g_string_append(pretty
->string
, ", ");
1453 pretty
->start_line
= false;
1454 if (pretty
->options
.print_scope_field_names
) {
1455 print_name_equal(pretty
, "event.fields");
1457 ret
= print_field(pretty
, main_field
,
1458 pretty
->options
.print_payload_field_names
, NULL
, 0);
1465 int flush_buf(struct pretty_component
*pretty
)
1469 if (pretty
->string
->len
== 0) {
1473 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, pretty
->out
) != 1) {
1482 enum bt_component_status
pretty_print_event(struct pretty_component
*pretty
,
1483 struct bt_notification
*event_notif
)
1485 enum bt_component_status ret
;
1486 struct bt_event
*event
=
1487 bt_notification_event_borrow_event(event_notif
);
1488 struct bt_clock_class_priority_map
*cc_prio_map
=
1489 bt_notification_event_borrow_clock_class_priority_map(
1493 BT_ASSERT(cc_prio_map
);
1494 pretty
->start_line
= true;
1495 g_string_assign(pretty
->string
, "");
1496 ret
= print_event_header(pretty
, event
, cc_prio_map
);
1497 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1501 ret
= print_stream_packet_context(pretty
, event
);
1502 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1506 if (pretty
->options
.verbose
) {
1507 ret
= print_event_header_raw(pretty
, event
);
1508 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1513 ret
= print_stream_event_context(pretty
, event
);
1514 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1518 ret
= print_event_context(pretty
, event
);
1519 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1523 ret
= print_event_payload(pretty
, event
);
1524 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1528 g_string_append_c(pretty
->string
, '\n');
1529 if (flush_buf(pretty
)) {
1530 ret
= BT_COMPONENT_STATUS_ERROR
;
1539 enum bt_component_status
pretty_print_discarded_elements(
1540 struct pretty_component
*pretty
,
1541 struct bt_notification
*notif
)
1543 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1544 struct bt_stream
*stream
= NULL
;
1545 struct bt_stream_class
*stream_class
= NULL
;
1546 struct bt_trace
*trace
= NULL
;
1547 const char *stream_name
;
1548 const char *trace_name
;
1549 const unsigned char *trace_uuid
;
1550 int64_t stream_class_id
;
1552 bool is_discarded_events
;
1554 struct bt_clock_value
*clock_value
= NULL
;
1557 switch (bt_notification_get_type(notif
)) {
1558 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
1559 stream
= bt_notification_discarded_events_borrow_stream(notif
);
1560 count
= bt_notification_discarded_events_get_count(notif
);
1561 is_discarded_events
= true;
1563 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
1564 stream
= bt_notification_discarded_packets_borrow_stream(notif
);
1565 count
= bt_notification_discarded_packets_get_count(notif
);
1566 is_discarded_events
= false;
1573 stream_name
= bt_stream_get_name(stream
);
1575 /* Stream class ID */
1576 stream_class
= bt_stream_borrow_class(stream
);
1577 BT_ASSERT(stream_class
);
1578 stream_class_id
= bt_stream_class_get_id(stream_class
);
1581 stream_id
= bt_stream_get_id(stream
);
1584 trace
= bt_stream_class_borrow_trace(stream_class
);
1586 trace_name
= bt_trace_get_name(trace
);
1588 trace_name
= "(unknown)";
1592 trace_uuid
= bt_trace_get_uuid(trace
);
1595 * Print to standard error stream to remain backward compatible
1596 * with Babeltrace 1.
1599 "%s%sWARNING%s%s: Tracer discarded %" PRId64
" %s%s between [",
1600 bt_common_color_fg_yellow(),
1601 bt_common_color_bold(),
1602 bt_common_color_reset(),
1603 bt_common_color_fg_yellow(),
1604 count
, is_discarded_events
? "event" : "packet",
1605 count
== 1 ? "" : "s");
1606 g_string_assign(pretty
->string
, "");
1607 clock_value
= is_discarded_events
?
1608 bt_notification_discarded_events_borrow_begin_clock_value(notif
) :
1609 bt_notification_discarded_packets_borrow_begin_clock_value(notif
);
1610 print_timestamp_wall(pretty
, clock_value
);
1611 fprintf(stderr
, "%s] and [", pretty
->string
->str
);
1612 g_string_assign(pretty
->string
, "");
1613 clock_value
= is_discarded_events
?
1614 bt_notification_discarded_events_borrow_end_clock_value(notif
) :
1615 bt_notification_discarded_packets_borrow_end_clock_value(notif
);
1616 print_timestamp_wall(pretty
, clock_value
);
1617 fprintf(stderr
, "%s] in trace \"%s\" ",
1618 pretty
->string
->str
, trace_name
);
1622 "(UUID: %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x) ",
1640 fprintf(stderr
, "(no UUID) ");
1643 fprintf(stderr
, "within stream \"%s\" (stream class ID: %" PRId64
", ",
1644 stream_name
, stream_class_id
);
1646 if (stream_id
>= 0) {
1647 fprintf(stderr
, "stream ID: %" PRId64
, stream_id
);
1649 fprintf(stderr
, "no stream ID");
1652 fprintf(stderr
, ").%s\n", bt_common_color_reset());