cleanup error paths in trimmer, writer and ctfcopytrace
[babeltrace.git] / plugins / libctfcopytrace / clock-fields.c
CommitLineData
b2f1f465
JD
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
43static
44int find_update_struct_clock_fields(FILE *err, struct bt_ctf_field_type *type,
45 struct bt_ctf_clock_class *writer_clock_class);
46static
47int find_update_array_clock_fields(FILE *err, struct bt_ctf_field_type *type,
48 struct bt_ctf_clock_class *writer_clock_class);
49static
50int find_update_enum_clock_fields(FILE *err, struct bt_ctf_field_type *type,
51 struct bt_ctf_clock_class *writer_clock_class);
52static
53int find_update_sequence_clock_fields(FILE *err, struct bt_ctf_field_type *type,
54 struct bt_ctf_clock_class *writer_clock_class);
55static
56int find_update_variant_clock_fields(FILE *err, struct bt_ctf_field_type *type,
57 struct bt_ctf_clock_class *writer_clock_class);
58
59static
60int 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);
64static
65int 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);
69static
70int 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);
74static
75int 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);
79static
80int 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);
83static
84int 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
88static
89int update_header_clock_int_field_type(FILE *err, struct bt_ctf_field_type *type,
90 struct bt_ctf_clock_class *writer_clock_class)
91{
9ae49d3d 92 struct bt_ctf_clock_class *clock = NULL;
b2f1f465
JD
93 int ret;
94
95 clock = bt_ctf_field_type_integer_get_mapped_clock_class(type);
96 if (!clock) {
97 return 0;
98 }
9ae49d3d 99 BT_PUT(clock);
b2f1f465
JD
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
116end:
117 return ret;
118}
119
120static
121int 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_TYPE_ID_INTEGER:
128 return update_header_clock_int_field_type(err, type,
129 writer_clock_class);
130 case BT_CTF_TYPE_ID_STRUCT:
131 return find_update_struct_clock_fields(err, type,
132 writer_clock_class);
133 case BT_CTF_TYPE_ID_ARRAY:
134 return find_update_array_clock_fields(err, type,
135 writer_clock_class);
136 case BT_CTF_TYPE_ID_SEQUENCE:
137 return find_update_sequence_clock_fields(err, type,
138 writer_clock_class);
139 case BT_CTF_TYPE_ID_UNTAGGED_VARIANT:
140 case BT_CTF_TYPE_ID_VARIANT:
141 return find_update_variant_clock_fields(err, type,
142 writer_clock_class);
143 case BT_CTF_TYPE_ID_ENUM:
144 return find_update_enum_clock_fields(err, type,
145 writer_clock_class);
146 break;
147 default:
148 break;
149 }
150
151 ret = 0;
152
153 return ret;
154}
155
156static
157int find_update_variant_clock_fields(FILE *err, struct bt_ctf_field_type *type,
158 struct bt_ctf_clock_class *writer_clock_class)
159{
160 int count, i, ret;
9ae49d3d 161 struct bt_ctf_field_type *entry_type = NULL;
b2f1f465
JD
162
163 count = bt_ctf_field_type_variant_get_field_count(type);
164 for (i = 0; i < count; i++) {
b2f1f465
JD
165 const char *entry_name;
166
167 ret = bt_ctf_field_type_variant_get_field(type,
168 &entry_name, &entry_type, i);
169 if (ret) {
170 fprintf(err, "[error] %s in %s:%d\n",
171 __func__, __FILE__, __LINE__);
9ae49d3d 172 goto error;
b2f1f465
JD
173 }
174 ret = find_update_clock_fields(err, entry_type,
175 writer_clock_class);
b2f1f465
JD
176 if (ret) {
177 fprintf(err, "[error] %s in %s:%d\n",
178 __func__, __FILE__, __LINE__);
9ae49d3d 179 goto error;
b2f1f465 180 }
9ae49d3d 181 BT_PUT(entry_type);
b2f1f465
JD
182 }
183
184 ret = 0;
9ae49d3d 185 goto end;
b2f1f465 186
9ae49d3d
JD
187error:
188 ret = -1;
b2f1f465 189end:
9ae49d3d 190 bt_put(entry_type);
b2f1f465
JD
191 return ret;
192}
193
194static
195int find_update_struct_clock_fields(FILE *err, struct bt_ctf_field_type *type,
196 struct bt_ctf_clock_class *writer_clock_class)
197{
198 int count, i, ret;
9ae49d3d 199 struct bt_ctf_field_type *entry_type = NULL;
b2f1f465
JD
200
201 count = bt_ctf_field_type_structure_get_field_count(type);
202 for (i = 0; i < count; i++) {
b2f1f465
JD
203 const char *entry_name;
204
205 ret = bt_ctf_field_type_structure_get_field(type,
206 &entry_name, &entry_type, i);
207 if (ret) {
208 fprintf(err, "[error] %s in %s:%d\n",
209 __func__, __FILE__, __LINE__);
9ae49d3d 210 goto error;
b2f1f465
JD
211 }
212 ret = find_update_clock_fields(err, entry_type,
213 writer_clock_class);
b2f1f465
JD
214 if (ret) {
215 fprintf(err, "[error] %s in %s:%d\n",
216 __func__, __FILE__, __LINE__);
9ae49d3d 217 goto error;
b2f1f465 218 }
9ae49d3d 219 BT_PUT(entry_type);
b2f1f465
JD
220 }
221
222 ret = 0;
9ae49d3d 223 goto end;
b2f1f465 224
9ae49d3d
JD
225error:
226 bt_put(entry_type);
b2f1f465
JD
227end:
228 return ret;
229}
230
231static
232int find_update_sequence_clock_fields(FILE *err, struct bt_ctf_field_type *type,
233 struct bt_ctf_clock_class *writer_clock_class)
234{
235 int ret;
9ae49d3d 236 struct bt_ctf_field_type *entry_type = NULL;
b2f1f465
JD
237
238 entry_type = bt_ctf_field_type_sequence_get_element_type(type);
9ae49d3d
JD
239 if (!entry_type) {
240 fprintf(err, "[error] %s in %s:%d\n",
241 __func__, __FILE__, __LINE__);
242 goto error;
243 }
b2f1f465 244 ret = find_update_clock_fields(err, entry_type, writer_clock_class);
9ae49d3d 245 BT_PUT(entry_type);
b2f1f465
JD
246 if (ret) {
247 fprintf(err, "[error] %s in %s:%d\n",
248 __func__, __FILE__, __LINE__);
9ae49d3d 249 goto error;
b2f1f465
JD
250 }
251
252 ret = 0;
9ae49d3d 253 goto end;
b2f1f465 254
9ae49d3d
JD
255error:
256 ret = -1;
b2f1f465
JD
257end:
258 return ret;
259}
260
261static
262int find_update_array_clock_fields(FILE *err, struct bt_ctf_field_type *type,
263 struct bt_ctf_clock_class *writer_clock_class)
264{
265 int ret;
9ae49d3d 266 struct bt_ctf_field_type *entry_type = NULL;
b2f1f465
JD
267
268 entry_type = bt_ctf_field_type_array_get_element_type(type);
9ae49d3d
JD
269 if (!entry_type) {
270 fprintf(err, "[error] %s in %s:%d\n",
271 __func__, __FILE__, __LINE__);
272 goto error;
273 }
b2f1f465 274 ret = find_update_clock_fields(err, entry_type, writer_clock_class);
9ae49d3d 275 BT_PUT(entry_type);
b2f1f465
JD
276 if (ret) {
277 fprintf(err, "[error] %s in %s:%d\n",
278 __func__, __FILE__, __LINE__);
279 ret = -1;
280 goto end;
281 }
282
283 ret = 0;
9ae49d3d 284 goto end;
b2f1f465 285
9ae49d3d
JD
286error:
287 ret = -1;
b2f1f465
JD
288end:
289 return ret;
290}
291
292static
293int find_update_enum_clock_fields(FILE *err, struct bt_ctf_field_type *type,
294 struct bt_ctf_clock_class *writer_clock_class)
295{
296 int ret;
9ae49d3d 297 struct bt_ctf_field_type *entry_type = NULL;
b2f1f465
JD
298
299 entry_type = bt_ctf_field_type_enumeration_get_container_type(type);
9ae49d3d
JD
300 if (!entry_type) {
301 fprintf(err, "[error] %s in %s:%d\n",
302 __func__, __FILE__, __LINE__);
303 goto error;
304 }
b2f1f465 305 ret = find_update_clock_fields(err, entry_type, writer_clock_class);
9ae49d3d 306 BT_PUT(entry_type);
b2f1f465
JD
307 if (ret) {
308 fprintf(err, "[error] %s in %s:%d\n",
309 __func__, __FILE__, __LINE__);
9ae49d3d 310 goto error;
b2f1f465
JD
311 }
312
313 ret = 0;
9ae49d3d 314 goto end;
b2f1f465 315
9ae49d3d
JD
316error:
317 ret = -1;
b2f1f465
JD
318end:
319 return ret;
320}
321
322BT_HIDDEN
323struct bt_ctf_field_type *override_header_type(FILE *err,
324 struct bt_ctf_field_type *type,
325 struct bt_ctf_trace *writer_trace)
326{
327 struct bt_ctf_field_type *new_type = NULL;
9ae49d3d 328 struct bt_ctf_clock_class *writer_clock_class = NULL;
b2f1f465 329 int ret;
b2f1f465
JD
330
331 /* FIXME multi-clock? */
332 writer_clock_class = bt_ctf_trace_get_clock_class(writer_trace, 0);
333 if (!writer_clock_class) {
334 fprintf(err, "[error] %s in %s:%d\n",
335 __func__, __FILE__, __LINE__);
9ae49d3d 336 goto error;
b2f1f465
JD
337 }
338
339 new_type = bt_ctf_field_type_copy(type);
340 if (!new_type) {
341 fprintf(err, "[error] %s in %s:%d\n",
342 __func__, __FILE__, __LINE__);
9ae49d3d 343 goto error;
b2f1f465
JD
344 }
345
346 if (bt_ctf_field_type_get_type_id(new_type) != BT_CTF_TYPE_ID_STRUCT) {
347 fprintf(err, "[error] Unexpected header field type\n");
348 goto error;
349 }
350
351 ret = find_update_struct_clock_fields(err, new_type, writer_clock_class);
352 if (ret) {
353 fprintf(err, "[error] %s in %s:%d\n",
354 __func__, __FILE__, __LINE__);
355 goto error;
356 }
9ae49d3d 357 BT_PUT(writer_clock_class);
b2f1f465 358
9ae49d3d 359 goto end;
b2f1f465
JD
360
361error:
b2f1f465 362 bt_put(writer_clock_class);
9ae49d3d 363 BT_PUT(new_type);
b2f1f465
JD
364end:
365 return new_type;
366}
367
368static
369int copy_float_field(FILE *err, struct bt_ctf_field *field,
370 struct bt_ctf_field_type *type,
371 struct bt_ctf_field *copy_field)
372{
373 double value;
374 int ret;
375
376 ret = bt_ctf_field_floating_point_get_value(field, &value);
377 if (ret) {
378 ret = -1;
379 fprintf(err, "[error] %s in %s:%d\n", __func__,
380 __FILE__, __LINE__);
381 goto end;
382 }
383 ret = bt_ctf_field_floating_point_set_value(copy_field, value);
384 if (ret) {
385 ret = -1;
386 fprintf(err, "[error] %s in %s:%d\n", __func__,
387 __FILE__, __LINE__);
388 goto end;
389 }
390
391 ret = 0;
392
393end:
394 return ret;
395}
396
397static
398int copy_string_field(FILE *err, struct bt_ctf_field *field,
399 struct bt_ctf_field_type *type,
400 struct bt_ctf_field *copy_field)
401{
402 const char *value;
403 int ret;
404
405 value = bt_ctf_field_string_get_value(field);
406 if (!value) {
407 ret = -1;
408 fprintf(err, "[error] %s in %s:%d\n", __func__,
409 __FILE__, __LINE__);
410 goto end;
411 }
412 ret = bt_ctf_field_string_set_value(copy_field, value);
413 if (ret) {
414 ret = -1;
415 fprintf(err, "[error] %s in %s:%d\n", __func__,
416 __FILE__, __LINE__);
417 goto end;
418 }
419
420 ret = 0;
421
422end:
423 return ret;
424}
425
426BT_HIDDEN
427int copy_override_field(FILE *err, struct bt_ctf_event *event,
428 struct bt_ctf_event *writer_event, struct bt_ctf_field *field,
429 struct bt_ctf_field *copy_field)
430{
9ae49d3d 431 struct bt_ctf_field_type *type = NULL;
b2f1f465
JD
432 int ret;
433
434 type = bt_ctf_field_get_type(field);
435 if (!type) {
436 ret = -1;
437 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
438 __LINE__);
439 goto end;
440 }
441
442 switch (bt_ctf_field_type_get_type_id(type)) {
443 case BT_CTF_TYPE_ID_INTEGER:
444 ret = copy_find_clock_int_field(err, event, writer_event,
445 field, type, copy_field);
446 break;
447 case BT_CTF_TYPE_ID_STRUCT:
448 ret = copy_find_clock_struct_field(err, event, writer_event,
449 field, type, copy_field);
450 break;
451 case BT_CTF_TYPE_ID_FLOAT:
452 ret = copy_float_field(err, field, type, copy_field);
453 break;
454 case BT_CTF_TYPE_ID_ENUM:
455 ret = copy_find_clock_enum_field(err, event, writer_event,
456 field, type, copy_field);
457 break;
458 case BT_CTF_TYPE_ID_STRING:
459 ret = copy_string_field(err, field, type, copy_field);
460 break;
461 case BT_CTF_TYPE_ID_ARRAY:
462 ret = copy_find_clock_array_field(err, event, writer_event,
463 field, type, copy_field);
464 break;
465 case BT_CTF_TYPE_ID_SEQUENCE:
466 ret = copy_find_clock_sequence_field(err, event, writer_event,
467 field, type, copy_field);
468 break;
469 case BT_CTF_TYPE_ID_UNTAGGED_VARIANT:
470 case BT_CTF_TYPE_ID_VARIANT:
471 ret = copy_find_clock_variant_field(err, event, writer_event,
472 field, type, copy_field);
473 break;
474 /* No default, we want to catch missing field types. */
475 case BT_CTF_TYPE_ID_UNKNOWN:
476 case BT_CTF_NR_TYPE_IDS:
477 break;
478 }
479
480 ret = 0;
9ae49d3d 481 BT_PUT(type);
b2f1f465
JD
482
483end:
484 return ret;
485}
486
487static
488int copy_find_clock_enum_field(FILE *err, struct bt_ctf_event *event,
489 struct bt_ctf_event *writer_event, struct bt_ctf_field *field,
490 struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field)
491{
492 int ret;
9ae49d3d 493 struct bt_ctf_field *container = NULL, *copy_container = NULL;
b2f1f465
JD
494
495 container = bt_ctf_field_enumeration_get_container(field);
496 if (!container) {
497 ret = -1;
498 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
499 __LINE__);
500 goto end;
501 }
502
503 copy_container = bt_ctf_field_enumeration_get_container(copy_field);
504 if (!copy_container) {
505 ret = -1;
506 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
507 __LINE__);
9ae49d3d 508 goto error;
b2f1f465
JD
509 }
510
511 ret = copy_override_field(err, event, writer_event, container,
512 copy_container);
513 if (ret) {
514 ret = -1;
515 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
516 __LINE__);
9ae49d3d 517 goto error;
b2f1f465 518 }
9ae49d3d
JD
519
520 ret = 0;
521 goto end;
b2f1f465 522
9ae49d3d
JD
523error:
524 ret = -1;
525end:
b2f1f465 526 bt_put(copy_container);
b2f1f465 527 bt_put(container);
b2f1f465
JD
528 return ret;
529}
530
531static
532int copy_find_clock_variant_field(FILE *err, struct bt_ctf_event *event,
533 struct bt_ctf_event *writer_event, struct bt_ctf_field *field,
534 struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field)
535{
536 int ret;
9ae49d3d
JD
537 struct bt_ctf_field *tag = NULL;
538 struct bt_ctf_field *variant_field = NULL, *copy_variant_field = NULL;
b2f1f465
JD
539
540 tag = bt_ctf_field_variant_get_tag(field);
541 if (!tag) {
b2f1f465
JD
542 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
543 __LINE__);
9ae49d3d 544 goto error;
b2f1f465
JD
545 }
546
547 variant_field = bt_ctf_field_variant_get_field(field, tag);
548 if (!variant_field) {
b2f1f465
JD
549 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
550 __LINE__);
9ae49d3d 551 goto error;
b2f1f465
JD
552 }
553
554 copy_variant_field = bt_ctf_field_variant_get_field(copy_field, tag);
555 if (!copy_variant_field) {
b2f1f465
JD
556 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
557 __LINE__);
9ae49d3d 558 goto error;
b2f1f465
JD
559 }
560
561 ret = copy_override_field(err, event, writer_event, variant_field,
562 copy_variant_field);
563 if (ret) {
b2f1f465
JD
564 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
565 __LINE__);
9ae49d3d 566 goto error;
b2f1f465
JD
567 }
568
569 ret = 0;
9ae49d3d 570 goto end;
b2f1f465 571
9ae49d3d
JD
572error:
573 ret = -1;
574end:
b2f1f465 575 bt_put(copy_variant_field);
b2f1f465 576 bt_put(variant_field);
b2f1f465 577 bt_put(tag);
b2f1f465
JD
578 return ret;
579}
580
581static
582int copy_find_clock_sequence_field(FILE *err,
583 struct bt_ctf_event *event, struct bt_ctf_event *writer_event,
584 struct bt_ctf_field *field, struct bt_ctf_field_type *type,
585 struct bt_ctf_field *copy_field)
586{
587 int ret;
588 uint64_t i, count;
9ae49d3d
JD
589 struct bt_ctf_field *length_field = NULL;
590 struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL;
b2f1f465
JD
591
592 length_field = bt_ctf_field_sequence_get_length(field);
593 if (!length_field) {
b2f1f465
JD
594 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
595 __LINE__);
9ae49d3d 596 goto error;
b2f1f465
JD
597 }
598
599 ret = bt_ctf_field_unsigned_integer_get_value(length_field, &count);
600 if (ret) {
b2f1f465
JD
601 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
602 __LINE__);
9ae49d3d 603 goto error;
b2f1f465
JD
604 }
605
606 ret = bt_ctf_field_sequence_set_length(copy_field, length_field);
b2f1f465 607 if (ret) {
b2f1f465
JD
608 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
609 __LINE__);
9ae49d3d 610 goto error;
b2f1f465 611 }
9ae49d3d 612 BT_PUT(length_field);
b2f1f465
JD
613
614 for (i = 0; i < count; i++) {
b2f1f465
JD
615 entry_field = bt_ctf_field_sequence_get_field(field, i);
616 if (!entry_field) {
b2f1f465
JD
617 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
618 __LINE__);
9ae49d3d 619 goto error;
b2f1f465
JD
620 }
621
622 entry_copy = bt_ctf_field_sequence_get_field(copy_field, i);
623 if (!entry_copy) {
b2f1f465
JD
624 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
625 __LINE__);
9ae49d3d 626 goto error;
b2f1f465
JD
627 }
628
629 ret = copy_override_field(err, event, writer_event, entry_field,
630 entry_copy);
b2f1f465 631 if (ret) {
b2f1f465
JD
632 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
633 __LINE__);
9ae49d3d 634 goto error;
b2f1f465 635 }
9ae49d3d
JD
636 BT_PUT(entry_field);
637 BT_PUT(entry_copy);
b2f1f465
JD
638 }
639
b2f1f465 640 ret = 0;
9ae49d3d 641 goto end;
b2f1f465 642
9ae49d3d
JD
643error:
644 bt_put(length_field);
645 bt_put(entry_field);
646 bt_put(entry_copy);
647 ret = -1;
b2f1f465
JD
648end:
649 return ret;
650}
651
652static
653int copy_find_clock_array_field(FILE *err,
654 struct bt_ctf_event *event, struct bt_ctf_event *writer_event,
655 struct bt_ctf_field *field, struct bt_ctf_field_type *type,
656 struct bt_ctf_field *copy_field)
657{
658 int ret, count, i;
9ae49d3d 659 struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL;
b2f1f465
JD
660
661 count = bt_ctf_field_type_array_get_length(type);
b2f1f465 662 for (i = 0; i < count; i++) {
b2f1f465
JD
663 entry_field = bt_ctf_field_array_get_field(field, i);
664 if (!entry_field) {
b2f1f465
JD
665 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
666 __LINE__);
9ae49d3d 667 goto error;
b2f1f465
JD
668 }
669
670 entry_copy = bt_ctf_field_array_get_field(copy_field, i);
671 if (!entry_copy) {
b2f1f465
JD
672 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
673 __LINE__);
9ae49d3d 674 goto error;
b2f1f465
JD
675 }
676
677 ret = copy_override_field(err, event, writer_event, entry_field,
678 entry_copy);
b2f1f465
JD
679 if (ret) {
680 ret = -1;
681 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
682 __LINE__);
9ae49d3d 683 goto error;
b2f1f465 684 }
9ae49d3d
JD
685 BT_PUT(entry_field);
686 BT_PUT(entry_copy);
b2f1f465
JD
687 }
688
b2f1f465 689 ret = 0;
9ae49d3d
JD
690 goto end;
691
692error:
693 bt_put(entry_field);
694 bt_put(entry_copy);
b2f1f465
JD
695
696end:
697 return ret;
698}
699
700static
701int 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;
9ae49d3d
JD
707 struct bt_ctf_field_type *entry_type = NULL;
708 struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL;
b2f1f465
JD
709
710 count = bt_ctf_field_type_structure_get_field_count(type);
711 for (i = 0; i < count; i++) {
b2f1f465 712 const char *entry_name;
b2f1f465
JD
713
714 entry_field = bt_ctf_field_structure_get_field_by_index(field, i);
715 if (!entry_field) {
b2f1f465
JD
716 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
717 __LINE__);
9ae49d3d 718 goto error;
b2f1f465
JD
719 }
720
721 ret = bt_ctf_field_type_structure_get_field(type, &entry_name,
722 &entry_type, i);
723 if (ret) {
b2f1f465
JD
724 fprintf(err, "[error] %s in %s:%d\n",
725 __func__, __FILE__, __LINE__);
9ae49d3d 726 goto error;
b2f1f465
JD
727 }
728
729 entry_copy = bt_ctf_field_structure_get_field_by_index(copy_field, i);
730 if (!entry_copy) {
b2f1f465
JD
731 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
732 __LINE__);
9ae49d3d 733 goto error;
b2f1f465
JD
734 }
735
736 ret = copy_override_field(err, event, writer_event, entry_field,
737 entry_copy);
b2f1f465 738 if (ret) {
b2f1f465
JD
739 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
740 __LINE__);
9ae49d3d 741 goto error;
b2f1f465 742 }
9ae49d3d
JD
743 BT_PUT(entry_copy);
744 BT_PUT(entry_field);
745 BT_PUT(entry_type);
b2f1f465
JD
746 }
747
748 ret = 0;
9ae49d3d 749 goto end;
b2f1f465 750
9ae49d3d
JD
751error:
752 bt_put(entry_type);
753 bt_put(entry_field);
754 bt_put(entry_copy);
755 ret = -1;
b2f1f465
JD
756end:
757 return ret;
758}
759
760static
761int 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 }
b2f1f465
JD
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
803end:
804 return ret;
805}
806
807struct bt_ctf_clock_class *stream_class_get_clock_class(FILE *err,
808 struct bt_ctf_stream_class *stream_class)
809{
9ae49d3d 810 struct bt_ctf_trace *trace = NULL;
b2f1f465
JD
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(trace, 0);
822
823 bt_put(trace);
824end:
825 return clock_class;
826}
827
828struct bt_ctf_clock_class *event_get_clock_class(FILE *err, struct bt_ctf_event *event)
829{
9ae49d3d
JD
830 struct bt_ctf_event_class *event_class = NULL;
831 struct bt_ctf_stream_class *stream_class = NULL;
b2f1f465
JD
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);
9ae49d3d 842 BT_PUT(event_class);
b2f1f465
JD
843 if (!stream_class) {
844 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
845 __LINE__);
9ae49d3d 846 goto end;
b2f1f465
JD
847 }
848
849 clock_class = stream_class_get_clock_class(err, stream_class);
850 bt_put(stream_class);
851
b2f1f465
JD
852end:
853 return clock_class;
854}
855
856static
857int 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{
9ae49d3d
JD
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;
b2f1f465
JD
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);
9ae49d3d 873 BT_PUT(clock_class);
b2f1f465
JD
874 if (!clock_value) {
875 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
876 __LINE__);
9ae49d3d 877 goto error;
b2f1f465
JD
878 }
879
880 ret = bt_ctf_clock_value_get_value(clock_value, &value);
9ae49d3d 881 BT_PUT(clock_value);
b2f1f465
JD
882 if (ret) {
883 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
884 __LINE__);
9ae49d3d 885 goto error;
b2f1f465
JD
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__);
9ae49d3d 892 goto error;
b2f1f465
JD
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__);
9ae49d3d 899 goto error;
b2f1f465
JD
900 }
901
902 writer_clock_value = bt_ctf_clock_value_create(writer_clock_class, value);
9ae49d3d 903 BT_PUT(writer_clock_class);
b2f1f465
JD
904 if (!writer_clock_value) {
905 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
906 __LINE__);
9ae49d3d 907 goto error;
b2f1f465
JD
908 }
909
910 ret = bt_ctf_event_set_clock_value(writer_event, writer_clock_value);
9ae49d3d 911 BT_PUT(writer_clock_value);
b2f1f465
JD
912 if (ret) {
913 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
914 __LINE__);
9ae49d3d 915 goto error;
b2f1f465
JD
916 }
917
918 ret = 0;
9ae49d3d 919 goto end;
b2f1f465 920
9ae49d3d
JD
921error:
922 ret = -1;
b2f1f465
JD
923end:
924 return ret;
925}
926
This page took 0.061108 seconds and 4 git commands to generate.