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