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