b8e281355b05abc44d798b8c337586719f28127e
[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 count, i, ret;
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_by_index(
238 event_class, i);
239 if (!attr_name) {
240 fprintf(err, "[error] %s in %s:%d\n", __func__,
241 __FILE__, __LINE__);
242 goto error;
243 }
244 attr_value = bt_ctf_event_class_get_attribute_value_by_index(
245 event_class, i);
246 if (!attr_value) {
247 fprintf(err, "[error] %s in %s:%d\n", __func__,
248 __FILE__, __LINE__);
249 goto error;
250 }
251
252 ret = bt_ctf_event_class_set_attribute(writer_event_class,
253 attr_name, attr_value);
254 BT_PUT(attr_value);
255 if (ret < 0) {
256 fprintf(err, "[error] %s in %s:%d\n", __func__,
257 __FILE__, __LINE__);
258 goto error;
259 }
260 }
261
262 payload_type = bt_ctf_event_class_get_payload_type(event_class);
263 if (payload_type) {
264 ret = bt_ctf_event_class_set_payload_type(writer_event_class,
265 payload_type);
266 if (ret < 0) {
267 fprintf(err, "[error] %s in %s:%d\n", __func__,
268 __FILE__, __LINE__);
269 goto error;
270 }
271 BT_PUT(payload_type);
272 }
273
274 context = bt_ctf_event_class_get_context_type(event_class);
275 if (context) {
276 ret = bt_ctf_event_class_set_context_type(
277 writer_event_class, context);
278 BT_PUT(context);
279 if (ret < 0) {
280 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
281 __LINE__);
282 goto error;
283 }
284 }
285
286 goto end;
287
288 error:
289 BT_PUT(writer_event_class);
290 end:
291 return writer_event_class;
292 }
293
294 BT_HIDDEN
295 enum bt_component_status ctf_copy_event_classes(FILE *err,
296 struct bt_ctf_stream_class *stream_class,
297 struct bt_ctf_stream_class *writer_stream_class)
298 {
299 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
300 struct bt_ctf_event_class *event_class = NULL, *writer_event_class = NULL;
301 int count, i;
302
303 count = bt_ctf_stream_class_get_event_class_count(stream_class);
304 if (count < 0) {
305 fprintf(err, "[error] %s in %s:%d\n", __func__,
306 __FILE__, __LINE__);
307 goto end;
308 }
309
310 for (i = 0; i < count; i++) {
311 int int_ret;
312
313 event_class = bt_ctf_stream_class_get_event_class_by_index(
314 stream_class, i);
315 if (!event_class) {
316 fprintf(err, "[error] %s in %s:%d\n", __func__,
317 __FILE__, __LINE__);
318 ret = BT_COMPONENT_STATUS_ERROR;
319 goto error;
320 }
321 if (i < bt_ctf_stream_class_get_event_class_count(writer_stream_class)) {
322 writer_event_class = bt_ctf_stream_class_get_event_class_by_index(
323 writer_stream_class, i);
324 if (writer_event_class) {
325 /*
326 * If the writer_event_class already exists,
327 * just skip it. It can be used to resync the
328 * event_classes after a trace has become
329 * static.
330 */
331 BT_PUT(writer_event_class);
332 BT_PUT(event_class);
333 continue;
334 }
335 }
336
337 writer_event_class = ctf_copy_event_class(err, event_class);
338 if (!writer_event_class) {
339 fprintf(err, "[error] %s in %s:%d\n", __func__,
340 __FILE__, __LINE__);
341 ret = BT_COMPONENT_STATUS_ERROR;
342 goto error;
343 }
344
345 int_ret = bt_ctf_stream_class_add_event_class(writer_stream_class,
346 writer_event_class);
347 if (int_ret < 0) {
348 fprintf(err, "[error] Failed to add event class\n");
349 fprintf(err, "[error] %s in %s:%d\n", __func__,
350 __FILE__, __LINE__);
351 ret = BT_COMPONENT_STATUS_ERROR;
352 goto error;
353 }
354 BT_PUT(writer_event_class);
355 BT_PUT(event_class);
356 }
357
358 goto end;
359
360 error:
361 bt_put(event_class);
362 bt_put(writer_event_class);
363 end:
364 return ret;
365 }
366
367 BT_HIDDEN
368 struct bt_ctf_stream_class *ctf_copy_stream_class(FILE *err,
369 struct bt_ctf_stream_class *stream_class,
370 struct bt_ctf_trace *writer_trace,
371 bool override_ts64)
372 {
373 struct bt_ctf_field_type *type = NULL;
374 struct bt_ctf_stream_class *writer_stream_class = NULL;
375 int ret_int;
376 const char *name = bt_ctf_stream_class_get_name(stream_class);
377
378 if (strlen(name) == 0) {
379 name = NULL;
380 }
381
382 writer_stream_class = bt_ctf_stream_class_create(name);
383 if (!writer_stream_class) {
384 fprintf(err, "[error] %s in %s:%d\n",
385 __func__, __FILE__, __LINE__);
386 goto end;
387 }
388
389 type = bt_ctf_stream_class_get_packet_context_type(stream_class);
390 if (!type) {
391 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
392 __LINE__);
393 goto error;
394 }
395
396 ret_int = bt_ctf_stream_class_set_packet_context_type(
397 writer_stream_class, type);
398 if (ret_int < 0) {
399 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
400 __LINE__);
401 goto error;
402 }
403 BT_PUT(type);
404
405 type = bt_ctf_stream_class_get_event_header_type(stream_class);
406 if (!type) {
407 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
408 __LINE__);
409 goto error;
410 }
411
412 if (override_ts64) {
413 struct bt_ctf_field_type *new_event_header_type;
414
415 new_event_header_type = override_header_type(err, type,
416 writer_trace);
417 if (!new_event_header_type) {
418 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
419 __LINE__);
420 goto error;
421 }
422 ret_int = bt_ctf_stream_class_set_event_header_type(
423 writer_stream_class, new_event_header_type);
424 BT_PUT(new_event_header_type);
425 if (ret_int < 0) {
426 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
427 __LINE__);
428 goto error;
429 }
430 } else {
431 ret_int = bt_ctf_stream_class_set_event_header_type(
432 writer_stream_class, type);
433 if (ret_int < 0) {
434 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
435 __LINE__);
436 goto error;
437 }
438 }
439 BT_PUT(type);
440
441 type = bt_ctf_stream_class_get_event_context_type(stream_class);
442 if (type) {
443 ret_int = bt_ctf_stream_class_set_event_context_type(
444 writer_stream_class, type);
445 if (ret_int < 0) {
446 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
447 __LINE__);
448 goto error;
449 }
450 }
451 BT_PUT(type);
452
453 goto end;
454
455 error:
456 BT_PUT(writer_stream_class);
457 end:
458 bt_put(type);
459 return writer_stream_class;
460 }
461
462 BT_HIDDEN
463 enum bt_component_status ctf_copy_packet_context_field(FILE *err,
464 struct bt_ctf_field *field, const char *field_name,
465 struct bt_ctf_field *writer_packet_context,
466 struct bt_ctf_field_type *writer_packet_context_type)
467 {
468 enum bt_component_status ret;
469 struct bt_ctf_field *writer_field = NULL;
470 struct bt_ctf_field_type *field_type = NULL;
471 int int_ret;
472 uint64_t value;
473
474 field_type = bt_ctf_field_get_type(field);
475 if (!field_type) {
476 ret = BT_COMPONENT_STATUS_ERROR;
477 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
478 __LINE__);
479 goto end;
480 }
481 /*
482 * Only support for integers for now.
483 */
484 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
485 fprintf(err, "[error] Unsupported packet context field type\n");
486 ret = BT_COMPONENT_STATUS_ERROR;
487 goto error;
488 }
489 BT_PUT(field_type);
490
491 writer_field = bt_ctf_field_structure_get_field_by_name(
492 writer_packet_context, field_name);
493 if (!writer_field) {
494 ret = BT_COMPONENT_STATUS_ERROR;
495 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
496 __LINE__);
497 goto end;
498 }
499
500 int_ret = bt_ctf_field_unsigned_integer_get_value(field, &value);
501 if (int_ret < 0) {
502 fprintf(err, "[error] Wrong packet_context field type\n");
503 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
504 __LINE__);
505 ret = BT_COMPONENT_STATUS_ERROR;
506 goto end;
507 }
508
509 int_ret = bt_ctf_field_unsigned_integer_set_value(writer_field, value);
510 if (int_ret < 0) {
511 ret = BT_COMPONENT_STATUS_ERROR;
512 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
513 __LINE__);
514 goto end;
515 }
516
517 ret = BT_COMPONENT_STATUS_OK;
518
519 goto end;
520
521 error:
522 bt_put(field_type);
523 end:
524 bt_put(writer_field);
525 return ret;
526 }
527
528 BT_HIDDEN
529 struct bt_ctf_field *ctf_copy_packet_context(FILE *err,
530 struct bt_ctf_packet *packet,
531 struct bt_ctf_stream *writer_stream)
532 {
533 enum bt_component_status ret;
534 struct bt_ctf_field *packet_context = NULL, *writer_packet_context = NULL;
535 struct bt_ctf_field_type *struct_type = NULL, *writer_packet_context_type = NULL;
536 struct bt_ctf_stream_class *writer_stream_class = NULL;
537 struct bt_ctf_field *field = NULL;
538 struct bt_ctf_field_type *field_type = NULL;
539 int nr_fields, i;
540
541 packet_context = bt_ctf_packet_get_context(packet);
542 if (!packet_context) {
543 goto end;
544 }
545
546 writer_stream_class = bt_ctf_stream_get_class(writer_stream);
547 if (!writer_stream_class) {
548 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
549 __LINE__);
550 goto error;
551 }
552
553 writer_packet_context_type = bt_ctf_stream_class_get_packet_context_type(
554 writer_stream_class);
555 BT_PUT(writer_stream_class);
556 if (!writer_packet_context_type) {
557 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
558 __LINE__);
559 goto error;
560 }
561
562 struct_type = bt_ctf_field_get_type(packet_context);
563 if (!struct_type) {
564 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
565 __LINE__);
566 goto error;
567 }
568
569 writer_packet_context = bt_ctf_field_create(writer_packet_context_type);
570 if (!writer_packet_context) {
571 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
572 __LINE__);
573 goto error;
574 }
575
576 nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type);
577 for (i = 0; i < nr_fields; i++) {
578 const char *field_name;
579
580 field = bt_ctf_field_structure_get_field_by_index(
581 packet_context, i);
582 if (!field) {
583 fprintf(err, "[error] %s in %s:%d\n", __func__,
584 __FILE__, __LINE__);
585 goto error;
586 }
587 if (bt_ctf_field_type_structure_get_field_by_index(struct_type,
588 &field_name, &field_type, i) < 0) {
589 fprintf(err, "[error] %s in %s:%d\n", __func__,
590 __FILE__, __LINE__);
591 goto error;
592 }
593
594 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
595 fprintf(err, "[error] Unexpected packet context field type\n");
596 goto error;
597 }
598
599 ret = ctf_copy_packet_context_field(err, field, field_name,
600 writer_packet_context, writer_packet_context_type);
601 BT_PUT(field_type);
602 BT_PUT(field);
603 if (ret != BT_COMPONENT_STATUS_OK) {
604 fprintf(err, "[error] %s in %s:%d\n", __func__,
605 __FILE__, __LINE__);
606 goto error;
607 }
608 }
609
610 goto end;
611
612 error:
613 BT_PUT(writer_packet_context);
614 end:
615 bt_put(field);
616 bt_put(field_type);
617 bt_put(struct_type);
618 bt_put(writer_packet_context_type);
619 bt_put(writer_stream_class);
620 bt_put(packet_context);
621 return writer_packet_context;
622 }
623
624 BT_HIDDEN
625 int ctf_copy_event_header(FILE *err, struct bt_ctf_event *event,
626 struct bt_ctf_event_class *writer_event_class,
627 struct bt_ctf_event *writer_event,
628 struct bt_ctf_field *event_header)
629 {
630 struct bt_ctf_clock_class *clock_class = NULL, *writer_clock_class = NULL;
631 struct bt_ctf_clock_value *clock_value = NULL, *writer_clock_value = NULL;
632
633 int ret;
634 struct bt_ctf_field *writer_event_header = NULL;
635 uint64_t value;
636
637 clock_class = event_get_clock_class(err, event);
638 if (!clock_class) {
639 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
640 __LINE__);
641 goto error;
642 }
643
644 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
645 BT_PUT(clock_class);
646 if (!clock_value) {
647 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
648 __LINE__);
649 goto error;
650 }
651
652 ret = bt_ctf_clock_value_get_value(clock_value, &value);
653 BT_PUT(clock_value);
654 if (ret) {
655 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
656 __LINE__);
657 goto error;
658 }
659
660 writer_clock_class = event_get_clock_class(err, writer_event);
661 if (!writer_clock_class) {
662 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
663 __LINE__);
664 goto error;
665 }
666
667 writer_clock_value = bt_ctf_clock_value_create(writer_clock_class, value);
668 BT_PUT(writer_clock_class);
669 if (!writer_clock_value) {
670 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
671 __LINE__);
672 goto error;
673 }
674
675 ret = bt_ctf_event_set_clock_value(writer_event, writer_clock_value);
676 BT_PUT(writer_clock_value);
677 if (ret) {
678 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
679 __LINE__);
680 goto error;
681 }
682
683 writer_event_header = bt_ctf_field_copy(event_header);
684 if (!writer_event_header) {
685 fprintf(err, "[error] %s in %s:%d\n", __func__,
686 __FILE__, __LINE__);
687 goto end;
688 }
689
690 ret = bt_ctf_event_set_header(writer_event, writer_event_header);
691 BT_PUT(writer_event_header);
692 if (ret < 0) {
693 fprintf(err, "[error] %s in %s:%d\n", __func__,
694 __FILE__, __LINE__);
695 goto error;
696 }
697
698 ret = 0;
699
700 goto end;
701
702 error:
703 ret = -1;
704 end:
705 return ret;
706 }
707
708 BT_HIDDEN
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_event_payload(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_event_payload(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 BT_HIDDEN
820 enum bt_component_status ctf_copy_trace(FILE *err, struct bt_ctf_trace *trace,
821 struct bt_ctf_trace *writer_trace)
822 {
823 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
824 int field_count, i, int_ret;
825 struct bt_ctf_field_type *header_type = NULL;
826 enum bt_ctf_byte_order order;
827 const char *trace_name;
828
829 field_count = bt_ctf_trace_get_environment_field_count(trace);
830 for (i = 0; i < field_count; i++) {
831 int ret_int;
832 const char *name;
833 struct bt_value *value = NULL;
834
835 name = bt_ctf_trace_get_environment_field_name_by_index(
836 trace, i);
837 if (!name) {
838 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
839 __LINE__);
840 ret = BT_COMPONENT_STATUS_ERROR;
841 goto end;
842 }
843 value = bt_ctf_trace_get_environment_field_value_by_index(
844 trace, i);
845 if (!value) {
846 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
847 __LINE__);
848 ret = BT_COMPONENT_STATUS_ERROR;
849 goto end;
850 }
851
852 ret_int = bt_ctf_trace_set_environment_field(writer_trace,
853 name, value);
854 BT_PUT(value);
855 if (ret_int < 0) {
856 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
857 __LINE__);
858 fprintf(err, "[error] Unable to set environment field %s\n",
859 name);
860 ret = BT_COMPONENT_STATUS_ERROR;
861 goto end;
862 }
863 }
864
865 order = bt_ctf_trace_get_native_byte_order(trace);
866 if (order == BT_CTF_BYTE_ORDER_UNKNOWN) {
867 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
868 ret = BT_COMPONENT_STATUS_ERROR;
869 goto end;
870 }
871
872 /*
873 * Only explicitly set the writer trace's native byte order if
874 * the original trace has a specific one. Otherwise leave what
875 * the CTF writer object chooses, which is the machine's native
876 * byte order.
877 */
878 if (order != BT_CTF_BYTE_ORDER_NONE) {
879 ret = bt_ctf_trace_set_native_byte_order(writer_trace, order);
880 if (ret) {
881 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
882 ret = BT_COMPONENT_STATUS_ERROR;
883 goto end;
884 }
885 }
886
887 header_type = bt_ctf_trace_get_packet_header_type(writer_trace);
888 if (header_type) {
889 int_ret = bt_ctf_trace_set_packet_header_type(writer_trace, header_type);
890 BT_PUT(header_type);
891 if (int_ret < 0) {
892 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
893 ret = BT_COMPONENT_STATUS_ERROR;
894 goto end;
895 }
896 }
897
898 trace_name = bt_ctf_trace_get_name(trace);
899 if (trace_name) {
900 int_ret = bt_ctf_trace_set_name(writer_trace, trace_name);
901 if (int_ret < 0) {
902 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
903 ret = BT_COMPONENT_STATUS_ERROR;
904 goto end;
905 }
906 }
907
908 end:
909 return ret;
910 }
This page took 0.047692 seconds and 3 git commands to generate.