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/bitfield.h>
40 #include <babeltrace/common-internal.h>
44 #define NSEC_PER_SEC 1000000000LL
46 #define COLOR_NAME BT_COMMON_COLOR_BOLD
47 #define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN
48 #define COLOR_RST BT_COMMON_COLOR_RESET
49 #define COLOR_STRING_VALUE BT_COMMON_COLOR_BOLD
50 #define COLOR_NUMBER_VALUE BT_COMMON_COLOR_BOLD
51 #define COLOR_ENUM_MAPPING_NAME BT_COMMON_COLOR_BOLD
52 #define COLOR_UNKNOWN BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED
53 #define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA
54 #define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW
57 const char *rem_(const char *str
)
66 int64_t real_timestamp
; /* Relative to UNIX epoch. */
67 uint64_t clock_value
; /* In cycles. */
71 enum bt_component_status
print_field(struct text_component
*text
,
72 struct bt_ctf_field
*field
, bool print_names
,
73 GQuark
*filters_fields
, int filter_array_len
);
76 void print_name_equal(struct text_component
*text
, const char *name
)
78 if (text
->use_colors
) {
79 fprintf(text
->out
, "%s%s%s = ", COLOR_NAME
, name
, COLOR_RST
);
81 fputs(name
, text
->out
);
86 void print_field_name_equal(struct text_component
*text
, const char *name
)
88 if (text
->use_colors
) {
89 fprintf(text
->out
, "%s%s%s = ", COLOR_FIELD_NAME
, name
,
92 fputs(name
, text
->out
);
97 void print_timestamp_cycles(struct text_component
*text
,
98 struct bt_ctf_clock_class
*clock_class
,
99 struct bt_ctf_event
*event
)
102 struct bt_ctf_clock_value
*clock_value
;
105 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
107 fputs("????????????????????", text
->out
);
111 ret
= bt_ctf_clock_value_get_value(clock_value
, &cycles
);
114 fprintf(text
->out
, "Error");
117 fprintf(text
->out
, "%020" PRIu64
, cycles
);
119 if (text
->last_cycles_timestamp
!= -1ULL) {
120 text
->delta_cycles
= cycles
- text
->last_cycles_timestamp
;
122 text
->last_cycles_timestamp
= cycles
;
126 void print_timestamp_wall(struct text_component
*text
,
127 struct bt_ctf_clock_class
*clock_class
,
128 struct bt_ctf_event
*event
)
131 struct bt_ctf_clock_value
*clock_value
;
132 int64_t ts_nsec
= 0; /* add configurable offset */
133 int64_t ts_sec
= 0; /* add configurable offset */
134 uint64_t ts_sec_abs
, ts_nsec_abs
;
137 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
139 fputs("??:??:??.?????????", text
->out
);
143 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
, &ts_nsec
);
146 fprintf(text
->out
, "Error");
150 if (text
->last_real_timestamp
!= -1ULL) {
151 text
->delta_real_timestamp
= ts_nsec
- text
->last_real_timestamp
;
153 text
->last_real_timestamp
= ts_nsec
;
155 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
156 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
157 if (ts_sec
>= 0 && ts_nsec
>= 0) {
160 ts_nsec_abs
= ts_nsec
;
161 } else if (ts_sec
> 0 && ts_nsec
< 0) {
163 ts_sec_abs
= ts_sec
- 1;
164 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
165 } else if (ts_sec
== 0 && ts_nsec
< 0) {
168 ts_nsec_abs
= -ts_nsec
;
169 } else if (ts_sec
< 0 && ts_nsec
> 0) {
171 ts_sec_abs
= -(ts_sec
+ 1);
172 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
173 } else if (ts_sec
< 0 && ts_nsec
== 0) {
175 ts_sec_abs
= -ts_sec
;
176 ts_nsec_abs
= ts_nsec
;
177 } else { /* (ts_sec < 0 && ts_nsec < 0) */
179 ts_sec_abs
= -ts_sec
;
180 ts_nsec_abs
= -ts_nsec
;
183 if (!text
->options
.clock_seconds
) {
185 time_t time_s
= (time_t) ts_sec_abs
;
188 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
192 if (!text
->options
.clock_gmt
) {
195 res
= localtime_r(&time_s
, &tm
);
197 fprintf(stderr
, "[warning] Unable to get localtime.\n");
203 res
= gmtime_r(&time_s
, &tm
);
205 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
209 if (text
->options
.clock_date
) {
213 /* Print date and time */
214 res
= strftime(timestr
, sizeof(timestr
),
217 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
220 fprintf(text
->out
, "%s", timestr
);
222 /* Print time in HH:MM:SS.ns */
223 fprintf(text
->out
, "%02d:%02d:%02d.%09" PRIu64
,
224 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec_abs
);
228 fprintf(text
->out
, "%s%" PRId64
".%09" PRIu64
,
229 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
235 enum bt_component_status
print_event_timestamp(struct text_component
*text
,
236 struct bt_ctf_event
*event
, bool *start_line
)
238 bool print_names
= text
->options
.print_header_field_names
;
239 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
240 struct bt_ctf_stream
*stream
= NULL
;
241 struct bt_ctf_stream_class
*stream_class
= NULL
;
242 struct bt_ctf_trace
*trace
= NULL
;
243 struct bt_ctf_clock_class
*clock_class
= NULL
;
244 FILE *out
= text
->out
;
246 stream
= bt_ctf_event_get_stream(event
);
248 ret
= BT_COMPONENT_STATUS_ERROR
;
252 stream_class
= bt_ctf_stream_get_class(stream
);
254 ret
= BT_COMPONENT_STATUS_ERROR
;
257 trace
= bt_ctf_stream_class_get_trace(stream_class
);
259 ret
= BT_COMPONENT_STATUS_ERROR
;
262 clock_class
= bt_ctf_trace_get_clock_class(trace
, 0);
264 ret
= BT_COMPONENT_STATUS_ERROR
;
269 print_name_equal(text
, "timestamp");
273 if (text
->use_colors
) {
274 fputs(COLOR_TIMESTAMP
, text
->out
);
276 if (text
->options
.print_timestamp_cycles
) {
277 print_timestamp_cycles(text
, clock_class
, event
);
279 print_timestamp_wall(text
, clock_class
, event
);
281 if (text
->use_colors
) {
282 fputs(COLOR_RST
, text
->out
);
288 if (text
->options
.print_delta_field
) {
290 fputs(", ", text
->out
);
291 print_name_equal(text
, "delta");
293 fputs("(", text
->out
);
295 if (text
->options
.print_timestamp_cycles
) {
296 if (text
->delta_cycles
== -1ULL) {
297 fputs("+??????????\?\?) ", text
->out
); /* Not a trigraph. */
299 fprintf(text
->out
, "+%012" PRIu64
, text
->delta_cycles
);
302 if (text
->delta_real_timestamp
!= -1ULL) {
303 uint64_t delta_sec
, delta_nsec
, delta
;
305 delta
= text
->delta_real_timestamp
;
306 delta_sec
= delta
/ NSEC_PER_SEC
;
307 delta_nsec
= delta
% NSEC_PER_SEC
;
308 fprintf(text
->out
, "+%" PRIu64
".%09" PRIu64
,
309 delta_sec
, delta_nsec
);
311 fputs("+?.?????????", text
->out
);
315 fputs(") ", text
->out
);
318 *start_line
= !print_names
;
323 bt_put(stream_class
);
329 enum bt_component_status
print_event_header(struct text_component
*text
,
330 struct bt_ctf_event
*event
)
332 bool print_names
= text
->options
.print_header_field_names
;
333 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
334 struct bt_ctf_event_class
*event_class
= NULL
;
335 struct bt_ctf_stream_class
*stream_class
= NULL
;
336 struct bt_ctf_trace
*trace_class
= NULL
;
338 event_class
= bt_ctf_event_get_class(event
);
340 ret
= BT_COMPONENT_STATUS_ERROR
;
343 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
345 ret
= BT_COMPONENT_STATUS_ERROR
;
348 trace_class
= bt_ctf_stream_class_get_trace(stream_class
);
350 ret
= BT_COMPONENT_STATUS_ERROR
;
353 if (!text
->start_line
) {
354 fputs(", ", text
->out
);
356 text
->start_line
= false;
357 ret
= print_event_timestamp(text
, event
, &text
->start_line
);
358 if (ret
!= BT_COMPONENT_STATUS_OK
) {
361 if (text
->options
.print_trace_field
) {
364 name
= bt_ctf_trace_get_name(trace_class
);
366 if (!text
->start_line
) {
367 fputs(", ", text
->out
);
369 text
->start_line
= false;
371 print_name_equal(text
, "trace");
373 fprintf(text
->out
, "%s", name
);
376 if (text
->options
.print_trace_hostname_field
) {
377 struct bt_value
*hostname_str
;
379 hostname_str
= bt_ctf_trace_get_environment_field_value_by_name(trace_class
,
384 if (!text
->start_line
) {
385 fputs(", ", text
->out
);
387 text
->start_line
= false;
389 print_name_equal(text
, "trace:hostname");
391 if (bt_value_string_get(hostname_str
, &str
)
392 == BT_VALUE_STATUS_OK
) {
393 fprintf(text
->out
, "%s", str
);
395 bt_put(hostname_str
);
398 if (text
->options
.print_trace_domain_field
) {
399 struct bt_value
*domain_str
;
401 domain_str
= bt_ctf_trace_get_environment_field_value_by_name(trace_class
,
406 if (!text
->start_line
) {
407 fputs(", ", text
->out
);
409 text
->start_line
= false;
411 print_name_equal(text
, "trace:domain");
413 if (bt_value_string_get(domain_str
, &str
)
414 == BT_VALUE_STATUS_OK
) {
415 fprintf(text
->out
, "%s", str
);
420 if (text
->options
.print_trace_procname_field
) {
421 struct bt_value
*procname_str
;
423 procname_str
= bt_ctf_trace_get_environment_field_value_by_name(trace_class
,
428 if (!text
->start_line
) {
429 fputs(", ", text
->out
);
431 text
->start_line
= false;
433 print_name_equal(text
, "trace:procname");
435 if (bt_value_string_get(procname_str
, &str
)
436 == BT_VALUE_STATUS_OK
) {
437 fprintf(text
->out
, "%s", str
);
439 bt_put(procname_str
);
442 if (text
->options
.print_trace_vpid_field
) {
443 struct bt_value
*vpid_value
;
445 vpid_value
= bt_ctf_trace_get_environment_field_value_by_name(trace_class
,
450 if (!text
->start_line
) {
451 fputs(", ", text
->out
);
453 text
->start_line
= false;
455 print_name_equal(text
, "trace:vpid");
457 if (bt_value_integer_get(vpid_value
, &value
)
458 == BT_VALUE_STATUS_OK
) {
459 fprintf(text
->out
, "(%" PRId64
")", value
);
464 if (text
->options
.print_loglevel_field
) {
465 struct bt_value
*loglevel_str
, *loglevel_value
;
467 loglevel_str
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
469 loglevel_value
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
471 if (loglevel_str
|| loglevel_value
) {
472 bool has_str
= false;
474 if (!text
->start_line
) {
475 fputs(", ", text
->out
);
477 text
->start_line
= false;
479 print_name_equal(text
, "loglevel");
484 if (bt_value_string_get(loglevel_str
, &str
)
485 == BT_VALUE_STATUS_OK
) {
486 fprintf(text
->out
, "%s", str
);
490 if (loglevel_value
) {
493 if (bt_value_integer_get(loglevel_value
, &value
)
494 == BT_VALUE_STATUS_OK
) {
495 fprintf(text
->out
, "%s(%" PRId64
")",
496 has_str
? " " : "", value
);
499 bt_put(loglevel_str
);
500 bt_put(loglevel_value
);
503 if (text
->options
.print_emf_field
) {
504 struct bt_value
*uri_str
;
506 uri_str
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
509 if (!text
->start_line
) {
510 fputs(", ", text
->out
);
512 text
->start_line
= false;
514 print_name_equal(text
, "model.emf.uri");
519 if (bt_value_string_get(uri_str
, &str
)
520 == BT_VALUE_STATUS_OK
) {
521 fprintf(text
->out
, "%s", str
);
527 if (!text
->start_line
) {
528 fputs(", ", text
->out
);
530 text
->start_line
= false;
532 print_name_equal(text
, "name");
534 if (text
->use_colors
) {
535 fputs(COLOR_EVENT_NAME
, text
->out
);
537 fputs(bt_ctf_event_class_get_name(event_class
), text
->out
);
538 if (text
->use_colors
) {
539 fputs(COLOR_RST
, text
->out
);
543 bt_put(stream_class
);
549 enum bt_component_status
print_integer(struct text_component
*text
,
550 struct bt_ctf_field
*field
)
552 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
553 struct bt_ctf_field_type
*field_type
= NULL
;
554 enum bt_ctf_integer_base base
;
555 enum bt_ctf_string_encoding encoding
;
561 bool rst_color
= false;
563 field_type
= bt_ctf_field_get_type(field
);
565 ret
= BT_COMPONENT_STATUS_ERROR
;
568 signedness
= bt_ctf_field_type_integer_get_signed(field_type
);
569 if (signedness
< 0) {
570 ret
= BT_COMPONENT_STATUS_ERROR
;
574 if (bt_ctf_field_unsigned_integer_get_value(field
, &v
.u
) < 0) {
575 ret
= BT_COMPONENT_STATUS_ERROR
;
579 if (bt_ctf_field_signed_integer_get_value(field
, &v
.s
) < 0) {
580 ret
= BT_COMPONENT_STATUS_ERROR
;
585 encoding
= bt_ctf_field_type_integer_get_encoding(field_type
);
587 case BT_CTF_STRING_ENCODING_UTF8
:
588 case BT_CTF_STRING_ENCODING_ASCII
:
589 g_string_append_c(text
->string
, (int) v
.u
);
591 case BT_CTF_STRING_ENCODING_NONE
:
592 case BT_CTF_STRING_ENCODING_UNKNOWN
:
595 ret
= BT_COMPONENT_STATUS_ERROR
;
599 if (text
->use_colors
) {
600 fputs(COLOR_NUMBER_VALUE
, text
->out
);
604 base
= bt_ctf_field_type_integer_get_base(field_type
);
606 case BT_CTF_INTEGER_BASE_BINARY
:
610 len
= bt_ctf_field_type_integer_get_size(field_type
);
612 ret
= BT_COMPONENT_STATUS_ERROR
;
615 fprintf(text
->out
, "0b");
616 v
.u
= _bt_piecewise_lshift(v
.u
, 64 - len
);
617 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
618 fprintf(text
->out
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
619 v
.u
= _bt_piecewise_lshift(v
.u
, 1);
623 case BT_CTF_INTEGER_BASE_OCTAL
:
628 len
= bt_ctf_field_type_integer_get_size(field_type
);
630 ret
= BT_COMPONENT_STATUS_ERROR
;
637 /* Round length to the nearest 3-bit */
638 rounded_len
= (((len
- 1) / 3) + 1) * 3;
639 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
643 fprintf(text
->out
, "0%" PRIo64
, v
.u
);
646 case BT_CTF_INTEGER_BASE_DECIMAL
:
648 fprintf(text
->out
, "%" PRIu64
, v
.u
);
650 fprintf(text
->out
, "%" PRId64
, v
.s
);
653 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
657 len
= bt_ctf_field_type_integer_get_size(field_type
);
659 ret
= BT_COMPONENT_STATUS_ERROR
;
663 /* Round length to the nearest nibble */
664 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
666 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
669 fprintf(text
->out
, "0x%" PRIX64
, v
.u
);
673 ret
= BT_COMPONENT_STATUS_ERROR
;
678 fputs(COLOR_RST
, text
->out
);
685 enum bt_component_status
print_enum(struct text_component
*text
,
686 struct bt_ctf_field
*field
)
688 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
689 struct bt_ctf_field
*container_field
= NULL
;
690 struct bt_ctf_field_type
*enumeration_field_type
= NULL
;
691 struct bt_ctf_field_type
*container_field_type
= NULL
;
692 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
696 enumeration_field_type
= bt_ctf_field_get_type(field
);
697 if (!enumeration_field_type
) {
698 ret
= BT_COMPONENT_STATUS_ERROR
;
701 container_field
= bt_ctf_field_enumeration_get_container(field
);
702 if (!container_field
) {
703 ret
= BT_COMPONENT_STATUS_ERROR
;
706 container_field_type
= bt_ctf_field_get_type(container_field
);
707 if (!container_field_type
) {
708 ret
= BT_COMPONENT_STATUS_ERROR
;
711 is_signed
= bt_ctf_field_type_integer_get_signed(container_field_type
);
713 ret
= BT_COMPONENT_STATUS_ERROR
;
719 if (bt_ctf_field_signed_integer_get_value(container_field
,
721 ret
= BT_COMPONENT_STATUS_ERROR
;
724 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
725 enumeration_field_type
, value
);
729 if (bt_ctf_field_unsigned_integer_get_value(container_field
,
731 ret
= BT_COMPONENT_STATUS_ERROR
;
734 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
735 enumeration_field_type
, value
);
738 ret
= BT_COMPONENT_STATUS_ERROR
;
741 fprintf(text
->out
, "( ");
743 const char *mapping_name
;
745 if (bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
746 iter
, &mapping_name
, NULL
, NULL
) < 0) {
747 ret
= BT_COMPONENT_STATUS_ERROR
;
751 fprintf(text
->out
, ", ");
752 if (text
->use_colors
) {
753 fputs(COLOR_ENUM_MAPPING_NAME
, text
->out
);
755 // TODO: escape string
756 fprintf(text
->out
, "\"%s\"", mapping_name
);
757 if (text
->use_colors
) {
758 fputs(COLOR_RST
, text
->out
);
760 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
) < 0) {
765 if (text
->use_colors
) {
766 fputs(COLOR_UNKNOWN
, text
->out
);
768 fprintf(text
->out
, "<unknown>");
769 if (text
->use_colors
) {
770 fputs(COLOR_RST
, text
->out
);
773 fprintf(text
->out
, " : container = ");
774 ret
= print_integer(text
, container_field
);
775 if (ret
!= BT_COMPONENT_STATUS_OK
) {
778 fprintf(text
->out
, " )");
781 bt_put(container_field_type
);
782 bt_put(container_field
);
783 bt_put(enumeration_field_type
);
788 int filter_field_name(struct text_component
*text
, const char *field_name
,
789 GQuark
*filter_fields
, int filter_array_len
)
792 GQuark field_quark
= g_quark_try_string(field_name
);
794 if (!field_quark
|| text
->options
.verbose
) {
798 for (i
= 0; i
< filter_array_len
; i
++) {
799 if (field_quark
== filter_fields
[i
]) {
807 enum bt_component_status
print_struct_field(struct text_component
*text
,
808 struct bt_ctf_field
*_struct
,
809 struct bt_ctf_field_type
*struct_type
,
810 int i
, bool print_names
, int *nr_printed_fields
,
811 GQuark
*filter_fields
, int filter_array_len
)
813 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
814 const char *field_name
;
815 struct bt_ctf_field
*field
= NULL
;
816 struct bt_ctf_field_type
*field_type
= NULL
;;
818 field
= bt_ctf_field_structure_get_field_by_index(_struct
, i
);
820 ret
= BT_COMPONENT_STATUS_ERROR
;
823 if (bt_ctf_field_type_structure_get_field(struct_type
,
824 &field_name
, &field_type
, i
) < 0) {
825 ret
= BT_COMPONENT_STATUS_ERROR
;
829 if (filter_fields
&& !filter_field_name(text
, field_name
,
830 filter_fields
, filter_array_len
)) {
831 ret
= BT_COMPONENT_STATUS_OK
;
835 if (*nr_printed_fields
> 0) {
836 fprintf(text
->out
, ", ");
838 fprintf(text
->out
, " ");
841 print_field_name_equal(text
, rem_(field_name
));
843 ret
= print_field(text
, field
, print_names
, NULL
, 0);
844 *nr_printed_fields
+= 1;
852 enum bt_component_status
print_struct(struct text_component
*text
,
853 struct bt_ctf_field
*_struct
, bool print_names
,
854 GQuark
*filter_fields
, int filter_array_len
)
856 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
857 struct bt_ctf_field_type
*struct_type
= NULL
;
858 int nr_fields
, i
, nr_printed_fields
;
860 struct_type
= bt_ctf_field_get_type(_struct
);
862 ret
= BT_COMPONENT_STATUS_ERROR
;
865 nr_fields
= bt_ctf_field_type_structure_get_field_count(struct_type
);
867 ret
= BT_COMPONENT_STATUS_ERROR
;
870 fprintf(text
->out
, "{");
872 nr_printed_fields
= 0;
873 for (i
= 0; i
< nr_fields
; i
++) {
874 ret
= print_struct_field(text
, _struct
, struct_type
, i
,
875 print_names
, &nr_printed_fields
, filter_fields
,
877 if (ret
!= BT_COMPONENT_STATUS_OK
) {
882 fprintf(text
->out
, " }");
889 enum bt_component_status
print_array_field(struct text_component
*text
,
890 struct bt_ctf_field
*array
, uint64_t i
,
891 bool is_string
, bool print_names
)
893 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
894 struct bt_ctf_field
*field
= NULL
;
898 fprintf(text
->out
, ", ");
900 fprintf(text
->out
, " ");
903 field
= bt_ctf_field_array_get_field(array
, i
);
905 ret
= BT_COMPONENT_STATUS_ERROR
;
908 ret
= print_field(text
, field
, print_names
, NULL
, 0);
915 enum bt_component_status
print_array(struct text_component
*text
,
916 struct bt_ctf_field
*array
, bool print_names
)
918 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
919 struct bt_ctf_field_type
*array_type
= NULL
, *field_type
= NULL
;
920 enum bt_ctf_type_id type_id
;
923 bool is_string
= false;
925 array_type
= bt_ctf_field_get_type(array
);
927 ret
= BT_COMPONENT_STATUS_ERROR
;
930 field_type
= bt_ctf_field_type_array_get_element_type(array_type
);
932 ret
= BT_COMPONENT_STATUS_ERROR
;
935 len
= bt_ctf_field_type_array_get_length(array_type
);
937 ret
= BT_COMPONENT_STATUS_ERROR
;
940 type_id
= bt_ctf_field_type_get_type_id(field_type
);
941 if (type_id
== BT_CTF_TYPE_ID_INTEGER
) {
942 enum bt_ctf_string_encoding encoding
;
944 encoding
= bt_ctf_field_type_integer_get_encoding(field_type
);
945 if (encoding
== BT_CTF_STRING_ENCODING_UTF8
946 || encoding
== BT_CTF_STRING_ENCODING_ASCII
) {
947 int integer_len
, integer_alignment
;
949 integer_len
= bt_ctf_field_type_integer_get_size(field_type
);
950 if (integer_len
< 0) {
951 return BT_COMPONENT_STATUS_ERROR
;
953 integer_alignment
= bt_ctf_field_type_get_alignment(field_type
);
954 if (integer_alignment
< 0) {
955 return BT_COMPONENT_STATUS_ERROR
;
957 if (integer_len
== CHAR_BIT
958 && integer_alignment
== CHAR_BIT
) {
965 g_string_assign(text
->string
, "");
967 fprintf(text
->out
, "[");
971 for (i
= 0; i
< len
; i
++) {
972 ret
= print_array_field(text
, array
, i
, is_string
, print_names
);
973 if (ret
!= BT_COMPONENT_STATUS_OK
) {
980 if (text
->use_colors
) {
981 fputs(COLOR_STRING_VALUE
, text
->out
);
983 // TODO: escape string
984 fprintf(text
->out
, "\"%s\"", text
->string
->str
);
985 if (text
->use_colors
) {
986 fputs(COLOR_RST
, text
->out
);
989 fprintf(text
->out
, " ]");
998 enum bt_component_status
print_sequence_field(struct text_component
*text
,
999 struct bt_ctf_field
*seq
, uint64_t i
,
1000 bool is_string
, bool print_names
)
1002 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1003 struct bt_ctf_field
*field
= NULL
;
1007 fprintf(text
->out
, ", ");
1009 fprintf(text
->out
, " ");
1012 field
= bt_ctf_field_sequence_get_field(seq
, i
);
1014 ret
= BT_COMPONENT_STATUS_ERROR
;
1017 ret
= print_field(text
, field
, print_names
, NULL
, 0);
1024 enum bt_component_status
print_sequence(struct text_component
*text
,
1025 struct bt_ctf_field
*seq
, bool print_names
)
1027 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1028 struct bt_ctf_field_type
*seq_type
= NULL
, *field_type
= NULL
;
1029 struct bt_ctf_field
*length_field
= NULL
;
1030 enum bt_ctf_type_id type_id
;
1033 bool is_string
= false;
1035 seq_type
= bt_ctf_field_get_type(seq
);
1037 ret
= BT_COMPONENT_STATUS_ERROR
;
1040 length_field
= bt_ctf_field_sequence_get_length(seq
);
1041 if (!length_field
) {
1042 ret
= BT_COMPONENT_STATUS_ERROR
;
1045 if (bt_ctf_field_unsigned_integer_get_value(length_field
, &len
) < 0) {
1046 ret
= BT_COMPONENT_STATUS_ERROR
;
1049 field_type
= bt_ctf_field_type_sequence_get_element_type(seq_type
);
1051 ret
= BT_COMPONENT_STATUS_ERROR
;
1054 type_id
= bt_ctf_field_type_get_type_id(field_type
);
1055 if (type_id
== BT_CTF_TYPE_ID_INTEGER
) {
1056 enum bt_ctf_string_encoding encoding
;
1058 encoding
= bt_ctf_field_type_integer_get_encoding(field_type
);
1059 if (encoding
== BT_CTF_STRING_ENCODING_UTF8
1060 || encoding
== BT_CTF_STRING_ENCODING_ASCII
) {
1061 int integer_len
, integer_alignment
;
1063 integer_len
= bt_ctf_field_type_integer_get_size(field_type
);
1064 if (integer_len
< 0) {
1065 ret
= BT_COMPONENT_STATUS_ERROR
;
1068 integer_alignment
= bt_ctf_field_type_get_alignment(field_type
);
1069 if (integer_alignment
< 0) {
1070 ret
= BT_COMPONENT_STATUS_ERROR
;
1073 if (integer_len
== CHAR_BIT
1074 && integer_alignment
== CHAR_BIT
) {
1081 g_string_assign(text
->string
, "");
1083 fprintf(text
->out
, "[");
1087 for (i
= 0; i
< len
; i
++) {
1088 ret
= print_sequence_field(text
, seq
, i
,
1089 is_string
, print_names
);
1090 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1097 if (text
->use_colors
) {
1098 fputs(COLOR_STRING_VALUE
, text
->out
);
1100 // TODO: escape string
1101 fprintf(text
->out
, "\"%s\"", text
->string
->str
);
1102 if (text
->use_colors
) {
1103 fputs(COLOR_RST
, text
->out
);
1106 fprintf(text
->out
, " ]");
1109 bt_put(length_field
);
1116 enum bt_component_status
print_variant(struct text_component
*text
,
1117 struct bt_ctf_field
*variant
, bool print_names
)
1119 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1120 struct bt_ctf_field
*field
= NULL
;
1122 field
= bt_ctf_field_variant_get_current_field(variant
);
1124 ret
= BT_COMPONENT_STATUS_ERROR
;
1127 fprintf(text
->out
, "{ ");
1131 struct bt_ctf_field
*tag_field
= NULL
;
1132 const char *tag_choice
;
1133 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1135 tag_field
= bt_ctf_field_variant_get_tag(variant
);
1137 ret
= BT_COMPONENT_STATUS_ERROR
;
1141 iter
= bt_ctf_field_enumeration_get_mappings(tag_field
);
1144 ret
= BT_COMPONENT_STATUS_ERROR
;
1149 bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1150 iter
, &tag_choice
, NULL
, NULL
);
1154 ret
= BT_COMPONENT_STATUS_ERROR
;
1157 print_field_name_equal(text
, rem_(tag_choice
));
1161 ret
= print_field(text
, field
, print_names
, NULL
, 0);
1162 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1166 fprintf(text
->out
, " }");
1173 enum bt_component_status
print_field(struct text_component
*text
,
1174 struct bt_ctf_field
*field
, bool print_names
,
1175 GQuark
*filter_fields
, int filter_array_len
)
1177 enum bt_ctf_type_id type_id
;
1179 type_id
= bt_ctf_field_get_type_id(field
);
1181 case CTF_TYPE_INTEGER
:
1182 return print_integer(text
, field
);
1183 case CTF_TYPE_FLOAT
:
1187 if (bt_ctf_field_floating_point_get_value(field
, &v
)) {
1188 return BT_COMPONENT_STATUS_ERROR
;
1190 if (text
->use_colors
) {
1191 fputs(COLOR_NUMBER_VALUE
, text
->out
);
1193 fprintf(text
->out
, "%g", v
);
1194 if (text
->use_colors
) {
1195 fputs(COLOR_RST
, text
->out
);
1197 return BT_COMPONENT_STATUS_OK
;
1200 return print_enum(text
, field
);
1201 case CTF_TYPE_STRING
:
1202 if (text
->use_colors
) {
1203 fputs(COLOR_STRING_VALUE
, text
->out
);
1205 // TODO: escape the string value
1206 fprintf(text
->out
, "\"%s\"",
1207 bt_ctf_field_string_get_value(field
));
1208 if (text
->use_colors
) {
1209 fputs(COLOR_RST
, text
->out
);
1211 return BT_COMPONENT_STATUS_OK
;
1212 case CTF_TYPE_STRUCT
:
1213 return print_struct(text
, field
, print_names
, filter_fields
,
1215 case CTF_TYPE_UNTAGGED_VARIANT
:
1216 case CTF_TYPE_VARIANT
:
1217 return print_variant(text
, field
, print_names
);
1218 case CTF_TYPE_ARRAY
:
1219 return print_array(text
, field
, print_names
);
1220 case CTF_TYPE_SEQUENCE
:
1221 return print_sequence(text
, field
, print_names
);
1223 fprintf(text
->err
, "[error] Unknown type id: %d\n", (int) type_id
);
1224 return BT_COMPONENT_STATUS_ERROR
;
1229 enum bt_component_status
print_stream_packet_context(struct text_component
*text
,
1230 struct bt_ctf_event
*event
)
1232 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1233 struct bt_ctf_packet
*packet
= NULL
;
1234 struct bt_ctf_field
*main_field
= NULL
;
1236 packet
= bt_ctf_event_get_packet(event
);
1238 ret
= BT_COMPONENT_STATUS_ERROR
;
1241 main_field
= bt_ctf_packet_get_context(packet
);
1245 if (!text
->start_line
) {
1246 fputs(", ", text
->out
);
1248 text
->start_line
= false;
1249 if (text
->options
.print_scope_field_names
) {
1250 print_name_equal(text
, "stream.packet.context");
1252 ret
= print_field(text
, main_field
,
1253 text
->options
.print_context_field_names
,
1254 stream_packet_context_quarks
,
1255 STREAM_PACKET_CONTEXT_QUARKS_LEN
);
1263 enum bt_component_status
print_event_header_raw(struct text_component
*text
,
1264 struct bt_ctf_event
*event
)
1266 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1267 struct bt_ctf_field
*main_field
= NULL
;
1269 main_field
= bt_ctf_event_get_header(event
);
1273 if (!text
->start_line
) {
1274 fputs(", ", text
->out
);
1276 text
->start_line
= false;
1277 if (text
->options
.print_scope_field_names
) {
1278 print_name_equal(text
, "stream.event.header");
1280 ret
= print_field(text
, main_field
,
1281 text
->options
.print_header_field_names
, NULL
, 0);
1288 enum bt_component_status
print_stream_event_context(struct text_component
*text
,
1289 struct bt_ctf_event
*event
)
1291 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1292 struct bt_ctf_field
*main_field
= NULL
;
1294 main_field
= bt_ctf_event_get_stream_event_context(event
);
1298 if (!text
->start_line
) {
1299 fputs(", ", text
->out
);
1301 text
->start_line
= false;
1302 if (text
->options
.print_scope_field_names
) {
1303 print_name_equal(text
, "stream.event.context");
1305 ret
= print_field(text
, main_field
,
1306 text
->options
.print_context_field_names
, NULL
, 0);
1313 enum bt_component_status
print_event_context(struct text_component
*text
,
1314 struct bt_ctf_event
*event
)
1316 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1317 struct bt_ctf_field
*main_field
= NULL
;
1319 main_field
= bt_ctf_event_get_event_context(event
);
1323 if (!text
->start_line
) {
1324 fputs(", ", text
->out
);
1326 text
->start_line
= false;
1327 if (text
->options
.print_scope_field_names
) {
1328 print_name_equal(text
, "event.context");
1330 ret
= print_field(text
, main_field
,
1331 text
->options
.print_context_field_names
, NULL
, 0);
1338 enum bt_component_status
print_event_payload(struct text_component
*text
,
1339 struct bt_ctf_event
*event
)
1341 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1342 struct bt_ctf_field
*main_field
= NULL
;
1344 main_field
= bt_ctf_event_get_payload_field(event
);
1348 if (!text
->start_line
) {
1349 fputs(", ", text
->out
);
1351 text
->start_line
= false;
1352 if (text
->options
.print_scope_field_names
) {
1353 print_name_equal(text
, "event.fields");
1355 ret
= print_field(text
, main_field
,
1356 text
->options
.print_payload_field_names
, NULL
, 0);
1363 enum bt_component_status
text_print_event(struct text_component
*text
,
1364 struct bt_ctf_event
*event
)
1366 enum bt_component_status ret
;
1368 text
->start_line
= true;
1369 ret
= print_event_header(text
, event
);
1370 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1374 ret
= print_stream_packet_context(text
, event
);
1375 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1379 ret
= print_event_header_raw(text
, event
);
1380 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1384 ret
= print_stream_event_context(text
, event
);
1385 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1389 ret
= print_event_context(text
, event
);
1390 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1394 ret
= print_event_payload(text
, event
);
1395 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1399 fputc('\n', text
->out
);