fix warnings
[babeltrace.git] / plugins / libctfcopytrace / ctfcopytrace.c
1 /*
2 * copytrace.c
3 *
4 * Babeltrace library to create a copy of a CTF trace
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 <assert.h>
38
39 #include "ctfcopytrace.h"
40 #include "clock-fields.h"
41
42 struct bt_ctf_clock_class *ctf_copy_clock_class(FILE *err,
43 struct bt_ctf_clock_class *clock_class)
44 {
45 int64_t offset, offset_s;
46 int int_ret;
47 uint64_t u64_ret;
48 const char *name, *description;
49 struct bt_ctf_clock_class *writer_clock_class = NULL;
50
51 assert(err && clock_class);
52
53 name = bt_ctf_clock_class_get_name(clock_class);
54 if (!name) {
55 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
56 __LINE__);
57 goto end;
58 }
59
60 writer_clock_class = bt_ctf_clock_class_create(name);
61 if (!writer_clock_class) {
62 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
63 __LINE__);
64 goto end;
65 }
66
67 description = bt_ctf_clock_class_get_description(clock_class);
68 if (!description) {
69 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
70 __LINE__);
71 goto end_destroy;
72 }
73
74 int_ret = bt_ctf_clock_class_set_description(writer_clock_class,
75 description);
76 if (int_ret != 0) {
77 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
78 __LINE__);
79 goto end_destroy;
80 }
81
82 u64_ret = bt_ctf_clock_class_get_frequency(clock_class);
83 if (u64_ret == -1ULL) {
84 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
85 __LINE__);
86 goto end_destroy;
87 }
88 int_ret = bt_ctf_clock_class_set_frequency(writer_clock_class, u64_ret);
89 if (int_ret != 0) {
90 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
91 __LINE__);
92 goto end_destroy;
93 }
94
95 u64_ret = bt_ctf_clock_class_get_precision(clock_class);
96 if (u64_ret == -1ULL) {
97 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
98 __LINE__);
99 goto end_destroy;
100 }
101 int_ret = bt_ctf_clock_class_set_precision(writer_clock_class,
102 u64_ret);
103 if (int_ret != 0) {
104 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
105 __LINE__);
106 goto end_destroy;
107 }
108
109 int_ret = bt_ctf_clock_class_get_offset_s(clock_class, &offset_s);
110 if (int_ret != 0) {
111 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
112 __LINE__);
113 goto end_destroy;
114 }
115
116 int_ret = bt_ctf_clock_class_set_offset_s(writer_clock_class, offset_s);
117 if (int_ret != 0) {
118 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
119 __LINE__);
120 goto end_destroy;
121 }
122
123 int_ret = bt_ctf_clock_class_get_offset_cycles(clock_class, &offset);
124 if (int_ret != 0) {
125 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
126 __LINE__);
127 goto end_destroy;
128 }
129
130 int_ret = bt_ctf_clock_class_set_offset_cycles(writer_clock_class, offset);
131 if (int_ret != 0) {
132 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
133 __LINE__);
134 goto end_destroy;
135 }
136
137 int_ret = bt_ctf_clock_class_get_is_absolute(clock_class);
138 if (int_ret == -1) {
139 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
140 __LINE__);
141 goto end_destroy;
142 }
143
144 int_ret = bt_ctf_clock_class_set_is_absolute(writer_clock_class, int_ret);
145 if (int_ret != 0) {
146 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
147 __LINE__);
148 goto end_destroy;
149 }
150
151 goto end;
152
153 end_destroy:
154 BT_PUT(writer_clock_class);
155 end:
156 return writer_clock_class;
157 }
158
159 enum bt_component_status ctf_copy_clock_classes(FILE *err,
160 struct bt_ctf_trace *writer_trace,
161 struct bt_ctf_stream_class *writer_stream_class,
162 struct bt_ctf_trace *trace)
163 {
164 enum bt_component_status ret;
165 int int_ret, clock_class_count, i;
166
167 clock_class_count = bt_ctf_trace_get_clock_class_count(trace);
168
169 for (i = 0; i < clock_class_count; i++) {
170 struct bt_ctf_clock_class *writer_clock_class;
171 struct bt_ctf_clock_class *clock_class =
172 bt_ctf_trace_get_clock_class(trace, i);
173
174 if (!clock_class) {
175 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
176 __LINE__);
177 ret = BT_COMPONENT_STATUS_ERROR;
178 goto end;
179 }
180
181 writer_clock_class = ctf_copy_clock_class(err, clock_class);
182 bt_put(clock_class);
183 if (!writer_clock_class) {
184 fprintf(err, "Failed to copy clock class");
185 ret = BT_COMPONENT_STATUS_ERROR;
186 goto end;
187 }
188
189 int_ret = bt_ctf_trace_add_clock_class(writer_trace, writer_clock_class);
190 if (int_ret != 0) {
191 BT_PUT(writer_clock_class);
192 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
193 __LINE__);
194 ret = BT_COMPONENT_STATUS_ERROR;
195 goto end;
196 }
197
198 /*
199 * Ownership transferred to the trace.
200 */
201 bt_put(writer_clock_class);
202 }
203
204 ret = BT_COMPONENT_STATUS_OK;
205
206 end:
207 return ret;
208 }
209
210 struct bt_ctf_event_class *ctf_copy_event_class(FILE *err,
211 struct bt_ctf_event_class *event_class)
212 {
213 struct bt_ctf_event_class *writer_event_class = NULL;
214 const char *name;
215 int count, i;
216
217 name = bt_ctf_event_class_get_name(event_class);
218 if (!name) {
219 fprintf(err, "[error] %s in %s:%d\n", __func__,
220 __FILE__, __LINE__);
221 goto end;
222 }
223
224 writer_event_class = bt_ctf_event_class_create(name);
225 if (!writer_event_class) {
226 fprintf(err, "[error] %s in %s:%d\n", __func__,
227 __FILE__, __LINE__);
228 goto end;
229 }
230
231 count = bt_ctf_event_class_get_attribute_count(event_class);
232 for (i = 0; i < count; i++) {
233 const char *attr_name;
234 struct bt_value *attr_value;
235 int ret;
236
237 attr_name = bt_ctf_event_class_get_attribute_name(event_class, i);
238 if (!attr_name) {
239 fprintf(err, "[error] %s in %s:%d\n", __func__,
240 __FILE__, __LINE__);
241 goto error;
242 }
243 attr_value = bt_ctf_event_class_get_attribute_value(event_class, i);
244 if (!attr_value) {
245 fprintf(err, "[error] %s in %s:%d\n", __func__,
246 __FILE__, __LINE__);
247 goto error;
248 }
249
250 ret = bt_ctf_event_class_set_attribute(writer_event_class,
251 attr_name, attr_value);
252 BT_PUT(attr_value);
253 if (ret < 0) {
254 fprintf(err, "[error] %s in %s:%d\n", __func__,
255 __FILE__, __LINE__);
256 goto error;
257 }
258 }
259
260 count = bt_ctf_event_class_get_field_count(event_class);
261 for (i = 0; i < count; i++) {
262 const char *field_name;
263 struct bt_ctf_field_type *field_type;
264 int ret;
265
266 ret = bt_ctf_event_class_get_field(event_class, &field_name,
267 &field_type, i);
268 if (ret < 0) {
269 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
270 goto error;
271 }
272
273 ret = bt_ctf_event_class_add_field(writer_event_class, field_type,
274 field_name);
275 BT_PUT(field_type);
276 if (ret < 0) {
277 fprintf(err, "[error] Cannot add field %s\n", field_name);
278 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
279 goto error;
280 }
281 }
282
283 goto end;
284
285 error:
286 BT_PUT(writer_event_class);
287 end:
288 return writer_event_class;
289 }
290
291 enum bt_component_status ctf_copy_event_classes(FILE *err,
292 struct bt_ctf_stream_class *stream_class,
293 struct bt_ctf_stream_class *writer_stream_class)
294 {
295 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
296 struct bt_ctf_event_class *event_class = NULL, *writer_event_class = NULL;
297 int count, i;
298
299 count = bt_ctf_stream_class_get_event_class_count(stream_class);
300 if (count < 0) {
301 fprintf(err, "[error] %s in %s:%d\n", __func__,
302 __FILE__, __LINE__);
303 goto end;
304 }
305
306 for (i = 0; i < count; i++) {
307 struct bt_ctf_field_type *context;
308 int int_ret;
309
310 event_class = bt_ctf_stream_class_get_event_class(
311 stream_class, i);
312 if (!event_class) {
313 fprintf(err, "[error] %s in %s:%d\n", __func__,
314 __FILE__, __LINE__);
315 ret = BT_COMPONENT_STATUS_ERROR;
316 goto error;
317 }
318 writer_event_class = ctf_copy_event_class(err, event_class);
319 if (!writer_event_class) {
320 fprintf(err, "[error] %s in %s:%d\n", __func__,
321 __FILE__, __LINE__);
322 ret = BT_COMPONENT_STATUS_ERROR;
323 goto error;
324 }
325
326 context = bt_ctf_event_class_get_context_type(event_class);
327 if (!context) {
328 fprintf(err, "[error] %s in %s:%d\n", __func__,
329 __FILE__, __LINE__);
330 ret = BT_COMPONENT_STATUS_ERROR;
331 goto error;
332 }
333 ret = bt_ctf_event_class_set_context_type(writer_event_class, context);
334 BT_PUT(context);
335 if (ret < 0) {
336 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
337 __LINE__);
338 goto error;
339 }
340
341 int_ret = bt_ctf_stream_class_add_event_class(writer_stream_class,
342 writer_event_class);
343 if (int_ret < 0) {
344 fprintf(err, "[error] Failed to add event class\n");
345 fprintf(err, "[error] %s in %s:%d\n", __func__,
346 __FILE__, __LINE__);
347 ret = BT_COMPONENT_STATUS_ERROR;
348 goto error;
349 }
350 BT_PUT(writer_event_class);
351 BT_PUT(event_class);
352 }
353
354 goto end;
355
356 error:
357 bt_put(event_class);
358 bt_put(writer_event_class);
359 end:
360 return ret;
361 }
362
363 struct bt_ctf_stream_class *ctf_copy_stream_class(FILE *err,
364 struct bt_ctf_stream_class *stream_class,
365 struct bt_ctf_trace *writer_trace,
366 bool override_ts64)
367 {
368 struct bt_ctf_field_type *type = NULL;
369 struct bt_ctf_stream_class *writer_stream_class = NULL;
370 int ret_int;
371 const char *name = bt_ctf_stream_class_get_name(stream_class);
372
373 if (strlen(name) == 0) {
374 name = NULL;
375 }
376
377 writer_stream_class = bt_ctf_stream_class_create(name);
378 if (!writer_stream_class) {
379 fprintf(err, "[error] %s in %s:%d\n",
380 __func__, __FILE__, __LINE__);
381 goto end;
382 }
383
384 type = bt_ctf_stream_class_get_packet_context_type(stream_class);
385 if (!type) {
386 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
387 __LINE__);
388 goto error;
389 }
390
391 ret_int = bt_ctf_stream_class_set_packet_context_type(
392 writer_stream_class, type);
393 if (ret_int < 0) {
394 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
395 __LINE__);
396 goto error;
397 }
398 BT_PUT(type);
399
400 type = bt_ctf_stream_class_get_event_header_type(stream_class);
401 if (!type) {
402 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
403 __LINE__);
404 goto error;
405 }
406
407 if (override_ts64) {
408 struct bt_ctf_field_type *new_event_header_type;
409
410 new_event_header_type = override_header_type(err, type,
411 writer_trace);
412 if (!new_event_header_type) {
413 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
414 __LINE__);
415 goto error;
416 }
417 ret_int = bt_ctf_stream_class_set_event_header_type(
418 writer_stream_class, new_event_header_type);
419 BT_PUT(new_event_header_type);
420 if (ret_int < 0) {
421 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
422 __LINE__);
423 goto error;
424 }
425 } else {
426 ret_int = bt_ctf_stream_class_set_event_header_type(
427 writer_stream_class, type);
428 if (ret_int < 0) {
429 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
430 __LINE__);
431 goto error;
432 }
433 }
434 BT_PUT(type);
435
436 type = bt_ctf_stream_class_get_event_context_type(stream_class);
437 if (type) {
438 ret_int = bt_ctf_stream_class_set_event_context_type(
439 writer_stream_class, type);
440 if (ret_int < 0) {
441 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
442 __LINE__);
443 goto error;
444 }
445 }
446 BT_PUT(type);
447
448 goto end;
449
450 error:
451 BT_PUT(writer_stream_class);
452 end:
453 bt_put(type);
454 return writer_stream_class;
455 }
456
457 enum bt_component_status ctf_copy_packet_context_field(FILE *err,
458 struct bt_ctf_field *field, const char *field_name,
459 struct bt_ctf_field *writer_packet_context,
460 struct bt_ctf_field_type *writer_packet_context_type)
461 {
462 enum bt_component_status ret;
463 struct bt_ctf_field *writer_field = NULL;
464 struct bt_ctf_field_type *field_type = NULL;
465 int int_ret;
466 uint64_t value;
467
468 field_type = bt_ctf_field_get_type(field);
469 if (!field_type) {
470 ret = BT_COMPONENT_STATUS_ERROR;
471 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
472 __LINE__);
473 goto end;
474 }
475 /*
476 * Only support for integers for now.
477 */
478 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) {
479 fprintf(err, "[error] Unsupported packet context field type\n");
480 ret = BT_COMPONENT_STATUS_ERROR;
481 goto error;
482 }
483 BT_PUT(field_type);
484
485 writer_field = bt_ctf_field_structure_get_field(writer_packet_context,
486 field_name);
487 if (!writer_field) {
488 ret = BT_COMPONENT_STATUS_ERROR;
489 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
490 __LINE__);
491 goto end;
492 }
493
494 int_ret = bt_ctf_field_unsigned_integer_get_value(field, &value);
495 if (int_ret < 0) {
496 fprintf(err, "[error] Wrong packet_context field type\n");
497 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
498 __LINE__);
499 ret = BT_COMPONENT_STATUS_ERROR;
500 goto end;
501 }
502
503 int_ret = bt_ctf_field_unsigned_integer_set_value(writer_field, value);
504 if (int_ret < 0) {
505 ret = BT_COMPONENT_STATUS_ERROR;
506 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
507 __LINE__);
508 goto end;
509 }
510
511 ret = BT_COMPONENT_STATUS_OK;
512
513 goto end;
514
515 error:
516 bt_put(field_type);
517 end:
518 bt_put(writer_field);
519 return ret;
520 }
521
522 struct bt_ctf_field *ctf_copy_packet_context(FILE *err,
523 struct bt_ctf_packet *packet,
524 struct bt_ctf_stream *writer_stream)
525 {
526 enum bt_component_status ret;
527 struct bt_ctf_field *packet_context = NULL, *writer_packet_context = NULL;
528 struct bt_ctf_field_type *struct_type = NULL, *writer_packet_context_type = NULL;
529 struct bt_ctf_stream_class *writer_stream_class = NULL;
530 struct bt_ctf_field *field = NULL;
531 struct bt_ctf_field_type *field_type;
532 int nr_fields, i;
533
534 packet_context = bt_ctf_packet_get_context(packet);
535 if (!packet_context) {
536 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
537 __LINE__);
538 goto error;
539 }
540
541 writer_stream_class = bt_ctf_stream_get_class(writer_stream);
542 if (!writer_stream_class) {
543 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
544 __LINE__);
545 goto error;
546 }
547
548 writer_packet_context_type = bt_ctf_stream_class_get_packet_context_type(
549 writer_stream_class);
550 BT_PUT(writer_stream_class);
551 if (!writer_packet_context_type) {
552 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
553 __LINE__);
554 goto error;
555 }
556
557 struct_type = bt_ctf_field_get_type(packet_context);
558 if (!struct_type) {
559 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
560 __LINE__);
561 goto error;
562 }
563
564 writer_packet_context = bt_ctf_field_create(writer_packet_context_type);
565 if (!writer_packet_context) {
566 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
567 __LINE__);
568 goto error;
569 }
570
571 nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type);
572 for (i = 0; i < nr_fields; i++) {
573 const char *field_name;
574
575 field = bt_ctf_field_structure_get_field_by_index(
576 packet_context, i);
577 if (!field) {
578 fprintf(err, "[error] %s in %s:%d\n", __func__,
579 __FILE__, __LINE__);
580 goto error;
581 }
582 if (bt_ctf_field_type_structure_get_field(struct_type,
583 &field_name, &field_type, i) < 0) {
584 fprintf(err, "[error] %s in %s:%d\n", __func__,
585 __FILE__, __LINE__);
586 goto error;
587 }
588 if (!strncmp(field_name, "content_size", strlen("content_size")) ||
589 !strncmp(field_name, "packet_size",
590 strlen("packet_size"))) {
591 BT_PUT(field_type);
592 BT_PUT(field);
593 continue;
594 }
595
596 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) {
597 fprintf(err, "[error] Unexpected packet context field type\n");
598 goto error;
599 }
600
601 ret = ctf_copy_packet_context_field(err, field, field_name,
602 writer_packet_context, writer_packet_context_type);
603 BT_PUT(field_type);
604 BT_PUT(field);
605 if (ret != BT_COMPONENT_STATUS_OK) {
606 fprintf(err, "[error] %s in %s:%d\n", __func__,
607 __FILE__, __LINE__);
608 goto error;
609 }
610 }
611
612 goto end;
613
614 error:
615 BT_PUT(writer_packet_context);
616 end:
617 bt_put(field);
618 bt_put(field_type);
619 bt_put(struct_type);
620 bt_put(writer_packet_context_type);
621 bt_put(writer_stream_class);
622 bt_put(packet_context);
623 return writer_packet_context;
624 }
625
626 int ctf_copy_event_header(FILE *err, struct bt_ctf_event *event,
627 struct bt_ctf_event_class *writer_event_class,
628 struct bt_ctf_event *writer_event,
629 struct bt_ctf_field *event_header)
630 {
631 struct bt_ctf_clock_class *clock_class = NULL, *writer_clock_class = NULL;
632 struct bt_ctf_clock_value *clock_value = NULL, *writer_clock_value = NULL;
633
634 int ret;
635 struct bt_ctf_field *writer_event_header = NULL;
636 uint64_t value;
637
638 clock_class = event_get_clock_class(err, event);
639 if (!clock_class) {
640 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
641 __LINE__);
642 goto error;
643 }
644
645 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
646 BT_PUT(clock_class);
647 if (!clock_value) {
648 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
649 __LINE__);
650 goto error;
651 }
652
653 ret = bt_ctf_clock_value_get_value(clock_value, &value);
654 BT_PUT(clock_value);
655 if (ret) {
656 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
657 __LINE__);
658 goto error;
659 }
660
661 writer_clock_class = event_get_clock_class(err, writer_event);
662 if (!writer_clock_class) {
663 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
664 __LINE__);
665 goto error;
666 }
667
668 writer_clock_value = bt_ctf_clock_value_create(writer_clock_class, value);
669 BT_PUT(writer_clock_class);
670 if (!writer_clock_value) {
671 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
672 __LINE__);
673 goto error;
674 }
675
676 ret = bt_ctf_event_set_clock_value(writer_event, writer_clock_value);
677 BT_PUT(writer_clock_value);
678 if (ret) {
679 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
680 __LINE__);
681 goto error;
682 }
683
684 writer_event_header = bt_ctf_field_copy(event_header);
685 if (!writer_event_header) {
686 fprintf(err, "[error] %s in %s:%d\n", __func__,
687 __FILE__, __LINE__);
688 goto end;
689 }
690
691 ret = bt_ctf_event_set_header(writer_event, writer_event_header);
692 BT_PUT(writer_event_header);
693 if (ret < 0) {
694 fprintf(err, "[error] %s in %s:%d\n", __func__,
695 __FILE__, __LINE__);
696 goto error;
697 }
698
699 ret = 0;
700
701 goto end;
702
703 error:
704 ret = -1;
705 end:
706 return ret;
707 }
708
709 struct bt_ctf_event *ctf_copy_event(FILE *err, struct bt_ctf_event *event,
710 struct bt_ctf_event_class *writer_event_class,
711 bool override_ts64)
712 {
713 struct bt_ctf_event *writer_event = NULL;
714 struct bt_ctf_field *field = NULL, *copy_field = NULL;
715 int ret;
716
717 writer_event = bt_ctf_event_create(writer_event_class);
718 if (!writer_event) {
719 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
720 __LINE__);
721 goto end;
722 }
723
724 field = bt_ctf_event_get_header(event);
725 if (!field) {
726 fprintf(err, "[error] %s in %s:%d\n", __func__,
727 __FILE__, __LINE__);
728 goto error;
729 }
730
731 /*
732 * If override_ts64, we override all integer fields mapped to a clock
733 * to a uint64_t field type, otherwise, we just copy it as is.
734 */
735 if (override_ts64) {
736 copy_field = bt_ctf_event_get_header(writer_event);
737 if (!copy_field) {
738 fprintf(err, "[error] %s in %s:%d\n", __func__,
739 __FILE__, __LINE__);
740 goto error;
741 }
742
743 ret = copy_override_field(err, event, writer_event, field,
744 copy_field);
745 if (ret) {
746 fprintf(err, "[error] %s in %s:%d\n", __func__,
747 __FILE__, __LINE__);
748 goto error;
749 }
750 BT_PUT(copy_field);
751 } else {
752 ret = ctf_copy_event_header(err, event, writer_event_class,
753 writer_event, field);
754 if (ret) {
755 fprintf(err, "[error] %s in %s:%d\n", __func__,
756 __FILE__, __LINE__);
757 goto error;
758 }
759 }
760 BT_PUT(field);
761
762 /* Optional field, so it can fail silently. */
763 field = bt_ctf_event_get_stream_event_context(event);
764 copy_field = bt_ctf_field_copy(field);
765 if (copy_field) {
766 ret = bt_ctf_event_set_stream_event_context(writer_event,
767 copy_field);
768 if (ret < 0) {
769 fprintf(err, "[error] %s in %s:%d\n", __func__,
770 __FILE__, __LINE__);
771 goto error;
772 }
773 }
774 BT_PUT(field);
775 BT_PUT(copy_field);
776
777 /* Optional field, so it can fail silently. */
778 field = bt_ctf_event_get_event_context(event);
779 copy_field = bt_ctf_field_copy(field);
780 if (copy_field) {
781 ret = bt_ctf_event_set_event_context(writer_event, copy_field);
782 if (ret < 0) {
783 fprintf(err, "[error] %s in %s:%d\n", __func__,
784 __FILE__, __LINE__);
785 goto error;
786 }
787 }
788 BT_PUT(field);
789 BT_PUT(copy_field);
790
791 field = bt_ctf_event_get_payload_field(event);
792 if (!field) {
793 fprintf(err, "[error] %s in %s:%d\n", __func__,
794 __FILE__, __LINE__);
795 goto error;
796 }
797 copy_field = bt_ctf_field_copy(field);
798 if (copy_field) {
799 ret = bt_ctf_event_set_payload_field(writer_event, copy_field);
800 if (ret < 0) {
801 fprintf(err, "[error] %s in %s:%d\n", __func__,
802 __FILE__, __LINE__);
803 goto error;
804 }
805 }
806 BT_PUT(field);
807 BT_PUT(copy_field);
808
809 goto end;
810
811 error:
812 BT_PUT(writer_event);
813 end:
814 bt_put(field);
815 bt_put(copy_field);
816 return writer_event;
817 }
818
819 enum bt_component_status ctf_copy_trace(FILE *err, struct bt_ctf_trace *trace,
820 struct bt_ctf_trace *writer_trace)
821 {
822 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
823 int field_count, i, int_ret;
824 struct bt_ctf_field_type *header_type = NULL;
825
826 field_count = bt_ctf_trace_get_environment_field_count(trace);
827 for (i = 0; i < field_count; i++) {
828 int ret_int;
829 const char *name;
830 struct bt_value *value = NULL;
831
832 name = bt_ctf_trace_get_environment_field_name(trace, i);
833 if (!name) {
834 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
835 __LINE__);
836 ret = BT_COMPONENT_STATUS_ERROR;
837 goto end;
838 }
839 value = bt_ctf_trace_get_environment_field_value(trace, i);
840 if (!value) {
841 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
842 __LINE__);
843 ret = BT_COMPONENT_STATUS_ERROR;
844 goto end;
845 }
846
847 ret_int = bt_ctf_trace_set_environment_field(writer_trace,
848 name, value);
849 BT_PUT(value);
850 if (ret_int < 0) {
851 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
852 __LINE__);
853 fprintf(err, "[error] Unable to set environment field %s\n",
854 name);
855 ret = BT_COMPONENT_STATUS_ERROR;
856 goto end;
857 }
858 }
859
860 header_type = bt_ctf_trace_get_packet_header_type(writer_trace);
861 if (!header_type) {
862 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
863 ret = BT_COMPONENT_STATUS_ERROR;
864 goto end;
865 }
866
867 int_ret = bt_ctf_trace_set_packet_header_type(writer_trace, header_type);
868 BT_PUT(header_type);
869 if (int_ret < 0) {
870 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
871 ret = BT_COMPONENT_STATUS_ERROR;
872 goto end;
873 }
874
875 end:
876 return ret;
877 }
This page took 0.047445 seconds and 4 git commands to generate.