Avoid unnecessary inclusions in public headers
[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
3e4f69d3
JD
29#define BT_LOG_TAG "PLUGIN-CTFCOPYTRACE-LIB-CLOCK-FIELDS"
30#include "logging.h"
31
9d408fca 32#include <babeltrace/babeltrace.h>
b2f1f465
JD
33#include <assert.h>
34#include <stdio.h>
35
36#include "clock-fields.h"
37
38static
39int find_update_struct_clock_fields(FILE *err, struct bt_ctf_field_type *type,
40 struct bt_ctf_clock_class *writer_clock_class);
41static
42int find_update_array_clock_fields(FILE *err, struct bt_ctf_field_type *type,
43 struct bt_ctf_clock_class *writer_clock_class);
44static
45int find_update_enum_clock_fields(FILE *err, struct bt_ctf_field_type *type,
46 struct bt_ctf_clock_class *writer_clock_class);
47static
48int find_update_sequence_clock_fields(FILE *err, struct bt_ctf_field_type *type,
49 struct bt_ctf_clock_class *writer_clock_class);
50static
51int find_update_variant_clock_fields(FILE *err, struct bt_ctf_field_type *type,
52 struct bt_ctf_clock_class *writer_clock_class);
53
54static
55int copy_find_clock_int_field(FILE *err,
56 struct bt_ctf_event *event, struct bt_ctf_event *writer_event,
57 struct bt_ctf_field *field, struct bt_ctf_field_type *type,
58 struct bt_ctf_field *copy_field);
59static
60int copy_find_clock_struct_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_array_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_sequence_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_variant_field(FILE *err, struct bt_ctf_event *event,
76 struct bt_ctf_event *writer_event, struct bt_ctf_field *field,
77 struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field);
78static
79int copy_find_clock_enum_field(FILE *err, struct bt_ctf_event *event,
80 struct bt_ctf_event *writer_event, struct bt_ctf_field *field,
81 struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field);
82
83static
84int update_header_clock_int_field_type(FILE *err, struct bt_ctf_field_type *type,
85 struct bt_ctf_clock_class *writer_clock_class)
86{
9ae49d3d 87 struct bt_ctf_clock_class *clock = NULL;
b2f1f465
JD
88 int ret;
89
90 clock = bt_ctf_field_type_integer_get_mapped_clock_class(type);
91 if (!clock) {
92 return 0;
93 }
9ae49d3d 94 BT_PUT(clock);
b2f1f465
JD
95
96 ret = bt_ctf_field_type_integer_set_size(type, 64);
97 if (ret) {
3e4f69d3 98 BT_LOGE_STR("Failed to set integer size to 64.");
b2f1f465
JD
99 goto end;
100 }
101
102 ret = bt_ctf_field_type_integer_set_mapped_clock_class(type,
103 writer_clock_class);
104 if (ret) {
3e4f69d3 105 BT_LOGE_STR("Failed to map integer to clock_class.");
b2f1f465
JD
106 goto end;
107 }
108
109end:
110 return ret;
111}
112
113static
114int find_update_clock_fields(FILE *err, struct bt_ctf_field_type *type,
115 struct bt_ctf_clock_class *writer_clock_class)
116{
117 int ret;
118
119 switch (bt_ctf_field_type_get_type_id(type)) {
1487a16a 120 case BT_CTF_FIELD_TYPE_ID_INTEGER:
b2f1f465
JD
121 return update_header_clock_int_field_type(err, type,
122 writer_clock_class);
1487a16a 123 case BT_CTF_FIELD_TYPE_ID_STRUCT:
b2f1f465
JD
124 return find_update_struct_clock_fields(err, type,
125 writer_clock_class);
1487a16a 126 case BT_CTF_FIELD_TYPE_ID_ARRAY:
b2f1f465
JD
127 return find_update_array_clock_fields(err, type,
128 writer_clock_class);
1487a16a 129 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
b2f1f465
JD
130 return find_update_sequence_clock_fields(err, type,
131 writer_clock_class);
1487a16a 132 case BT_CTF_FIELD_TYPE_ID_VARIANT:
b2f1f465
JD
133 return find_update_variant_clock_fields(err, type,
134 writer_clock_class);
1487a16a 135 case BT_CTF_FIELD_TYPE_ID_ENUM:
b2f1f465
JD
136 return find_update_enum_clock_fields(err, type,
137 writer_clock_class);
138 break;
139 default:
140 break;
141 }
142
143 ret = 0;
144
145 return ret;
146}
147
148static
149int find_update_variant_clock_fields(FILE *err, struct bt_ctf_field_type *type,
150 struct bt_ctf_clock_class *writer_clock_class)
151{
152 int count, i, ret;
9ae49d3d 153 struct bt_ctf_field_type *entry_type = NULL;
b2f1f465
JD
154
155 count = bt_ctf_field_type_variant_get_field_count(type);
156 for (i = 0; i < count; i++) {
b2f1f465
JD
157 const char *entry_name;
158
159 ret = bt_ctf_field_type_variant_get_field(type,
160 &entry_name, &entry_type, i);
161 if (ret) {
3e4f69d3 162 BT_LOGE_STR("Failed to get variant field.");
9ae49d3d 163 goto error;
b2f1f465 164 }
3e4f69d3 165
b2f1f465
JD
166 ret = find_update_clock_fields(err, entry_type,
167 writer_clock_class);
b2f1f465 168 if (ret) {
3e4f69d3 169 BT_LOGE_STR("Failed to find clock fields.");
9ae49d3d 170 goto error;
b2f1f465 171 }
9ae49d3d 172 BT_PUT(entry_type);
b2f1f465
JD
173 }
174
175 ret = 0;
9ae49d3d 176 goto end;
b2f1f465 177
9ae49d3d
JD
178error:
179 ret = -1;
b2f1f465 180end:
9ae49d3d 181 bt_put(entry_type);
b2f1f465
JD
182 return ret;
183}
184
185static
186int find_update_struct_clock_fields(FILE *err, struct bt_ctf_field_type *type,
187 struct bt_ctf_clock_class *writer_clock_class)
188{
189 int count, i, ret;
9ae49d3d 190 struct bt_ctf_field_type *entry_type = NULL;
b2f1f465
JD
191
192 count = bt_ctf_field_type_structure_get_field_count(type);
193 for (i = 0; i < count; i++) {
b2f1f465
JD
194 const char *entry_name;
195
196 ret = bt_ctf_field_type_structure_get_field(type,
197 &entry_name, &entry_type, i);
198 if (ret) {
3e4f69d3 199 BT_LOGE_STR("Failed to get struct field.");
9ae49d3d 200 goto error;
b2f1f465 201 }
3e4f69d3 202
b2f1f465
JD
203 ret = find_update_clock_fields(err, entry_type,
204 writer_clock_class);
b2f1f465 205 if (ret) {
3e4f69d3 206 BT_LOGE_STR("Failed to find clock fields.");
9ae49d3d 207 goto error;
b2f1f465 208 }
9ae49d3d 209 BT_PUT(entry_type);
b2f1f465
JD
210 }
211
212 ret = 0;
9ae49d3d 213 goto end;
b2f1f465 214
9ae49d3d
JD
215error:
216 bt_put(entry_type);
b2f1f465
JD
217end:
218 return ret;
219}
220
221static
222int find_update_sequence_clock_fields(FILE *err, struct bt_ctf_field_type *type,
223 struct bt_ctf_clock_class *writer_clock_class)
224{
225 int ret;
9ae49d3d 226 struct bt_ctf_field_type *entry_type = NULL;
b2f1f465
JD
227
228 entry_type = bt_ctf_field_type_sequence_get_element_type(type);
3e4f69d3
JD
229 assert(entry_type);
230
b2f1f465 231 ret = find_update_clock_fields(err, entry_type, writer_clock_class);
9ae49d3d 232 BT_PUT(entry_type);
b2f1f465 233 if (ret) {
3e4f69d3 234 BT_LOGE_STR("Failed to find clock fields.");
9ae49d3d 235 goto error;
b2f1f465
JD
236 }
237
238 ret = 0;
9ae49d3d 239 goto end;
b2f1f465 240
9ae49d3d
JD
241error:
242 ret = -1;
b2f1f465
JD
243end:
244 return ret;
245}
246
247static
248int find_update_array_clock_fields(FILE *err, struct bt_ctf_field_type *type,
249 struct bt_ctf_clock_class *writer_clock_class)
250{
3e4f69d3 251 int ret = 0;
9ae49d3d 252 struct bt_ctf_field_type *entry_type = NULL;
b2f1f465
JD
253
254 entry_type = bt_ctf_field_type_array_get_element_type(type);
3e4f69d3
JD
255 assert(entry_type);
256
b2f1f465 257 ret = find_update_clock_fields(err, entry_type, writer_clock_class);
9ae49d3d 258 BT_PUT(entry_type);
b2f1f465 259 if (ret) {
3e4f69d3 260 BT_LOGE_STR("Failed to find clock fields.");
b2f1f465
JD
261 ret = -1;
262 goto end;
263 }
264
b2f1f465
JD
265end:
266 return ret;
267}
268
269static
270int find_update_enum_clock_fields(FILE *err, struct bt_ctf_field_type *type,
271 struct bt_ctf_clock_class *writer_clock_class)
272{
273 int ret;
9ae49d3d 274 struct bt_ctf_field_type *entry_type = NULL;
b2f1f465
JD
275
276 entry_type = bt_ctf_field_type_enumeration_get_container_type(type);
3e4f69d3
JD
277 assert(entry_type);
278
b2f1f465 279 ret = find_update_clock_fields(err, entry_type, writer_clock_class);
9ae49d3d 280 BT_PUT(entry_type);
b2f1f465 281 if (ret) {
3e4f69d3 282 BT_LOGE_STR("Failed to find clock fields.");
9ae49d3d 283 goto error;
b2f1f465
JD
284 }
285
286 ret = 0;
9ae49d3d 287 goto end;
b2f1f465 288
9ae49d3d
JD
289error:
290 ret = -1;
b2f1f465
JD
291end:
292 return ret;
293}
294
295BT_HIDDEN
296struct bt_ctf_field_type *override_header_type(FILE *err,
297 struct bt_ctf_field_type *type,
298 struct bt_ctf_trace *writer_trace)
299{
300 struct bt_ctf_field_type *new_type = NULL;
9ae49d3d 301 struct bt_ctf_clock_class *writer_clock_class = NULL;
b2f1f465 302 int ret;
b2f1f465
JD
303
304 /* FIXME multi-clock? */
9ac68eb1 305 writer_clock_class = bt_ctf_trace_get_clock_class_by_index(writer_trace, 0);
3e4f69d3 306 assert(writer_clock_class);
b2f1f465
JD
307
308 new_type = bt_ctf_field_type_copy(type);
309 if (!new_type) {
3e4f69d3 310 BT_LOGE_STR("Failed to copy field type.");
9ae49d3d 311 goto error;
b2f1f465
JD
312 }
313
1487a16a 314 if (bt_ctf_field_type_get_type_id(new_type) != BT_CTF_FIELD_TYPE_ID_STRUCT) {
3e4f69d3
JD
315 BT_LOGE("Expected header field type to be struct: type=%d",
316 bt_ctf_field_type_get_type_id(new_type));
b2f1f465
JD
317 goto error;
318 }
319
320 ret = find_update_struct_clock_fields(err, new_type, writer_clock_class);
321 if (ret) {
3e4f69d3 322 BT_LOGE_STR("Failed to find clock fields in struct.");
b2f1f465
JD
323 goto error;
324 }
9ae49d3d 325 BT_PUT(writer_clock_class);
b2f1f465 326
9ae49d3d 327 goto end;
b2f1f465
JD
328
329error:
b2f1f465 330 bt_put(writer_clock_class);
9ae49d3d 331 BT_PUT(new_type);
b2f1f465
JD
332end:
333 return new_type;
334}
335
336static
337int copy_float_field(FILE *err, struct bt_ctf_field *field,
338 struct bt_ctf_field_type *type,
339 struct bt_ctf_field *copy_field)
340{
341 double value;
342 int ret;
343
344 ret = bt_ctf_field_floating_point_get_value(field, &value);
345 if (ret) {
3e4f69d3
JD
346 BT_LOGE_STR("Failed to get value.");
347 goto error;
b2f1f465 348 }
3e4f69d3 349
b2f1f465
JD
350 ret = bt_ctf_field_floating_point_set_value(copy_field, value);
351 if (ret) {
352 ret = -1;
3e4f69d3 353 BT_LOGE_STR("Failed to set floating point value.");
b2f1f465
JD
354 goto end;
355 }
356
357 ret = 0;
3e4f69d3 358 goto end;
b2f1f465 359
3e4f69d3
JD
360error:
361 ret = -1;
b2f1f465
JD
362end:
363 return ret;
364}
365
366static
367int copy_string_field(FILE *err, struct bt_ctf_field *field,
368 struct bt_ctf_field_type *type,
369 struct bt_ctf_field *copy_field)
370{
371 const char *value;
372 int ret;
373
374 value = bt_ctf_field_string_get_value(field);
375 if (!value) {
3e4f69d3
JD
376 BT_LOGE_STR("Failed to get value.");
377 goto error;
b2f1f465 378 }
3e4f69d3 379
b2f1f465
JD
380 ret = bt_ctf_field_string_set_value(copy_field, value);
381 if (ret) {
382 ret = -1;
3e4f69d3 383 BT_LOGE_STR("Failed to set string value.");
b2f1f465
JD
384 goto end;
385 }
386
387 ret = 0;
3e4f69d3 388 goto end;
b2f1f465 389
3e4f69d3
JD
390error:
391 ret = -1;
b2f1f465
JD
392end:
393 return ret;
394}
395
396BT_HIDDEN
397int copy_override_field(FILE *err, struct bt_ctf_event *event,
398 struct bt_ctf_event *writer_event, struct bt_ctf_field *field,
399 struct bt_ctf_field *copy_field)
400{
9ae49d3d 401 struct bt_ctf_field_type *type = NULL;
3e4f69d3 402 int ret = 0;
b2f1f465
JD
403
404 type = bt_ctf_field_get_type(field);
3e4f69d3 405 assert(type);
b2f1f465
JD
406
407 switch (bt_ctf_field_type_get_type_id(type)) {
1487a16a 408 case BT_CTF_FIELD_TYPE_ID_INTEGER:
b2f1f465
JD
409 ret = copy_find_clock_int_field(err, event, writer_event,
410 field, type, copy_field);
411 break;
1487a16a 412 case BT_CTF_FIELD_TYPE_ID_STRUCT:
b2f1f465
JD
413 ret = copy_find_clock_struct_field(err, event, writer_event,
414 field, type, copy_field);
415 break;
1487a16a 416 case BT_CTF_FIELD_TYPE_ID_FLOAT:
b2f1f465
JD
417 ret = copy_float_field(err, field, type, copy_field);
418 break;
1487a16a 419 case BT_CTF_FIELD_TYPE_ID_ENUM:
b2f1f465
JD
420 ret = copy_find_clock_enum_field(err, event, writer_event,
421 field, type, copy_field);
422 break;
1487a16a 423 case BT_CTF_FIELD_TYPE_ID_STRING:
b2f1f465
JD
424 ret = copy_string_field(err, field, type, copy_field);
425 break;
1487a16a 426 case BT_CTF_FIELD_TYPE_ID_ARRAY:
b2f1f465
JD
427 ret = copy_find_clock_array_field(err, event, writer_event,
428 field, type, copy_field);
429 break;
1487a16a 430 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
b2f1f465
JD
431 ret = copy_find_clock_sequence_field(err, event, writer_event,
432 field, type, copy_field);
433 break;
1487a16a 434 case BT_CTF_FIELD_TYPE_ID_VARIANT:
b2f1f465
JD
435 ret = copy_find_clock_variant_field(err, event, writer_event,
436 field, type, copy_field);
437 break;
438 /* No default, we want to catch missing field types. */
1487a16a 439 case BT_CTF_FIELD_TYPE_ID_UNKNOWN:
b2f1f465
JD
440 case BT_CTF_NR_TYPE_IDS:
441 break;
442 }
443
9ae49d3d 444 BT_PUT(type);
b2f1f465 445
b2f1f465
JD
446 return ret;
447}
448
449static
450int copy_find_clock_enum_field(FILE *err, struct bt_ctf_event *event,
451 struct bt_ctf_event *writer_event, struct bt_ctf_field *field,
452 struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field)
453{
454 int ret;
9ae49d3d 455 struct bt_ctf_field *container = NULL, *copy_container = NULL;
b2f1f465
JD
456
457 container = bt_ctf_field_enumeration_get_container(field);
3e4f69d3 458 assert(container);
b2f1f465
JD
459
460 copy_container = bt_ctf_field_enumeration_get_container(copy_field);
3e4f69d3 461 assert(copy_container);
b2f1f465
JD
462
463 ret = copy_override_field(err, event, writer_event, container,
464 copy_container);
465 if (ret) {
466 ret = -1;
3e4f69d3 467 BT_LOGE_STR("Failed to override enum field.");
9ae49d3d 468 goto error;
b2f1f465 469 }
9ac68eb1 470
9ae49d3d
JD
471 ret = 0;
472 goto end;
b2f1f465 473
9ae49d3d
JD
474error:
475 ret = -1;
476end:
b2f1f465 477 bt_put(copy_container);
b2f1f465 478 bt_put(container);
b2f1f465
JD
479 return ret;
480}
481
482static
483int copy_find_clock_variant_field(FILE *err, struct bt_ctf_event *event,
484 struct bt_ctf_event *writer_event, struct bt_ctf_field *field,
485 struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field)
486{
487 int ret;
9ae49d3d
JD
488 struct bt_ctf_field *tag = NULL;
489 struct bt_ctf_field *variant_field = NULL, *copy_variant_field = NULL;
b2f1f465
JD
490
491 tag = bt_ctf_field_variant_get_tag(field);
3e4f69d3 492 assert(tag);
b2f1f465
JD
493
494 variant_field = bt_ctf_field_variant_get_field(field, tag);
495 if (!variant_field) {
3e4f69d3 496 BT_LOGE_STR("Failed to get variant field.");
9ae49d3d 497 goto error;
b2f1f465
JD
498 }
499
500 copy_variant_field = bt_ctf_field_variant_get_field(copy_field, tag);
3e4f69d3 501 assert(copy_variant_field);
b2f1f465
JD
502
503 ret = copy_override_field(err, event, writer_event, variant_field,
504 copy_variant_field);
505 if (ret) {
3e4f69d3 506 BT_LOGE_STR("Failed to override variant field.");
9ae49d3d 507 goto error;
b2f1f465
JD
508 }
509
510 ret = 0;
9ae49d3d 511 goto end;
b2f1f465 512
9ae49d3d
JD
513error:
514 ret = -1;
515end:
b2f1f465 516 bt_put(copy_variant_field);
b2f1f465 517 bt_put(variant_field);
b2f1f465 518 bt_put(tag);
b2f1f465
JD
519 return ret;
520}
521
522static
523int copy_find_clock_sequence_field(FILE *err,
524 struct bt_ctf_event *event, struct bt_ctf_event *writer_event,
525 struct bt_ctf_field *field, struct bt_ctf_field_type *type,
526 struct bt_ctf_field *copy_field)
527{
528 int ret;
529 uint64_t i, count;
9ae49d3d
JD
530 struct bt_ctf_field *length_field = NULL;
531 struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL;
b2f1f465
JD
532
533 length_field = bt_ctf_field_sequence_get_length(field);
3e4f69d3 534 assert(length_field);
b2f1f465
JD
535
536 ret = bt_ctf_field_unsigned_integer_get_value(length_field, &count);
537 if (ret) {
3e4f69d3 538 BT_LOGE("Failed to get value.");
9ae49d3d 539 goto error;
b2f1f465
JD
540 }
541
542 ret = bt_ctf_field_sequence_set_length(copy_field, length_field);
b2f1f465 543 if (ret) {
3e4f69d3 544 BT_LOGE_STR("Failed to set sequence length.");
9ae49d3d 545 goto error;
b2f1f465 546 }
9ae49d3d 547 BT_PUT(length_field);
b2f1f465
JD
548
549 for (i = 0; i < count; i++) {
b2f1f465
JD
550 entry_field = bt_ctf_field_sequence_get_field(field, i);
551 if (!entry_field) {
3e4f69d3 552 BT_LOGE_STR("Failed to get sequence field.");
9ae49d3d 553 goto error;
b2f1f465
JD
554 }
555
556 entry_copy = bt_ctf_field_sequence_get_field(copy_field, i);
3e4f69d3 557 assert(entry_copy);
b2f1f465
JD
558
559 ret = copy_override_field(err, event, writer_event, entry_field,
560 entry_copy);
b2f1f465 561 if (ret) {
3e4f69d3 562 BT_LOGE_STR("Faield to override field in sequence.");
9ae49d3d 563 goto error;
b2f1f465 564 }
9ae49d3d
JD
565 BT_PUT(entry_field);
566 BT_PUT(entry_copy);
b2f1f465
JD
567 }
568
b2f1f465 569 ret = 0;
9ae49d3d 570 goto end;
b2f1f465 571
9ae49d3d
JD
572error:
573 bt_put(length_field);
574 bt_put(entry_field);
575 bt_put(entry_copy);
576 ret = -1;
b2f1f465
JD
577end:
578 return ret;
579}
580
581static
582int copy_find_clock_array_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, count, i;
9ae49d3d 588 struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL;
b2f1f465
JD
589
590 count = bt_ctf_field_type_array_get_length(type);
b2f1f465 591 for (i = 0; i < count; i++) {
b2f1f465
JD
592 entry_field = bt_ctf_field_array_get_field(field, i);
593 if (!entry_field) {
f01de2ae 594 ret = -1;
3e4f69d3 595 BT_LOGE_STR("Failed to get array field.");
9ae49d3d 596 goto error;
b2f1f465
JD
597 }
598
599 entry_copy = bt_ctf_field_array_get_field(copy_field, i);
3e4f69d3 600 assert(entry_copy);
b2f1f465
JD
601
602 ret = copy_override_field(err, event, writer_event, entry_field,
603 entry_copy);
b2f1f465
JD
604 if (ret) {
605 ret = -1;
3e4f69d3 606 BT_LOGE_STR("Failed to override field in array.");
9ae49d3d 607 goto error;
b2f1f465 608 }
9ae49d3d
JD
609 BT_PUT(entry_field);
610 BT_PUT(entry_copy);
b2f1f465
JD
611 }
612
b2f1f465 613 ret = 0;
9ae49d3d
JD
614 goto end;
615
616error:
617 bt_put(entry_field);
618 bt_put(entry_copy);
b2f1f465
JD
619
620end:
621 return ret;
622}
623
624static
625int copy_find_clock_struct_field(FILE *err,
626 struct bt_ctf_event *event, struct bt_ctf_event *writer_event,
627 struct bt_ctf_field *field, struct bt_ctf_field_type *type,
628 struct bt_ctf_field *copy_field)
629{
630 int count, i, ret;
9ae49d3d
JD
631 struct bt_ctf_field_type *entry_type = NULL;
632 struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL;
b2f1f465
JD
633
634 count = bt_ctf_field_type_structure_get_field_count(type);
635 for (i = 0; i < count; i++) {
b2f1f465 636 const char *entry_name;
b2f1f465
JD
637
638 entry_field = bt_ctf_field_structure_get_field_by_index(field, i);
639 if (!entry_field) {
3e4f69d3 640 BT_LOGE_STR("Failed to get struct field.");
9ae49d3d 641 goto error;
b2f1f465
JD
642 }
643
644 ret = bt_ctf_field_type_structure_get_field(type, &entry_name,
645 &entry_type, i);
646 if (ret) {
3e4f69d3 647 BT_LOGE_STR("Failed to get struct field.");
9ae49d3d 648 goto error;
b2f1f465
JD
649 }
650
651 entry_copy = bt_ctf_field_structure_get_field_by_index(copy_field, i);
3e4f69d3 652 assert(entry_copy);
b2f1f465
JD
653
654 ret = copy_override_field(err, event, writer_event, entry_field,
655 entry_copy);
b2f1f465 656 if (ret) {
3e4f69d3 657 BT_LOGE_STR("Failed to override field in struct.");
9ae49d3d 658 goto error;
b2f1f465 659 }
9ae49d3d
JD
660 BT_PUT(entry_copy);
661 BT_PUT(entry_field);
662 BT_PUT(entry_type);
b2f1f465
JD
663 }
664
665 ret = 0;
9ae49d3d 666 goto end;
b2f1f465 667
9ae49d3d
JD
668error:
669 bt_put(entry_type);
670 bt_put(entry_field);
671 bt_put(entry_copy);
672 ret = -1;
b2f1f465
JD
673end:
674 return ret;
675}
676
677static
678int set_int_value(FILE *err, struct bt_ctf_field *field,
679 struct bt_ctf_field *copy_field,
680 struct bt_ctf_field_type *type)
681{
682 uint64_t uvalue;
683 int64_t value;
684 int ret;
685
686 if (bt_ctf_field_type_integer_get_signed(type)) {
687 ret = bt_ctf_field_signed_integer_get_value(field, &value);
688 if (ret) {
3e4f69d3
JD
689 BT_LOGE("Failed to get value.");
690 goto error;
b2f1f465 691 }
3e4f69d3 692
b2f1f465
JD
693 ret = bt_ctf_field_signed_integer_set_value(copy_field, value);
694 if (ret) {
695 ret = -1;
3e4f69d3 696 BT_LOGE_STR("Failed to set signed integer value.");
b2f1f465
JD
697 goto end;
698 }
699 } else {
700 ret = bt_ctf_field_unsigned_integer_get_value(field,
701 &uvalue);
702 if (ret) {
3e4f69d3
JD
703 BT_LOGE("Failed to get value.");
704 goto error;
b2f1f465 705 }
3e4f69d3 706
b2f1f465
JD
707 ret = bt_ctf_field_unsigned_integer_set_value(copy_field, uvalue);
708 if (ret) {
709 ret = -1;
3e4f69d3 710 BT_LOGE_STR("Failed to set unsigned integer value.");
b2f1f465
JD
711 goto end;
712 }
713 }
714 ret = 0;
3e4f69d3 715 goto end;
b2f1f465 716
3e4f69d3
JD
717error:
718 ret = -1;
b2f1f465
JD
719end:
720 return ret;
721}
722
723struct bt_ctf_clock_class *stream_class_get_clock_class(FILE *err,
724 struct bt_ctf_stream_class *stream_class)
725{
9ae49d3d 726 struct bt_ctf_trace *trace = NULL;
b2f1f465
JD
727 struct bt_ctf_clock_class *clock_class = NULL;
728
729 trace = bt_ctf_stream_class_get_trace(stream_class);
3e4f69d3 730 assert(trace);
b2f1f465
JD
731
732 /* FIXME multi-clock? */
9ac68eb1 733 clock_class = bt_ctf_trace_get_clock_class_by_index(trace, 0);
b2f1f465
JD
734
735 bt_put(trace);
3e4f69d3 736
b2f1f465
JD
737 return clock_class;
738}
739
740struct bt_ctf_clock_class *event_get_clock_class(FILE *err, struct bt_ctf_event *event)
741{
9ae49d3d
JD
742 struct bt_ctf_event_class *event_class = NULL;
743 struct bt_ctf_stream_class *stream_class = NULL;
b2f1f465
JD
744 struct bt_ctf_clock_class *clock_class = NULL;
745
746 event_class = bt_ctf_event_get_class(event);
3e4f69d3 747 assert(event_class);
b2f1f465
JD
748
749 stream_class = bt_ctf_event_class_get_stream_class(event_class);
9ae49d3d 750 BT_PUT(event_class);
3e4f69d3 751 assert(stream_class);
b2f1f465
JD
752
753 clock_class = stream_class_get_clock_class(err, stream_class);
754 bt_put(stream_class);
755
b2f1f465
JD
756 return clock_class;
757}
758
759static
760int copy_find_clock_int_field(FILE *err,
761 struct bt_ctf_event *event, struct bt_ctf_event *writer_event,
762 struct bt_ctf_field *field, struct bt_ctf_field_type *type,
763 struct bt_ctf_field *copy_field)
764{
9ae49d3d
JD
765 struct bt_ctf_clock_class *clock_class = NULL, *writer_clock_class = NULL;
766 struct bt_ctf_clock_value *clock_value = NULL, *writer_clock_value = NULL;
b2f1f465
JD
767 uint64_t value;
768 int ret;
769
770 clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(type);
771 if (!clock_class) {
772 return set_int_value(err, field, copy_field, type);
773 }
774
775 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
9ae49d3d 776 BT_PUT(clock_class);
3e4f69d3 777 assert(clock_value);
b2f1f465
JD
778
779 ret = bt_ctf_clock_value_get_value(clock_value, &value);
9ae49d3d 780 BT_PUT(clock_value);
b2f1f465 781 if (ret) {
3e4f69d3 782 BT_LOGE("Failed to get clock value.");
9ae49d3d 783 goto error;
b2f1f465
JD
784 }
785
786 ret = bt_ctf_field_unsigned_integer_set_value(copy_field, value);
787 if (ret) {
3e4f69d3 788 BT_LOGE_STR("Failed to set unsigned integer value.");
9ae49d3d 789 goto error;
b2f1f465
JD
790 }
791
792 writer_clock_class = event_get_clock_class(err, writer_event);
3e4f69d3 793 assert(writer_clock_class);
b2f1f465
JD
794
795 writer_clock_value = bt_ctf_clock_value_create(writer_clock_class, value);
9ae49d3d 796 BT_PUT(writer_clock_class);
b2f1f465 797 if (!writer_clock_value) {
3e4f69d3 798 BT_LOGE_STR("Failed to create clock value.");
9ae49d3d 799 goto error;
b2f1f465
JD
800 }
801
802 ret = bt_ctf_event_set_clock_value(writer_event, writer_clock_value);
9ae49d3d 803 BT_PUT(writer_clock_value);
b2f1f465 804 if (ret) {
3e4f69d3 805 BT_LOGE_STR("Failed to set clock value.");
9ae49d3d 806 goto error;
b2f1f465
JD
807 }
808
809 ret = 0;
9ae49d3d 810 goto end;
b2f1f465 811
9ae49d3d
JD
812error:
813 ret = -1;
b2f1f465
JD
814end:
815 return ret;
816}
817
This page took 0.068609 seconds and 4 git commands to generate.