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