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