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/ctf-ir/event.h>
31 #include <babeltrace/ctf-ir/event-class.h>
32 #include <babeltrace/ctf-ir/packet.h>
33 #include <babeltrace/ctf-ir/stream.h>
34 #include <babeltrace/ctf-ir/stream-class.h>
35 #include <babeltrace/ctf-ir/clock-class.h>
36 #include <babeltrace/ctf-ir/field-types.h>
37 #include <babeltrace/ctf-ir/fields.h>
38 #include <babeltrace/ctf-ir/trace.h>
39 #include <babeltrace/graph/notification-event.h>
40 #include <babeltrace/graph/clock-class-priority-map.h>
41 #include <babeltrace/bitfield-internal.h>
42 #include <babeltrace/common-internal.h>
43 #include <babeltrace/compat/time-internal.h>
48 #define NSEC_PER_SEC 1000000000LL
50 #define COLOR_NAME BT_COMMON_COLOR_BOLD
51 #define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN
52 #define COLOR_RST BT_COMMON_COLOR_RESET
53 #define COLOR_STRING_VALUE BT_COMMON_COLOR_BOLD
54 #define COLOR_NUMBER_VALUE BT_COMMON_COLOR_BOLD
55 #define COLOR_ENUM_MAPPING_NAME BT_COMMON_COLOR_BOLD
56 #define COLOR_UNKNOWN BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED
57 #define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA
58 #define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW
61 const char *rem_(const char *str
)
70 int64_t real_timestamp
; /* Relative to UNIX epoch. */
71 uint64_t clock_value
; /* In cycles. */
75 enum bt_component_status
print_field(struct pretty_component
*pretty
,
76 struct bt_ctf_field
*field
, bool print_names
,
77 GQuark
*filters_fields
, int filter_array_len
);
80 void print_name_equal(struct pretty_component
*pretty
, const char *name
)
82 if (pretty
->use_colors
) {
83 g_string_append_printf(pretty
->string
, "%s%s%s = ", COLOR_NAME
,
86 g_string_append_printf(pretty
->string
, "%s = ", name
);
91 void print_field_name_equal(struct pretty_component
*pretty
, const char *name
)
93 if (pretty
->use_colors
) {
94 g_string_append_printf(pretty
->string
, "%s%s%s = ",
95 COLOR_FIELD_NAME
, name
, COLOR_RST
);
97 g_string_append_printf(pretty
->string
, "%s = ", name
);
102 void print_timestamp_cycles(struct pretty_component
*pretty
,
103 struct bt_ctf_clock_class
*clock_class
,
104 struct bt_ctf_event
*event
)
107 struct bt_ctf_clock_value
*clock_value
;
110 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
112 g_string_append(pretty
->string
, "????????????????????");
116 ret
= bt_ctf_clock_value_get_value(clock_value
, &cycles
);
119 // TODO: log, this is unexpected
120 g_string_append(pretty
->string
, "Error");
124 g_string_append_printf(pretty
->string
, "%020" PRIu64
, cycles
);
126 if (pretty
->last_cycles_timestamp
!= -1ULL) {
127 pretty
->delta_cycles
= cycles
- pretty
->last_cycles_timestamp
;
129 pretty
->last_cycles_timestamp
= cycles
;
133 void print_timestamp_wall(struct pretty_component
*pretty
,
134 struct bt_ctf_clock_class
*clock_class
,
135 struct bt_ctf_event
*event
)
138 struct bt_ctf_clock_value
*clock_value
;
139 int64_t ts_nsec
= 0; /* add configurable offset */
140 int64_t ts_sec
= 0; /* add configurable offset */
141 uint64_t ts_sec_abs
, ts_nsec_abs
;
144 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
146 g_string_append(pretty
->string
, "??:??:??.?????????");
150 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
, &ts_nsec
);
153 // TODO: log, this is unexpected
154 g_string_append(pretty
->string
, "Error");
158 if (pretty
->last_real_timestamp
!= -1ULL) {
159 pretty
->delta_real_timestamp
= ts_nsec
- pretty
->last_real_timestamp
;
162 pretty
->last_real_timestamp
= ts_nsec
;
163 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
164 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
166 if (ts_sec
>= 0 && ts_nsec
>= 0) {
169 ts_nsec_abs
= ts_nsec
;
170 } else if (ts_sec
> 0 && ts_nsec
< 0) {
172 ts_sec_abs
= ts_sec
- 1;
173 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
174 } else if (ts_sec
== 0 && ts_nsec
< 0) {
177 ts_nsec_abs
= -ts_nsec
;
178 } else if (ts_sec
< 0 && ts_nsec
> 0) {
180 ts_sec_abs
= -(ts_sec
+ 1);
181 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
182 } else if (ts_sec
< 0 && ts_nsec
== 0) {
184 ts_sec_abs
= -ts_sec
;
185 ts_nsec_abs
= ts_nsec
;
186 } else { /* (ts_sec < 0 && ts_nsec < 0) */
188 ts_sec_abs
= -ts_sec
;
189 ts_nsec_abs
= -ts_nsec
;
192 if (!pretty
->options
.clock_seconds
) {
194 time_t time_s
= (time_t) ts_sec_abs
;
198 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
202 if (!pretty
->options
.clock_gmt
) {
205 res
= bt_localtime_r(&time_s
, &tm
);
208 fprintf(stderr
, "[warning] Unable to get localtime.\n");
214 res
= bt_gmtime_r(&time_s
, &tm
);
217 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
221 if (pretty
->options
.clock_date
) {
225 /* Print date and time */
226 res
= strftime(timestr
, sizeof(timestr
),
230 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
234 g_string_append(pretty
->string
, timestr
);
237 /* Print time in HH:MM:SS.ns */
238 g_string_append_printf(pretty
->string
,
239 "%02d:%02d:%02d.%09" PRIu64
, tm
.tm_hour
, tm
.tm_min
,
240 tm
.tm_sec
, ts_nsec_abs
);
244 g_string_append_printf(pretty
->string
, "%s%" PRId64
".%09" PRIu64
,
245 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
251 enum bt_component_status
print_event_timestamp(struct pretty_component
*pretty
,
252 struct bt_ctf_event
*event
,
253 struct bt_clock_class_priority_map
*cc_prio_map
,
256 bool print_names
= pretty
->options
.print_header_field_names
;
257 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
258 struct bt_ctf_stream
*stream
= NULL
;
259 struct bt_ctf_stream_class
*stream_class
= NULL
;
260 struct bt_ctf_trace
*trace
= NULL
;
261 struct bt_ctf_clock_class
*clock_class
= NULL
;
263 stream
= bt_ctf_event_get_stream(event
);
265 ret
= BT_COMPONENT_STATUS_ERROR
;
269 stream_class
= bt_ctf_stream_get_class(stream
);
271 ret
= BT_COMPONENT_STATUS_ERROR
;
274 trace
= bt_ctf_stream_class_get_trace(stream_class
);
276 ret
= BT_COMPONENT_STATUS_ERROR
;
280 if (bt_clock_class_priority_map_get_clock_class_count(cc_prio_map
) == 0) {
281 /* No clock class: skip the timestamp without an error */
286 bt_clock_class_priority_map_get_highest_priority_clock_class(
289 ret
= BT_COMPONENT_STATUS_ERROR
;
294 print_name_equal(pretty
, "timestamp");
296 g_string_append(pretty
->string
, "[");
298 if (pretty
->use_colors
) {
299 g_string_append(pretty
->string
, COLOR_TIMESTAMP
);
301 if (pretty
->options
.print_timestamp_cycles
) {
302 print_timestamp_cycles(pretty
, clock_class
, event
);
304 print_timestamp_wall(pretty
, clock_class
, event
);
306 if (pretty
->use_colors
) {
307 g_string_append(pretty
->string
, COLOR_RST
);
311 g_string_append(pretty
->string
, "] ");
313 if (pretty
->options
.print_delta_field
) {
315 g_string_append(pretty
->string
, ", ");
316 print_name_equal(pretty
, "delta");
318 g_string_append(pretty
->string
, "(");
320 if (pretty
->options
.print_timestamp_cycles
) {
321 if (pretty
->delta_cycles
== -1ULL) {
322 g_string_append(pretty
->string
,
323 "+??????????\?\?) "); /* Not a trigraph. */
325 g_string_append_printf(pretty
->string
,
326 "+%012" PRIu64
, pretty
->delta_cycles
);
329 if (pretty
->delta_real_timestamp
!= -1ULL) {
330 uint64_t delta_sec
, delta_nsec
, delta
;
332 delta
= pretty
->delta_real_timestamp
;
333 delta_sec
= delta
/ NSEC_PER_SEC
;
334 delta_nsec
= delta
% NSEC_PER_SEC
;
335 g_string_append_printf(pretty
->string
,
336 "+%" PRIu64
".%09" PRIu64
,
337 delta_sec
, delta_nsec
);
339 g_string_append(pretty
->string
, "+?.?????????");
343 g_string_append(pretty
->string
, ") ");
346 *start_line
= !print_names
;
351 bt_put(stream_class
);
357 enum bt_component_status
print_event_header(struct pretty_component
*pretty
,
358 struct bt_ctf_event
*event
,
359 struct bt_clock_class_priority_map
*cc_prio_map
)
361 bool print_names
= pretty
->options
.print_header_field_names
;
362 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
363 struct bt_ctf_event_class
*event_class
= NULL
;
364 struct bt_ctf_stream_class
*stream_class
= NULL
;
365 struct bt_ctf_trace
*trace_class
= NULL
;
368 event_class
= bt_ctf_event_get_class(event
);
370 ret
= BT_COMPONENT_STATUS_ERROR
;
373 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
375 ret
= BT_COMPONENT_STATUS_ERROR
;
378 trace_class
= bt_ctf_stream_class_get_trace(stream_class
);
380 ret
= BT_COMPONENT_STATUS_ERROR
;
383 ret
= print_event_timestamp(pretty
, event
, cc_prio_map
,
384 &pretty
->start_line
);
385 if (ret
!= BT_COMPONENT_STATUS_OK
) {
388 if (pretty
->options
.print_trace_field
) {
391 name
= bt_ctf_trace_get_name(trace_class
);
393 if (!pretty
->start_line
) {
394 g_string_append(pretty
->string
, ", ");
397 print_name_equal(pretty
, "trace");
400 g_string_append(pretty
->string
, name
);
403 g_string_append(pretty
->string
, " ");
407 if (pretty
->options
.print_trace_hostname_field
) {
408 struct bt_value
*hostname_str
;
410 hostname_str
= bt_ctf_trace_get_environment_field_value_by_name(trace_class
,
415 if (!pretty
->start_line
) {
416 g_string_append(pretty
->string
, ", ");
419 print_name_equal(pretty
, "trace:hostname");
421 if (bt_value_string_get(hostname_str
, &str
)
422 == BT_VALUE_STATUS_OK
) {
423 g_string_append(pretty
->string
, str
);
425 bt_put(hostname_str
);
429 if (pretty
->options
.print_trace_domain_field
) {
430 struct bt_value
*domain_str
;
432 domain_str
= bt_ctf_trace_get_environment_field_value_by_name(trace_class
,
437 if (!pretty
->start_line
) {
438 g_string_append(pretty
->string
, ", ");
441 print_name_equal(pretty
, "trace:domain");
442 } else if (dom_print
) {
443 g_string_append(pretty
->string
, ":");
445 if (bt_value_string_get(domain_str
, &str
)
446 == BT_VALUE_STATUS_OK
) {
447 g_string_append(pretty
->string
, str
);
453 if (pretty
->options
.print_trace_procname_field
) {
454 struct bt_value
*procname_str
;
456 procname_str
= bt_ctf_trace_get_environment_field_value_by_name(trace_class
,
461 if (!pretty
->start_line
) {
462 g_string_append(pretty
->string
, ", ");
465 print_name_equal(pretty
, "trace:procname");
466 } else if (dom_print
) {
467 g_string_append(pretty
->string
, ":");
469 if (bt_value_string_get(procname_str
, &str
)
470 == BT_VALUE_STATUS_OK
) {
471 g_string_append(pretty
->string
, str
);
473 bt_put(procname_str
);
477 if (pretty
->options
.print_trace_vpid_field
) {
478 struct bt_value
*vpid_value
;
480 vpid_value
= bt_ctf_trace_get_environment_field_value_by_name(trace_class
,
485 if (!pretty
->start_line
) {
486 g_string_append(pretty
->string
, ", ");
489 print_name_equal(pretty
, "trace:vpid");
490 } else if (dom_print
) {
491 g_string_append(pretty
->string
, ":");
493 if (bt_value_integer_get(vpid_value
, &value
)
494 == BT_VALUE_STATUS_OK
) {
495 g_string_append_printf(pretty
->string
, "(%" PRId64
")", value
);
501 if (pretty
->options
.print_loglevel_field
) {
502 struct bt_value
*loglevel_str
, *loglevel_value
;
504 loglevel_str
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
506 loglevel_value
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
508 if (loglevel_str
|| loglevel_value
) {
509 bool has_str
= false;
511 if (!pretty
->start_line
) {
512 g_string_append(pretty
->string
, ", ");
515 print_name_equal(pretty
, "loglevel");
516 } else if (dom_print
) {
517 g_string_append(pretty
->string
, ":");
522 if (bt_value_string_get(loglevel_str
, &str
)
523 == BT_VALUE_STATUS_OK
) {
524 g_string_append(pretty
->string
, str
);
528 if (loglevel_value
) {
531 if (bt_value_integer_get(loglevel_value
, &value
)
532 == BT_VALUE_STATUS_OK
) {
533 g_string_append_printf(pretty
->string
, "%s(%" PRId64
")",
534 has_str
? " " : "", value
);
537 bt_put(loglevel_str
);
538 bt_put(loglevel_value
);
542 if (pretty
->options
.print_emf_field
) {
543 struct bt_value
*uri_str
;
545 uri_str
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
548 if (!pretty
->start_line
) {
549 g_string_append(pretty
->string
, ", ");
552 print_name_equal(pretty
, "model.emf.uri");
553 } else if (dom_print
) {
554 g_string_append(pretty
->string
, ":");
559 if (bt_value_string_get(uri_str
, &str
)
560 == BT_VALUE_STATUS_OK
) {
561 g_string_append(pretty
->string
, str
);
568 if (dom_print
&& !print_names
) {
569 g_string_append(pretty
->string
, " ");
571 if (!pretty
->start_line
) {
572 g_string_append(pretty
->string
, ", ");
574 pretty
->start_line
= true;
576 print_name_equal(pretty
, "name");
578 if (pretty
->use_colors
) {
579 g_string_append(pretty
->string
, COLOR_EVENT_NAME
);
581 g_string_append(pretty
->string
, bt_ctf_event_class_get_name(event_class
));
582 if (pretty
->use_colors
) {
583 g_string_append(pretty
->string
, COLOR_RST
);
586 g_string_append(pretty
->string
, ": ");
588 g_string_append(pretty
->string
, ", ");
592 bt_put(stream_class
);
598 enum bt_component_status
print_integer(struct pretty_component
*pretty
,
599 struct bt_ctf_field
*field
)
601 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
602 struct bt_ctf_field_type
*field_type
= NULL
;
603 enum bt_ctf_integer_base base
;
604 enum bt_ctf_string_encoding encoding
;
610 bool rst_color
= false;
612 field_type
= bt_ctf_field_get_type(field
);
614 ret
= BT_COMPONENT_STATUS_ERROR
;
617 signedness
= bt_ctf_field_type_integer_get_signed(field_type
);
618 if (signedness
< 0) {
619 ret
= BT_COMPONENT_STATUS_ERROR
;
623 if (bt_ctf_field_unsigned_integer_get_value(field
, &v
.u
) < 0) {
624 ret
= BT_COMPONENT_STATUS_ERROR
;
628 if (bt_ctf_field_signed_integer_get_value(field
, &v
.s
) < 0) {
629 ret
= BT_COMPONENT_STATUS_ERROR
;
634 encoding
= bt_ctf_field_type_integer_get_encoding(field_type
);
636 case BT_CTF_STRING_ENCODING_UTF8
:
637 case BT_CTF_STRING_ENCODING_ASCII
:
638 g_string_append_c(pretty
->tmp_string
, (int) v
.u
);
640 case BT_CTF_STRING_ENCODING_NONE
:
641 case BT_CTF_STRING_ENCODING_UNKNOWN
:
644 ret
= BT_COMPONENT_STATUS_ERROR
;
648 if (pretty
->use_colors
) {
649 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
653 base
= bt_ctf_field_type_integer_get_base(field_type
);
655 case BT_CTF_INTEGER_BASE_BINARY
:
659 len
= bt_ctf_field_type_integer_get_size(field_type
);
661 ret
= BT_COMPONENT_STATUS_ERROR
;
664 g_string_append(pretty
->string
, "0b");
665 v
.u
= _bt_piecewise_lshift(v
.u
, 64 - len
);
666 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
667 g_string_append_printf(pretty
->string
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
668 v
.u
= _bt_piecewise_lshift(v
.u
, 1);
672 case BT_CTF_INTEGER_BASE_OCTAL
:
677 len
= bt_ctf_field_type_integer_get_size(field_type
);
679 ret
= BT_COMPONENT_STATUS_ERROR
;
686 /* Round length to the nearest 3-bit */
687 rounded_len
= (((len
- 1) / 3) + 1) * 3;
688 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
692 g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
695 case BT_CTF_INTEGER_BASE_DECIMAL
:
696 case BT_CTF_INTEGER_BASE_UNSPECIFIED
:
698 g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
700 g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
703 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
707 len
= bt_ctf_field_type_integer_get_size(field_type
);
709 ret
= BT_COMPONENT_STATUS_ERROR
;
713 /* Round length to the nearest nibble */
714 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
716 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
719 g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
723 ret
= BT_COMPONENT_STATUS_ERROR
;
728 g_string_append(pretty
->string
, COLOR_RST
);
735 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
739 g_string_append_c(pretty
->string
, '"');
741 for (i
= 0; i
< strlen(str
); i
++) {
742 /* Escape sequences not recognized by iscntrl(). */
745 g_string_append(pretty
->string
, "\\\\");
748 g_string_append(pretty
->string
, "\\\'");
751 g_string_append(pretty
->string
, "\\\"");
754 g_string_append(pretty
->string
, "\\\?");
758 /* Standard characters. */
759 if (!iscntrl(str
[i
])) {
760 g_string_append_c(pretty
->string
, str
[i
]);
766 g_string_append(pretty
->string
, "\\0");
769 g_string_append(pretty
->string
, "\\a");
772 g_string_append(pretty
->string
, "\\b");
775 g_string_append(pretty
->string
, "\\e");
778 g_string_append(pretty
->string
, "\\f");
781 g_string_append(pretty
->string
, "\\n");
784 g_string_append(pretty
->string
, "\\r");
787 g_string_append(pretty
->string
, "\\t");
790 g_string_append(pretty
->string
, "\\v");
793 /* Unhandled control-sequence, print as hex. */
794 g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
799 g_string_append_c(pretty
->string
, '"');
803 enum bt_component_status
print_enum(struct pretty_component
*pretty
,
804 struct bt_ctf_field
*field
)
806 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
807 struct bt_ctf_field
*container_field
= NULL
;
808 struct bt_ctf_field_type
*enumeration_field_type
= NULL
;
809 struct bt_ctf_field_type
*container_field_type
= NULL
;
810 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
814 enumeration_field_type
= bt_ctf_field_get_type(field
);
815 if (!enumeration_field_type
) {
816 ret
= BT_COMPONENT_STATUS_ERROR
;
819 container_field
= bt_ctf_field_enumeration_get_container(field
);
820 if (!container_field
) {
821 ret
= BT_COMPONENT_STATUS_ERROR
;
824 container_field_type
= bt_ctf_field_get_type(container_field
);
825 if (!container_field_type
) {
826 ret
= BT_COMPONENT_STATUS_ERROR
;
829 is_signed
= bt_ctf_field_type_integer_get_signed(container_field_type
);
831 ret
= BT_COMPONENT_STATUS_ERROR
;
837 if (bt_ctf_field_signed_integer_get_value(container_field
,
839 ret
= BT_COMPONENT_STATUS_ERROR
;
842 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
843 enumeration_field_type
, value
);
847 if (bt_ctf_field_unsigned_integer_get_value(container_field
,
849 ret
= BT_COMPONENT_STATUS_ERROR
;
852 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
853 enumeration_field_type
, value
);
856 ret
= BT_COMPONENT_STATUS_ERROR
;
859 g_string_append(pretty
->string
, "( ");
861 const char *mapping_name
;
863 if (bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
864 iter
, &mapping_name
, NULL
, NULL
) < 0) {
865 ret
= BT_COMPONENT_STATUS_ERROR
;
869 g_string_append(pretty
->string
, ", ");
870 if (pretty
->use_colors
) {
871 g_string_append(pretty
->string
, COLOR_ENUM_MAPPING_NAME
);
873 print_escape_string(pretty
, mapping_name
);
874 if (pretty
->use_colors
) {
875 g_string_append(pretty
->string
, COLOR_RST
);
877 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
) < 0) {
882 if (pretty
->use_colors
) {
883 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
885 g_string_append(pretty
->string
, "<unknown>");
886 if (pretty
->use_colors
) {
887 g_string_append(pretty
->string
, COLOR_RST
);
890 g_string_append(pretty
->string
, " : container = ");
891 ret
= print_integer(pretty
, container_field
);
892 if (ret
!= BT_COMPONENT_STATUS_OK
) {
895 g_string_append(pretty
->string
, " )");
898 bt_put(container_field_type
);
899 bt_put(container_field
);
900 bt_put(enumeration_field_type
);
905 int filter_field_name(struct pretty_component
*pretty
, const char *field_name
,
906 GQuark
*filter_fields
, int filter_array_len
)
909 GQuark field_quark
= g_quark_try_string(field_name
);
911 if (!field_quark
|| pretty
->options
.verbose
) {
915 for (i
= 0; i
< filter_array_len
; i
++) {
916 if (field_quark
== filter_fields
[i
]) {
924 enum bt_component_status
print_struct_field(struct pretty_component
*pretty
,
925 struct bt_ctf_field
*_struct
,
926 struct bt_ctf_field_type
*struct_type
,
927 int i
, bool print_names
, int *nr_printed_fields
,
928 GQuark
*filter_fields
, int filter_array_len
)
930 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
931 const char *field_name
;
932 struct bt_ctf_field
*field
= NULL
;
933 struct bt_ctf_field_type
*field_type
= NULL
;;
935 field
= bt_ctf_field_structure_get_field_by_index(_struct
, i
);
937 ret
= BT_COMPONENT_STATUS_ERROR
;
940 if (bt_ctf_field_type_structure_get_field(struct_type
,
941 &field_name
, &field_type
, i
) < 0) {
942 ret
= BT_COMPONENT_STATUS_ERROR
;
946 if (filter_fields
&& !filter_field_name(pretty
, field_name
,
947 filter_fields
, filter_array_len
)) {
948 ret
= BT_COMPONENT_STATUS_OK
;
952 if (*nr_printed_fields
> 0) {
953 g_string_append(pretty
->string
, ", ");
955 g_string_append(pretty
->string
, " ");
958 print_field_name_equal(pretty
, rem_(field_name
));
960 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
961 *nr_printed_fields
+= 1;
969 enum bt_component_status
print_struct(struct pretty_component
*pretty
,
970 struct bt_ctf_field
*_struct
, bool print_names
,
971 GQuark
*filter_fields
, int filter_array_len
)
973 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
974 struct bt_ctf_field_type
*struct_type
= NULL
;
975 int nr_fields
, i
, nr_printed_fields
;
977 struct_type
= bt_ctf_field_get_type(_struct
);
979 ret
= BT_COMPONENT_STATUS_ERROR
;
982 nr_fields
= bt_ctf_field_type_structure_get_field_count(struct_type
);
984 ret
= BT_COMPONENT_STATUS_ERROR
;
987 g_string_append(pretty
->string
, "{");
989 nr_printed_fields
= 0;
990 for (i
= 0; i
< nr_fields
; i
++) {
991 ret
= print_struct_field(pretty
, _struct
, struct_type
, i
,
992 print_names
, &nr_printed_fields
, filter_fields
,
994 if (ret
!= BT_COMPONENT_STATUS_OK
) {
999 g_string_append(pretty
->string
, " }");
1001 bt_put(struct_type
);
1006 enum bt_component_status
print_array_field(struct pretty_component
*pretty
,
1007 struct bt_ctf_field
*array
, uint64_t i
,
1008 bool is_string
, bool print_names
)
1010 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1011 struct bt_ctf_field
*field
= NULL
;
1015 g_string_append(pretty
->string
, ", ");
1017 g_string_append(pretty
->string
, " ");
1020 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
1023 field
= bt_ctf_field_array_get_field(array
, i
);
1025 ret
= BT_COMPONENT_STATUS_ERROR
;
1028 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
1035 enum bt_component_status
print_array(struct pretty_component
*pretty
,
1036 struct bt_ctf_field
*array
, bool print_names
)
1038 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1039 struct bt_ctf_field_type
*array_type
= NULL
, *field_type
= NULL
;
1040 enum bt_ctf_field_type_id type_id
;
1043 bool is_string
= false;
1045 array_type
= bt_ctf_field_get_type(array
);
1047 ret
= BT_COMPONENT_STATUS_ERROR
;
1050 field_type
= bt_ctf_field_type_array_get_element_type(array_type
);
1052 ret
= BT_COMPONENT_STATUS_ERROR
;
1055 len
= bt_ctf_field_type_array_get_length(array_type
);
1057 ret
= BT_COMPONENT_STATUS_ERROR
;
1060 type_id
= bt_ctf_field_type_get_type_id(field_type
);
1061 if (type_id
== BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1062 enum bt_ctf_string_encoding encoding
;
1064 encoding
= bt_ctf_field_type_integer_get_encoding(field_type
);
1065 if (encoding
== BT_CTF_STRING_ENCODING_UTF8
1066 || encoding
== BT_CTF_STRING_ENCODING_ASCII
) {
1067 int integer_len
, integer_alignment
;
1069 integer_len
= bt_ctf_field_type_integer_get_size(field_type
);
1070 if (integer_len
< 0) {
1071 return BT_COMPONENT_STATUS_ERROR
;
1073 integer_alignment
= bt_ctf_field_type_get_alignment(field_type
);
1074 if (integer_alignment
< 0) {
1075 return BT_COMPONENT_STATUS_ERROR
;
1077 if (integer_len
== CHAR_BIT
1078 && integer_alignment
== CHAR_BIT
) {
1085 g_string_assign(pretty
->tmp_string
, "");
1087 g_string_append(pretty
->string
, "[");
1091 for (i
= 0; i
< len
; i
++) {
1092 ret
= print_array_field(pretty
, array
, i
, is_string
, print_names
);
1093 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1100 if (pretty
->use_colors
) {
1101 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1103 print_escape_string(pretty
, pretty
->tmp_string
->str
);
1104 if (pretty
->use_colors
) {
1105 g_string_append(pretty
->string
, COLOR_RST
);
1108 g_string_append(pretty
->string
, " ]");
1117 enum bt_component_status
print_sequence_field(struct pretty_component
*pretty
,
1118 struct bt_ctf_field
*seq
, uint64_t i
,
1119 bool is_string
, bool print_names
)
1121 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1122 struct bt_ctf_field
*field
= NULL
;
1126 g_string_append(pretty
->string
, ", ");
1128 g_string_append(pretty
->string
, " ");
1131 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
1134 field
= bt_ctf_field_sequence_get_field(seq
, i
);
1136 ret
= BT_COMPONENT_STATUS_ERROR
;
1139 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
1146 enum bt_component_status
print_sequence(struct pretty_component
*pretty
,
1147 struct bt_ctf_field
*seq
, bool print_names
)
1149 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1150 struct bt_ctf_field_type
*seq_type
= NULL
, *field_type
= NULL
;
1151 struct bt_ctf_field
*length_field
= NULL
;
1152 enum bt_ctf_field_type_id type_id
;
1155 bool is_string
= false;
1157 seq_type
= bt_ctf_field_get_type(seq
);
1159 ret
= BT_COMPONENT_STATUS_ERROR
;
1162 length_field
= bt_ctf_field_sequence_get_length(seq
);
1163 if (!length_field
) {
1164 ret
= BT_COMPONENT_STATUS_ERROR
;
1167 if (bt_ctf_field_unsigned_integer_get_value(length_field
, &len
) < 0) {
1168 ret
= BT_COMPONENT_STATUS_ERROR
;
1171 field_type
= bt_ctf_field_type_sequence_get_element_type(seq_type
);
1173 ret
= BT_COMPONENT_STATUS_ERROR
;
1176 type_id
= bt_ctf_field_type_get_type_id(field_type
);
1177 if (type_id
== BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1178 enum bt_ctf_string_encoding encoding
;
1180 encoding
= bt_ctf_field_type_integer_get_encoding(field_type
);
1181 if (encoding
== BT_CTF_STRING_ENCODING_UTF8
1182 || encoding
== BT_CTF_STRING_ENCODING_ASCII
) {
1183 int integer_len
, integer_alignment
;
1185 integer_len
= bt_ctf_field_type_integer_get_size(field_type
);
1186 if (integer_len
< 0) {
1187 ret
= BT_COMPONENT_STATUS_ERROR
;
1190 integer_alignment
= bt_ctf_field_type_get_alignment(field_type
);
1191 if (integer_alignment
< 0) {
1192 ret
= BT_COMPONENT_STATUS_ERROR
;
1195 if (integer_len
== CHAR_BIT
1196 && integer_alignment
== CHAR_BIT
) {
1203 g_string_assign(pretty
->tmp_string
, "");
1205 g_string_append(pretty
->string
, "[");
1209 for (i
= 0; i
< len
; i
++) {
1210 ret
= print_sequence_field(pretty
, seq
, i
,
1211 is_string
, print_names
);
1212 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1219 if (pretty
->use_colors
) {
1220 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1222 print_escape_string(pretty
, pretty
->tmp_string
->str
);
1223 if (pretty
->use_colors
) {
1224 g_string_append(pretty
->string
, COLOR_RST
);
1227 g_string_append(pretty
->string
, " ]");
1230 bt_put(length_field
);
1237 enum bt_component_status
print_variant(struct pretty_component
*pretty
,
1238 struct bt_ctf_field
*variant
, bool print_names
)
1240 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1241 struct bt_ctf_field
*field
= NULL
;
1243 field
= bt_ctf_field_variant_get_current_field(variant
);
1245 ret
= BT_COMPONENT_STATUS_ERROR
;
1248 g_string_append(pretty
->string
, "{ ");
1252 struct bt_ctf_field
*tag_field
= NULL
;
1253 const char *tag_choice
;
1254 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1256 tag_field
= bt_ctf_field_variant_get_tag(variant
);
1258 ret
= BT_COMPONENT_STATUS_ERROR
;
1262 iter
= bt_ctf_field_enumeration_get_mappings(tag_field
);
1265 ret
= BT_COMPONENT_STATUS_ERROR
;
1270 bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1271 iter
, &tag_choice
, NULL
, NULL
);
1275 ret
= BT_COMPONENT_STATUS_ERROR
;
1278 print_field_name_equal(pretty
, rem_(tag_choice
));
1282 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
1283 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1287 g_string_append(pretty
->string
, " }");
1294 enum bt_component_status
print_field(struct pretty_component
*pretty
,
1295 struct bt_ctf_field
*field
, bool print_names
,
1296 GQuark
*filter_fields
, int filter_array_len
)
1298 enum bt_ctf_field_type_id type_id
;
1300 type_id
= bt_ctf_field_get_type_id(field
);
1302 case CTF_TYPE_INTEGER
:
1303 return print_integer(pretty
, field
);
1304 case CTF_TYPE_FLOAT
:
1308 if (bt_ctf_field_floating_point_get_value(field
, &v
)) {
1309 return BT_COMPONENT_STATUS_ERROR
;
1311 if (pretty
->use_colors
) {
1312 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
1314 g_string_append_printf(pretty
->string
, "%g", v
);
1315 if (pretty
->use_colors
) {
1316 g_string_append(pretty
->string
, COLOR_RST
);
1318 return BT_COMPONENT_STATUS_OK
;
1321 return print_enum(pretty
, field
);
1322 case CTF_TYPE_STRING
:
1323 if (pretty
->use_colors
) {
1324 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1326 print_escape_string(pretty
, bt_ctf_field_string_get_value(field
));
1327 if (pretty
->use_colors
) {
1328 g_string_append(pretty
->string
, COLOR_RST
);
1330 return BT_COMPONENT_STATUS_OK
;
1331 case CTF_TYPE_STRUCT
:
1332 return print_struct(pretty
, field
, print_names
, filter_fields
,
1334 case CTF_TYPE_VARIANT
:
1335 return print_variant(pretty
, field
, print_names
);
1336 case CTF_TYPE_ARRAY
:
1337 return print_array(pretty
, field
, print_names
);
1338 case CTF_TYPE_SEQUENCE
:
1339 return print_sequence(pretty
, field
, print_names
);
1341 // TODO: log instead
1342 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) type_id
);
1343 return BT_COMPONENT_STATUS_ERROR
;
1348 enum bt_component_status
print_stream_packet_context(struct pretty_component
*pretty
,
1349 struct bt_ctf_event
*event
)
1351 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1352 struct bt_ctf_packet
*packet
= NULL
;
1353 struct bt_ctf_field
*main_field
= NULL
;
1355 packet
= bt_ctf_event_get_packet(event
);
1357 ret
= BT_COMPONENT_STATUS_ERROR
;
1360 main_field
= bt_ctf_packet_get_context(packet
);
1364 if (!pretty
->start_line
) {
1365 g_string_append(pretty
->string
, ", ");
1367 pretty
->start_line
= false;
1368 if (pretty
->options
.print_scope_field_names
) {
1369 print_name_equal(pretty
, "stream.packet.context");
1371 ret
= print_field(pretty
, main_field
,
1372 pretty
->options
.print_context_field_names
,
1373 stream_packet_context_quarks
,
1374 STREAM_PACKET_CONTEXT_QUARKS_LEN
);
1382 enum bt_component_status
print_event_header_raw(struct pretty_component
*pretty
,
1383 struct bt_ctf_event
*event
)
1385 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1386 struct bt_ctf_field
*main_field
= NULL
;
1388 main_field
= bt_ctf_event_get_header(event
);
1392 if (!pretty
->start_line
) {
1393 g_string_append(pretty
->string
, ", ");
1395 pretty
->start_line
= false;
1396 if (pretty
->options
.print_scope_field_names
) {
1397 print_name_equal(pretty
, "stream.event.header");
1399 ret
= print_field(pretty
, main_field
,
1400 pretty
->options
.print_header_field_names
, NULL
, 0);
1407 enum bt_component_status
print_stream_event_context(struct pretty_component
*pretty
,
1408 struct bt_ctf_event
*event
)
1410 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1411 struct bt_ctf_field
*main_field
= NULL
;
1413 main_field
= bt_ctf_event_get_stream_event_context(event
);
1417 if (!pretty
->start_line
) {
1418 g_string_append(pretty
->string
, ", ");
1420 pretty
->start_line
= false;
1421 if (pretty
->options
.print_scope_field_names
) {
1422 print_name_equal(pretty
, "stream.event.context");
1424 ret
= print_field(pretty
, main_field
,
1425 pretty
->options
.print_context_field_names
, NULL
, 0);
1432 enum bt_component_status
print_event_context(struct pretty_component
*pretty
,
1433 struct bt_ctf_event
*event
)
1435 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1436 struct bt_ctf_field
*main_field
= NULL
;
1438 main_field
= bt_ctf_event_get_event_context(event
);
1442 if (!pretty
->start_line
) {
1443 g_string_append(pretty
->string
, ", ");
1445 pretty
->start_line
= false;
1446 if (pretty
->options
.print_scope_field_names
) {
1447 print_name_equal(pretty
, "event.context");
1449 ret
= print_field(pretty
, main_field
,
1450 pretty
->options
.print_context_field_names
, NULL
, 0);
1457 enum bt_component_status
print_event_payload(struct pretty_component
*pretty
,
1458 struct bt_ctf_event
*event
)
1460 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1461 struct bt_ctf_field
*main_field
= NULL
;
1463 main_field
= bt_ctf_event_get_event_payload(event
);
1467 if (!pretty
->start_line
) {
1468 g_string_append(pretty
->string
, ", ");
1470 pretty
->start_line
= false;
1471 if (pretty
->options
.print_scope_field_names
) {
1472 print_name_equal(pretty
, "event.fields");
1474 ret
= print_field(pretty
, main_field
,
1475 pretty
->options
.print_payload_field_names
, NULL
, 0);
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_ctf_event
*event
=
1487 bt_notification_event_get_event(event_notif
);
1488 struct bt_clock_class_priority_map
*cc_prio_map
=
1489 bt_notification_event_get_clock_class_priority_map(event_notif
);
1492 assert(cc_prio_map
);
1493 pretty
->start_line
= true;
1494 g_string_assign(pretty
->string
, "");
1495 ret
= print_event_header(pretty
, event
, cc_prio_map
);
1496 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1500 ret
= print_stream_packet_context(pretty
, event
);
1501 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1505 if (pretty
->options
.verbose
) {
1506 ret
= print_event_header_raw(pretty
, event
);
1507 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1512 ret
= print_stream_event_context(pretty
, event
);
1513 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1517 ret
= print_event_context(pretty
, event
);
1518 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1522 ret
= print_event_payload(pretty
, event
);
1523 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1527 g_string_append_c(pretty
->string
, '\n');
1528 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, pretty
->out
) != 1) {
1529 ret
= BT_COMPONENT_STATUS_ERROR
;
1535 bt_put(cc_prio_map
);