4 * Babeltrace CTF Text Output Plugin Event Printing
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-ir/event.h>
30 #include <babeltrace/ctf-ir/event-class.h>
31 #include <babeltrace/ctf-ir/packet.h>
32 #include <babeltrace/ctf-ir/stream.h>
33 #include <babeltrace/ctf-ir/stream-class.h>
34 #include <babeltrace/ctf-ir/clock.h>
35 #include <babeltrace/ctf-ir/field-types.h>
36 #include <babeltrace/ctf-ir/fields.h>
37 #include <babeltrace/bitfield.h>
42 const char *rem_(const char *str
)
51 int64_t real_timestamp
; /* Relative to UNIX epoch. */
52 uint64_t clock_value
; /* In cycles. */
56 enum bt_component_status
print_field(struct text_component
*text
,
57 struct bt_ctf_field
*field
, bool print_names
);
60 void print_timestamp_cycles(struct text_component
*text
,
61 struct bt_ctf_clock
*clock
,
62 struct bt_ctf_event
*event
)
64 fputs("00000000000000000000", text
->out
);
68 void print_timestamp_wall(struct text_component
*text
,
69 struct bt_ctf_clock
*clock
,
70 struct bt_ctf_event
*event
)
72 fputs("??:??:??.?????????", text
->out
);
76 enum bt_component_status
get_event_timestamp(struct bt_ctf_event
*event
)
79 uint64_t value, frequency;
80 int64_t offset_s, offset;
82 return BT_COMPONENT_STATUS_OK
;
86 enum bt_component_status
print_event_timestamp(struct text_component
*text
,
87 struct bt_ctf_event
*event
)
89 bool print_names
= text
->options
.print_header_field_names
;
90 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
91 struct bt_ctf_stream
*stream
= NULL
;
92 struct bt_ctf_clock
*clock
= NULL
;
93 FILE *out
= text
->out
;
94 FILE *err
= text
->err
;
95 uint64_t real_timestamp
;
97 stream
= bt_ctf_event_get_stream(event
);
99 ret
= BT_COMPONENT_STATUS_ERROR
;
103 clock
= bt_ctf_event_get_clock(event
);
105 /* Stream has no timestamp. */
106 //puts("no_timestamp!");
110 fputs(print_names
? "timestamp = " : "[", out
);
111 if (text
->options
.print_timestamp_cycles
) {
112 print_timestamp_cycles(text
, clock
, event
);
114 print_timestamp_wall(text
, clock
, event
);
117 fputs(print_names
? ", " : "] ", out
);
118 if (!text
->options
.print_delta_field
) {
128 enum bt_component_status
print_event_header(struct text_component
*text
,
129 struct bt_ctf_event
*event
)
131 bool print_names
= text
->options
.print_header_field_names
;
132 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
133 struct bt_ctf_event_class
*event_class
= NULL
;
135 event_class
= bt_ctf_event_get_class(event
);
137 ret
= BT_COMPONENT_STATUS_ERROR
;
140 ret
= print_event_timestamp(text
, event
);
141 if (ret
!= BT_COMPONENT_STATUS_OK
) {
145 fputs("name = ", text
->out
);
147 fputs(bt_ctf_event_class_get_name(event_class
), text
->out
);
155 enum bt_component_status
print_integer(struct text_component
*text
,
156 struct bt_ctf_field
*field
)
158 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
159 struct bt_ctf_field_type
*field_type
= NULL
;
160 enum bt_ctf_integer_base base
;
161 enum bt_ctf_string_encoding encoding
;
168 field_type
= bt_ctf_field_get_type(field
);
170 ret
= BT_COMPONENT_STATUS_ERROR
;
173 signedness
= bt_ctf_field_type_integer_get_signed(field_type
);
174 if (signedness
< 0) {
175 ret
= BT_COMPONENT_STATUS_ERROR
;
179 if (bt_ctf_field_unsigned_integer_get_value(field
, &v
.u
) < 0) {
180 ret
= BT_COMPONENT_STATUS_ERROR
;
184 if (bt_ctf_field_signed_integer_get_value(field
, &v
.s
) < 0) {
185 ret
= BT_COMPONENT_STATUS_ERROR
;
190 encoding
= bt_ctf_field_type_integer_get_encoding(field_type
);
192 case BT_CTF_STRING_ENCODING_UTF8
:
193 case BT_CTF_STRING_ENCODING_ASCII
:
194 g_string_append_c(text
->string
, (int) v
.u
);
196 case BT_CTF_STRING_ENCODING_NONE
:
197 case BT_CTF_STRING_ENCODING_UNKNOWN
:
200 ret
= BT_COMPONENT_STATUS_ERROR
;
204 base
= bt_ctf_field_type_integer_get_base(field_type
);
206 case BT_CTF_INTEGER_BASE_BINARY
:
210 len
= bt_ctf_field_type_integer_get_size(field_type
);
212 ret
= BT_COMPONENT_STATUS_ERROR
;
215 fprintf(text
->out
, "0b");
216 v
.u
= _bt_piecewise_lshift(v
.u
, 64 - len
);
217 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
218 fprintf(text
->out
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
219 v
.u
= _bt_piecewise_lshift(v
.u
, 1);
223 case BT_CTF_INTEGER_BASE_OCTAL
:
228 len
= bt_ctf_field_type_integer_get_size(field_type
);
230 ret
= BT_COMPONENT_STATUS_ERROR
;
237 /* Round length to the nearest 3-bit */
238 rounded_len
= (((len
- 1) / 3) + 1) * 3;
239 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
243 fprintf(text
->out
, "0%" PRIo64
, v
.u
);
246 case BT_CTF_INTEGER_BASE_DECIMAL
:
248 fprintf(text
->out
, "%" PRIu64
, v
.u
);
250 fprintf(text
->out
, "%" PRId64
, v
.s
);
253 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
257 len
= bt_ctf_field_type_integer_get_size(field_type
);
259 ret
= BT_COMPONENT_STATUS_ERROR
;
263 /* Round length to the nearest nibble */
264 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
266 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
269 fprintf(text
->out
, "0x%" PRIX64
, v
.u
);
273 ret
= BT_COMPONENT_STATUS_ERROR
;
282 enum bt_component_status
print_enum(struct text_component
*text
,
283 struct bt_ctf_field
*field
)
285 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
286 struct bt_ctf_field
*container_field
= NULL
;
287 const char *mapping_name
;
289 container_field
= bt_ctf_field_enumeration_get_container(field
);
290 if (!container_field
) {
291 ret
= BT_COMPONENT_STATUS_ERROR
;
294 mapping_name
= bt_ctf_field_enumeration_get_mapping_name(field
);
296 fprintf(text
->out
, "( \"%s\"", mapping_name
);
298 fprintf(text
->out
, "( <unknown>");
300 fprintf(text
->out
, " : container = ");
301 ret
= print_integer(text
, container_field
);
302 if (ret
!= BT_COMPONENT_STATUS_OK
) {
305 fprintf(text
->out
, " )");
307 bt_put(container_field
);
312 enum bt_component_status
print_struct_field(struct text_component
*text
,
313 struct bt_ctf_field
*_struct
,
314 struct bt_ctf_field_type
*struct_type
,
315 int i
, bool print_names
)
317 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
318 const char *field_name
;
319 struct bt_ctf_field
*field
= NULL
;
320 struct bt_ctf_field_type
*field_type
= NULL
;;
322 field
= bt_ctf_field_structure_get_field_by_index(_struct
, i
);
324 ret
= BT_COMPONENT_STATUS_ERROR
;
327 if (bt_ctf_field_type_structure_get_field(struct_type
,
328 &field_name
, &field_type
, i
) < 0) {
329 ret
= BT_COMPONENT_STATUS_ERROR
;
334 fprintf(text
->out
, ", ");
336 fprintf(text
->out
, " ");
339 fprintf(text
->out
, "%s = ", rem_(field_name
));
341 ret
= print_field(text
, field
, print_names
);
349 enum bt_component_status
print_struct(struct text_component
*text
,
350 struct bt_ctf_field
*_struct
, bool print_names
)
352 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
353 struct bt_ctf_field_type
*struct_type
= NULL
;
356 struct_type
= bt_ctf_field_get_type(_struct
);
358 ret
= BT_COMPONENT_STATUS_ERROR
;
361 nr_fields
= bt_ctf_field_type_structure_get_field_count(struct_type
);
363 ret
= BT_COMPONENT_STATUS_ERROR
;
366 fprintf(text
->out
, "{");
368 for (i
= 0; i
< nr_fields
; i
++) {
369 ret
= print_struct_field(text
, _struct
, struct_type
, i
,
371 if (ret
!= BT_COMPONENT_STATUS_OK
) {
376 fprintf(text
->out
, " }");
383 enum bt_component_status
print_array_field(struct text_component
*text
,
384 struct bt_ctf_field
*array
, uint64_t i
,
385 bool is_string
, bool print_names
)
387 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
388 struct bt_ctf_field
*field
= NULL
;
392 fprintf(text
->out
, ", ");
394 fprintf(text
->out
, " ");
397 field
= bt_ctf_field_array_get_field(array
, i
);
399 ret
= BT_COMPONENT_STATUS_ERROR
;
402 ret
= print_field(text
, field
, print_names
);
409 enum bt_component_status
print_array(struct text_component
*text
,
410 struct bt_ctf_field
*array
, bool print_names
)
412 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
413 struct bt_ctf_field_type
*array_type
= NULL
, *field_type
= NULL
;
414 enum bt_ctf_type_id type_id
;
417 bool is_string
= false;
419 array_type
= bt_ctf_field_get_type(array
);
421 ret
= BT_COMPONENT_STATUS_ERROR
;
424 field_type
= bt_ctf_field_type_array_get_element_type(array_type
);
426 ret
= BT_COMPONENT_STATUS_ERROR
;
429 len
= bt_ctf_field_type_array_get_length(array_type
);
431 ret
= BT_COMPONENT_STATUS_ERROR
;
434 type_id
= bt_ctf_field_type_get_type_id(field_type
);
435 if (type_id
== BT_CTF_TYPE_ID_INTEGER
) {
436 enum bt_ctf_string_encoding encoding
;
438 encoding
= bt_ctf_field_type_integer_get_encoding(field_type
);
439 if (encoding
== BT_CTF_STRING_ENCODING_UTF8
440 || encoding
== BT_CTF_STRING_ENCODING_ASCII
) {
441 int integer_len
, integer_alignment
;
443 integer_len
= bt_ctf_field_type_integer_get_size(field_type
);
444 if (integer_len
< 0) {
445 return BT_COMPONENT_STATUS_ERROR
;
447 integer_alignment
= bt_ctf_field_type_get_alignment(field_type
);
448 if (integer_alignment
< 0) {
449 return BT_COMPONENT_STATUS_ERROR
;
451 if (integer_len
== CHAR_BIT
452 && integer_alignment
== CHAR_BIT
) {
459 g_string_assign(text
->string
, "");
461 fprintf(text
->out
, "[");
465 for (i
= 0; i
< len
; i
++) {
466 ret
= print_array_field(text
, array
, i
, is_string
, print_names
);
467 if (ret
!= BT_COMPONENT_STATUS_OK
) {
474 fprintf(text
->out
, "\"%s\"", text
->string
->str
);
476 fprintf(text
->out
, " ]");
485 enum bt_component_status
print_sequence_field(struct text_component
*text
,
486 struct bt_ctf_field
*seq
, uint64_t i
,
487 bool is_string
, bool print_names
)
489 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
490 struct bt_ctf_field
*field
= NULL
;
494 fprintf(text
->out
, ", ");
496 fprintf(text
->out
, " ");
499 field
= bt_ctf_field_sequence_get_field(seq
, i
);
501 ret
= BT_COMPONENT_STATUS_ERROR
;
504 ret
= print_field(text
, field
, print_names
);
511 enum bt_component_status
print_sequence(struct text_component
*text
,
512 struct bt_ctf_field
*seq
, bool print_names
)
514 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
515 struct bt_ctf_field_type
*seq_type
= NULL
, *field_type
= NULL
;
516 struct bt_ctf_field
*length_field
= NULL
;
517 enum bt_ctf_type_id type_id
;
520 bool is_string
= false;
522 seq_type
= bt_ctf_field_get_type(seq
);
524 ret
= BT_COMPONENT_STATUS_ERROR
;
527 length_field
= bt_ctf_field_sequence_get_length(seq
);
529 ret
= BT_COMPONENT_STATUS_ERROR
;
532 if (bt_ctf_field_unsigned_integer_get_value(length_field
, &len
) < 0) {
533 ret
= BT_COMPONENT_STATUS_ERROR
;
536 field_type
= bt_ctf_field_type_sequence_get_element_type(seq_type
);
538 ret
= BT_COMPONENT_STATUS_ERROR
;
541 type_id
= bt_ctf_field_type_get_type_id(field_type
);
542 if (type_id
== BT_CTF_TYPE_ID_INTEGER
) {
543 enum bt_ctf_string_encoding encoding
;
545 encoding
= bt_ctf_field_type_integer_get_encoding(field_type
);
546 if (encoding
== BT_CTF_STRING_ENCODING_UTF8
547 || encoding
== BT_CTF_STRING_ENCODING_ASCII
) {
548 int integer_len
, integer_alignment
;
550 integer_len
= bt_ctf_field_type_integer_get_size(field_type
);
551 if (integer_len
< 0) {
552 ret
= BT_COMPONENT_STATUS_ERROR
;
555 integer_alignment
= bt_ctf_field_type_get_alignment(field_type
);
556 if (integer_alignment
< 0) {
557 ret
= BT_COMPONENT_STATUS_ERROR
;
560 if (integer_len
== CHAR_BIT
561 && integer_alignment
== CHAR_BIT
) {
568 g_string_assign(text
->string
, "");
570 fprintf(text
->out
, "[");
574 for (i
= 0; i
< len
; i
++) {
575 ret
= print_sequence_field(text
, seq
, i
,
576 is_string
, print_names
);
577 if (ret
!= BT_COMPONENT_STATUS_OK
) {
584 fprintf(text
->out
, "\"%s\"", text
->string
->str
);
586 fprintf(text
->out
, " ]");
589 bt_put(length_field
);
596 enum bt_component_status
print_variant(struct text_component
*text
,
597 struct bt_ctf_field
*variant
, bool print_names
)
599 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
600 struct bt_ctf_field
*field
= NULL
;
602 field
= bt_ctf_field_variant_get_current_field(variant
);
604 ret
= BT_COMPONENT_STATUS_ERROR
;
607 fprintf(text
->out
, "{ ");
610 struct bt_ctf_field
*tag_field
= NULL
;
611 const char *tag_choice
;
613 tag_field
= bt_ctf_field_variant_get_tag(variant
);
615 ret
= BT_COMPONENT_STATUS_ERROR
;
618 tag_choice
= bt_ctf_field_enumeration_get_mapping_name(tag_field
);
621 ret
= BT_COMPONENT_STATUS_ERROR
;
624 fprintf(text
->out
, "%s = ", rem_(tag_choice
));
627 ret
= print_field(text
, field
, print_names
);
628 if (ret
!= BT_COMPONENT_STATUS_OK
) {
632 fprintf(text
->out
, " }");
639 enum bt_component_status
print_field(struct text_component
*text
,
640 struct bt_ctf_field
*field
, bool print_names
)
642 enum bt_ctf_type_id type_id
;
644 type_id
= bt_ctf_field_get_type_id(field
);
646 case CTF_TYPE_INTEGER
:
647 return print_integer(text
, field
);
652 if (bt_ctf_field_floating_point_get_value(field
, &v
)) {
653 return BT_COMPONENT_STATUS_ERROR
;
655 fprintf(text
->out
, "%g", v
);
656 return BT_COMPONENT_STATUS_OK
;
659 return print_enum(text
, field
);
660 case CTF_TYPE_STRING
:
661 fprintf(text
->out
, "\"%s\"", bt_ctf_field_string_get_value(field
));
662 return BT_COMPONENT_STATUS_OK
;
663 case CTF_TYPE_STRUCT
:
664 return print_struct(text
, field
, print_names
);
665 case CTF_TYPE_UNTAGGED_VARIANT
:
666 case CTF_TYPE_VARIANT
:
667 return print_variant(text
, field
, print_names
);
669 return print_array(text
, field
, print_names
);
670 case CTF_TYPE_SEQUENCE
:
671 return print_sequence(text
, field
, print_names
);
673 fprintf(text
->err
, "[error] Unknown type id: %d\n", (int) type_id
);
674 return BT_COMPONENT_STATUS_ERROR
;
679 enum bt_component_status
print_stream_packet_context(struct text_component
*text
,
680 struct bt_ctf_event
*event
)
682 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
683 struct bt_ctf_packet
*packet
= NULL
;
684 struct bt_ctf_field
*main_field
= NULL
;
686 packet
= bt_ctf_event_get_packet(event
);
688 ret
= BT_COMPONENT_STATUS_ERROR
;
691 main_field
= bt_ctf_packet_get_context(packet
);
693 ret
= BT_COMPONENT_STATUS_ERROR
;
696 if (text
->options
.print_scope_field_names
) {
697 fputs(" stream.packet.context = ", text
->out
);
699 ret
= print_field(text
, main_field
,
700 text
->options
.print_context_field_names
);
708 enum bt_component_status
print_event_header_raw(struct text_component
*text
,
709 struct bt_ctf_event
*event
)
711 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
712 struct bt_ctf_field
*main_field
= NULL
;
714 main_field
= bt_ctf_event_get_header(event
);
716 ret
= BT_COMPONENT_STATUS_ERROR
;
719 if (text
->options
.print_scope_field_names
) {
720 fputs(" stream.event.header = ", text
->out
);
722 ret
= print_field(text
, main_field
,
723 text
->options
.print_context_field_names
);
730 enum bt_component_status
print_stream_event_context(struct text_component
*text
,
731 struct bt_ctf_event
*event
)
733 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
734 struct bt_ctf_field
*main_field
= NULL
;
736 main_field
= bt_ctf_event_get_stream_event_context(event
);
738 ret
= BT_COMPONENT_STATUS_ERROR
;
741 if (text
->options
.print_scope_field_names
) {
742 fputs(" stream.event.context = ", text
->out
);
744 ret
= print_field(text
, main_field
,
745 text
->options
.print_context_field_names
);
752 enum bt_component_status
print_event_context(struct text_component
*text
,
753 struct bt_ctf_event
*event
)
755 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
756 struct bt_ctf_field
*main_field
= NULL
;
758 main_field
= bt_ctf_event_get_event_context(event
);
760 ret
= BT_COMPONENT_STATUS_ERROR
;
763 if (text
->options
.print_scope_field_names
) {
764 fputs(" event.context = ", text
->out
);
766 ret
= print_field(text
, main_field
,
767 text
->options
.print_context_field_names
);
774 enum bt_component_status
print_event_payload(struct text_component
*text
,
775 struct bt_ctf_event
*event
)
777 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
778 struct bt_ctf_field
*main_field
= NULL
;
780 main_field
= bt_ctf_event_get_payload_field(event
);
782 ret
= BT_COMPONENT_STATUS_ERROR
;
785 if (text
->options
.print_scope_field_names
) {
786 fputs(" event.fields = ", text
->out
);
788 ret
= print_field(text
, main_field
,
789 text
->options
.print_payload_field_names
);
796 enum bt_component_status
text_print_event(struct text_component
*text
,
797 struct bt_ctf_event
*event
)
799 enum bt_component_status ret
;
802 text
->options
.print_scope_field_names
= true;
803 text
->options
.print_payload_field_names
= true;
804 text
->options
.print_context_field_names
= true;
806 ret
= print_event_header(text
, event
);
807 if (ret
!= BT_COMPONENT_STATUS_OK
) {
810 fputs(",", text
->out
);
812 ret
= print_stream_packet_context(text
, event
);
813 if (ret
!= BT_COMPONENT_STATUS_OK
) {
816 fputs(",", text
->out
);
818 ret
= print_event_header_raw(text
, event
);
819 if (ret
!= BT_COMPONENT_STATUS_OK
) {
822 fputs(",", text
->out
);
824 ret
= print_stream_event_context(text
, event
);
825 if (ret
!= BT_COMPONENT_STATUS_OK
) {
828 fputs(",", text
->out
);
830 ret
= print_event_context(text
, event
);
831 if (ret
!= BT_COMPONENT_STATUS_OK
) {
834 fputs(",", text
->out
);
836 ret
= print_event_payload(text
, event
);
837 if (ret
!= BT_COMPONENT_STATUS_OK
) {
841 fputc('\n', text
->out
);