8ae0e73bc4adaa2503c35c5090aa87a5206eac12
[babeltrace.git] / plugins / libctfcopytrace / clock-fields.c
1 /*
2 * clock-fields.c
3 *
4 * Babeltrace - Update clock fields to write uint64 values
5 *
6 * Copyright 2017 Julien Desfossez <jdesfossez@efficios.com>
7 *
8 * Author: Julien Desfossez <jdesfossez@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
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>
38 #include <assert.h>
39 #include <stdio.h>
40
41 #include "clock-fields.h"
42
43 static
44 int find_update_struct_clock_fields(FILE *err, struct bt_ctf_field_type *type,
45 struct bt_ctf_clock_class *writer_clock_class);
46 static
47 int find_update_array_clock_fields(FILE *err, struct bt_ctf_field_type *type,
48 struct bt_ctf_clock_class *writer_clock_class);
49 static
50 int find_update_enum_clock_fields(FILE *err, struct bt_ctf_field_type *type,
51 struct bt_ctf_clock_class *writer_clock_class);
52 static
53 int find_update_sequence_clock_fields(FILE *err, struct bt_ctf_field_type *type,
54 struct bt_ctf_clock_class *writer_clock_class);
55 static
56 int find_update_variant_clock_fields(FILE *err, struct bt_ctf_field_type *type,
57 struct bt_ctf_clock_class *writer_clock_class);
58
59 static
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);
64 static
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);
69 static
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);
74 static
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);
79 static
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);
83 static
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);
87
88 static
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)
91 {
92 struct bt_ctf_clock_class *clock = NULL;
93 int ret;
94
95 clock = bt_ctf_field_type_integer_get_mapped_clock_class(type);
96 if (!clock) {
97 return 0;
98 }
99 BT_PUT(clock);
100
101 ret = bt_ctf_field_type_integer_set_size(type, 64);
102 if (ret) {
103 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
104 __LINE__);
105 goto end;
106 }
107
108 ret = bt_ctf_field_type_integer_set_mapped_clock_class(type,
109 writer_clock_class);
110 if (ret) {
111 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
112 __LINE__);
113 goto end;
114 }
115
116 end:
117 return ret;
118 }
119
120 static
121 int find_update_clock_fields(FILE *err, struct bt_ctf_field_type *type,
122 struct bt_ctf_clock_class *writer_clock_class)
123 {
124 int ret;
125
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,
129 writer_clock_class);
130 case BT_CTF_FIELD_TYPE_ID_STRUCT:
131 return find_update_struct_clock_fields(err, type,
132 writer_clock_class);
133 case BT_CTF_FIELD_TYPE_ID_ARRAY:
134 return find_update_array_clock_fields(err, type,
135 writer_clock_class);
136 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
137 return find_update_sequence_clock_fields(err, type,
138 writer_clock_class);
139 case BT_CTF_FIELD_TYPE_ID_VARIANT:
140 return find_update_variant_clock_fields(err, type,
141 writer_clock_class);
142 case BT_CTF_FIELD_TYPE_ID_ENUM:
143 return find_update_enum_clock_fields(err, type,
144 writer_clock_class);
145 break;
146 default:
147 break;
148 }
149
150 ret = 0;
151
152 return ret;
153 }
154
155 static
156 int find_update_variant_clock_fields(FILE *err, struct bt_ctf_field_type *type,
157 struct bt_ctf_clock_class *writer_clock_class)
158 {
159 int count, i, ret;
160 struct bt_ctf_field_type *entry_type = NULL;
161
162 count = bt_ctf_field_type_variant_get_field_count(type);
163 for (i = 0; i < count; i++) {
164 const char *entry_name;
165
166 ret = bt_ctf_field_type_variant_get_field(type,
167 &entry_name, &entry_type, i);
168 if (ret) {
169 fprintf(err, "[error] %s in %s:%d\n",
170 __func__, __FILE__, __LINE__);
171 goto error;
172 }
173 ret = find_update_clock_fields(err, entry_type,
174 writer_clock_class);
175 if (ret) {
176 fprintf(err, "[error] %s in %s:%d\n",
177 __func__, __FILE__, __LINE__);
178 goto error;
179 }
180 BT_PUT(entry_type);
181 }
182
183 ret = 0;
184 goto end;
185
186 error:
187 ret = -1;
188 end:
189 bt_put(entry_type);
190 return ret;
191 }
192
193 static
194 int find_update_struct_clock_fields(FILE *err, struct bt_ctf_field_type *type,
195 struct bt_ctf_clock_class *writer_clock_class)
196 {
197 int count, i, ret;
198 struct bt_ctf_field_type *entry_type = NULL;
199
200 count = bt_ctf_field_type_structure_get_field_count(type);
201 for (i = 0; i < count; i++) {
202 const char *entry_name;
203
204 ret = bt_ctf_field_type_structure_get_field(type,
205 &entry_name, &entry_type, i);
206 if (ret) {
207 fprintf(err, "[error] %s in %s:%d\n",
208 __func__, __FILE__, __LINE__);
209 goto error;
210 }
211 ret = find_update_clock_fields(err, entry_type,
212 writer_clock_class);
213 if (ret) {
214 fprintf(err, "[error] %s in %s:%d\n",
215 __func__, __FILE__, __LINE__);
216 goto error;
217 }
218 BT_PUT(entry_type);
219 }
220
221 ret = 0;
222 goto end;
223
224 error:
225 bt_put(entry_type);
226 end:
227 return ret;
228 }
229
230 static
231 int find_update_sequence_clock_fields(FILE *err, struct bt_ctf_field_type *type,
232 struct bt_ctf_clock_class *writer_clock_class)
233 {
234 int ret;
235 struct bt_ctf_field_type *entry_type = NULL;
236
237 entry_type = bt_ctf_field_type_sequence_get_element_type(type);
238 if (!entry_type) {
239 fprintf(err, "[error] %s in %s:%d\n",
240 __func__, __FILE__, __LINE__);
241 goto error;
242 }
243 ret = find_update_clock_fields(err, entry_type, writer_clock_class);
244 BT_PUT(entry_type);
245 if (ret) {
246 fprintf(err, "[error] %s in %s:%d\n",
247 __func__, __FILE__, __LINE__);
248 goto error;
249 }
250
251 ret = 0;
252 goto end;
253
254 error:
255 ret = -1;
256 end:
257 return ret;
258 }
259
260 static
261 int find_update_array_clock_fields(FILE *err, struct bt_ctf_field_type *type,
262 struct bt_ctf_clock_class *writer_clock_class)
263 {
264 int ret;
265 struct bt_ctf_field_type *entry_type = NULL;
266
267 entry_type = bt_ctf_field_type_array_get_element_type(type);
268 if (!entry_type) {
269 fprintf(err, "[error] %s in %s:%d\n",
270 __func__, __FILE__, __LINE__);
271 goto error;
272 }
273 ret = find_update_clock_fields(err, entry_type, writer_clock_class);
274 BT_PUT(entry_type);
275 if (ret) {
276 fprintf(err, "[error] %s in %s:%d\n",
277 __func__, __FILE__, __LINE__);
278 ret = -1;
279 goto end;
280 }
281
282 ret = 0;
283 goto end;
284
285 error:
286 ret = -1;
287 end:
288 return ret;
289 }
290
291 static
292 int find_update_enum_clock_fields(FILE *err, struct bt_ctf_field_type *type,
293 struct bt_ctf_clock_class *writer_clock_class)
294 {
295 int ret;
296 struct bt_ctf_field_type *entry_type = NULL;
297
298 entry_type = bt_ctf_field_type_enumeration_get_container_type(type);
299 if (!entry_type) {
300 fprintf(err, "[error] %s in %s:%d\n",
301 __func__, __FILE__, __LINE__);
302 goto error;
303 }
304 ret = find_update_clock_fields(err, entry_type, writer_clock_class);
305 BT_PUT(entry_type);
306 if (ret) {
307 fprintf(err, "[error] %s in %s:%d\n",
308 __func__, __FILE__, __LINE__);
309 goto error;
310 }
311
312 ret = 0;
313 goto end;
314
315 error:
316 ret = -1;
317 end:
318 return ret;
319 }
320
321 BT_HIDDEN
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)
325 {
326 struct bt_ctf_field_type *new_type = NULL;
327 struct bt_ctf_clock_class *writer_clock_class = NULL;
328 int ret;
329
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__);
335 goto error;
336 }
337
338 new_type = bt_ctf_field_type_copy(type);
339 if (!new_type) {
340 fprintf(err, "[error] %s in %s:%d\n",
341 __func__, __FILE__, __LINE__);
342 goto error;
343 }
344
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");
347 goto error;
348 }
349
350 ret = find_update_struct_clock_fields(err, new_type, writer_clock_class);
351 if (ret) {
352 fprintf(err, "[error] %s in %s:%d\n",
353 __func__, __FILE__, __LINE__);
354 goto error;
355 }
356 BT_PUT(writer_clock_class);
357
358 goto end;
359
360 error:
361 bt_put(writer_clock_class);
362 BT_PUT(new_type);
363 end:
364 return new_type;
365 }
366
367 static
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)
371 {
372 double value;
373 int ret;
374
375 ret = bt_ctf_field_floating_point_get_value(field, &value);
376 if (ret) {
377 ret = -1;
378 fprintf(err, "[error] %s in %s:%d\n", __func__,
379 __FILE__, __LINE__);
380 goto end;
381 }
382 ret = bt_ctf_field_floating_point_set_value(copy_field, value);
383 if (ret) {
384 ret = -1;
385 fprintf(err, "[error] %s in %s:%d\n", __func__,
386 __FILE__, __LINE__);
387 goto end;
388 }
389
390 ret = 0;
391
392 end:
393 return ret;
394 }
395
396 static
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)
400 {
401 const char *value;
402 int ret;
403
404 value = bt_ctf_field_string_get_value(field);
405 if (!value) {
406 ret = -1;
407 fprintf(err, "[error] %s in %s:%d\n", __func__,
408 __FILE__, __LINE__);
409 goto end;
410 }
411 ret = bt_ctf_field_string_set_value(copy_field, value);
412 if (ret) {
413 ret = -1;
414 fprintf(err, "[error] %s in %s:%d\n", __func__,
415 __FILE__, __LINE__);
416 goto end;
417 }
418
419 ret = 0;
420
421 end:
422 return ret;
423 }
424
425 BT_HIDDEN
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)
429 {
430 struct bt_ctf_field_type *type = NULL;
431 int ret;
432
433 type = bt_ctf_field_get_type(field);
434 if (!type) {
435 ret = -1;
436 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
437 __LINE__);
438 goto end;
439 }
440
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);
445 break;
446 case BT_CTF_FIELD_TYPE_ID_STRUCT:
447 ret = copy_find_clock_struct_field(err, event, writer_event,
448 field, type, copy_field);
449 break;
450 case BT_CTF_FIELD_TYPE_ID_FLOAT:
451 ret = copy_float_field(err, field, type, copy_field);
452 break;
453 case BT_CTF_FIELD_TYPE_ID_ENUM:
454 ret = copy_find_clock_enum_field(err, event, writer_event,
455 field, type, copy_field);
456 break;
457 case BT_CTF_FIELD_TYPE_ID_STRING:
458 ret = copy_string_field(err, field, type, copy_field);
459 break;
460 case BT_CTF_FIELD_TYPE_ID_ARRAY:
461 ret = copy_find_clock_array_field(err, event, writer_event,
462 field, type, copy_field);
463 break;
464 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
465 ret = copy_find_clock_sequence_field(err, event, writer_event,
466 field, type, copy_field);
467 break;
468 case BT_CTF_FIELD_TYPE_ID_VARIANT:
469 ret = copy_find_clock_variant_field(err, event, writer_event,
470 field, type, copy_field);
471 break;
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:
475 break;
476 }
477
478 ret = 0;
479 BT_PUT(type);
480
481 end:
482 return ret;
483 }
484
485 static
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)
489 {
490 int ret;
491 struct bt_ctf_field *container = NULL, *copy_container = NULL;
492
493 container = bt_ctf_field_enumeration_get_container(field);
494 if (!container) {
495 ret = -1;
496 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
497 __LINE__);
498 goto end;
499 }
500
501 copy_container = bt_ctf_field_enumeration_get_container(copy_field);
502 if (!copy_container) {
503 ret = -1;
504 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
505 __LINE__);
506 goto error;
507 }
508
509 ret = copy_override_field(err, event, writer_event, container,
510 copy_container);
511 if (ret) {
512 ret = -1;
513 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
514 __LINE__);
515 goto error;
516 }
517
518 ret = 0;
519 goto end;
520
521 error:
522 ret = -1;
523 end:
524 bt_put(copy_container);
525 bt_put(container);
526 return ret;
527 }
528
529 static
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)
533 {
534 int ret;
535 struct bt_ctf_field *tag = NULL;
536 struct bt_ctf_field *variant_field = NULL, *copy_variant_field = NULL;
537
538 tag = bt_ctf_field_variant_get_tag(field);
539 if (!tag) {
540 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
541 __LINE__);
542 goto error;
543 }
544
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__,
548 __LINE__);
549 goto error;
550 }
551
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__,
555 __LINE__);
556 goto error;
557 }
558
559 ret = copy_override_field(err, event, writer_event, variant_field,
560 copy_variant_field);
561 if (ret) {
562 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
563 __LINE__);
564 goto error;
565 }
566
567 ret = 0;
568 goto end;
569
570 error:
571 ret = -1;
572 end:
573 bt_put(copy_variant_field);
574 bt_put(variant_field);
575 bt_put(tag);
576 return ret;
577 }
578
579 static
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)
584 {
585 int ret;
586 uint64_t i, count;
587 struct bt_ctf_field *length_field = NULL;
588 struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL;
589
590 length_field = bt_ctf_field_sequence_get_length(field);
591 if (!length_field) {
592 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
593 __LINE__);
594 goto error;
595 }
596
597 ret = bt_ctf_field_unsigned_integer_get_value(length_field, &count);
598 if (ret) {
599 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
600 __LINE__);
601 goto error;
602 }
603
604 ret = bt_ctf_field_sequence_set_length(copy_field, length_field);
605 if (ret) {
606 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
607 __LINE__);
608 goto error;
609 }
610 BT_PUT(length_field);
611
612 for (i = 0; i < count; i++) {
613 entry_field = bt_ctf_field_sequence_get_field(field, i);
614 if (!entry_field) {
615 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
616 __LINE__);
617 goto error;
618 }
619
620 entry_copy = bt_ctf_field_sequence_get_field(copy_field, i);
621 if (!entry_copy) {
622 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
623 __LINE__);
624 goto error;
625 }
626
627 ret = copy_override_field(err, event, writer_event, entry_field,
628 entry_copy);
629 if (ret) {
630 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
631 __LINE__);
632 goto error;
633 }
634 BT_PUT(entry_field);
635 BT_PUT(entry_copy);
636 }
637
638 ret = 0;
639 goto end;
640
641 error:
642 bt_put(length_field);
643 bt_put(entry_field);
644 bt_put(entry_copy);
645 ret = -1;
646 end:
647 return ret;
648 }
649
650 static
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)
655 {
656 int ret, count, i;
657 struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL;
658
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);
662 if (!entry_field) {
663 ret = -1;
664 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
665 __LINE__);
666 goto error;
667 }
668
669 entry_copy = bt_ctf_field_array_get_field(copy_field, i);
670 if (!entry_copy) {
671 ret = -1;
672 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
673 __LINE__);
674 goto error;
675 }
676
677 ret = copy_override_field(err, event, writer_event, entry_field,
678 entry_copy);
679 if (ret) {
680 ret = -1;
681 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
682 __LINE__);
683 goto error;
684 }
685 BT_PUT(entry_field);
686 BT_PUT(entry_copy);
687 }
688
689 ret = 0;
690 goto end;
691
692 error:
693 bt_put(entry_field);
694 bt_put(entry_copy);
695
696 end:
697 return ret;
698 }
699
700 static
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)
705 {
706 int count, i, ret;
707 struct bt_ctf_field_type *entry_type = NULL;
708 struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL;
709
710 count = bt_ctf_field_type_structure_get_field_count(type);
711 for (i = 0; i < count; i++) {
712 const char *entry_name;
713
714 entry_field = bt_ctf_field_structure_get_field_by_index(field, i);
715 if (!entry_field) {
716 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
717 __LINE__);
718 goto error;
719 }
720
721 ret = bt_ctf_field_type_structure_get_field(type, &entry_name,
722 &entry_type, i);
723 if (ret) {
724 fprintf(err, "[error] %s in %s:%d\n",
725 __func__, __FILE__, __LINE__);
726 goto error;
727 }
728
729 entry_copy = bt_ctf_field_structure_get_field_by_index(copy_field, i);
730 if (!entry_copy) {
731 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
732 __LINE__);
733 goto error;
734 }
735
736 ret = copy_override_field(err, event, writer_event, entry_field,
737 entry_copy);
738 if (ret) {
739 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
740 __LINE__);
741 goto error;
742 }
743 BT_PUT(entry_copy);
744 BT_PUT(entry_field);
745 BT_PUT(entry_type);
746 }
747
748 ret = 0;
749 goto end;
750
751 error:
752 bt_put(entry_type);
753 bt_put(entry_field);
754 bt_put(entry_copy);
755 ret = -1;
756 end:
757 return ret;
758 }
759
760 static
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)
764 {
765 uint64_t uvalue;
766 int64_t value;
767 int ret;
768
769 if (bt_ctf_field_type_integer_get_signed(type)) {
770 ret = bt_ctf_field_signed_integer_get_value(field, &value);
771 if (ret) {
772 ret = -1;
773 fprintf(err, "[error] %s in %s:%d\n", __func__,
774 __FILE__, __LINE__);
775 goto end;
776 }
777 ret = bt_ctf_field_signed_integer_set_value(copy_field, value);
778 if (ret) {
779 ret = -1;
780 fprintf(err, "[error] %s in %s:%d\n", __func__,
781 __FILE__, __LINE__);
782 goto end;
783 }
784 } else {
785 ret = bt_ctf_field_unsigned_integer_get_value(field,
786 &uvalue);
787 if (ret) {
788 ret = -1;
789 fprintf(err, "[error] %s in %s:%d\n", __func__,
790 __FILE__, __LINE__);
791 goto end;
792 }
793 ret = bt_ctf_field_unsigned_integer_set_value(copy_field, uvalue);
794 if (ret) {
795 ret = -1;
796 fprintf(err, "[error] %s in %s:%d\n", __func__,
797 __FILE__, __LINE__);
798 goto end;
799 }
800 }
801 ret = 0;
802
803 end:
804 return ret;
805 }
806
807 struct bt_ctf_clock_class *stream_class_get_clock_class(FILE *err,
808 struct bt_ctf_stream_class *stream_class)
809 {
810 struct bt_ctf_trace *trace = NULL;
811 struct bt_ctf_clock_class *clock_class = NULL;
812
813 trace = bt_ctf_stream_class_get_trace(stream_class);
814 if (!trace) {
815 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
816 __LINE__);
817 goto end;
818 }
819
820 /* FIXME multi-clock? */
821 clock_class = bt_ctf_trace_get_clock_class_by_index(trace, 0);
822
823 bt_put(trace);
824 end:
825 return clock_class;
826 }
827
828 struct bt_ctf_clock_class *event_get_clock_class(FILE *err, struct bt_ctf_event *event)
829 {
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;
833
834 event_class = bt_ctf_event_get_class(event);
835 if (!event_class) {
836 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
837 __LINE__);
838 goto end;
839 }
840
841 stream_class = bt_ctf_event_class_get_stream_class(event_class);
842 BT_PUT(event_class);
843 if (!stream_class) {
844 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
845 __LINE__);
846 goto end;
847 }
848
849 clock_class = stream_class_get_clock_class(err, stream_class);
850 bt_put(stream_class);
851
852 end:
853 return clock_class;
854 }
855
856 static
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)
861 {
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;
864 uint64_t value;
865 int ret;
866
867 clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(type);
868 if (!clock_class) {
869 return set_int_value(err, field, copy_field, type);
870 }
871
872 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
873 BT_PUT(clock_class);
874 if (!clock_value) {
875 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
876 __LINE__);
877 goto error;
878 }
879
880 ret = bt_ctf_clock_value_get_value(clock_value, &value);
881 BT_PUT(clock_value);
882 if (ret) {
883 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
884 __LINE__);
885 goto error;
886 }
887
888 ret = bt_ctf_field_unsigned_integer_set_value(copy_field, value);
889 if (ret) {
890 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
891 __LINE__);
892 goto error;
893 }
894
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__,
898 __LINE__);
899 goto error;
900 }
901
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__,
906 __LINE__);
907 goto error;
908 }
909
910 ret = bt_ctf_event_set_clock_value(writer_event, writer_clock_value);
911 BT_PUT(writer_clock_value);
912 if (ret) {
913 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
914 __LINE__);
915 goto error;
916 }
917
918 ret = 0;
919 goto end;
920
921 error:
922 ret = -1;
923 end:
924 return ret;
925 }
926
This page took 0.047914 seconds and 3 git commands to generate.