4 * Babeltrace library to create a copy of a CTF trace
6 * Copyright 2017 Julien Desfossez <jdesfossez@efficios.com>
8 * Author: Julien Desfossez <jdesfossez@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
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>
39 #include "ctfcopytrace.h"
40 #include "clock-fields.h"
42 struct bt_ctf_clock_class
*ctf_copy_clock_class(FILE *err
,
43 struct bt_ctf_clock_class
*clock_class
)
45 int64_t offset
, offset_s
;
48 const char *name
, *description
;
49 struct bt_ctf_clock_class
*writer_clock_class
= NULL
;
51 assert(err
&& clock_class
);
53 name
= bt_ctf_clock_class_get_name(clock_class
);
55 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
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__
,
67 description
= bt_ctf_clock_class_get_description(clock_class
);
69 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
74 int_ret
= bt_ctf_clock_class_set_description(writer_clock_class
,
77 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
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__
,
88 int_ret
= bt_ctf_clock_class_set_frequency(writer_clock_class
, u64_ret
);
90 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
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__
,
101 int_ret
= bt_ctf_clock_class_set_precision(writer_clock_class
,
104 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
109 int_ret
= bt_ctf_clock_class_get_offset_s(clock_class
, &offset_s
);
111 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
116 int_ret
= bt_ctf_clock_class_set_offset_s(writer_clock_class
, offset_s
);
118 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
123 int_ret
= bt_ctf_clock_class_get_offset_cycles(clock_class
, &offset
);
125 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
130 int_ret
= bt_ctf_clock_class_set_offset_cycles(writer_clock_class
, offset
);
132 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
137 int_ret
= bt_ctf_clock_class_get_is_absolute(clock_class
);
139 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
144 int_ret
= bt_ctf_clock_class_set_is_absolute(writer_clock_class
, int_ret
);
146 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
154 BT_PUT(writer_clock_class
);
156 return writer_clock_class
;
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
)
164 enum bt_component_status ret
;
165 int int_ret
, clock_class_count
, i
;
167 clock_class_count
= bt_ctf_trace_get_clock_class_count(trace
);
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
);
175 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
177 ret
= BT_COMPONENT_STATUS_ERROR
;
181 writer_clock_class
= ctf_copy_clock_class(err
, clock_class
);
183 if (!writer_clock_class
) {
184 fprintf(err
, "Failed to copy clock class");
185 ret
= BT_COMPONENT_STATUS_ERROR
;
189 int_ret
= bt_ctf_trace_add_clock_class(writer_trace
, writer_clock_class
);
191 BT_PUT(writer_clock_class
);
192 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
194 ret
= BT_COMPONENT_STATUS_ERROR
;
199 * Ownership transferred to the trace.
201 bt_put(writer_clock_class
);
204 ret
= BT_COMPONENT_STATUS_OK
;
210 struct bt_ctf_event_class
*ctf_copy_event_class(FILE *err
,
211 struct bt_ctf_event_class
*event_class
)
213 struct bt_ctf_event_class
*writer_event_class
= NULL
;
217 name
= bt_ctf_event_class_get_name(event_class
);
219 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
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__
,
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
;
237 attr_name
= bt_ctf_event_class_get_attribute_name(event_class
, i
);
239 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
243 attr_value
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
245 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
250 ret
= bt_ctf_event_class_set_attribute(writer_event_class
,
251 attr_name
, attr_value
);
254 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
260 count
= bt_ctf_event_class_get_field_count(event_class
);
261 for (i
= 0; i
< count
; i
++) {
262 const char *field_name
;
263 struct bt_ctf_field_type
*field_type
;
266 ret
= bt_ctf_event_class_get_field(event_class
, &field_name
,
269 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
273 ret
= bt_ctf_event_class_add_field(writer_event_class
, field_type
,
277 fprintf(err
, "[error] Cannot add field %s\n", field_name
);
278 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
286 BT_PUT(writer_event_class
);
288 return writer_event_class
;
291 enum bt_component_status
ctf_copy_event_classes(FILE *err
,
292 struct bt_ctf_stream_class
*stream_class
,
293 struct bt_ctf_stream_class
*writer_stream_class
)
295 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
296 struct bt_ctf_event_class
*event_class
= NULL
, *writer_event_class
= NULL
;
299 count
= bt_ctf_stream_class_get_event_class_count(stream_class
);
301 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
306 for (i
= 0; i
< count
; i
++) {
307 struct bt_ctf_field_type
*context
;
310 event_class
= bt_ctf_stream_class_get_event_class(
313 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
315 ret
= BT_COMPONENT_STATUS_ERROR
;
318 writer_event_class
= ctf_copy_event_class(err
, event_class
);
319 if (!writer_event_class
) {
320 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
322 ret
= BT_COMPONENT_STATUS_ERROR
;
326 context
= bt_ctf_event_class_get_context_type(event_class
);
328 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
330 ret
= BT_COMPONENT_STATUS_ERROR
;
333 ret
= bt_ctf_event_class_set_context_type(writer_event_class
, context
);
336 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
341 int_ret
= bt_ctf_stream_class_add_event_class(writer_stream_class
,
344 fprintf(err
, "[error] Failed to add event class\n");
345 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
347 ret
= BT_COMPONENT_STATUS_ERROR
;
350 BT_PUT(writer_event_class
);
358 bt_put(writer_event_class
);
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
,
368 struct bt_ctf_field_type
*type
= NULL
;
369 struct bt_ctf_stream_class
*writer_stream_class
= NULL
;
371 const char *name
= bt_ctf_stream_class_get_name(stream_class
);
373 if (strlen(name
) == 0) {
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__
);
384 type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
386 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
391 ret_int
= bt_ctf_stream_class_set_packet_context_type(
392 writer_stream_class
, type
);
394 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
400 type
= bt_ctf_stream_class_get_event_header_type(stream_class
);
402 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
408 struct bt_ctf_field_type
*new_event_header_type
;
410 new_event_header_type
= override_header_type(err
, type
,
412 if (!new_event_header_type
) {
413 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
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
);
421 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
426 ret_int
= bt_ctf_stream_class_set_event_header_type(
427 writer_stream_class
, type
);
429 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
436 type
= bt_ctf_stream_class_get_event_context_type(stream_class
);
438 ret_int
= bt_ctf_stream_class_set_event_context_type(
439 writer_stream_class
, type
);
441 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
451 BT_PUT(writer_stream_class
);
454 return writer_stream_class
;
457 enum bt_component_status
ctf_copy_packet_context_field(FILE *err
,
458 struct bt_ctf_field
*field
, const char *field_name
,
459 struct bt_ctf_field
*writer_packet_context
,
460 struct bt_ctf_field_type
*writer_packet_context_type
)
462 enum bt_component_status ret
;
463 struct bt_ctf_field
*writer_field
= NULL
;
464 struct bt_ctf_field_type
*field_type
= NULL
;
468 field_type
= bt_ctf_field_get_type(field
);
470 ret
= BT_COMPONENT_STATUS_ERROR
;
471 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
476 * Only support for integers for now.
478 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
479 fprintf(err
, "[error] Unsupported packet context field type\n");
480 ret
= BT_COMPONENT_STATUS_ERROR
;
485 writer_field
= bt_ctf_field_structure_get_field(writer_packet_context
,
488 ret
= BT_COMPONENT_STATUS_ERROR
;
489 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
494 int_ret
= bt_ctf_field_unsigned_integer_get_value(field
, &value
);
496 fprintf(err
, "[error] Wrong packet_context field type\n");
497 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
499 ret
= BT_COMPONENT_STATUS_ERROR
;
503 int_ret
= bt_ctf_field_unsigned_integer_set_value(writer_field
, value
);
505 ret
= BT_COMPONENT_STATUS_ERROR
;
506 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
511 ret
= BT_COMPONENT_STATUS_OK
;
518 bt_put(writer_field
);
522 struct bt_ctf_field
*ctf_copy_packet_context(FILE *err
,
523 struct bt_ctf_packet
*packet
,
524 struct bt_ctf_stream
*writer_stream
)
526 enum bt_component_status ret
;
527 struct bt_ctf_field
*packet_context
= NULL
, *writer_packet_context
= NULL
;
528 struct bt_ctf_field_type
*struct_type
= NULL
, *writer_packet_context_type
= NULL
;
529 struct bt_ctf_stream_class
*writer_stream_class
= NULL
;
530 struct bt_ctf_field
*field
= NULL
;
531 struct bt_ctf_field_type
*field_type
;
534 packet_context
= bt_ctf_packet_get_context(packet
);
535 if (!packet_context
) {
536 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
541 writer_stream_class
= bt_ctf_stream_get_class(writer_stream
);
542 if (!writer_stream_class
) {
543 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
548 writer_packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
549 writer_stream_class
);
550 BT_PUT(writer_stream_class
);
551 if (!writer_packet_context_type
) {
552 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
557 struct_type
= bt_ctf_field_get_type(packet_context
);
559 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
564 writer_packet_context
= bt_ctf_field_create(writer_packet_context_type
);
565 if (!writer_packet_context
) {
566 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
571 nr_fields
= bt_ctf_field_type_structure_get_field_count(struct_type
);
572 for (i
= 0; i
< nr_fields
; i
++) {
573 const char *field_name
;
575 field
= bt_ctf_field_structure_get_field_by_index(
578 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
582 if (bt_ctf_field_type_structure_get_field(struct_type
,
583 &field_name
, &field_type
, i
) < 0) {
584 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
588 if (!strncmp(field_name
, "content_size", strlen("content_size")) ||
589 !strncmp(field_name
, "packet_size",
590 strlen("packet_size"))) {
596 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
597 fprintf(err
, "[error] Unexpected packet context field type\n");
601 ret
= ctf_copy_packet_context_field(err
, field
, field_name
,
602 writer_packet_context
, writer_packet_context_type
);
605 if (ret
!= BT_COMPONENT_STATUS_OK
) {
606 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
615 BT_PUT(writer_packet_context
);
620 bt_put(writer_packet_context_type
);
621 bt_put(writer_stream_class
);
622 bt_put(packet_context
);
623 return writer_packet_context
;
626 int ctf_copy_event_header(FILE *err
, struct bt_ctf_event
*event
,
627 struct bt_ctf_event_class
*writer_event_class
,
628 struct bt_ctf_event
*writer_event
,
629 struct bt_ctf_field
*event_header
)
631 struct bt_ctf_clock_class
*clock_class
= NULL
, *writer_clock_class
= NULL
;
632 struct bt_ctf_clock_value
*clock_value
= NULL
, *writer_clock_value
= NULL
;
635 struct bt_ctf_field
*writer_event_header
= NULL
;
638 clock_class
= event_get_clock_class(err
, event
);
640 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
645 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
648 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
653 ret
= bt_ctf_clock_value_get_value(clock_value
, &value
);
656 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
661 writer_clock_class
= event_get_clock_class(err
, writer_event
);
662 if (!writer_clock_class
) {
663 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
668 writer_clock_value
= bt_ctf_clock_value_create(writer_clock_class
, value
);
669 BT_PUT(writer_clock_class
);
670 if (!writer_clock_value
) {
671 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
676 ret
= bt_ctf_event_set_clock_value(writer_event
, writer_clock_value
);
677 BT_PUT(writer_clock_value
);
679 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
684 writer_event_header
= bt_ctf_field_copy(event_header
);
685 if (!writer_event_header
) {
686 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
691 ret
= bt_ctf_event_set_header(writer_event
, writer_event_header
);
692 BT_PUT(writer_event_header
);
694 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
709 struct bt_ctf_event
*ctf_copy_event(FILE *err
, struct bt_ctf_event
*event
,
710 struct bt_ctf_event_class
*writer_event_class
,
713 struct bt_ctf_event
*writer_event
= NULL
;
714 struct bt_ctf_field
*field
= NULL
, *copy_field
= NULL
;
717 writer_event
= bt_ctf_event_create(writer_event_class
);
719 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
724 field
= bt_ctf_event_get_header(event
);
726 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
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.
736 copy_field
= bt_ctf_event_get_header(writer_event
);
738 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
743 ret
= copy_override_field(err
, event
, writer_event
, field
,
746 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
752 ret
= ctf_copy_event_header(err
, event
, writer_event_class
,
753 writer_event
, field
);
755 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
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
);
766 ret
= bt_ctf_event_set_stream_event_context(writer_event
,
769 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
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
);
781 ret
= bt_ctf_event_set_event_context(writer_event
, copy_field
);
783 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
791 field
= bt_ctf_event_get_payload_field(event
);
793 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
797 copy_field
= bt_ctf_field_copy(field
);
799 ret
= bt_ctf_event_set_payload_field(writer_event
, copy_field
);
801 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
812 BT_PUT(writer_event
);
819 enum bt_component_status
ctf_copy_trace(FILE *err
, struct bt_ctf_trace
*trace
,
820 struct bt_ctf_trace
*writer_trace
)
822 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
823 int field_count
, i
, int_ret
;
824 struct bt_ctf_field_type
*header_type
= NULL
;
826 field_count
= bt_ctf_trace_get_environment_field_count(trace
);
827 for (i
= 0; i
< field_count
; i
++) {
830 struct bt_value
*value
= NULL
;
832 name
= bt_ctf_trace_get_environment_field_name(trace
, i
);
834 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
836 ret
= BT_COMPONENT_STATUS_ERROR
;
839 value
= bt_ctf_trace_get_environment_field_value(trace
, i
);
841 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
843 ret
= BT_COMPONENT_STATUS_ERROR
;
847 ret_int
= bt_ctf_trace_set_environment_field(writer_trace
,
851 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
853 fprintf(err
, "[error] Unable to set environment field %s\n",
855 ret
= BT_COMPONENT_STATUS_ERROR
;
860 header_type
= bt_ctf_trace_get_packet_header_type(writer_trace
);
862 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
863 ret
= BT_COMPONENT_STATUS_ERROR
;
867 int_ret
= bt_ctf_trace_set_packet_header_type(writer_trace
, header_type
);
870 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
871 ret
= BT_COMPONENT_STATUS_ERROR
;