Fix copytrace: copy packet_context
[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 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
415 __LINE__);
416 goto error;
417 }
418
419 if (override_ts64) {
420 struct bt_ctf_field_type *new_event_header_type;
421
422 new_event_header_type = override_header_type(err, type,
423 writer_trace);
424 if (!new_event_header_type) {
425 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
426 __LINE__);
427 goto error;
428 }
429 ret_int = bt_ctf_stream_class_set_event_header_type(
430 writer_stream_class, new_event_header_type);
431 BT_PUT(new_event_header_type);
432 if (ret_int < 0) {
433 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
434 __LINE__);
435 goto error;
436 }
437 } else {
438 ret_int = bt_ctf_stream_class_set_event_header_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 type = bt_ctf_stream_class_get_event_context_type(stream_class);
449 if (type) {
450 ret_int = bt_ctf_stream_class_set_event_context_type(
451 writer_stream_class, type);
452 if (ret_int < 0) {
453 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
454 __LINE__);
455 goto error;
456 }
457 }
458 BT_PUT(type);
459
460 goto end;
461
462 error:
463 BT_PUT(writer_stream_class);
464 end:
465 bt_put(type);
466 return writer_stream_class;
467 }
468
469 BT_HIDDEN
470 int ctf_stream_copy_packet_context(FILE *err, struct bt_ctf_packet *packet,
471 struct bt_ctf_stream *writer_stream)
472 {
473 struct bt_ctf_field *packet_context = NULL, *writer_packet_context = NULL;
474 int ret = 0;
475
476 packet_context = bt_ctf_packet_get_context(packet);
477 if (!packet_context) {
478 goto end;
479 }
480
481 writer_packet_context = bt_ctf_field_copy(packet_context);
482 if (!writer_packet_context) {
483 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
484 __LINE__);
485 goto error;
486 }
487
488 ret = bt_ctf_stream_set_packet_context(writer_stream,
489 writer_packet_context);
490 if (ret) {
491 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
492 __LINE__);
493 goto error;
494 }
495
496 goto end;
497
498 error:
499 ret = -1;
500 end:
501 bt_put(packet_context);
502 bt_put(writer_packet_context);
503 return ret;
504 }
505
506 BT_HIDDEN
507 int ctf_packet_copy_context(FILE *err, struct bt_ctf_packet *packet,
508 struct bt_ctf_stream *writer_stream,
509 struct bt_ctf_packet *writer_packet)
510 {
511 struct bt_ctf_field *packet_context = NULL, *writer_packet_context = NULL;
512 int ret = 0;
513
514 packet_context = bt_ctf_packet_get_context(packet);
515 if (!packet_context) {
516 goto end;
517 }
518
519 writer_packet_context = bt_ctf_field_copy(packet_context);
520 if (!writer_packet_context) {
521 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
522 __LINE__);
523 goto error;
524 }
525
526 ret = bt_ctf_packet_set_context(writer_packet, writer_packet_context);
527 if (ret) {
528 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
529 __LINE__);
530 goto error;
531 }
532
533 goto end;
534
535 error:
536 ret = -1;
537 end:
538 bt_put(writer_packet_context);
539 bt_put(packet_context);
540 return ret;
541 }
542
543 BT_HIDDEN
544 int ctf_copy_event_header(FILE *err, struct bt_ctf_event *event,
545 struct bt_ctf_event_class *writer_event_class,
546 struct bt_ctf_event *writer_event,
547 struct bt_ctf_field *event_header)
548 {
549 struct bt_ctf_clock_class *clock_class = NULL, *writer_clock_class = NULL;
550 struct bt_ctf_clock_value *clock_value = NULL, *writer_clock_value = NULL;
551
552 int ret;
553 struct bt_ctf_field *writer_event_header = NULL;
554 uint64_t value;
555
556 clock_class = event_get_clock_class(err, event);
557 if (!clock_class) {
558 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
559 __LINE__);
560 goto error;
561 }
562
563 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
564 BT_PUT(clock_class);
565 if (!clock_value) {
566 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
567 __LINE__);
568 goto error;
569 }
570
571 ret = bt_ctf_clock_value_get_value(clock_value, &value);
572 BT_PUT(clock_value);
573 if (ret) {
574 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
575 __LINE__);
576 goto error;
577 }
578
579 writer_clock_class = event_get_clock_class(err, writer_event);
580 if (!writer_clock_class) {
581 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
582 __LINE__);
583 goto error;
584 }
585
586 writer_clock_value = bt_ctf_clock_value_create(writer_clock_class, value);
587 BT_PUT(writer_clock_class);
588 if (!writer_clock_value) {
589 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
590 __LINE__);
591 goto error;
592 }
593
594 ret = bt_ctf_event_set_clock_value(writer_event, writer_clock_value);
595 BT_PUT(writer_clock_value);
596 if (ret) {
597 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
598 __LINE__);
599 goto error;
600 }
601
602 writer_event_header = bt_ctf_field_copy(event_header);
603 if (!writer_event_header) {
604 fprintf(err, "[error] %s in %s:%d\n", __func__,
605 __FILE__, __LINE__);
606 goto end;
607 }
608
609 ret = bt_ctf_event_set_header(writer_event, writer_event_header);
610 BT_PUT(writer_event_header);
611 if (ret < 0) {
612 fprintf(err, "[error] %s in %s:%d\n", __func__,
613 __FILE__, __LINE__);
614 goto error;
615 }
616
617 ret = 0;
618
619 goto end;
620
621 error:
622 ret = -1;
623 end:
624 return ret;
625 }
626
627 BT_HIDDEN
628 struct bt_ctf_event *ctf_copy_event(FILE *err, struct bt_ctf_event *event,
629 struct bt_ctf_event_class *writer_event_class,
630 bool override_ts64)
631 {
632 struct bt_ctf_event *writer_event = NULL;
633 struct bt_ctf_field *field = NULL, *copy_field = NULL;
634 int ret;
635
636 writer_event = bt_ctf_event_create(writer_event_class);
637 if (!writer_event) {
638 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
639 __LINE__);
640 goto end;
641 }
642
643 field = bt_ctf_event_get_header(event);
644 if (!field) {
645 fprintf(err, "[error] %s in %s:%d\n", __func__,
646 __FILE__, __LINE__);
647 goto error;
648 }
649
650 /*
651 * If override_ts64, we override all integer fields mapped to a clock
652 * to a uint64_t field type, otherwise, we just copy it as is.
653 */
654 if (override_ts64) {
655 copy_field = bt_ctf_event_get_header(writer_event);
656 if (!copy_field) {
657 fprintf(err, "[error] %s in %s:%d\n", __func__,
658 __FILE__, __LINE__);
659 goto error;
660 }
661
662 ret = copy_override_field(err, event, writer_event, field,
663 copy_field);
664 if (ret) {
665 fprintf(err, "[error] %s in %s:%d\n", __func__,
666 __FILE__, __LINE__);
667 goto error;
668 }
669 BT_PUT(copy_field);
670 } else {
671 ret = ctf_copy_event_header(err, event, writer_event_class,
672 writer_event, field);
673 if (ret) {
674 fprintf(err, "[error] %s in %s:%d\n", __func__,
675 __FILE__, __LINE__);
676 goto error;
677 }
678 }
679 BT_PUT(field);
680
681 /* Optional field, so it can fail silently. */
682 field = bt_ctf_event_get_stream_event_context(event);
683 if (field) {
684 copy_field = bt_ctf_field_copy(field);
685 if (!copy_field) {
686 fprintf(err, "[error] %s in %s:%d\n", __func__,
687 __FILE__, __LINE__);
688 goto error;
689 }
690 ret = bt_ctf_event_set_stream_event_context(writer_event,
691 copy_field);
692 if (ret < 0) {
693 fprintf(err, "[error] %s in %s:%d\n", __func__,
694 __FILE__, __LINE__);
695 goto error;
696 }
697 BT_PUT(field);
698 BT_PUT(copy_field);
699 }
700
701 /* Optional field, so it can fail silently. */
702 field = bt_ctf_event_get_event_context(event);
703 if (field) {
704 copy_field = bt_ctf_field_copy(field);
705 if (!copy_field) {
706 fprintf(err, "[error] %s in %s:%d\n", __func__,
707 __FILE__, __LINE__);
708 goto error;
709 }
710 ret = bt_ctf_event_set_event_context(writer_event, copy_field);
711 if (ret < 0) {
712 fprintf(err, "[error] %s in %s:%d\n", __func__,
713 __FILE__, __LINE__);
714 goto error;
715 }
716 BT_PUT(field);
717 BT_PUT(copy_field);
718 }
719
720 field = bt_ctf_event_get_event_payload(event);
721 if (field) {
722 copy_field = bt_ctf_field_copy(field);
723 if (!copy_field) {
724 fprintf(err, "[error] %s in %s:%d\n", __func__,
725 __FILE__, __LINE__);
726 goto error;
727 }
728 ret = bt_ctf_event_set_event_payload(writer_event, copy_field);
729 if (ret < 0) {
730 fprintf(err, "[error] %s in %s:%d\n", __func__,
731 __FILE__, __LINE__);
732 goto error;
733 }
734 BT_PUT(field);
735 BT_PUT(copy_field);
736 }
737
738 goto end;
739
740 error:
741 BT_PUT(writer_event);
742 end:
743 bt_put(field);
744 bt_put(copy_field);
745 return writer_event;
746 }
747
748 BT_HIDDEN
749 enum bt_component_status ctf_copy_trace(FILE *err, struct bt_ctf_trace *trace,
750 struct bt_ctf_trace *writer_trace)
751 {
752 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
753 int field_count, i, int_ret;
754 struct bt_ctf_field_type *header_type = NULL;
755 enum bt_ctf_byte_order order;
756 const char *trace_name;
757
758 field_count = bt_ctf_trace_get_environment_field_count(trace);
759 for (i = 0; i < field_count; i++) {
760 int ret_int;
761 const char *name;
762 struct bt_value *value = NULL;
763
764 name = bt_ctf_trace_get_environment_field_name_by_index(
765 trace, i);
766 if (!name) {
767 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
768 __LINE__);
769 ret = BT_COMPONENT_STATUS_ERROR;
770 goto end;
771 }
772 value = bt_ctf_trace_get_environment_field_value_by_index(
773 trace, i);
774 if (!value) {
775 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
776 __LINE__);
777 ret = BT_COMPONENT_STATUS_ERROR;
778 goto end;
779 }
780
781 ret_int = bt_ctf_trace_set_environment_field(writer_trace,
782 name, value);
783 BT_PUT(value);
784 if (ret_int < 0) {
785 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
786 __LINE__);
787 fprintf(err, "[error] Unable to set environment field %s\n",
788 name);
789 ret = BT_COMPONENT_STATUS_ERROR;
790 goto end;
791 }
792 }
793
794 order = bt_ctf_trace_get_native_byte_order(trace);
795 if (order == BT_CTF_BYTE_ORDER_UNKNOWN) {
796 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
797 ret = BT_COMPONENT_STATUS_ERROR;
798 goto end;
799 }
800
801 /*
802 * Only explicitly set the writer trace's native byte order if
803 * the original trace has a specific one. Otherwise leave what
804 * the CTF writer object chooses, which is the machine's native
805 * byte order.
806 */
807 if (order != BT_CTF_BYTE_ORDER_UNSPECIFIED) {
808 ret = bt_ctf_trace_set_native_byte_order(writer_trace, order);
809 if (ret) {
810 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
811 ret = BT_COMPONENT_STATUS_ERROR;
812 goto end;
813 }
814 }
815
816 header_type = bt_ctf_trace_get_packet_header_type(writer_trace);
817 if (header_type) {
818 int_ret = bt_ctf_trace_set_packet_header_type(writer_trace, header_type);
819 BT_PUT(header_type);
820 if (int_ret < 0) {
821 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
822 ret = BT_COMPONENT_STATUS_ERROR;
823 goto end;
824 }
825 }
826
827 trace_name = bt_ctf_trace_get_name(trace);
828 if (trace_name) {
829 int_ret = bt_ctf_trace_set_name(writer_trace, trace_name);
830 if (int_ret < 0) {
831 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
832 ret = BT_COMPONENT_STATUS_ERROR;
833 goto end;
834 }
835 }
836
837 end:
838 return ret;
839 }
This page took 0.047903 seconds and 4 git commands to generate.