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/babeltrace.h>
33 #include <babeltrace/assert-internal.h>
36 #include "clock-field.h"
39 int find_update_struct_clock_fields(FILE *err
, bt_field_type
*type
,
40 const bt_clock_class
*writer_clock_class
);
42 int find_update_array_clock_fields(FILE *err
, bt_field_type
*type
,
43 const bt_clock_class
*writer_clock_class
);
45 int find_update_enum_clock_fields(FILE *err
, bt_field_type
*type
,
46 const bt_clock_class
*writer_clock_class
);
48 int find_update_sequence_clock_fields(FILE *err
, bt_field_type
*type
,
49 const bt_clock_class
*writer_clock_class
);
51 int find_update_variant_clock_fields(FILE *err
, bt_field_type
*type
,
52 const bt_clock_class
*writer_clock_class
);
55 int copy_find_clock_int_field(FILE *err
,
56 const bt_event
*event
, const bt_event
*writer_event
,
57 const bt_field
*field
, bt_field_type
*type
,
58 const bt_field
*copy_field
);
60 int copy_find_clock_struct_field(FILE *err
,
61 const bt_event
*event
, const bt_event
*writer_event
,
62 const bt_field
*field
, bt_field_type
*type
,
63 const bt_field
*copy_field
);
65 int copy_find_clock_array_field(FILE *err
,
66 const bt_event
*event
, const bt_event
*writer_event
,
67 const bt_field
*field
, bt_field_type
*type
,
68 const bt_field
*copy_field
);
70 int copy_find_clock_sequence_field(FILE *err
,
71 const bt_event
*event
, const bt_event
*writer_event
,
72 const bt_field
*field
, bt_field_type
*type
,
73 const bt_field
*copy_field
);
75 int copy_find_clock_variant_field(FILE *err
, const bt_event
*event
,
76 const bt_event
*writer_event
, const bt_field
*field
,
77 bt_field_type
*type
, const bt_field
*copy_field
);
79 int copy_find_clock_enum_field(FILE *err
, const bt_event
*event
,
80 const bt_event
*writer_event
, const bt_field
*field
,
81 bt_field_type
*type
, const bt_field
*copy_field
);
84 int update_header_clock_int_field_type(FILE *err
, bt_field_type
*type
,
85 const bt_clock_class
*writer_clock_class
)
87 const bt_clock_class
*clock
= NULL
;
90 clock
= bt_field_type_integer_get_mapped_clock_class(type
);
94 BT_CLOCK_CLASS_PUT_REF_AND_RESET(clock
);
96 ret
= bt_field_type_integer_set_size(type
, 64);
98 BT_LOGE_STR("Failed to set integer size to 64.");
102 ret
= bt_field_type_integer_set_mapped_clock_class(type
,
105 BT_LOGE_STR("Failed to map integer to clock_class.");
114 int find_update_clock_fields(FILE *err
, bt_field_type
*type
,
115 const bt_clock_class
*writer_clock_class
)
119 switch (bt_field_type_get_type_id(type
)) {
120 case BT_FIELD_TYPE_ID_INTEGER
:
121 return update_header_clock_int_field_type(err
, type
,
123 case BT_FIELD_TYPE_ID_STRUCT
:
124 return find_update_struct_clock_fields(err
, type
,
126 case BT_FIELD_TYPE_ID_ARRAY
:
127 return find_update_array_clock_fields(err
, type
,
129 case BT_FIELD_TYPE_ID_SEQUENCE
:
130 return find_update_sequence_clock_fields(err
, type
,
132 case BT_FIELD_TYPE_ID_VARIANT
:
133 return find_update_variant_clock_fields(err
, type
,
135 case BT_FIELD_TYPE_ID_ENUM
:
136 return find_update_enum_clock_fields(err
, type
,
149 int find_update_variant_clock_fields(FILE *err
, bt_field_type
*type
,
150 const bt_clock_class
*writer_clock_class
)
153 bt_field_type
*entry_type
= NULL
;
155 count
= bt_field_type_variant_get_field_count(type
);
156 for (i
= 0; i
< count
; i
++) {
157 const char *entry_name
;
159 ret
= bt_field_type_variant_get_field_by_index(type
,
160 &entry_name
, &entry_type
, i
);
162 BT_LOGE_STR("Failed to get variant field.");
166 ret
= find_update_clock_fields(err
, entry_type
,
169 BT_LOGE_STR("Failed to find clock fields.");
172 BT_OBJECT_PUT_REF_AND_RESET(entry_type
);
181 bt_object_put_ref(entry_type
);
186 int find_update_struct_clock_fields(FILE *err
, bt_field_type
*type
,
187 const bt_clock_class
*writer_clock_class
)
190 bt_field_type
*entry_type
= NULL
;
192 count
= bt_field_type_structure_get_field_count(type
);
193 for (i
= 0; i
< count
; i
++) {
194 const char *entry_name
;
196 ret
= bt_field_type_structure_get_field_by_index(type
,
197 &entry_name
, &entry_type
, i
);
199 BT_LOGE_STR("Failed to get struct field.");
203 ret
= find_update_clock_fields(err
, entry_type
,
206 BT_LOGE_STR("Failed to find clock fields.");
209 BT_OBJECT_PUT_REF_AND_RESET(entry_type
);
216 bt_object_put_ref(entry_type
);
222 int find_update_sequence_clock_fields(FILE *err
, bt_field_type
*type
,
223 const bt_clock_class
*writer_clock_class
)
226 bt_field_type
*entry_type
= NULL
;
228 entry_type
= bt_field_type_sequence_get_element_type(type
);
229 BT_ASSERT(entry_type
);
231 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
232 BT_OBJECT_PUT_REF_AND_RESET(entry_type
);
234 BT_LOGE_STR("Failed to find clock fields.");
248 int find_update_array_clock_fields(FILE *err
, bt_field_type
*type
,
249 const bt_clock_class
*writer_clock_class
)
252 bt_field_type
*entry_type
= NULL
;
254 entry_type
= bt_field_type_array_get_element_type(type
);
255 BT_ASSERT(entry_type
);
257 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
258 BT_OBJECT_PUT_REF_AND_RESET(entry_type
);
260 BT_LOGE_STR("Failed to find clock fields.");
270 int find_update_enum_clock_fields(FILE *err
, bt_field_type
*type
,
271 const bt_clock_class
*writer_clock_class
)
274 bt_field_type
*entry_type
= NULL
;
276 entry_type
= bt_field_type_enumeration_get_container_type(type
);
277 BT_ASSERT(entry_type
);
279 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
280 BT_OBJECT_PUT_REF_AND_RESET(entry_type
);
282 BT_LOGE_STR("Failed to find clock fields.");
296 bt_field_type
*override_header_type(FILE *err
,
298 const bt_trace
*writer_trace
)
300 bt_field_type
*new_type
= NULL
;
301 const bt_clock_class
*writer_clock_class
= NULL
;
304 /* FIXME multi-clock? */
305 writer_clock_class
= bt_trace_get_clock_class_by_index(writer_trace
, 0);
306 BT_ASSERT(writer_clock_class
);
308 new_type
= bt_field_type_copy(type
);
310 BT_LOGE_STR("Failed to copy field type.");
314 if (bt_field_type_get_type_id(new_type
) != BT_FIELD_TYPE_ID_STRUCT
) {
315 BT_LOGE("Expected header field type to be struct: type=%d",
316 bt_field_type_get_type_id(new_type
));
320 ret
= find_update_struct_clock_fields(err
, new_type
, writer_clock_class
);
322 BT_LOGE_STR("Failed to find clock fields in struct.");
325 BT_CLOCK_CLASS_PUT_REF_AND_RESET(writer_clock_class
);
330 bt_clock_class_put_ref(writer_clock_class
);
331 BT_OBJECT_PUT_REF_AND_RESET(new_type
);
337 int copy_float_field(FILE *err
, const bt_field
*field
,
339 const bt_field
*copy_field
)
344 ret
= bt_field_floating_point_get_value(field
, &value
);
346 BT_LOGE_STR("Failed to get value.");
350 ret
= bt_field_floating_point_set_value(copy_field
, value
);
353 BT_LOGE_STR("Failed to set floating point value.");
367 int copy_string_field(FILE *err
, const bt_field
*field
,
369 const bt_field
*copy_field
)
374 value
= bt_field_string_get_value(field
);
376 BT_LOGE_STR("Failed to get value.");
380 ret
= bt_field_string_set_value(copy_field
, value
);
383 BT_LOGE_STR("Failed to set string value.");
397 int copy_override_field(FILE *err
, const bt_event
*event
,
398 const bt_event
*writer_event
, const bt_field
*field
,
399 const bt_field
*copy_field
)
401 bt_field_type
*type
= NULL
;
404 type
= bt_field_get_type(field
);
407 switch (bt_field_type_get_type_id(type
)) {
408 case BT_FIELD_TYPE_ID_INTEGER
:
409 ret
= copy_find_clock_int_field(err
, event
, writer_event
,
410 field
, type
, copy_field
);
412 case BT_FIELD_TYPE_ID_STRUCT
:
413 ret
= copy_find_clock_struct_field(err
, event
, writer_event
,
414 field
, type
, copy_field
);
416 case BT_FIELD_TYPE_ID_FLOAT
:
417 ret
= copy_float_field(err
, field
, type
, copy_field
);
419 case BT_FIELD_TYPE_ID_ENUM
:
420 ret
= copy_find_clock_enum_field(err
, event
, writer_event
,
421 field
, type
, copy_field
);
423 case BT_FIELD_TYPE_ID_STRING
:
424 ret
= copy_string_field(err
, field
, type
, copy_field
);
426 case BT_FIELD_TYPE_ID_ARRAY
:
427 ret
= copy_find_clock_array_field(err
, event
, writer_event
,
428 field
, type
, copy_field
);
430 case BT_FIELD_TYPE_ID_SEQUENCE
:
431 ret
= copy_find_clock_sequence_field(err
, event
, writer_event
,
432 field
, type
, copy_field
);
434 case BT_FIELD_TYPE_ID_VARIANT
:
435 ret
= copy_find_clock_variant_field(err
, event
, writer_event
,
436 field
, type
, copy_field
);
438 /* No default, we want to catch missing field types. */
439 case BT_FIELD_TYPE_ID_UNKNOWN
:
440 case BT_FIELD_TYPE_ID_NR
:
444 BT_OBJECT_PUT_REF_AND_RESET(type
);
450 int copy_find_clock_enum_field(FILE *err
, const bt_event
*event
,
451 const bt_event
*writer_event
, const bt_field
*field
,
452 bt_field_type
*type
, const bt_field
*copy_field
)
455 const bt_field
*container
= NULL
, *copy_container
= NULL
;
457 container
= bt_field_enumeration_get_container(field
);
458 BT_ASSERT(container
);
460 copy_container
= bt_field_enumeration_get_container(copy_field
);
461 BT_ASSERT(copy_container
);
463 ret
= copy_override_field(err
, event
, writer_event
, container
,
467 BT_LOGE_STR("Failed to override enum field.");
477 bt_object_put_ref(copy_container
);
478 bt_object_put_ref(container
);
483 int copy_find_clock_variant_field(FILE *err
, const bt_event
*event
,
484 const bt_event
*writer_event
, const bt_field
*field
,
485 bt_field_type
*type
, const bt_field
*copy_field
)
488 const bt_field
*tag
= NULL
;
489 const bt_field
*variant_field
= NULL
, *copy_variant_field
= NULL
;
491 tag
= bt_field_variant_get_tag(field
);
494 variant_field
= bt_field_variant_get_field(field
, tag
);
495 if (!variant_field
) {
496 BT_LOGE_STR("Failed to get variant field.");
500 copy_variant_field
= bt_field_variant_get_field(copy_field
, tag
);
501 BT_ASSERT(copy_variant_field
);
503 ret
= copy_override_field(err
, event
, writer_event
, variant_field
,
506 BT_LOGE_STR("Failed to override variant field.");
516 bt_object_put_ref(copy_variant_field
);
517 bt_object_put_ref(variant_field
);
518 bt_object_put_ref(tag
);
523 int copy_find_clock_sequence_field(FILE *err
,
524 const bt_event
*event
, const bt_event
*writer_event
,
525 const bt_field
*field
, bt_field_type
*type
,
526 const bt_field
*copy_field
)
530 const bt_field
*length_field
= NULL
;
531 const bt_field
*entry_field
= NULL
, *entry_copy
= NULL
;
533 length_field
= bt_field_sequence_get_length(field
);
534 BT_ASSERT(length_field
);
536 ret
= bt_field_unsigned_integer_get_value(length_field
, &count
);
538 BT_LOGE("Failed to get value.");
542 ret
= bt_field_sequence_set_length(copy_field
, length_field
);
544 BT_LOGE_STR("Failed to set sequence length.");
547 BT_OBJECT_PUT_REF_AND_RESET(length_field
);
549 for (i
= 0; i
< count
; i
++) {
550 entry_field
= bt_field_sequence_get_field(field
, i
);
552 BT_LOGE_STR("Failed to get sequence field.");
556 entry_copy
= bt_field_sequence_get_field(copy_field
, i
);
557 BT_ASSERT(entry_copy
);
559 ret
= copy_override_field(err
, event
, writer_event
, entry_field
,
562 BT_LOGE_STR("Faield to override field in sequence.");
565 BT_OBJECT_PUT_REF_AND_RESET(entry_field
);
566 BT_OBJECT_PUT_REF_AND_RESET(entry_copy
);
573 bt_object_put_ref(length_field
);
574 bt_object_put_ref(entry_field
);
575 bt_object_put_ref(entry_copy
);
582 int copy_find_clock_array_field(FILE *err
,
583 const bt_event
*event
, const bt_event
*writer_event
,
584 const bt_field
*field
, bt_field_type
*type
,
585 const bt_field
*copy_field
)
588 const bt_field
*entry_field
= NULL
, *entry_copy
= NULL
;
590 count
= bt_field_type_array_get_length(type
);
591 for (i
= 0; i
< count
; i
++) {
592 entry_field
= bt_field_array_get_field(field
, i
);
595 BT_LOGE_STR("Failed to get array field.");
599 entry_copy
= bt_field_array_get_field(copy_field
, i
);
600 BT_ASSERT(entry_copy
);
602 ret
= copy_override_field(err
, event
, writer_event
, entry_field
,
606 BT_LOGE_STR("Failed to override field in array.");
609 BT_OBJECT_PUT_REF_AND_RESET(entry_field
);
610 BT_OBJECT_PUT_REF_AND_RESET(entry_copy
);
617 bt_object_put_ref(entry_field
);
618 bt_object_put_ref(entry_copy
);
625 int copy_find_clock_struct_field(FILE *err
,
626 const bt_event
*event
, const bt_event
*writer_event
,
627 const bt_field
*field
, bt_field_type
*type
,
628 const bt_field
*copy_field
)
631 bt_field_type
*entry_type
= NULL
;
632 const bt_field
*entry_field
= NULL
, *entry_copy
= NULL
;
634 count
= bt_field_type_structure_get_field_count(type
);
635 for (i
= 0; i
< count
; i
++) {
636 const char *entry_name
;
638 entry_field
= bt_field_structure_get_field_by_index(field
, i
);
640 BT_LOGE_STR("Failed to get struct field.");
644 ret
= bt_field_type_structure_get_field_by_index(type
, &entry_name
,
647 BT_LOGE_STR("Failed to get struct field.");
651 entry_copy
= bt_field_structure_get_field_by_index(copy_field
, i
);
652 BT_ASSERT(entry_copy
);
654 ret
= copy_override_field(err
, event
, writer_event
, entry_field
,
657 BT_LOGE_STR("Failed to override field in struct.");
660 BT_OBJECT_PUT_REF_AND_RESET(entry_copy
);
661 BT_OBJECT_PUT_REF_AND_RESET(entry_field
);
662 BT_OBJECT_PUT_REF_AND_RESET(entry_type
);
669 bt_object_put_ref(entry_type
);
670 bt_object_put_ref(entry_field
);
671 bt_object_put_ref(entry_copy
);
678 int set_int_value(FILE *err
, const bt_field
*field
,
679 const bt_field
*copy_field
,
686 if (bt_field_type_integer_is_signed(type
)) {
687 ret
= bt_field_signed_integer_get_value(field
, &value
);
689 BT_LOGE("Failed to get value.");
693 ret
= bt_field_signed_integer_set_value(copy_field
, value
);
696 BT_LOGE_STR("Failed to set signed integer value.");
700 ret
= bt_field_unsigned_integer_get_value(field
,
703 BT_LOGE("Failed to get value.");
707 ret
= bt_field_unsigned_integer_set_value(copy_field
, uvalue
);
710 BT_LOGE_STR("Failed to set unsigned integer value.");
723 const bt_clock_class
*stream_class_get_clock_class(FILE *err
,
724 const bt_stream_class
*stream_class
)
726 const bt_trace
*trace
= NULL
;
727 const bt_clock_class
*clock_class
= NULL
;
729 trace
= bt_stream_class_get_trace(stream_class
);
732 /* FIXME multi-clock? */
733 clock_class
= bt_trace_get_clock_class_by_index(trace
, 0);
735 bt_trace_put_ref(trace
);
740 const bt_clock_class
*event_get_clock_class(FILE *err
, const bt_event
*event
)
742 const bt_event_class
*event_class
= NULL
;
743 const bt_stream_class
*stream_class
= NULL
;
744 const bt_clock_class
*clock_class
= NULL
;
746 event_class
= bt_event_get_class(event
);
747 BT_ASSERT(event_class
);
749 stream_class
= bt_event_class_get_stream_class(event_class
);
750 BT_EVENT_CLASS_PUT_REF_AND_RESET(event_class
);
751 BT_ASSERT(stream_class
);
753 clock_class
= stream_class_get_clock_class(err
, stream_class
);
754 bt_stream_class_put_ref(stream_class
);
760 int copy_find_clock_int_field(FILE *err
,
761 const bt_event
*event
, const bt_event
*writer_event
,
762 const bt_field
*field
, bt_field_type
*type
,
763 const bt_field
*copy_field
)
765 const bt_clock_class
*clock_class
= NULL
, *writer_clock_class
= NULL
;
766 bt_clock_value
*clock_value
= NULL
, *writer_clock_value
= NULL
;
770 clock_class
= bt_field_type_integer_get_mapped_clock_class(type
);
772 return set_int_value(err
, field
, copy_field
, type
);
775 clock_value
= bt_event_get_clock_value(event
, clock_class
);
776 BT_CLOCK_CLASS_PUT_REF_AND_RESET(clock_class
);
777 BT_ASSERT(clock_value
);
779 ret
= bt_clock_value_get_value(clock_value
, &value
);
780 BT_OBJECT_PUT_REF_AND_RESET(clock_value
);
782 BT_LOGE("Failed to get clock value.");
786 ret
= bt_field_unsigned_integer_set_value(copy_field
, value
);
788 BT_LOGE_STR("Failed to set unsigned integer value.");
792 writer_clock_class
= event_get_clock_class(err
, writer_event
);
793 BT_ASSERT(writer_clock_class
);
795 writer_clock_value
= bt_clock_value_create(writer_clock_class
, value
);
796 BT_CLOCK_CLASS_PUT_REF_AND_RESET(writer_clock_class
);
797 if (!writer_clock_value
) {
798 BT_LOGE_STR("Failed to create clock value.");
802 ret
= bt_event_set_clock_value(writer_event
, writer_clock_value
);
803 BT_OBJECT_PUT_REF_AND_RESET(writer_clock_value
);
805 BT_LOGE_STR("Failed to set clock value.");