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
;
577 bool rst_color
= false;
579 field_type
= bt_field_borrow_type(field
);
581 ret
= BT_COMPONENT_STATUS_ERROR
;
584 signedness
= bt_field_type_integer_is_signed(field_type
);
585 if (signedness
< 0) {
586 ret
= BT_COMPONENT_STATUS_ERROR
;
590 if (bt_field_integer_unsigned_get_value(field
, &v
.u
) < 0) {
591 ret
= BT_COMPONENT_STATUS_ERROR
;
595 if (bt_field_integer_signed_get_value(field
, &v
.s
) < 0) {
596 ret
= BT_COMPONENT_STATUS_ERROR
;
601 encoding
= bt_field_type_integer_get_encoding(field_type
);
603 case BT_STRING_ENCODING_UTF8
:
604 case BT_STRING_ENCODING_ASCII
:
605 g_string_append_c(pretty
->tmp_string
, (int) v
.u
);
607 case BT_STRING_ENCODING_NONE
:
608 case BT_STRING_ENCODING_UNKNOWN
:
611 ret
= BT_COMPONENT_STATUS_ERROR
;
615 if (pretty
->use_colors
) {
616 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
620 base
= bt_field_type_integer_get_base(field_type
);
622 case BT_INTEGER_BASE_BINARY
:
626 len
= bt_field_type_integer_get_size(field_type
);
628 ret
= BT_COMPONENT_STATUS_ERROR
;
631 g_string_append(pretty
->string
, "0b");
632 v
.u
= _bt_piecewise_lshift(v
.u
, 64 - len
);
633 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
634 g_string_append_printf(pretty
->string
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
635 v
.u
= _bt_piecewise_lshift(v
.u
, 1);
639 case BT_INTEGER_BASE_OCTAL
:
644 len
= bt_field_type_integer_get_size(field_type
);
646 ret
= BT_COMPONENT_STATUS_ERROR
;
653 /* Round length to the nearest 3-bit */
654 rounded_len
= (((len
- 1) / 3) + 1) * 3;
655 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
659 g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
662 case BT_INTEGER_BASE_DECIMAL
:
663 case BT_INTEGER_BASE_UNSPECIFIED
:
665 g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
667 g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
670 case BT_INTEGER_BASE_HEXADECIMAL
:
674 len
= bt_field_type_integer_get_size(field_type
);
676 ret
= BT_COMPONENT_STATUS_ERROR
;
680 /* Round length to the nearest nibble */
681 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
683 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
686 g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
690 ret
= BT_COMPONENT_STATUS_ERROR
;
695 g_string_append(pretty
->string
, COLOR_RST
);
701 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
705 g_string_append_c(pretty
->string
, '"');
707 for (i
= 0; i
< strlen(str
); i
++) {
708 /* Escape sequences not recognized by iscntrl(). */
711 g_string_append(pretty
->string
, "\\\\");
714 g_string_append(pretty
->string
, "\\\'");
717 g_string_append(pretty
->string
, "\\\"");
720 g_string_append(pretty
->string
, "\\\?");
724 /* Standard characters. */
725 if (!iscntrl(str
[i
])) {
726 g_string_append_c(pretty
->string
, str
[i
]);
732 g_string_append(pretty
->string
, "\\0");
735 g_string_append(pretty
->string
, "\\a");
738 g_string_append(pretty
->string
, "\\b");
741 g_string_append(pretty
->string
, "\\e");
744 g_string_append(pretty
->string
, "\\f");
747 g_string_append(pretty
->string
, "\\n");
750 g_string_append(pretty
->string
, "\\r");
753 g_string_append(pretty
->string
, "\\t");
756 g_string_append(pretty
->string
, "\\v");
759 /* Unhandled control-sequence, print as hex. */
760 g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
765 g_string_append_c(pretty
->string
, '"');
769 enum bt_component_status
print_enum(struct pretty_component
*pretty
,
770 struct bt_field
*field
)
772 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
773 struct bt_field
*container_field
= NULL
;
774 struct bt_field_type
*enumeration_field_type
= NULL
;
775 struct bt_field_type
*container_field_type
= NULL
;
776 struct bt_field_type_enumeration_mapping_iterator
*iter
= NULL
;
780 enumeration_field_type
= bt_field_borrow_type(field
);
781 if (!enumeration_field_type
) {
782 ret
= BT_COMPONENT_STATUS_ERROR
;
785 container_field
= bt_field_enumeration_borrow_container(field
);
786 if (!container_field
) {
787 ret
= BT_COMPONENT_STATUS_ERROR
;
790 container_field_type
= bt_field_borrow_type(container_field
);
791 if (!container_field_type
) {
792 ret
= BT_COMPONENT_STATUS_ERROR
;
795 is_signed
= bt_field_type_integer_is_signed(container_field_type
);
797 ret
= BT_COMPONENT_STATUS_ERROR
;
803 if (bt_field_integer_signed_get_value(container_field
,
805 ret
= BT_COMPONENT_STATUS_ERROR
;
808 iter
= bt_field_type_enumeration_signed_find_mappings_by_value(
809 enumeration_field_type
, value
);
813 if (bt_field_integer_unsigned_get_value(container_field
,
815 ret
= BT_COMPONENT_STATUS_ERROR
;
818 iter
= bt_field_type_enumeration_unsigned_find_mappings_by_value(
819 enumeration_field_type
, value
);
821 g_string_append(pretty
->string
, "( ");
822 ret
= bt_field_type_enumeration_mapping_iterator_next(iter
);
824 if (pretty
->use_colors
) {
825 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
827 g_string_append(pretty
->string
, "<unknown>");
828 if (pretty
->use_colors
) {
829 g_string_append(pretty
->string
, COLOR_RST
);
834 const char *mapping_name
;
836 if (bt_field_type_enumeration_mapping_iterator_signed_get(
837 iter
, &mapping_name
, NULL
, NULL
) < 0) {
838 ret
= BT_COMPONENT_STATUS_ERROR
;
842 g_string_append(pretty
->string
, ", ");
843 if (pretty
->use_colors
) {
844 g_string_append(pretty
->string
, COLOR_ENUM_MAPPING_NAME
);
846 print_escape_string(pretty
, mapping_name
);
847 if (pretty
->use_colors
) {
848 g_string_append(pretty
->string
, COLOR_RST
);
850 if (bt_field_type_enumeration_mapping_iterator_next(iter
) < 0) {
855 g_string_append(pretty
->string
, " : container = ");
856 ret
= print_integer(pretty
, container_field
);
857 if (ret
!= BT_COMPONENT_STATUS_OK
) {
860 g_string_append(pretty
->string
, " )");
867 int filter_field_name(struct pretty_component
*pretty
, const char *field_name
,
868 GQuark
*filter_fields
, int filter_array_len
)
871 GQuark field_quark
= g_quark_try_string(field_name
);
873 if (!field_quark
|| pretty
->options
.verbose
) {
877 for (i
= 0; i
< filter_array_len
; i
++) {
878 if (field_quark
== filter_fields
[i
]) {
886 enum bt_component_status
print_struct_field(struct pretty_component
*pretty
,
887 struct bt_field
*_struct
,
888 struct bt_field_type
*struct_type
,
889 int i
, bool print_names
, int *nr_printed_fields
,
890 GQuark
*filter_fields
, int filter_array_len
)
892 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
893 const char *field_name
;
894 struct bt_field
*field
= NULL
;
895 struct bt_field_type
*field_type
= NULL
;;
897 field
= bt_field_structure_borrow_field_by_index(_struct
, i
);
899 ret
= BT_COMPONENT_STATUS_ERROR
;
902 if (bt_field_type_structure_borrow_field_by_index(struct_type
,
903 &field_name
, &field_type
, i
) < 0) {
904 ret
= BT_COMPONENT_STATUS_ERROR
;
908 if (filter_fields
&& !filter_field_name(pretty
, field_name
,
909 filter_fields
, filter_array_len
)) {
910 ret
= BT_COMPONENT_STATUS_OK
;
914 if (*nr_printed_fields
> 0) {
915 g_string_append(pretty
->string
, ", ");
917 g_string_append(pretty
->string
, " ");
920 print_field_name_equal(pretty
, field_name
);
922 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
923 *nr_printed_fields
+= 1;
930 enum bt_component_status
print_struct(struct pretty_component
*pretty
,
931 struct bt_field
*_struct
, bool print_names
,
932 GQuark
*filter_fields
, int filter_array_len
)
934 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
935 struct bt_field_type
*struct_type
= NULL
;
936 int nr_fields
, i
, nr_printed_fields
;
938 struct_type
= bt_field_borrow_type(_struct
);
940 ret
= BT_COMPONENT_STATUS_ERROR
;
943 nr_fields
= bt_field_type_structure_get_field_count(struct_type
);
945 ret
= BT_COMPONENT_STATUS_ERROR
;
948 g_string_append(pretty
->string
, "{");
950 nr_printed_fields
= 0;
951 for (i
= 0; i
< nr_fields
; i
++) {
952 ret
= print_struct_field(pretty
, _struct
, struct_type
, i
,
953 print_names
, &nr_printed_fields
, filter_fields
,
955 if (ret
!= BT_COMPONENT_STATUS_OK
) {
960 g_string_append(pretty
->string
, " }");
967 enum bt_component_status
print_array_field(struct pretty_component
*pretty
,
968 struct bt_field
*array
, uint64_t i
,
969 bool is_string
, bool print_names
)
971 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
972 struct bt_field
*field
= NULL
;
976 g_string_append(pretty
->string
, ", ");
978 g_string_append(pretty
->string
, " ");
981 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
984 field
= bt_field_array_borrow_field(array
, i
);
986 ret
= BT_COMPONENT_STATUS_ERROR
;
989 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
996 enum bt_component_status
print_array(struct pretty_component
*pretty
,
997 struct bt_field
*array
, bool print_names
)
999 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1000 struct bt_field_type
*array_type
= NULL
, *field_type
= NULL
;
1001 enum bt_field_type_id type_id
;
1004 bool is_string
= false;
1006 array_type
= bt_field_borrow_type(array
);
1008 ret
= BT_COMPONENT_STATUS_ERROR
;
1011 field_type
= bt_field_type_array_borrow_element_field_type(array_type
);
1013 ret
= BT_COMPONENT_STATUS_ERROR
;
1016 len
= bt_field_type_array_get_length(array_type
);
1018 ret
= BT_COMPONENT_STATUS_ERROR
;
1021 type_id
= bt_field_type_get_type_id(field_type
);
1022 if (type_id
== BT_FIELD_TYPE_ID_INTEGER
) {
1023 enum bt_string_encoding encoding
;
1025 encoding
= bt_field_type_integer_get_encoding(field_type
);
1026 if (encoding
== BT_STRING_ENCODING_UTF8
1027 || encoding
== BT_STRING_ENCODING_ASCII
) {
1028 int integer_len
, integer_alignment
;
1030 integer_len
= bt_field_type_integer_get_size(field_type
);
1031 if (integer_len
< 0) {
1032 return BT_COMPONENT_STATUS_ERROR
;
1034 integer_alignment
= bt_field_type_get_alignment(field_type
);
1035 if (integer_alignment
< 0) {
1036 return BT_COMPONENT_STATUS_ERROR
;
1038 if (integer_len
== CHAR_BIT
1039 && integer_alignment
== CHAR_BIT
) {
1046 g_string_assign(pretty
->tmp_string
, "");
1048 g_string_append(pretty
->string
, "[");
1052 for (i
= 0; i
< len
; i
++) {
1053 ret
= print_array_field(pretty
, array
, i
, is_string
, print_names
);
1054 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1061 if (pretty
->use_colors
) {
1062 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1064 print_escape_string(pretty
, pretty
->tmp_string
->str
);
1065 if (pretty
->use_colors
) {
1066 g_string_append(pretty
->string
, COLOR_RST
);
1069 g_string_append(pretty
->string
, " ]");
1077 enum bt_component_status
print_sequence_field(struct pretty_component
*pretty
,
1078 struct bt_field
*seq
, uint64_t i
,
1079 bool is_string
, bool print_names
)
1081 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1082 struct bt_field
*field
= NULL
;
1086 g_string_append(pretty
->string
, ", ");
1088 g_string_append(pretty
->string
, " ");
1091 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
1094 field
= bt_field_sequence_borrow_field(seq
, i
);
1096 ret
= BT_COMPONENT_STATUS_ERROR
;
1099 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
1106 enum bt_component_status
print_sequence(struct pretty_component
*pretty
,
1107 struct bt_field
*seq
, bool print_names
)
1109 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1110 struct bt_field_type
*seq_type
= NULL
, *field_type
= NULL
;
1111 struct bt_field
*length_field
= NULL
;
1112 enum bt_field_type_id type_id
;
1115 bool is_string
= false;
1117 seq_type
= bt_field_borrow_type(seq
);
1119 ret
= BT_COMPONENT_STATUS_ERROR
;
1122 length_field
= bt_field_sequence_borrow_length(seq
);
1123 if (!length_field
) {
1124 ret
= BT_COMPONENT_STATUS_ERROR
;
1127 if (bt_field_integer_unsigned_get_value(length_field
, &len
) < 0) {
1128 ret
= BT_COMPONENT_STATUS_ERROR
;
1131 field_type
= bt_field_type_sequence_borrow_element_field_type(seq_type
);
1133 ret
= BT_COMPONENT_STATUS_ERROR
;
1136 type_id
= bt_field_type_get_type_id(field_type
);
1137 if (type_id
== BT_FIELD_TYPE_ID_INTEGER
) {
1138 enum bt_string_encoding encoding
;
1140 encoding
= bt_field_type_integer_get_encoding(field_type
);
1141 if (encoding
== BT_STRING_ENCODING_UTF8
1142 || encoding
== BT_STRING_ENCODING_ASCII
) {
1143 int integer_len
, integer_alignment
;
1145 integer_len
= bt_field_type_integer_get_size(field_type
);
1146 if (integer_len
< 0) {
1147 ret
= BT_COMPONENT_STATUS_ERROR
;
1150 integer_alignment
= bt_field_type_get_alignment(field_type
);
1151 if (integer_alignment
< 0) {
1152 ret
= BT_COMPONENT_STATUS_ERROR
;
1155 if (integer_len
== CHAR_BIT
1156 && integer_alignment
== CHAR_BIT
) {
1163 g_string_assign(pretty
->tmp_string
, "");
1165 g_string_append(pretty
->string
, "[");
1169 for (i
= 0; i
< len
; i
++) {
1170 ret
= print_sequence_field(pretty
, seq
, i
,
1171 is_string
, print_names
);
1172 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1179 if (pretty
->use_colors
) {
1180 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1182 print_escape_string(pretty
, pretty
->tmp_string
->str
);
1183 if (pretty
->use_colors
) {
1184 g_string_append(pretty
->string
, COLOR_RST
);
1187 g_string_append(pretty
->string
, " ]");
1195 enum bt_component_status
print_variant(struct pretty_component
*pretty
,
1196 struct bt_field
*variant
, bool print_names
)
1198 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1199 struct bt_field
*field
= NULL
;
1201 field
= bt_field_variant_borrow_current_field(variant
);
1203 ret
= BT_COMPONENT_STATUS_ERROR
;
1206 g_string_append(pretty
->string
, "{ ");
1210 struct bt_field
*tag_field
= NULL
;
1211 const char *tag_choice
;
1212 struct bt_field_type_enumeration_mapping_iterator
*iter
;
1214 tag_field
= bt_field_variant_borrow_tag(variant
);
1216 ret
= BT_COMPONENT_STATUS_ERROR
;
1220 iter
= bt_field_enumeration_get_mappings(tag_field
);
1221 iter_ret
= bt_field_type_enumeration_mapping_iterator_next(
1224 ret
= BT_COMPONENT_STATUS_ERROR
;
1229 bt_field_type_enumeration_mapping_iterator_signed_get(
1230 iter
, &tag_choice
, NULL
, NULL
);
1233 ret
= BT_COMPONENT_STATUS_ERROR
;
1236 print_field_name_equal(pretty
, tag_choice
);
1239 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
1240 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1244 g_string_append(pretty
->string
, " }");
1251 enum bt_component_status
print_field(struct pretty_component
*pretty
,
1252 struct bt_field
*field
, bool print_names
,
1253 GQuark
*filter_fields
, int filter_array_len
)
1255 enum bt_field_type_id type_id
;
1257 type_id
= bt_field_get_type_id(field
);
1259 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
1260 return print_integer(pretty
, field
);
1261 case BT_CTF_FIELD_TYPE_ID_FLOAT
:
1265 if (bt_field_floating_point_get_value(field
, &v
)) {
1266 return BT_COMPONENT_STATUS_ERROR
;
1268 if (pretty
->use_colors
) {
1269 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
1271 g_string_append_printf(pretty
->string
, "%g", v
);
1272 if (pretty
->use_colors
) {
1273 g_string_append(pretty
->string
, COLOR_RST
);
1275 return BT_COMPONENT_STATUS_OK
;
1277 case BT_CTF_FIELD_TYPE_ID_ENUM
:
1278 return print_enum(pretty
, field
);
1279 case BT_CTF_FIELD_TYPE_ID_STRING
:
1283 str
= bt_field_string_get_value(field
);
1285 return BT_COMPONENT_STATUS_ERROR
;
1288 if (pretty
->use_colors
) {
1289 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1291 print_escape_string(pretty
, str
);
1292 if (pretty
->use_colors
) {
1293 g_string_append(pretty
->string
, COLOR_RST
);
1295 return BT_COMPONENT_STATUS_OK
;
1297 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1298 return print_struct(pretty
, field
, print_names
, filter_fields
,
1300 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1301 return print_variant(pretty
, field
, print_names
);
1302 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1303 return print_array(pretty
, field
, print_names
);
1304 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1305 return print_sequence(pretty
, field
, print_names
);
1307 // TODO: log instead
1308 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) type_id
);
1309 return BT_COMPONENT_STATUS_ERROR
;
1314 enum bt_component_status
print_stream_packet_context(struct pretty_component
*pretty
,
1315 struct bt_event
*event
)
1317 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1318 struct bt_packet
*packet
= NULL
;
1319 struct bt_field
*main_field
= NULL
;
1321 packet
= bt_event_borrow_packet(event
);
1323 ret
= BT_COMPONENT_STATUS_ERROR
;
1326 main_field
= bt_packet_borrow_context(packet
);
1330 if (!pretty
->start_line
) {
1331 g_string_append(pretty
->string
, ", ");
1333 pretty
->start_line
= false;
1334 if (pretty
->options
.print_scope_field_names
) {
1335 print_name_equal(pretty
, "stream.packet.context");
1337 ret
= print_field(pretty
, main_field
,
1338 pretty
->options
.print_context_field_names
,
1339 stream_packet_context_quarks
,
1340 STREAM_PACKET_CONTEXT_QUARKS_LEN
);
1347 enum bt_component_status
print_event_header_raw(struct pretty_component
*pretty
,
1348 struct bt_event
*event
)
1350 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1351 struct bt_field
*main_field
= NULL
;
1353 main_field
= bt_event_borrow_header(event
);
1357 if (!pretty
->start_line
) {
1358 g_string_append(pretty
->string
, ", ");
1360 pretty
->start_line
= false;
1361 if (pretty
->options
.print_scope_field_names
) {
1362 print_name_equal(pretty
, "stream.event.header");
1364 ret
= print_field(pretty
, main_field
,
1365 pretty
->options
.print_header_field_names
, NULL
, 0);
1372 enum bt_component_status
print_stream_event_context(struct pretty_component
*pretty
,
1373 struct bt_event
*event
)
1375 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1376 struct bt_field
*main_field
= NULL
;
1378 main_field
= bt_event_borrow_stream_event_context(event
);
1382 if (!pretty
->start_line
) {
1383 g_string_append(pretty
->string
, ", ");
1385 pretty
->start_line
= false;
1386 if (pretty
->options
.print_scope_field_names
) {
1387 print_name_equal(pretty
, "stream.event.context");
1389 ret
= print_field(pretty
, main_field
,
1390 pretty
->options
.print_context_field_names
, NULL
, 0);
1397 enum bt_component_status
print_event_context(struct pretty_component
*pretty
,
1398 struct bt_event
*event
)
1400 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1401 struct bt_field
*main_field
= NULL
;
1403 main_field
= bt_event_borrow_context(event
);
1407 if (!pretty
->start_line
) {
1408 g_string_append(pretty
->string
, ", ");
1410 pretty
->start_line
= false;
1411 if (pretty
->options
.print_scope_field_names
) {
1412 print_name_equal(pretty
, "event.context");
1414 ret
= print_field(pretty
, main_field
,
1415 pretty
->options
.print_context_field_names
, NULL
, 0);
1422 enum bt_component_status
print_event_payload(struct pretty_component
*pretty
,
1423 struct bt_event
*event
)
1425 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1426 struct bt_field
*main_field
= NULL
;
1428 main_field
= bt_event_borrow_payload(event
);
1432 if (!pretty
->start_line
) {
1433 g_string_append(pretty
->string
, ", ");
1435 pretty
->start_line
= false;
1436 if (pretty
->options
.print_scope_field_names
) {
1437 print_name_equal(pretty
, "event.fields");
1439 ret
= print_field(pretty
, main_field
,
1440 pretty
->options
.print_payload_field_names
, NULL
, 0);
1447 int flush_buf(struct pretty_component
*pretty
)
1451 if (pretty
->string
->len
== 0) {
1455 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, pretty
->out
) != 1) {
1464 enum bt_component_status
pretty_print_event(struct pretty_component
*pretty
,
1465 struct bt_notification
*event_notif
)
1467 enum bt_component_status ret
;
1468 struct bt_event
*event
=
1469 bt_notification_event_borrow_event(event_notif
);
1470 struct bt_clock_class_priority_map
*cc_prio_map
=
1471 bt_notification_event_borrow_clock_class_priority_map(
1475 BT_ASSERT(cc_prio_map
);
1476 pretty
->start_line
= true;
1477 g_string_assign(pretty
->string
, "");
1478 ret
= print_event_header(pretty
, event
, cc_prio_map
);
1479 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1483 ret
= print_stream_packet_context(pretty
, event
);
1484 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1488 if (pretty
->options
.verbose
) {
1489 ret
= print_event_header_raw(pretty
, event
);
1490 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1495 ret
= print_stream_event_context(pretty
, event
);
1496 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1500 ret
= print_event_context(pretty
, event
);
1501 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1505 ret
= print_event_payload(pretty
, event
);
1506 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1510 g_string_append_c(pretty
->string
, '\n');
1511 if (flush_buf(pretty
)) {
1512 ret
= BT_COMPONENT_STATUS_ERROR
;
1521 enum bt_component_status
pretty_print_discarded_elements(
1522 struct pretty_component
*pretty
,
1523 struct bt_notification
*notif
)
1525 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1526 struct bt_stream
*stream
= NULL
;
1527 struct bt_stream_class
*stream_class
= NULL
;
1528 struct bt_trace
*trace
= NULL
;
1529 const char *stream_name
;
1530 const char *trace_name
;
1531 const unsigned char *trace_uuid
;
1532 int64_t stream_class_id
;
1534 bool is_discarded_events
;
1536 struct bt_clock_value
*clock_value
= NULL
;
1539 switch (bt_notification_get_type(notif
)) {
1540 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
1541 stream
= bt_notification_discarded_events_borrow_stream(notif
);
1542 count
= bt_notification_discarded_events_get_count(notif
);
1543 is_discarded_events
= true;
1545 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
1546 stream
= bt_notification_discarded_packets_borrow_stream(notif
);
1547 count
= bt_notification_discarded_packets_get_count(notif
);
1548 is_discarded_events
= false;
1555 stream_name
= bt_stream_get_name(stream
);
1557 /* Stream class ID */
1558 stream_class
= bt_stream_borrow_class(stream
);
1559 BT_ASSERT(stream_class
);
1560 stream_class_id
= bt_stream_class_get_id(stream_class
);
1563 stream_id
= bt_stream_get_id(stream
);
1566 trace
= bt_stream_class_borrow_trace(stream_class
);
1568 trace_name
= bt_trace_get_name(trace
);
1570 trace_name
= "(unknown)";
1574 trace_uuid
= bt_trace_get_uuid(trace
);
1577 * Print to standard error stream to remain backward compatible
1578 * with Babeltrace 1.
1581 "%s%sWARNING%s%s: Tracer discarded %" PRId64
" %s%s between [",
1582 bt_common_color_fg_yellow(),
1583 bt_common_color_bold(),
1584 bt_common_color_reset(),
1585 bt_common_color_fg_yellow(),
1586 count
, is_discarded_events
? "event" : "packet",
1587 count
== 1 ? "" : "s");
1588 g_string_assign(pretty
->string
, "");
1589 clock_value
= is_discarded_events
?
1590 bt_notification_discarded_events_borrow_begin_clock_value(notif
) :
1591 bt_notification_discarded_packets_borrow_begin_clock_value(notif
);
1592 print_timestamp_wall(pretty
, clock_value
);
1593 fprintf(stderr
, "%s] and [", pretty
->string
->str
);
1594 g_string_assign(pretty
->string
, "");
1595 clock_value
= is_discarded_events
?
1596 bt_notification_discarded_events_borrow_end_clock_value(notif
) :
1597 bt_notification_discarded_packets_borrow_end_clock_value(notif
);
1598 print_timestamp_wall(pretty
, clock_value
);
1599 fprintf(stderr
, "%s] in trace \"%s\" ",
1600 pretty
->string
->str
, trace_name
);
1604 "(UUID: %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x) ",
1622 fprintf(stderr
, "(no UUID) ");
1625 fprintf(stderr
, "within stream \"%s\" (stream class ID: %" PRId64
", ",
1626 stream_name
, stream_class_id
);
1628 if (stream_id
>= 0) {
1629 fprintf(stderr
, "stream ID: %" PRId64
, stream_id
);
1631 fprintf(stderr
, "no stream ID");
1634 fprintf(stderr
, ").%s\n", bt_common_color_reset());