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
;
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_TYPE_ID_INTEGER
:
128 return update_header_clock_int_field_type(err
, type
,
130 case BT_CTF_TYPE_ID_STRUCT
:
131 return find_update_struct_clock_fields(err
, type
,
133 case BT_CTF_TYPE_ID_ARRAY
:
134 return find_update_array_clock_fields(err
, type
,
136 case BT_CTF_TYPE_ID_SEQUENCE
:
137 return find_update_sequence_clock_fields(err
, type
,
139 case BT_CTF_TYPE_ID_UNTAGGED_VARIANT
:
140 case BT_CTF_TYPE_ID_VARIANT
:
141 return find_update_variant_clock_fields(err
, type
,
143 case BT_CTF_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
)
162 count
= bt_ctf_field_type_variant_get_field_count(type
);
163 for (i
= 0; i
< count
; i
++) {
164 struct bt_ctf_field_type
*entry_type
;
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__
);
175 ret
= find_update_clock_fields(err
, entry_type
,
179 fprintf(err
, "[error] %s in %s:%d\n",
180 __func__
, __FILE__
, __LINE__
);
193 int find_update_struct_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
194 struct bt_ctf_clock_class
*writer_clock_class
)
198 count
= bt_ctf_field_type_structure_get_field_count(type
);
199 for (i
= 0; i
< count
; i
++) {
200 struct bt_ctf_field_type
*entry_type
;
201 const char *entry_name
;
203 ret
= bt_ctf_field_type_structure_get_field(type
,
204 &entry_name
, &entry_type
, i
);
206 fprintf(err
, "[error] %s in %s:%d\n",
207 __func__
, __FILE__
, __LINE__
);
211 ret
= find_update_clock_fields(err
, entry_type
,
215 fprintf(err
, "[error] %s in %s:%d\n",
216 __func__
, __FILE__
, __LINE__
);
229 int find_update_sequence_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
230 struct bt_ctf_clock_class
*writer_clock_class
)
233 struct bt_ctf_field_type
*entry_type
;
235 entry_type
= bt_ctf_field_type_sequence_get_element_type(type
);
236 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
239 fprintf(err
, "[error] %s in %s:%d\n",
240 __func__
, __FILE__
, __LINE__
);
252 int find_update_array_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
253 struct bt_ctf_clock_class
*writer_clock_class
)
256 struct bt_ctf_field_type
*entry_type
;
258 entry_type
= bt_ctf_field_type_array_get_element_type(type
);
259 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
262 fprintf(err
, "[error] %s in %s:%d\n",
263 __func__
, __FILE__
, __LINE__
);
275 int find_update_enum_clock_fields(FILE *err
, struct bt_ctf_field_type
*type
,
276 struct bt_ctf_clock_class
*writer_clock_class
)
279 struct bt_ctf_field_type
*entry_type
;
281 entry_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
282 ret
= find_update_clock_fields(err
, entry_type
, writer_clock_class
);
285 fprintf(err
, "[error] %s in %s:%d\n",
286 __func__
, __FILE__
, __LINE__
);
298 struct bt_ctf_field_type
*override_header_type(FILE *err
,
299 struct bt_ctf_field_type
*type
,
300 struct bt_ctf_trace
*writer_trace
)
302 struct bt_ctf_field_type
*new_type
= NULL
;
304 struct bt_ctf_clock_class
*writer_clock_class
;
306 /* FIXME multi-clock? */
307 writer_clock_class
= bt_ctf_trace_get_clock_class(writer_trace
, 0);
308 if (!writer_clock_class
) {
309 fprintf(err
, "[error] %s in %s:%d\n",
310 __func__
, __FILE__
, __LINE__
);
314 new_type
= bt_ctf_field_type_copy(type
);
316 fprintf(err
, "[error] %s in %s:%d\n",
317 __func__
, __FILE__
, __LINE__
);
321 if (bt_ctf_field_type_get_type_id(new_type
) != BT_CTF_TYPE_ID_STRUCT
) {
322 fprintf(err
, "[error] Unexpected header field type\n");
326 ret
= find_update_struct_clock_fields(err
, new_type
, writer_clock_class
);
328 fprintf(err
, "[error] %s in %s:%d\n",
329 __func__
, __FILE__
, __LINE__
);
338 bt_put(writer_clock_class
);
344 int copy_float_field(FILE *err
, struct bt_ctf_field
*field
,
345 struct bt_ctf_field_type
*type
,
346 struct bt_ctf_field
*copy_field
)
351 ret
= bt_ctf_field_floating_point_get_value(field
, &value
);
354 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
358 ret
= bt_ctf_field_floating_point_set_value(copy_field
, value
);
361 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
373 int copy_string_field(FILE *err
, struct bt_ctf_field
*field
,
374 struct bt_ctf_field_type
*type
,
375 struct bt_ctf_field
*copy_field
)
380 value
= bt_ctf_field_string_get_value(field
);
383 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
387 ret
= bt_ctf_field_string_set_value(copy_field
, value
);
390 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
402 int copy_override_field(FILE *err
, struct bt_ctf_event
*event
,
403 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
404 struct bt_ctf_field
*copy_field
)
406 struct bt_ctf_field_type
*type
;
409 type
= bt_ctf_field_get_type(field
);
412 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
417 switch (bt_ctf_field_type_get_type_id(type
)) {
418 case BT_CTF_TYPE_ID_INTEGER
:
419 ret
= copy_find_clock_int_field(err
, event
, writer_event
,
420 field
, type
, copy_field
);
422 case BT_CTF_TYPE_ID_STRUCT
:
423 ret
= copy_find_clock_struct_field(err
, event
, writer_event
,
424 field
, type
, copy_field
);
426 case BT_CTF_TYPE_ID_FLOAT
:
427 ret
= copy_float_field(err
, field
, type
, copy_field
);
429 case BT_CTF_TYPE_ID_ENUM
:
430 ret
= copy_find_clock_enum_field(err
, event
, writer_event
,
431 field
, type
, copy_field
);
433 case BT_CTF_TYPE_ID_STRING
:
434 ret
= copy_string_field(err
, field
, type
, copy_field
);
436 case BT_CTF_TYPE_ID_ARRAY
:
437 ret
= copy_find_clock_array_field(err
, event
, writer_event
,
438 field
, type
, copy_field
);
440 case BT_CTF_TYPE_ID_SEQUENCE
:
441 ret
= copy_find_clock_sequence_field(err
, event
, writer_event
,
442 field
, type
, copy_field
);
444 case BT_CTF_TYPE_ID_UNTAGGED_VARIANT
:
445 case BT_CTF_TYPE_ID_VARIANT
:
446 ret
= copy_find_clock_variant_field(err
, event
, writer_event
,
447 field
, type
, copy_field
);
449 /* No default, we want to catch missing field types. */
450 case BT_CTF_TYPE_ID_UNKNOWN
:
451 case BT_CTF_NR_TYPE_IDS
:
463 int copy_find_clock_enum_field(FILE *err
, struct bt_ctf_event
*event
,
464 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
465 struct bt_ctf_field_type
*type
, struct bt_ctf_field
*copy_field
)
468 struct bt_ctf_field
*container
, *copy_container
;
470 container
= bt_ctf_field_enumeration_get_container(field
);
473 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
478 copy_container
= bt_ctf_field_enumeration_get_container(copy_field
);
479 if (!copy_container
) {
481 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
483 goto end_put_container
;
486 ret
= copy_override_field(err
, event
, writer_event
, container
,
490 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
492 goto end_put_copy_container
;
495 end_put_copy_container
:
496 bt_put(copy_container
);
504 int copy_find_clock_variant_field(FILE *err
, struct bt_ctf_event
*event
,
505 struct bt_ctf_event
*writer_event
, struct bt_ctf_field
*field
,
506 struct bt_ctf_field_type
*type
, struct bt_ctf_field
*copy_field
)
509 struct bt_ctf_field
*tag
;
510 struct bt_ctf_field
*variant_field
, *copy_variant_field
;
512 tag
= bt_ctf_field_variant_get_tag(field
);
515 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
520 variant_field
= bt_ctf_field_variant_get_field(field
, tag
);
521 if (!variant_field
) {
523 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
528 copy_variant_field
= bt_ctf_field_variant_get_field(copy_field
, tag
);
529 if (!copy_variant_field
) {
530 bt_put(variant_field
);
532 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
534 goto end_put_variant_field
;
537 ret
= copy_override_field(err
, event
, writer_event
, variant_field
,
541 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
543 goto end_put_copy_variand_field
;
548 end_put_copy_variand_field
:
549 bt_put(copy_variant_field
);
550 end_put_variant_field
:
551 bt_put(variant_field
);
559 int copy_find_clock_sequence_field(FILE *err
,
560 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
561 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
562 struct bt_ctf_field
*copy_field
)
566 struct bt_ctf_field_type
*entry_type
;
567 struct bt_ctf_field
*length_field
;
569 entry_type
= bt_ctf_field_type_sequence_get_element_type(type
);
572 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
577 length_field
= bt_ctf_field_sequence_get_length(field
);
580 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
585 ret
= bt_ctf_field_unsigned_integer_get_value(length_field
, &count
);
587 bt_put(length_field
);
589 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
594 ret
= bt_ctf_field_sequence_set_length(copy_field
, length_field
);
595 bt_put(length_field
);
598 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
603 for (i
= 0; i
< count
; i
++) {
604 struct bt_ctf_field
*entry_field
, *entry_copy
;
606 entry_field
= bt_ctf_field_sequence_get_field(field
, i
);
609 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
614 entry_copy
= bt_ctf_field_sequence_get_field(copy_field
, i
);
617 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
622 ret
= copy_override_field(err
, event
, writer_event
, entry_field
,
628 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
642 int copy_find_clock_array_field(FILE *err
,
643 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
644 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
645 struct bt_ctf_field
*copy_field
)
648 struct bt_ctf_field_type
*entry_type
;
650 count
= bt_ctf_field_type_array_get_length(type
);
651 entry_type
= bt_ctf_field_type_array_get_element_type(type
);
654 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
658 for (i
= 0; i
< count
; i
++) {
659 struct bt_ctf_field
*entry_field
, *entry_copy
;
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
,
683 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
697 int copy_find_clock_struct_field(FILE *err
,
698 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
699 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
700 struct bt_ctf_field
*copy_field
)
704 count
= bt_ctf_field_type_structure_get_field_count(type
);
705 for (i
= 0; i
< count
; i
++) {
706 struct bt_ctf_field_type
*entry_type
;
707 struct bt_ctf_field
*entry_field
;
708 const char *entry_name
;
709 struct bt_ctf_field
*entry_copy
;
711 entry_field
= bt_ctf_field_structure_get_field_by_index(field
, i
);
714 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
719 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
);
734 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
739 ret
= copy_override_field(err
, event
, writer_event
, entry_field
,
748 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 printf(" - v(s) = %ld\n", value
);
778 ret
= bt_ctf_field_signed_integer_set_value(copy_field
, value
);
781 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
786 ret
= bt_ctf_field_unsigned_integer_get_value(field
,
790 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
794 ret
= bt_ctf_field_unsigned_integer_set_value(copy_field
, uvalue
);
797 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
808 struct bt_ctf_clock_class
*stream_class_get_clock_class(FILE *err
,
809 struct bt_ctf_stream_class
*stream_class
)
811 struct bt_ctf_trace
*trace
;
812 struct bt_ctf_clock_class
*clock_class
= NULL
;
814 trace
= bt_ctf_stream_class_get_trace(stream_class
);
816 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
821 /* FIXME multi-clock? */
822 clock_class
= bt_ctf_trace_get_clock_class(trace
, 0);
829 struct bt_ctf_clock_class
*event_get_clock_class(FILE *err
, struct bt_ctf_event
*event
)
831 struct bt_ctf_event_class
*event_class
;
832 struct bt_ctf_stream_class
*stream_class
;
833 struct bt_ctf_clock_class
*clock_class
= NULL
;
835 event_class
= bt_ctf_event_get_class(event
);
837 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
842 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
844 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
846 goto end_put_event_class
;
849 clock_class
= stream_class_get_clock_class(err
, stream_class
);
850 bt_put(stream_class
);
859 int copy_find_clock_int_field(FILE *err
,
860 struct bt_ctf_event
*event
, struct bt_ctf_event
*writer_event
,
861 struct bt_ctf_field
*field
, struct bt_ctf_field_type
*type
,
862 struct bt_ctf_field
*copy_field
)
864 struct bt_ctf_clock_class
*clock_class
, *writer_clock_class
;
865 struct bt_ctf_clock_value
*clock_value
, *writer_clock_value
;
869 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(type
);
871 return set_int_value(err
, field
, copy_field
, type
);
874 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
877 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
882 ret
= bt_ctf_clock_value_get_value(clock_value
, &value
);
885 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
890 ret
= bt_ctf_field_unsigned_integer_set_value(copy_field
, value
);
892 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
897 writer_clock_class
= event_get_clock_class(err
, writer_event
);
898 if (!writer_clock_class
) {
899 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
904 writer_clock_value
= bt_ctf_clock_value_create(writer_clock_class
, value
);
905 bt_put(writer_clock_class
);
906 if (!writer_clock_value
) {
907 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
912 ret
= bt_ctf_event_set_clock_value(writer_event
, writer_clock_value
);
913 bt_put(writer_clock_value
);
915 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,