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 #define BT_LOG_TAG "PLUGIN-CTFCOPYTRACE-LIB-CLOCK-FIELDS"
32 #include <babeltrace/ctf-ir/event.h>
33 #include <babeltrace/ctf-ir/packet.h>
34 #include <babeltrace/ctf-ir/event-class.h>
35 #include <babeltrace/ctf-ir/stream.h>
36 #include <babeltrace/ctf-ir/stream-class.h>
37 #include <babeltrace/ctf-ir/clock-class.h>
38 #include <babeltrace/ctf-ir/fields.h>
39 #include <babeltrace/ctf-writer/stream.h>
40 #include <babeltrace/ctf-ir/field-types.h>
44 #include "clock-fields.h"
47 int find_update_struct_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
48 struct bt_ctf_clock_class
*writer_clock_class
);
50 int find_update_array_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
51 struct bt_ctf_clock_class
*writer_clock_class
);
53 int find_update_enum_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
54 struct bt_ctf_clock_class
*writer_clock_class
);
56 int find_update_sequence_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
57 struct bt_ctf_clock_class
*writer_clock_class
);
59 int find_update_variant_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
60 struct bt_ctf_clock_class
*writer_clock_class
);
63 int copy_find_clock_int_field(FILE *err
,
64 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
65 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
66 struct bt_ctf_field
*copy_field
);
68 int copy_find_clock_struct_field(FILE *err
,
69 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
70 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
71 struct bt_ctf_field
*copy_field
);
73 int copy_find_clock_array_field(FILE *err
,
74 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
75 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
76 struct bt_ctf_field
*copy_field
);
78 int copy_find_clock_sequence_field(FILE *err
,
79 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
80 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
81 struct bt_ctf_field
*copy_field
);
83 int copy_find_clock_variant_field(FILE *err
, struct bt_ctf_event
*event
,
84 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
85 struct bt_ctf_field_type
*type
, struct bt_ctf_field
*copy_field
);
87 int copy_find_clock_enum_field(FILE *err
, struct bt_ctf_event
*event
,
88 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
89 struct bt_ctf_field_type
*type
, struct bt_ctf_field
*copy_field
);
92 int update_header_clock_int_field_type(FILE *err
, struct bt_ctf_field_type
*type
,
93 struct bt_ctf_clock_class
*writer_clock_class
)
95 struct bt_ctf_clock_class
*clock
= NULL
;
98 clock
= bt_ctf_field_type_integer_get_mapped_clock_class(type
);
104 ret
= bt_ctf_field_type_integer_set_size(type
, 64);
106 BT_LOGE_STR("Failed to set integer size to 64.");
110 ret
= bt_ctf_field_type_integer_set_mapped_clock_class(type
,
113 BT_LOGE_STR("Failed to map integer to clock_class.");
122 int find_update_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
123 struct bt_ctf_clock_class
*writer_clock_class
)
127 switch (bt_ctf_field_type_get_type_id(type
)) {
128 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
129 return update_header_clock_int_field_type(err
, type
,
131 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
132 return find_update_struct_clock_fields(err
, type
,
134 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
135 return find_update_array_clock_fields(err
, type
,
137 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
138 return find_update_sequence_clock_fields(err
, type
,
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 BT_LOGE_STR("Failed to get variant field.");
174 ret
= find_update_clock_fields(err
, entry_type
,
177 BT_LOGE_STR("Failed to find clock fields.");
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 BT_LOGE_STR("Failed to get struct field.");
211 ret
= find_update_clock_fields(err
, entry_type
,
214 BT_LOGE_STR("Failed to find clock fields.");
230 int find_update_sequence_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
231 struct bt_ctf_clock_class
*writer_clock_class
)
234 struct bt_ctf_field_type
*entry_type
= NULL
;
236 entry_type
= bt_ctf_field_type_sequence_get_element_type(type
);
239 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
242 BT_LOGE_STR("Failed to find clock fields.");
256 int find_update_array_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
257 struct bt_ctf_clock_class
*writer_clock_class
)
260 struct bt_ctf_field_type
*entry_type
= NULL
;
262 entry_type
= bt_ctf_field_type_array_get_element_type(type
);
265 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
268 BT_LOGE_STR("Failed to find clock fields.");
278 int find_update_enum_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
279 struct bt_ctf_clock_class
*writer_clock_class
)
282 struct bt_ctf_field_type
*entry_type
= NULL
;
284 entry_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
287 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
290 BT_LOGE_STR("Failed to find clock fields.");
304 struct bt_ctf_field_type
*override_header_type(FILE *err
,
305 struct bt_ctf_field_type
*type
,
306 struct bt_ctf_trace
*writer_trace
)
308 struct bt_ctf_field_type
*new_type
= NULL
;
309 struct bt_ctf_clock_class
*writer_clock_class
= NULL
;
312 /* FIXME multi-clock? */
313 writer_clock_class
= bt_ctf_trace_get_clock_class_by_index(writer_trace
, 0);
314 assert(writer_clock_class
);
316 new_type
= bt_ctf_field_type_copy(type
);
318 BT_LOGE_STR("Failed to copy field type.");
322 if (bt_ctf_field_type_get_type_id(new_type
) != BT_CTF_FIELD_TYPE_ID_STRUCT
) {
323 BT_LOGE("Expected header field type to be struct: type=%d",
324 bt_ctf_field_type_get_type_id(new_type
));
328 ret
= find_update_struct_clock_fields(err
, new_type
, writer_clock_class
);
330 BT_LOGE_STR("Failed to find clock fields in struct.");
333 BT_PUT(writer_clock_class
);
338 bt_put(writer_clock_class
);
345 int copy_float_field(FILE *err
, struct bt_ctf_field
*field
,
346 struct bt_ctf_field_type
*type
,
347 struct bt_ctf_field
*copy_field
)
352 ret
= bt_ctf_field_floating_point_get_value(field
, &value
);
354 BT_LOGE_STR("Failed to get value.");
358 ret
= bt_ctf_field_floating_point_set_value(copy_field
, value
);
361 BT_LOGE_STR("Failed to set floating point value.");
375 int copy_string_field(FILE *err
, struct bt_ctf_field
*field
,
376 struct bt_ctf_field_type
*type
,
377 struct bt_ctf_field
*copy_field
)
382 value
= bt_ctf_field_string_get_value(field
);
384 BT_LOGE_STR("Failed to get value.");
388 ret
= bt_ctf_field_string_set_value(copy_field
, value
);
391 BT_LOGE_STR("Failed to set string value.");
405 int copy_override_field(FILE *err
, struct bt_ctf_event
*event
,
406 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
407 struct bt_ctf_field
*copy_field
)
409 struct bt_ctf_field_type
*type
= NULL
;
412 type
= bt_ctf_field_get_type(field
);
415 switch (bt_ctf_field_type_get_type_id(type
)) {
416 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
417 ret
= copy_find_clock_int_field(err
, event
, writer_event
,
418 field
, type
, copy_field
);
420 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
421 ret
= copy_find_clock_struct_field(err
, event
, writer_event
,
422 field
, type
, copy_field
);
424 case BT_CTF_FIELD_TYPE_ID_FLOAT
:
425 ret
= copy_float_field(err
, field
, type
, copy_field
);
427 case BT_CTF_FIELD_TYPE_ID_ENUM
:
428 ret
= copy_find_clock_enum_field(err
, event
, writer_event
,
429 field
, type
, copy_field
);
431 case BT_CTF_FIELD_TYPE_ID_STRING
:
432 ret
= copy_string_field(err
, field
, type
, copy_field
);
434 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
435 ret
= copy_find_clock_array_field(err
, event
, writer_event
,
436 field
, type
, copy_field
);
438 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
439 ret
= copy_find_clock_sequence_field(err
, event
, writer_event
,
440 field
, type
, copy_field
);
442 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
443 ret
= copy_find_clock_variant_field(err
, event
, writer_event
,
444 field
, type
, copy_field
);
446 /* No default, we want to catch missing field types. */
447 case BT_CTF_FIELD_TYPE_ID_UNKNOWN
:
448 case BT_CTF_NR_TYPE_IDS
:
458 int copy_find_clock_enum_field(FILE *err
, struct bt_ctf_event
*event
,
459 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
460 struct bt_ctf_field_type
*type
, struct bt_ctf_field
*copy_field
)
463 struct bt_ctf_field
*container
= NULL
, *copy_container
= NULL
;
465 container
= bt_ctf_field_enumeration_get_container(field
);
468 copy_container
= bt_ctf_field_enumeration_get_container(copy_field
);
469 assert(copy_container
);
471 ret
= copy_override_field(err
, event
, writer_event
, container
,
475 BT_LOGE_STR("Failed to override enum field.");
485 bt_put(copy_container
);
491 int copy_find_clock_variant_field(FILE *err
, struct bt_ctf_event
*event
,
492 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
493 struct bt_ctf_field_type
*type
, struct bt_ctf_field
*copy_field
)
496 struct bt_ctf_field
*tag
= NULL
;
497 struct bt_ctf_field
*variant_field
= NULL
, *copy_variant_field
= NULL
;
499 tag
= bt_ctf_field_variant_get_tag(field
);
502 variant_field
= bt_ctf_field_variant_get_field(field
, tag
);
503 if (!variant_field
) {
504 BT_LOGE_STR("Failed to get variant field.");
508 copy_variant_field
= bt_ctf_field_variant_get_field(copy_field
, tag
);
509 assert(copy_variant_field
);
511 ret
= copy_override_field(err
, event
, writer_event
, variant_field
,
514 BT_LOGE_STR("Failed to override variant field.");
524 bt_put(copy_variant_field
);
525 bt_put(variant_field
);
531 int copy_find_clock_sequence_field(FILE *err
,
532 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
533 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
534 struct bt_ctf_field
*copy_field
)
538 struct bt_ctf_field
*length_field
= NULL
;
539 struct bt_ctf_field
*entry_field
= NULL
, *entry_copy
= NULL
;
541 length_field
= bt_ctf_field_sequence_get_length(field
);
542 assert(length_field
);
544 ret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &count
);
546 BT_LOGE("Failed to get value.");
550 ret
= bt_ctf_field_sequence_set_length(copy_field
, length_field
);
552 BT_LOGE_STR("Failed to set sequence length.");
555 BT_PUT(length_field
);
557 for (i
= 0; i
< count
; i
++) {
558 entry_field
= bt_ctf_field_sequence_get_field(field
, i
);
560 BT_LOGE_STR("Failed to get sequence field.");
564 entry_copy
= bt_ctf_field_sequence_get_field(copy_field
, i
);
567 ret
= copy_override_field(err
, event
, writer_event
, entry_field
,
570 BT_LOGE_STR("Faield to override field in sequence.");
581 bt_put(length_field
);
590 int copy_find_clock_array_field(FILE *err
,
591 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
592 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
593 struct bt_ctf_field
*copy_field
)
596 struct bt_ctf_field
*entry_field
= NULL
, *entry_copy
= NULL
;
598 count
= bt_ctf_field_type_array_get_length(type
);
599 for (i
= 0; i
< count
; i
++) {
600 entry_field
= bt_ctf_field_array_get_field(field
, i
);
602 BT_LOGE_STR("Failed to get array field.");
606 entry_copy
= bt_ctf_field_array_get_field(copy_field
, i
);
609 ret
= copy_override_field(err
, event
, writer_event
, entry_field
,
613 BT_LOGE_STR("Failed to override field in array.");
632 int copy_find_clock_struct_field(FILE *err
,
633 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
634 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
635 struct bt_ctf_field
*copy_field
)
638 struct bt_ctf_field_type
*entry_type
= NULL
;
639 struct bt_ctf_field
*entry_field
= NULL
, *entry_copy
= NULL
;
641 count
= bt_ctf_field_type_structure_get_field_count(type
);
642 for (i
= 0; i
< count
; i
++) {
643 const char *entry_name
;
645 entry_field
= bt_ctf_field_structure_get_field_by_index(field
, i
);
647 BT_LOGE_STR("Failed to get struct field.");
651 ret
= bt_ctf_field_type_structure_get_field(type
, &entry_name
,
654 BT_LOGE_STR("Failed to get struct field.");
658 entry_copy
= bt_ctf_field_structure_get_field_by_index(copy_field
, i
);
661 ret
= copy_override_field(err
, event
, writer_event
, entry_field
,
664 BT_LOGE_STR("Failed to override field in struct.");
685 int set_int_value(FILE *err
, struct bt_ctf_field
*field
,
686 struct bt_ctf_field
*copy_field
,
687 struct bt_ctf_field_type
*type
)
693 if (bt_ctf_field_type_integer_get_signed(type
)) {
694 ret
= bt_ctf_field_signed_integer_get_value(field
, &value
);
696 BT_LOGE("Failed to get value.");
700 ret
= bt_ctf_field_signed_integer_set_value(copy_field
, value
);
703 BT_LOGE_STR("Failed to set signed integer value.");
707 ret
= bt_ctf_field_unsigned_integer_get_value(field
,
710 BT_LOGE("Failed to get value.");
714 ret
= bt_ctf_field_unsigned_integer_set_value(copy_field
, uvalue
);
717 BT_LOGE_STR("Failed to set unsigned integer value.");
730 struct bt_ctf_clock_class
*stream_class_get_clock_class(FILE *err
,
731 struct bt_ctf_stream_class
*stream_class
)
733 struct bt_ctf_trace
*trace
= NULL
;
734 struct bt_ctf_clock_class
*clock_class
= NULL
;
736 trace
= bt_ctf_stream_class_get_trace(stream_class
);
739 /* FIXME multi-clock? */
740 clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, 0);
747 struct bt_ctf_clock_class
*event_get_clock_class(FILE *err
, struct bt_ctf_event
*event
)
749 struct bt_ctf_event_class
*event_class
= NULL
;
750 struct bt_ctf_stream_class
*stream_class
= NULL
;
751 struct bt_ctf_clock_class
*clock_class
= NULL
;
753 event_class
= bt_ctf_event_get_class(event
);
756 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
758 assert(stream_class
);
760 clock_class
= stream_class_get_clock_class(err
, stream_class
);
761 bt_put(stream_class
);
767 int copy_find_clock_int_field(FILE *err
,
768 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
769 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
770 struct bt_ctf_field
*copy_field
)
772 struct bt_ctf_clock_class
*clock_class
= NULL
, *writer_clock_class
= NULL
;
773 struct bt_ctf_clock_value
*clock_value
= NULL
, *writer_clock_value
= NULL
;
777 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(type
);
779 return set_int_value(err
, field
, copy_field
, type
);
782 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
786 ret
= bt_ctf_clock_value_get_value(clock_value
, &value
);
789 BT_LOGE("Failed to get clock value.");
793 ret
= bt_ctf_field_unsigned_integer_set_value(copy_field
, value
);
795 BT_LOGE_STR("Failed to set unsigned integer value.");
799 writer_clock_class
= event_get_clock_class(err
, writer_event
);
800 assert(writer_clock_class
);
802 writer_clock_value
= bt_ctf_clock_value_create(writer_clock_class
, value
);
803 BT_PUT(writer_clock_class
);
804 if (!writer_clock_value
) {
805 BT_LOGE_STR("Failed to create clock value.");
809 ret
= bt_ctf_event_set_clock_value(writer_event
, writer_clock_value
);
810 BT_PUT(writer_clock_value
);
812 BT_LOGE_STR("Failed to set clock value.");