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_FIELD_TYPE_ID_VARIANT
:
140 return find_update_variant_clock_fields(err
, type
,
142 case BT_CTF_FIELD_TYPE_ID_ENUM
:
143 return find_update_enum_clock_fields(err
, type
,
156 int find_update_variant_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
157 struct bt_ctf_clock_class
*writer_clock_class
)
160 struct bt_ctf_field_type
*entry_type
= NULL
;
162 count
= bt_ctf_field_type_variant_get_field_count(type
);
163 for (i
= 0; i
< count
; i
++) {
164 const char *entry_name
;
166 ret
= bt_ctf_field_type_variant_get_field(type
,
167 &entry_name
, &entry_type
, i
);
169 fprintf(err
, "[error] %s in %s:%d\n",
170 __func__
, __FILE__
, __LINE__
);
173 ret
= find_update_clock_fields(err
, entry_type
,
176 fprintf(err
, "[error] %s in %s:%d\n",
177 __func__
, __FILE__
, __LINE__
);
194 int find_update_struct_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
195 struct bt_ctf_clock_class
*writer_clock_class
)
198 struct bt_ctf_field_type
*entry_type
= NULL
;
200 count
= bt_ctf_field_type_structure_get_field_count(type
);
201 for (i
= 0; i
< count
; i
++) {
202 const char *entry_name
;
204 ret
= bt_ctf_field_type_structure_get_field(type
,
205 &entry_name
, &entry_type
, i
);
207 fprintf(err
, "[error] %s in %s:%d\n",
208 __func__
, __FILE__
, __LINE__
);
211 ret
= find_update_clock_fields(err
, entry_type
,
214 fprintf(err
, "[error] %s in %s:%d\n",
215 __func__
, __FILE__
, __LINE__
);
231 int find_update_sequence_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
232 struct bt_ctf_clock_class
*writer_clock_class
)
235 struct bt_ctf_field_type
*entry_type
= NULL
;
237 entry_type
= bt_ctf_field_type_sequence_get_element_type(type
);
239 fprintf(err
, "[error] %s in %s:%d\n",
240 __func__
, __FILE__
, __LINE__
);
243 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
246 fprintf(err
, "[error] %s in %s:%d\n",
247 __func__
, __FILE__
, __LINE__
);
261 int find_update_array_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
262 struct bt_ctf_clock_class
*writer_clock_class
)
265 struct bt_ctf_field_type
*entry_type
= NULL
;
267 entry_type
= bt_ctf_field_type_array_get_element_type(type
);
269 fprintf(err
, "[error] %s in %s:%d\n",
270 __func__
, __FILE__
, __LINE__
);
273 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
276 fprintf(err
, "[error] %s in %s:%d\n",
277 __func__
, __FILE__
, __LINE__
);
292 int find_update_enum_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
293 struct bt_ctf_clock_class
*writer_clock_class
)
296 struct bt_ctf_field_type
*entry_type
= NULL
;
298 entry_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
300 fprintf(err
, "[error] %s in %s:%d\n",
301 __func__
, __FILE__
, __LINE__
);
304 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
307 fprintf(err
, "[error] %s in %s:%d\n",
308 __func__
, __FILE__
, __LINE__
);
322 struct bt_ctf_field_type
*override_header_type(FILE *err
,
323 struct bt_ctf_field_type
*type
,
324 struct bt_ctf_trace
*writer_trace
)
326 struct bt_ctf_field_type
*new_type
= NULL
;
327 struct bt_ctf_clock_class
*writer_clock_class
= NULL
;
330 /* FIXME multi-clock? */
331 writer_clock_class
= bt_ctf_trace_get_clock_class_by_index(writer_trace
, 0);
332 if (!writer_clock_class
) {
333 fprintf(err
, "[error] %s in %s:%d\n",
334 __func__
, __FILE__
, __LINE__
);
338 new_type
= bt_ctf_field_type_copy(type
);
340 fprintf(err
, "[error] %s in %s:%d\n",
341 __func__
, __FILE__
, __LINE__
);
345 if (bt_ctf_field_type_get_type_id(new_type
) != BT_CTF_FIELD_TYPE_ID_STRUCT
) {
346 fprintf(err
, "[error] Unexpected header field type\n");
350 ret
= find_update_struct_clock_fields(err
, new_type
, writer_clock_class
);
352 fprintf(err
, "[error] %s in %s:%d\n",
353 __func__
, __FILE__
, __LINE__
);
356 BT_PUT(writer_clock_class
);
361 bt_put(writer_clock_class
);
368 int copy_float_field(FILE *err
, struct bt_ctf_field
*field
,
369 struct bt_ctf_field_type
*type
,
370 struct bt_ctf_field
*copy_field
)
375 ret
= bt_ctf_field_floating_point_get_value(field
, &value
);
378 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
382 ret
= bt_ctf_field_floating_point_set_value(copy_field
, value
);
385 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
397 int copy_string_field(FILE *err
, struct bt_ctf_field
*field
,
398 struct bt_ctf_field_type
*type
,
399 struct bt_ctf_field
*copy_field
)
404 value
= bt_ctf_field_string_get_value(field
);
407 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
411 ret
= bt_ctf_field_string_set_value(copy_field
, value
);
414 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
426 int copy_override_field(FILE *err
, struct bt_ctf_event
*event
,
427 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
428 struct bt_ctf_field
*copy_field
)
430 struct bt_ctf_field_type
*type
= NULL
;
433 type
= bt_ctf_field_get_type(field
);
436 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
441 switch (bt_ctf_field_type_get_type_id(type
)) {
442 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
443 ret
= copy_find_clock_int_field(err
, event
, writer_event
,
444 field
, type
, copy_field
);
446 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
447 ret
= copy_find_clock_struct_field(err
, event
, writer_event
,
448 field
, type
, copy_field
);
450 case BT_CTF_FIELD_TYPE_ID_FLOAT
:
451 ret
= copy_float_field(err
, field
, type
, copy_field
);
453 case BT_CTF_FIELD_TYPE_ID_ENUM
:
454 ret
= copy_find_clock_enum_field(err
, event
, writer_event
,
455 field
, type
, copy_field
);
457 case BT_CTF_FIELD_TYPE_ID_STRING
:
458 ret
= copy_string_field(err
, field
, type
, copy_field
);
460 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
461 ret
= copy_find_clock_array_field(err
, event
, writer_event
,
462 field
, type
, copy_field
);
464 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
465 ret
= copy_find_clock_sequence_field(err
, event
, writer_event
,
466 field
, type
, copy_field
);
468 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
469 ret
= copy_find_clock_variant_field(err
, event
, writer_event
,
470 field
, type
, copy_field
);
472 /* No default, we want to catch missing field types. */
473 case BT_CTF_FIELD_TYPE_ID_UNKNOWN
:
474 case BT_CTF_NR_TYPE_IDS
:
486 int copy_find_clock_enum_field(FILE *err
, struct bt_ctf_event
*event
,
487 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
488 struct bt_ctf_field_type
*type
, struct bt_ctf_field
*copy_field
)
491 struct bt_ctf_field
*container
= NULL
, *copy_container
= NULL
;
493 container
= bt_ctf_field_enumeration_get_container(field
);
496 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
501 copy_container
= bt_ctf_field_enumeration_get_container(copy_field
);
502 if (!copy_container
) {
504 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
509 ret
= copy_override_field(err
, event
, writer_event
, container
,
513 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
524 bt_put(copy_container
);
530 int copy_find_clock_variant_field(FILE *err
, struct bt_ctf_event
*event
,
531 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
532 struct bt_ctf_field_type
*type
, struct bt_ctf_field
*copy_field
)
535 struct bt_ctf_field
*tag
= NULL
;
536 struct bt_ctf_field
*variant_field
= NULL
, *copy_variant_field
= NULL
;
538 tag
= bt_ctf_field_variant_get_tag(field
);
540 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
545 variant_field
= bt_ctf_field_variant_get_field(field
, tag
);
546 if (!variant_field
) {
547 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
552 copy_variant_field
= bt_ctf_field_variant_get_field(copy_field
, tag
);
553 if (!copy_variant_field
) {
554 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
559 ret
= copy_override_field(err
, event
, writer_event
, variant_field
,
562 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
573 bt_put(copy_variant_field
);
574 bt_put(variant_field
);
580 int copy_find_clock_sequence_field(FILE *err
,
581 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
582 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
583 struct bt_ctf_field
*copy_field
)
587 struct bt_ctf_field
*length_field
= NULL
;
588 struct bt_ctf_field
*entry_field
= NULL
, *entry_copy
= NULL
;
590 length_field
= bt_ctf_field_sequence_get_length(field
);
592 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
597 ret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &count
);
599 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
604 ret
= bt_ctf_field_sequence_set_length(copy_field
, length_field
);
606 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
610 BT_PUT(length_field
);
612 for (i
= 0; i
< count
; i
++) {
613 entry_field
= bt_ctf_field_sequence_get_field(field
, i
);
615 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
620 entry_copy
= bt_ctf_field_sequence_get_field(copy_field
, i
);
622 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
627 ret
= copy_override_field(err
, event
, writer_event
, entry_field
,
630 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
642 bt_put(length_field
);
651 int copy_find_clock_array_field(FILE *err
,
652 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
653 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
654 struct bt_ctf_field
*copy_field
)
657 struct bt_ctf_field
*entry_field
= NULL
, *entry_copy
= NULL
;
659 count
= bt_ctf_field_type_array_get_length(type
);
660 for (i
= 0; i
< count
; i
++) {
661 entry_field
= bt_ctf_field_array_get_field(field
, i
);
664 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
669 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__
,