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
:
697 g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
699 g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
702 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
706 len
= bt_ctf_field_type_integer_get_size(field_type
);
708 ret
= BT_COMPONENT_STATUS_ERROR
;
712 /* Round length to the nearest nibble */
713 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
715 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
718 g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
722 ret
= BT_COMPONENT_STATUS_ERROR
;
727 g_string_append(pretty
->string
, COLOR_RST
);
734 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
738 g_string_append_c(pretty
->string
, '"');
740 for (i
= 0; i
< strlen(str
); i
++) {
741 /* Escape sequences not recognized by iscntrl(). */
744 g_string_append(pretty
->string
, "\\\\");
747 g_string_append(pretty
->string
, "\\\'");
750 g_string_append(pretty
->string
, "\\\"");
753 g_string_append(pretty
->string
, "\\\?");
757 /* Standard characters. */
758 if (!iscntrl(str
[i
])) {
759 g_string_append_c(pretty
->string
, str
[i
]);
765 g_string_append(pretty
->string
, "\\0");
768 g_string_append(pretty
->string
, "\\a");
771 g_string_append(pretty
->string
, "\\b");
774 g_string_append(pretty
->string
, "\\e");
777 g_string_append(pretty
->string
, "\\f");
780 g_string_append(pretty
->string
, "\\n");
783 g_string_append(pretty
->string
, "\\r");
786 g_string_append(pretty
->string
, "\\t");
789 g_string_append(pretty
->string
, "\\v");
792 /* Unhandled control-sequence, print as hex. */
793 g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
798 g_string_append_c(pretty
->string
, '"');
802 enum bt_component_status
print_enum(struct pretty_component
*pretty
,
803 struct bt_ctf_field
*field
)
805 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
806 struct bt_ctf_field
*container_field
= NULL
;
807 struct bt_ctf_field_type
*enumeration_field_type
= NULL
;
808 struct bt_ctf_field_type
*container_field_type
= NULL
;
809 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
813 enumeration_field_type
= bt_ctf_field_get_type(field
);
814 if (!enumeration_field_type
) {
815 ret
= BT_COMPONENT_STATUS_ERROR
;
818 container_field
= bt_ctf_field_enumeration_get_container(field
);
819 if (!container_field
) {
820 ret
= BT_COMPONENT_STATUS_ERROR
;
823 container_field_type
= bt_ctf_field_get_type(container_field
);
824 if (!container_field_type
) {
825 ret
= BT_COMPONENT_STATUS_ERROR
;
828 is_signed
= bt_ctf_field_type_integer_get_signed(container_field_type
);
830 ret
= BT_COMPONENT_STATUS_ERROR
;
836 if (bt_ctf_field_signed_integer_get_value(container_field
,
838 ret
= BT_COMPONENT_STATUS_ERROR
;
841 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
842 enumeration_field_type
, value
);
846 if (bt_ctf_field_unsigned_integer_get_value(container_field
,
848 ret
= BT_COMPONENT_STATUS_ERROR
;
851 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
852 enumeration_field_type
, value
);
855 ret
= BT_COMPONENT_STATUS_ERROR
;
858 g_string_append(pretty
->string
, "( ");
860 const char *mapping_name
;
862 if (bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
863 iter
, &mapping_name
, NULL
, NULL
) < 0) {
864 ret
= BT_COMPONENT_STATUS_ERROR
;
868 g_string_append(pretty
->string
, ", ");
869 if (pretty
->use_colors
) {
870 g_string_append(pretty
->string
, COLOR_ENUM_MAPPING_NAME
);
872 print_escape_string(pretty
, mapping_name
);
873 if (pretty
->use_colors
) {
874 g_string_append(pretty
->string
, COLOR_RST
);
876 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
) < 0) {
881 if (pretty
->use_colors
) {
882 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
884 g_string_append(pretty
->string
, "<unknown>");
885 if (pretty
->use_colors
) {
886 g_string_append(pretty
->string
, COLOR_RST
);
889 g_string_append(pretty
->string
, " : container = ");
890 ret
= print_integer(pretty
, container_field
);
891 if (ret
!= BT_COMPONENT_STATUS_OK
) {
894 g_string_append(pretty
->string
, " )");
897 bt_put(container_field_type
);
898 bt_put(container_field
);
899 bt_put(enumeration_field_type
);
904 int filter_field_name(struct pretty_component
*pretty
, const char *field_name
,
905 GQuark
*filter_fields
, int filter_array_len
)
908 GQuark field_quark
= g_quark_try_string(field_name
);
910 if (!field_quark
|| pretty
->options
.verbose
) {
914 for (i
= 0; i
< filter_array_len
; i
++) {
915 if (field_quark
== filter_fields
[i
]) {
923 enum bt_component_status
print_struct_field(struct pretty_component
*pretty
,
924 struct bt_ctf_field
*_struct
,
925 struct bt_ctf_field_type
*struct_type
,
926 int i
, bool print_names
, int *nr_printed_fields
,
927 GQuark
*filter_fields
, int filter_array_len
)
929 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
930 const char *field_name
;
931 struct bt_ctf_field
*field
= NULL
;
932 struct bt_ctf_field_type
*field_type
= NULL
;;
934 field
= bt_ctf_field_structure_get_field_by_index(_struct
, i
);
936 ret
= BT_COMPONENT_STATUS_ERROR
;
939 if (bt_ctf_field_type_structure_get_field(struct_type
,
940 &field_name
, &field_type
, i
) < 0) {
941 ret
= BT_COMPONENT_STATUS_ERROR
;
945 if (filter_fields
&& !filter_field_name(pretty
, field_name
,
946 filter_fields
, filter_array_len
)) {
947 ret
= BT_COMPONENT_STATUS_OK
;
951 if (*nr_printed_fields
> 0) {
952 g_string_append(pretty
->string
, ", ");
954 g_string_append(pretty
->string
, " ");
957 print_field_name_equal(pretty
, rem_(field_name
));
959 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
960 *nr_printed_fields
+= 1;
968 enum bt_component_status
print_struct(struct pretty_component
*pretty
,
969 struct bt_ctf_field
*_struct
, bool print_names
,
970 GQuark
*filter_fields
, int filter_array_len
)
972 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
973 struct bt_ctf_field_type
*struct_type
= NULL
;
974 int nr_fields
, i
, nr_printed_fields
;
976 struct_type
= bt_ctf_field_get_type(_struct
);
978 ret
= BT_COMPONENT_STATUS_ERROR
;
981 nr_fields
= bt_ctf_field_type_structure_get_field_count(struct_type
);
983 ret
= BT_COMPONENT_STATUS_ERROR
;
986 g_string_append(pretty
->string
, "{");
988 nr_printed_fields
= 0;
989 for (i
= 0; i
< nr_fields
; i
++) {
990 ret
= print_struct_field(pretty
, _struct
, struct_type
, i
,
991 print_names
, &nr_printed_fields
, filter_fields
,
993 if (ret
!= BT_COMPONENT_STATUS_OK
) {
998 g_string_append(pretty
->string
, " }");
1000 bt_put(struct_type
);
1005 enum bt_component_status
print_array_field(struct pretty_component
*pretty
,
1006 struct bt_ctf_field
*array
, uint64_t i
,
1007 bool is_string
, bool print_names
)
1009 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1010 struct bt_ctf_field
*field
= NULL
;
1014 g_string_append(pretty
->string
, ", ");
1016 g_string_append(pretty
->string
, " ");
1019 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
1022 field
= bt_ctf_field_array_get_field(array
, i
);
1024 ret
= BT_COMPONENT_STATUS_ERROR
;
1027 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
1034 enum bt_component_status
print_array(struct pretty_component
*pretty
,
1035 struct bt_ctf_field
*array
, bool print_names
)
1037 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1038 struct bt_ctf_field_type
*array_type
= NULL
, *field_type
= NULL
;
1039 enum bt_ctf_field_type_id type_id
;
1042 bool is_string
= false;
1044 array_type
= bt_ctf_field_get_type(array
);
1046 ret
= BT_COMPONENT_STATUS_ERROR
;
1049 field_type
= bt_ctf_field_type_array_get_element_type(array_type
);
1051 ret
= BT_COMPONENT_STATUS_ERROR
;
1054 len
= bt_ctf_field_type_array_get_length(array_type
);
1056 ret
= BT_COMPONENT_STATUS_ERROR
;
1059 type_id
= bt_ctf_field_type_get_type_id(field_type
);
1060 if (type_id
== BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1061 enum bt_ctf_string_encoding encoding
;
1063 encoding
= bt_ctf_field_type_integer_get_encoding(field_type
);
1064 if (encoding
== BT_CTF_STRING_ENCODING_UTF8
1065 || encoding
== BT_CTF_STRING_ENCODING_ASCII
) {
1066 int integer_len
, integer_alignment
;
1068 integer_len
= bt_ctf_field_type_integer_get_size(field_type
);
1069 if (integer_len
< 0) {
1070 return BT_COMPONENT_STATUS_ERROR
;
1072 integer_alignment
= bt_ctf_field_type_get_alignment(field_type
);
1073 if (integer_alignment
< 0) {
1074 return BT_COMPONENT_STATUS_ERROR
;
1076 if (integer_len
== CHAR_BIT
1077 && integer_alignment
== CHAR_BIT
) {
1084 g_string_assign(pretty
->tmp_string
, "");
1086 g_string_append(pretty
->string
, "[");
1090 for (i
= 0; i
< len
; i
++) {
1091 ret
= print_array_field(pretty
, array
, i
, is_string
, print_names
);
1092 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1099 if (pretty
->use_colors
) {
1100 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1102 print_escape_string(pretty
, pretty
->tmp_string
->str
);
1103 if (pretty
->use_colors
) {
1104 g_string_append(pretty
->string
, COLOR_RST
);
1107 g_string_append(pretty
->string
, " ]");
1116 enum bt_component_status
print_sequence_field(struct pretty_component
*pretty
,
1117 struct bt_ctf_field
*seq
, uint64_t i
,
1118 bool is_string
, bool print_names
)
1120 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1121 struct bt_ctf_field
*field
= NULL
;
1125 g_string_append(pretty
->string
, ", ");
1127 g_string_append(pretty
->string
, " ");
1130 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
1133 field
= bt_ctf_field_sequence_get_field(seq
, i
);
1135 ret
= BT_COMPONENT_STATUS_ERROR
;
1138 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
1145 enum bt_component_status
print_sequence(struct pretty_component
*pretty
,
1146 struct bt_ctf_field
*seq
, bool print_names
)
1148 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1149 struct bt_ctf_field_type
*seq_type
= NULL
, *field_type
= NULL
;
1150 struct bt_ctf_field
*length_field
= NULL
;
1151 enum bt_ctf_field_type_id type_id
;
1154 bool is_string
= false;
1156 seq_type
= bt_ctf_field_get_type(seq
);
1158 ret
= BT_COMPONENT_STATUS_ERROR
;
1161 length_field
= bt_ctf_field_sequence_get_length(seq
);
1162 if (!length_field
) {
1163 ret
= BT_COMPONENT_STATUS_ERROR
;
1166 if (bt_ctf_field_unsigned_integer_get_value(length_field
, &len
) < 0) {
1167 ret
= BT_COMPONENT_STATUS_ERROR
;
1170 field_type
= bt_ctf_field_type_sequence_get_element_type(seq_type
);
1172 ret
= BT_COMPONENT_STATUS_ERROR
;
1175 type_id
= bt_ctf_field_type_get_type_id(field_type
);
1176 if (type_id
== BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1177 enum bt_ctf_string_encoding encoding
;
1179 encoding
= bt_ctf_field_type_integer_get_encoding(field_type
);
1180 if (encoding
== BT_CTF_STRING_ENCODING_UTF8
1181 || encoding
== BT_CTF_STRING_ENCODING_ASCII
) {
1182 int integer_len
, integer_alignment
;
1184 integer_len
= bt_ctf_field_type_integer_get_size(field_type
);
1185 if (integer_len
< 0) {
1186 ret
= BT_COMPONENT_STATUS_ERROR
;
1189 integer_alignment
= bt_ctf_field_type_get_alignment(field_type
);
1190 if (integer_alignment
< 0) {
1191 ret
= BT_COMPONENT_STATUS_ERROR
;
1194 if (integer_len
== CHAR_BIT
1195 && integer_alignment
== CHAR_BIT
) {
1202 g_string_assign(pretty
->tmp_string
, "");
1204 g_string_append(pretty
->string
, "[");
1208 for (i
= 0; i
< len
; i
++) {
1209 ret
= print_sequence_field(pretty
, seq
, i
,
1210 is_string
, print_names
);
1211 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1218 if (pretty
->use_colors
) {
1219 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1221 print_escape_string(pretty
, pretty
->tmp_string
->str
);
1222 if (pretty
->use_colors
) {
1223 g_string_append(pretty
->string
, COLOR_RST
);
1226 g_string_append(pretty
->string
, " ]");
1229 bt_put(length_field
);
1236 enum bt_component_status
print_variant(struct pretty_component
*pretty
,
1237 struct bt_ctf_field
*variant
, bool print_names
)
1239 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1240 struct bt_ctf_field
*field
= NULL
;
1242 field
= bt_ctf_field_variant_get_current_field(variant
);
1244 ret
= BT_COMPONENT_STATUS_ERROR
;
1247 g_string_append(pretty
->string
, "{ ");
1251 struct bt_ctf_field
*tag_field
= NULL
;
1252 const char *tag_choice
;
1253 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1255 tag_field
= bt_ctf_field_variant_get_tag(variant
);
1257 ret
= BT_COMPONENT_STATUS_ERROR
;
1261 iter
= bt_ctf_field_enumeration_get_mappings(tag_field
);
1264 ret
= BT_COMPONENT_STATUS_ERROR
;
1269 bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1270 iter
, &tag_choice
, NULL
, NULL
);
1274 ret
= BT_COMPONENT_STATUS_ERROR
;
1277 print_field_name_equal(pretty
, rem_(tag_choice
));
1281 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
1282 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1286 g_string_append(pretty
->string
, " }");
1293 enum bt_component_status
print_field(struct pretty_component
*pretty
,
1294 struct bt_ctf_field
*field
, bool print_names
,
1295 GQuark
*filter_fields
, int filter_array_len
)
1297 enum bt_ctf_field_type_id type_id
;
1299 type_id
= bt_ctf_field_get_type_id(field
);
1301 case CTF_TYPE_INTEGER
:
1302 return print_integer(pretty
, field
);
1303 case CTF_TYPE_FLOAT
:
1307 if (bt_ctf_field_floating_point_get_value(field
, &v
)) {
1308 return BT_COMPONENT_STATUS_ERROR
;
1310 if (pretty
->use_colors
) {
1311 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
1313 g_string_append_printf(pretty
->string
, "%g", v
);
1314 if (pretty
->use_colors
) {
1315 g_string_append(pretty
->string
, COLOR_RST
);
1317 return BT_COMPONENT_STATUS_OK
;
1320 return print_enum(pretty
, field
);
1321 case CTF_TYPE_STRING
:
1322 if (pretty
->use_colors
) {
1323 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1325 print_escape_string(pretty
, bt_ctf_field_string_get_value(field
));
1326 if (pretty
->use_colors
) {
1327 g_string_append(pretty
->string
, COLOR_RST
);
1329 return BT_COMPONENT_STATUS_OK
;
1330 case CTF_TYPE_STRUCT
:
1331 return print_struct(pretty
, field
, print_names
, filter_fields
,
1333 case CTF_TYPE_VARIANT
:
1334 return print_variant(pretty
, field
, print_names
);
1335 case CTF_TYPE_ARRAY
:
1336 return print_array(pretty
, field
, print_names
);
1337 case CTF_TYPE_SEQUENCE
:
1338 return print_sequence(pretty
, field
, print_names
);
1340 // TODO: log instead
1341 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) type_id
);
1342 return BT_COMPONENT_STATUS_ERROR
;
1347 enum bt_component_status
print_stream_packet_context(struct pretty_component
*pretty
,
1348 struct bt_ctf_event
*event
)
1350 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1351 struct bt_ctf_packet
*packet
= NULL
;
1352 struct bt_ctf_field
*main_field
= NULL
;
1354 packet
= bt_ctf_event_get_packet(event
);
1356 ret
= BT_COMPONENT_STATUS_ERROR
;
1359 main_field
= bt_ctf_packet_get_context(packet
);
1363 if (!pretty
->start_line
) {
1364 g_string_append(pretty
->string
, ", ");
1366 pretty
->start_line
= false;
1367 if (pretty
->options
.print_scope_field_names
) {
1368 print_name_equal(pretty
, "stream.packet.context");
1370 ret
= print_field(pretty
, main_field
,
1371 pretty
->options
.print_context_field_names
,
1372 stream_packet_context_quarks
,
1373 STREAM_PACKET_CONTEXT_QUARKS_LEN
);
1381 enum bt_component_status
print_event_header_raw(struct pretty_component
*pretty
,
1382 struct bt_ctf_event
*event
)
1384 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1385 struct bt_ctf_field
*main_field
= NULL
;
1387 main_field
= bt_ctf_event_get_header(event
);
1391 if (!pretty
->start_line
) {
1392 g_string_append(pretty
->string
, ", ");
1394 pretty
->start_line
= false;
1395 if (pretty
->options
.print_scope_field_names
) {
1396 print_name_equal(pretty
, "stream.event.header");
1398 ret
= print_field(pretty
, main_field
,
1399 pretty
->options
.print_header_field_names
, NULL
, 0);
1406 enum bt_component_status
print_stream_event_context(struct pretty_component
*pretty
,
1407 struct bt_ctf_event
*event
)
1409 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1410 struct bt_ctf_field
*main_field
= NULL
;
1412 main_field
= bt_ctf_event_get_stream_event_context(event
);
1416 if (!pretty
->start_line
) {
1417 g_string_append(pretty
->string
, ", ");
1419 pretty
->start_line
= false;
1420 if (pretty
->options
.print_scope_field_names
) {
1421 print_name_equal(pretty
, "stream.event.context");
1423 ret
= print_field(pretty
, main_field
,
1424 pretty
->options
.print_context_field_names
, NULL
, 0);
1431 enum bt_component_status
print_event_context(struct pretty_component
*pretty
,
1432 struct bt_ctf_event
*event
)
1434 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1435 struct bt_ctf_field
*main_field
= NULL
;
1437 main_field
= bt_ctf_event_get_event_context(event
);
1441 if (!pretty
->start_line
) {
1442 g_string_append(pretty
->string
, ", ");
1444 pretty
->start_line
= false;
1445 if (pretty
->options
.print_scope_field_names
) {
1446 print_name_equal(pretty
, "event.context");
1448 ret
= print_field(pretty
, main_field
,
1449 pretty
->options
.print_context_field_names
, NULL
, 0);
1456 enum bt_component_status
print_event_payload(struct pretty_component
*pretty
,
1457 struct bt_ctf_event
*event
)
1459 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1460 struct bt_ctf_field
*main_field
= NULL
;
1462 main_field
= bt_ctf_event_get_event_payload(event
);
1466 if (!pretty
->start_line
) {
1467 g_string_append(pretty
->string
, ", ");
1469 pretty
->start_line
= false;
1470 if (pretty
->options
.print_scope_field_names
) {
1471 print_name_equal(pretty
, "event.fields");
1473 ret
= print_field(pretty
, main_field
,
1474 pretty
->options
.print_payload_field_names
, NULL
, 0);
1481 enum bt_component_status
pretty_print_event(struct pretty_component
*pretty
,
1482 struct bt_notification
*event_notif
)
1484 enum bt_component_status ret
;
1485 struct bt_ctf_event
*event
=
1486 bt_notification_event_get_event(event_notif
);
1487 struct bt_clock_class_priority_map
*cc_prio_map
=
1488 bt_notification_event_get_clock_class_priority_map(event_notif
);
1491 assert(cc_prio_map
);
1492 pretty
->start_line
= true;
1493 g_string_assign(pretty
->string
, "");
1494 ret
= print_event_header(pretty
, event
, cc_prio_map
);
1495 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1499 ret
= print_stream_packet_context(pretty
, event
);
1500 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1504 if (pretty
->options
.verbose
) {
1505 ret
= print_event_header_raw(pretty
, event
);
1506 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1511 ret
= print_stream_event_context(pretty
, event
);
1512 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1516 ret
= print_event_context(pretty
, event
);
1517 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1521 ret
= print_event_payload(pretty
, event
);
1522 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1526 g_string_append_c(pretty
->string
, '\n');
1527 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, pretty
->out
) != 1) {
1528 ret
= BT_COMPONENT_STATUS_ERROR
;
1534 bt_put(cc_prio_map
);