4 * Babeltrace - Update clock fields to write uint64 values
6 * Copyright 2017 Julien Desfossez <jdesfossez@efficios.com>
8 * Author: Julien Desfossez <jdesfossez@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/packet.h>
31 #include <babeltrace/ctf-ir/event-class.h>
32 #include <babeltrace/ctf-ir/stream.h>
33 #include <babeltrace/ctf-ir/stream-class.h>
34 #include <babeltrace/ctf-ir/clock-class.h>
35 #include <babeltrace/ctf-ir/fields.h>
36 #include <babeltrace/ctf-writer/stream.h>
37 #include <babeltrace/ctf-ir/field-types.h>
41 #include "clock-fields.h"
44 int find_update_struct_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
45 struct bt_ctf_clock_class
*writer_clock_class
);
47 int find_update_array_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
48 struct bt_ctf_clock_class
*writer_clock_class
);
50 int find_update_enum_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
51 struct bt_ctf_clock_class
*writer_clock_class
);
53 int find_update_sequence_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
54 struct bt_ctf_clock_class
*writer_clock_class
);
56 int find_update_variant_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
57 struct bt_ctf_clock_class
*writer_clock_class
);
60 int copy_find_clock_int_field(FILE *err
,
61 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
62 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
63 struct bt_ctf_field
*copy_field
);
65 int copy_find_clock_struct_field(FILE *err
,
66 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
67 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
68 struct bt_ctf_field
*copy_field
);
70 int copy_find_clock_array_field(FILE *err
,
71 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
72 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
73 struct bt_ctf_field
*copy_field
);
75 int copy_find_clock_sequence_field(FILE *err
,
76 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
77 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
78 struct bt_ctf_field
*copy_field
);
80 int copy_find_clock_variant_field(FILE *err
, struct bt_ctf_event
*event
,
81 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
82 struct bt_ctf_field_type
*type
, struct bt_ctf_field
*copy_field
);
84 int copy_find_clock_enum_field(FILE *err
, struct bt_ctf_event
*event
,
85 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
86 struct bt_ctf_field_type
*type
, struct bt_ctf_field
*copy_field
);
89 int update_header_clock_int_field_type(FILE *err
, struct bt_ctf_field_type
*type
,
90 struct bt_ctf_clock_class
*writer_clock_class
)
92 struct bt_ctf_clock_class
*clock
= NULL
;
95 clock
= bt_ctf_field_type_integer_get_mapped_clock_class(type
);
101 ret
= bt_ctf_field_type_integer_set_size(type
, 64);
103 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
108 ret
= bt_ctf_field_type_integer_set_mapped_clock_class(type
,
111 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
121 int find_update_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
122 struct bt_ctf_clock_class
*writer_clock_class
)
126 switch (bt_ctf_field_type_get_type_id(type
)) {
127 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
128 return update_header_clock_int_field_type(err
, type
,
130 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
131 return find_update_struct_clock_fields(err
, type
,
133 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
134 return find_update_array_clock_fields(err
, type
,
136 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
137 return find_update_sequence_clock_fields(err
, type
,
139 case BT_CTF_TYPE_ID_UNTAGGED_VARIANT
:
140 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
141 return find_update_variant_clock_fields(err
, type
,
143 case BT_CTF_FIELD_TYPE_ID_ENUM
:
144 return find_update_enum_clock_fields(err
, type
,
157 int find_update_variant_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
158 struct bt_ctf_clock_class
*writer_clock_class
)
161 struct bt_ctf_field_type
*entry_type
= NULL
;
163 count
= bt_ctf_field_type_variant_get_field_count(type
);
164 for (i
= 0; i
< count
; i
++) {
165 const char *entry_name
;
167 ret
= bt_ctf_field_type_variant_get_field(type
,
168 &entry_name
, &entry_type
, i
);
170 fprintf(err
, "[error] %s in %s:%d\n",
171 __func__
, __FILE__
, __LINE__
);
174 ret
= find_update_clock_fields(err
, entry_type
,
177 fprintf(err
, "[error] %s in %s:%d\n",
178 __func__
, __FILE__
, __LINE__
);
195 int find_update_struct_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
196 struct bt_ctf_clock_class
*writer_clock_class
)
199 struct bt_ctf_field_type
*entry_type
= NULL
;
201 count
= bt_ctf_field_type_structure_get_field_count(type
);
202 for (i
= 0; i
< count
; i
++) {
203 const char *entry_name
;
205 ret
= bt_ctf_field_type_structure_get_field(type
,
206 &entry_name
, &entry_type
, i
);
208 fprintf(err
, "[error] %s in %s:%d\n",
209 __func__
, __FILE__
, __LINE__
);
212 ret
= find_update_clock_fields(err
, entry_type
,
215 fprintf(err
, "[error] %s in %s:%d\n",
216 __func__
, __FILE__
, __LINE__
);
232 int find_update_sequence_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
233 struct bt_ctf_clock_class
*writer_clock_class
)
236 struct bt_ctf_field_type
*entry_type
= NULL
;
238 entry_type
= bt_ctf_field_type_sequence_get_element_type(type
);
240 fprintf(err
, "[error] %s in %s:%d\n",
241 __func__
, __FILE__
, __LINE__
);
244 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
247 fprintf(err
, "[error] %s in %s:%d\n",
248 __func__
, __FILE__
, __LINE__
);
262 int find_update_array_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
263 struct bt_ctf_clock_class
*writer_clock_class
)
266 struct bt_ctf_field_type
*entry_type
= NULL
;
268 entry_type
= bt_ctf_field_type_array_get_element_type(type
);
270 fprintf(err
, "[error] %s in %s:%d\n",
271 __func__
, __FILE__
, __LINE__
);
274 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
277 fprintf(err
, "[error] %s in %s:%d\n",
278 __func__
, __FILE__
, __LINE__
);
293 int find_update_enum_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
294 struct bt_ctf_clock_class
*writer_clock_class
)
297 struct bt_ctf_field_type
*entry_type
= NULL
;
299 entry_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
301 fprintf(err
, "[error] %s in %s:%d\n",
302 __func__
, __FILE__
, __LINE__
);
305 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
308 fprintf(err
, "[error] %s in %s:%d\n",
309 __func__
, __FILE__
, __LINE__
);
323 struct bt_ctf_field_type
*override_header_type(FILE *err
,
324 struct bt_ctf_field_type
*type
,
325 struct bt_ctf_trace
*writer_trace
)
327 struct bt_ctf_field_type
*new_type
= NULL
;
328 struct bt_ctf_clock_class
*writer_clock_class
= NULL
;
331 /* FIXME multi-clock? */
332 writer_clock_class
= bt_ctf_trace_get_clock_class_by_index(writer_trace
, 0);
333 if (!writer_clock_class
) {
334 fprintf(err
, "[error] %s in %s:%d\n",
335 __func__
, __FILE__
, __LINE__
);
339 new_type
= bt_ctf_field_type_copy(type
);
341 fprintf(err
, "[error] %s in %s:%d\n",
342 __func__
, __FILE__
, __LINE__
);
346 if (bt_ctf_field_type_get_type_id(new_type
) != BT_CTF_FIELD_TYPE_ID_STRUCT
) {
347 fprintf(err
, "[error] Unexpected header field type\n");
351 ret
= find_update_struct_clock_fields(err
, new_type
, writer_clock_class
);
353 fprintf(err
, "[error] %s in %s:%d\n",
354 __func__
, __FILE__
, __LINE__
);
357 BT_PUT(writer_clock_class
);
362 bt_put(writer_clock_class
);
369 int copy_float_field(FILE *err
, struct bt_ctf_field
*field
,
370 struct bt_ctf_field_type
*type
,
371 struct bt_ctf_field
*copy_field
)
376 ret
= bt_ctf_field_floating_point_get_value(field
, &value
);
379 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
383 ret
= bt_ctf_field_floating_point_set_value(copy_field
, value
);
386 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
398 int copy_string_field(FILE *err
, struct bt_ctf_field
*field
,
399 struct bt_ctf_field_type
*type
,
400 struct bt_ctf_field
*copy_field
)
405 value
= bt_ctf_field_string_get_value(field
);
408 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
412 ret
= bt_ctf_field_string_set_value(copy_field
, value
);
415 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
427 int copy_override_field(FILE *err
, struct bt_ctf_event
*event
,
428 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
429 struct bt_ctf_field
*copy_field
)
431 struct bt_ctf_field_type
*type
= NULL
;
434 type
= bt_ctf_field_get_type(field
);
437 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
442 switch (bt_ctf_field_type_get_type_id(type
)) {
443 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
444 ret
= copy_find_clock_int_field(err
, event
, writer_event
,
445 field
, type
, copy_field
);
447 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
448 ret
= copy_find_clock_struct_field(err
, event
, writer_event
,
449 field
, type
, copy_field
);
451 case BT_CTF_FIELD_TYPE_ID_FLOAT
:
452 ret
= copy_float_field(err
, field
, type
, copy_field
);
454 case BT_CTF_FIELD_TYPE_ID_ENUM
:
455 ret
= copy_find_clock_enum_field(err
, event
, writer_event
,
456 field
, type
, copy_field
);
458 case BT_CTF_FIELD_TYPE_ID_STRING
:
459 ret
= copy_string_field(err
, field
, type
, copy_field
);
461 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
462 ret
= copy_find_clock_array_field(err
, event
, writer_event
,
463 field
, type
, copy_field
);
465 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
466 ret
= copy_find_clock_sequence_field(err
, event
, writer_event
,
467 field
, type
, copy_field
);
469 case BT_CTF_TYPE_ID_UNTAGGED_VARIANT
:
470 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
471 ret
= copy_find_clock_variant_field(err
, event
, writer_event
,
472 field
, type
, copy_field
);
474 /* No default, we want to catch missing field types. */
475 case BT_CTF_FIELD_TYPE_ID_UNKNOWN
:
476 case BT_CTF_NR_TYPE_IDS
:
488 int copy_find_clock_enum_field(FILE *err
, struct bt_ctf_event
*event
,
489 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
490 struct bt_ctf_field_type
*type
, struct bt_ctf_field
*copy_field
)
493 struct bt_ctf_field
*container
= NULL
, *copy_container
= NULL
;
495 container
= bt_ctf_field_enumeration_get_container(field
);
498 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
503 copy_container
= bt_ctf_field_enumeration_get_container(copy_field
);
504 if (!copy_container
) {
506 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
511 ret
= copy_override_field(err
, event
, writer_event
, container
,
515 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
526 bt_put(copy_container
);
532 int copy_find_clock_variant_field(FILE *err
, struct bt_ctf_event
*event
,
533 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
534 struct bt_ctf_field_type
*type
, struct bt_ctf_field
*copy_field
)
537 struct bt_ctf_field
*tag
= NULL
;
538 struct bt_ctf_field
*variant_field
= NULL
, *copy_variant_field
= NULL
;
540 tag
= bt_ctf_field_variant_get_tag(field
);
542 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
547 variant_field
= bt_ctf_field_variant_get_field(field
, tag
);
548 if (!variant_field
) {
549 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
554 copy_variant_field
= bt_ctf_field_variant_get_field(copy_field
, tag
);
555 if (!copy_variant_field
) {
556 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
561 ret
= copy_override_field(err
, event
, writer_event
, variant_field
,
564 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
575 bt_put(copy_variant_field
);
576 bt_put(variant_field
);
582 int copy_find_clock_sequence_field(FILE *err
,
583 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
584 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
585 struct bt_ctf_field
*copy_field
)
589 struct bt_ctf_field
*length_field
= NULL
;
590 struct bt_ctf_field
*entry_field
= NULL
, *entry_copy
= NULL
;
592 length_field
= bt_ctf_field_sequence_get_length(field
);
594 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
599 ret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &count
);
601 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
606 ret
= bt_ctf_field_sequence_set_length(copy_field
, length_field
);
608 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
612 BT_PUT(length_field
);
614 for (i
= 0; i
< count
; i
++) {
615 entry_field
= bt_ctf_field_sequence_get_field(field
, i
);
617 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
622 entry_copy
= bt_ctf_field_sequence_get_field(copy_field
, i
);
624 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
629 ret
= copy_override_field(err
, event
, writer_event
, entry_field
,
632 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
644 bt_put(length_field
);
653 int copy_find_clock_array_field(FILE *err
,
654 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
655 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
656 struct bt_ctf_field
*copy_field
)
659 struct bt_ctf_field
*entry_field
= NULL
, *entry_copy
= NULL
;
661 count
= bt_ctf_field_type_array_get_length(type
);
662 for (i
= 0; i
< count
; i
++) {
663 entry_field
= bt_ctf_field_array_get_field(field
, i
);
665 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
670 entry_copy
= bt_ctf_field_array_get_field(copy_field
, i
);
672 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
677 ret
= copy_override_field(err
, event
, writer_event
, entry_field
,
681 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
701 int copy_find_clock_struct_field(FILE *err
,
702 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
703 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
704 struct bt_ctf_field
*copy_field
)
707 struct bt_ctf_field_type
*entry_type
= NULL
;
708 struct bt_ctf_field
*entry_field
= NULL
, *entry_copy
= NULL
;
710 count
= bt_ctf_field_type_structure_get_field_count(type
);
711 for (i
= 0; i
< count
; i
++) {
712 const char *entry_name
;
714 entry_field
= bt_ctf_field_structure_get_field_by_index(field
, i
);
716 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
721 ret
= bt_ctf_field_type_structure_get_field(type
, &entry_name
,
724 fprintf(err
, "[error] %s in %s:%d\n",
725 __func__
, __FILE__
, __LINE__
);
729 entry_copy
= bt_ctf_field_structure_get_field_by_index(copy_field
, i
);
731 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
736 ret
= copy_override_field(err
, event
, writer_event
, entry_field
,
739 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
761 int set_int_value(FILE *err
, struct bt_ctf_field
*field
,
762 struct bt_ctf_field
*copy_field
,
763 struct bt_ctf_field_type
*type
)
769 if (bt_ctf_field_type_integer_get_signed(type
)) {
770 ret
= bt_ctf_field_signed_integer_get_value(field
, &value
);
773 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
777 ret
= bt_ctf_field_signed_integer_set_value(copy_field
, value
);
780 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
785 ret
= bt_ctf_field_unsigned_integer_get_value(field
,
789 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
793 ret
= bt_ctf_field_unsigned_integer_set_value(copy_field
, uvalue
);
796 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
807 struct bt_ctf_clock_class
*stream_class_get_clock_class(FILE *err
,
808 struct bt_ctf_stream_class
*stream_class
)
810 struct bt_ctf_trace
*trace
= NULL
;
811 struct bt_ctf_clock_class
*clock_class
= NULL
;
813 trace
= bt_ctf_stream_class_get_trace(stream_class
);
815 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
820 /* FIXME multi-clock? */
821 clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, 0);
828 struct bt_ctf_clock_class
*event_get_clock_class(FILE *err
, struct bt_ctf_event
*event
)
830 struct bt_ctf_event_class
*event_class
= NULL
;
831 struct bt_ctf_stream_class
*stream_class
= NULL
;
832 struct bt_ctf_clock_class
*clock_class
= NULL
;
834 event_class
= bt_ctf_event_get_class(event
);
836 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
841 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
844 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
849 clock_class
= stream_class_get_clock_class(err
, stream_class
);
850 bt_put(stream_class
);
857 int copy_find_clock_int_field(FILE *err
,
858 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
859 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
860 struct bt_ctf_field
*copy_field
)
862 struct bt_ctf_clock_class
*clock_class
= NULL
, *writer_clock_class
= NULL
;
863 struct bt_ctf_clock_value
*clock_value
= NULL
, *writer_clock_value
= NULL
;
867 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(type
);
869 return set_int_value(err
, field
, copy_field
, type
);
872 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
875 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
880 ret
= bt_ctf_clock_value_get_value(clock_value
, &value
);
883 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
888 ret
= bt_ctf_field_unsigned_integer_set_value(copy_field
, value
);
890 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
895 writer_clock_class
= event_get_clock_class(err
, writer_event
);
896 if (!writer_clock_class
) {
897 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
902 writer_clock_value
= bt_ctf_clock_value_create(writer_clock_class
, value
);
903 BT_PUT(writer_clock_class
);
904 if (!writer_clock_value
) {
905 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
910 ret
= bt_ctf_event_set_clock_value(writer_event
, writer_clock_value
);
911 BT_PUT(writer_clock_value
);
913 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,