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"
43 struct bt_ctf_clock_class
*ctf_copy_clock_class(FILE *err
,
44 struct bt_ctf_clock_class
*clock_class
)
46 int64_t offset
, offset_s
;
49 const char *name
, *description
;
50 struct bt_ctf_clock_class
*writer_clock_class
= NULL
;
52 assert(err
&& clock_class
);
54 name
= bt_ctf_clock_class_get_name(clock_class
);
56 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
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__
,
68 description
= bt_ctf_clock_class_get_description(clock_class
);
70 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
75 int_ret
= bt_ctf_clock_class_set_description(writer_clock_class
,
78 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
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__
,
89 int_ret
= bt_ctf_clock_class_set_frequency(writer_clock_class
, u64_ret
);
91 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
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__
,
102 int_ret
= bt_ctf_clock_class_set_precision(writer_clock_class
,
105 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
110 int_ret
= bt_ctf_clock_class_get_offset_s(clock_class
, &offset_s
);
112 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
117 int_ret
= bt_ctf_clock_class_set_offset_s(writer_clock_class
, offset_s
);
119 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
124 int_ret
= bt_ctf_clock_class_get_offset_cycles(clock_class
, &offset
);
126 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
131 int_ret
= bt_ctf_clock_class_set_offset_cycles(writer_clock_class
, offset
);
133 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
138 int_ret
= bt_ctf_clock_class_is_absolute(clock_class
);
140 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
145 int_ret
= bt_ctf_clock_class_set_is_absolute(writer_clock_class
, int_ret
);
147 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
155 BT_PUT(writer_clock_class
);
157 return writer_clock_class
;
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
)
166 enum bt_component_status ret
;
167 int int_ret
, clock_class_count
, i
;
169 clock_class_count
= bt_ctf_trace_get_clock_class_count(trace
);
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
);
177 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
179 ret
= BT_COMPONENT_STATUS_ERROR
;
183 writer_clock_class
= ctf_copy_clock_class(err
, clock_class
);
185 if (!writer_clock_class
) {
186 fprintf(err
, "Failed to copy clock class");
187 ret
= BT_COMPONENT_STATUS_ERROR
;
191 int_ret
= bt_ctf_trace_add_clock_class(writer_trace
, writer_clock_class
);
193 BT_PUT(writer_clock_class
);
194 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
196 ret
= BT_COMPONENT_STATUS_ERROR
;
201 * Ownership transferred to the trace.
203 bt_put(writer_clock_class
);
206 ret
= BT_COMPONENT_STATUS_OK
;
213 struct bt_ctf_event_class
*ctf_copy_event_class(FILE *err
,
214 struct bt_ctf_event_class
*event_class
)
216 struct bt_ctf_event_class
*writer_event_class
= NULL
;
220 name
= bt_ctf_event_class_get_name(event_class
);
222 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
227 writer_event_class
= bt_ctf_event_class_create(name
);
228 if (!writer_event_class
) {
229 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
234 count
= bt_ctf_event_class_get_attribute_count(event_class
);
235 for (i
= 0; i
< count
; i
++) {
236 const char *attr_name
;
237 struct bt_value
*attr_value
;
240 attr_name
= bt_ctf_event_class_get_attribute_name_by_index(
243 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
247 attr_value
= bt_ctf_event_class_get_attribute_value_by_index(
250 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
255 ret
= bt_ctf_event_class_set_attribute(writer_event_class
,
256 attr_name
, attr_value
);
259 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
265 count
= bt_ctf_event_class_get_payload_type_field_count(event_class
);
266 for (i
= 0; i
< count
; i
++) {
267 const char *field_name
;
268 struct bt_ctf_field_type
*field_type
;
271 ret
= bt_ctf_event_class_get_payload_type_field_by_index(
272 event_class
, &field_name
, &field_type
, i
);
274 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
278 ret
= bt_ctf_event_class_add_field(writer_event_class
, field_type
,
282 fprintf(err
, "[error] Cannot add field %s\n", field_name
);
283 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
291 BT_PUT(writer_event_class
);
293 return writer_event_class
;
297 enum bt_component_status
ctf_copy_event_classes(FILE *err
,
298 struct bt_ctf_stream_class
*stream_class
,
299 struct bt_ctf_stream_class
*writer_stream_class
)
301 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
302 struct bt_ctf_event_class
*event_class
= NULL
, *writer_event_class
= NULL
;
305 count
= bt_ctf_stream_class_get_event_class_count(stream_class
);
307 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
312 for (i
= 0; i
< count
; i
++) {
313 struct bt_ctf_field_type
*context
;
316 event_class
= bt_ctf_stream_class_get_event_class_by_index(
319 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
321 ret
= BT_COMPONENT_STATUS_ERROR
;
324 writer_event_class
= ctf_copy_event_class(err
, event_class
);
325 if (!writer_event_class
) {
326 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
328 ret
= BT_COMPONENT_STATUS_ERROR
;
332 context
= bt_ctf_event_class_get_context_type(event_class
);
334 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
336 ret
= BT_COMPONENT_STATUS_ERROR
;
339 ret
= bt_ctf_event_class_set_context_type(writer_event_class
, context
);
342 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
347 int_ret
= bt_ctf_stream_class_add_event_class(writer_stream_class
,
350 fprintf(err
, "[error] Failed to add event class\n");
351 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
353 ret
= BT_COMPONENT_STATUS_ERROR
;
356 BT_PUT(writer_event_class
);
364 bt_put(writer_event_class
);
370 struct bt_ctf_stream_class
*ctf_copy_stream_class(FILE *err
,
371 struct bt_ctf_stream_class
*stream_class
,
372 struct bt_ctf_trace
*writer_trace
,
375 struct bt_ctf_field_type
*type
= NULL
;
376 struct bt_ctf_stream_class
*writer_stream_class
= NULL
;
378 const char *name
= bt_ctf_stream_class_get_name(stream_class
);
380 if (strlen(name
) == 0) {
384 writer_stream_class
= bt_ctf_stream_class_create(name
);
385 if (!writer_stream_class
) {
386 fprintf(err
, "[error] %s in %s:%d\n",
387 __func__
, __FILE__
, __LINE__
);
391 type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
393 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
398 ret_int
= bt_ctf_stream_class_set_packet_context_type(
399 writer_stream_class
, type
);
401 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
407 type
= bt_ctf_stream_class_get_event_header_type(stream_class
);
409 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
415 struct bt_ctf_field_type
*new_event_header_type
;
417 new_event_header_type
= override_header_type(err
, type
,
419 if (!new_event_header_type
) {
420 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
424 ret_int
= bt_ctf_stream_class_set_event_header_type(
425 writer_stream_class
, new_event_header_type
);
426 BT_PUT(new_event_header_type
);
428 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
433 ret_int
= bt_ctf_stream_class_set_event_header_type(
434 writer_stream_class
, type
);
436 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
443 type
= bt_ctf_stream_class_get_event_context_type(stream_class
);
445 ret_int
= bt_ctf_stream_class_set_event_context_type(
446 writer_stream_class
, type
);
448 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
458 BT_PUT(writer_stream_class
);
461 return writer_stream_class
;
465 enum bt_component_status
ctf_copy_packet_context_field(FILE *err
,
466 struct bt_ctf_field
*field
, const char *field_name
,
467 struct bt_ctf_field
*writer_packet_context
,
468 struct bt_ctf_field_type
*writer_packet_context_type
)
470 enum bt_component_status ret
;
471 struct bt_ctf_field
*writer_field
= NULL
;
472 struct bt_ctf_field_type
*field_type
= NULL
;
476 field_type
= bt_ctf_field_get_type(field
);
478 ret
= BT_COMPONENT_STATUS_ERROR
;
479 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
484 * Only support for integers for now.
486 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
487 fprintf(err
, "[error] Unsupported packet context field type\n");
488 ret
= BT_COMPONENT_STATUS_ERROR
;
493 writer_field
= bt_ctf_field_structure_get_field_by_name(
494 writer_packet_context
, field_name
);
496 ret
= BT_COMPONENT_STATUS_ERROR
;
497 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
502 int_ret
= bt_ctf_field_unsigned_integer_get_value(field
, &value
);
504 fprintf(err
, "[error] Wrong packet_context field type\n");
505 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
507 ret
= BT_COMPONENT_STATUS_ERROR
;
511 int_ret
= bt_ctf_field_unsigned_integer_set_value(writer_field
, value
);
513 ret
= BT_COMPONENT_STATUS_ERROR
;
514 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
519 ret
= BT_COMPONENT_STATUS_OK
;
526 bt_put(writer_field
);
531 struct bt_ctf_field
*ctf_copy_packet_context(FILE *err
,
532 struct bt_ctf_packet
*packet
,
533 struct bt_ctf_stream
*writer_stream
,
534 int skip_content_size
)
536 enum bt_component_status ret
;
537 struct bt_ctf_field
*packet_context
= NULL
, *writer_packet_context
= NULL
;
538 struct bt_ctf_field_type
*struct_type
= NULL
, *writer_packet_context_type
= NULL
;
539 struct bt_ctf_stream_class
*writer_stream_class
= NULL
;
540 struct bt_ctf_field
*field
= NULL
;
541 struct bt_ctf_field_type
*field_type
= NULL
;
544 packet_context
= bt_ctf_packet_get_context(packet
);
545 if (!packet_context
) {
549 writer_stream_class
= bt_ctf_stream_get_class(writer_stream
);
550 if (!writer_stream_class
) {
551 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
556 writer_packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
557 writer_stream_class
);
558 BT_PUT(writer_stream_class
);
559 if (!writer_packet_context_type
) {
560 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
565 struct_type
= bt_ctf_field_get_type(packet_context
);
567 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
572 writer_packet_context
= bt_ctf_field_create(writer_packet_context_type
);
573 if (!writer_packet_context
) {
574 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
579 nr_fields
= bt_ctf_field_type_structure_get_field_count(struct_type
);
580 for (i
= 0; i
< nr_fields
; i
++) {
581 const char *field_name
;
583 field
= bt_ctf_field_structure_get_field_by_index(
586 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
590 if (bt_ctf_field_type_structure_get_field_by_index(struct_type
,
591 &field_name
, &field_type
, i
) < 0) {
592 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
596 if (skip_content_size
&&
597 (!strncmp(field_name
, "content_size", strlen("content_size")) ||
598 !strncmp(field_name
, "packet_size", strlen("packet_size")))) {
604 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
605 fprintf(err
, "[error] Unexpected packet context field type\n");
609 ret
= ctf_copy_packet_context_field(err
, field
, field_name
,
610 writer_packet_context
, writer_packet_context_type
);
613 if (ret
!= BT_COMPONENT_STATUS_OK
) {
614 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
623 BT_PUT(writer_packet_context
);
628 bt_put(writer_packet_context_type
);
629 bt_put(writer_stream_class
);
630 bt_put(packet_context
);
631 return writer_packet_context
;
635 int ctf_copy_event_header(FILE *err
, struct bt_ctf_event
*event
,
636 struct bt_ctf_event_class
*writer_event_class
,
637 struct bt_ctf_event
*writer_event
,
638 struct bt_ctf_field
*event_header
)
640 struct bt_ctf_clock_class
*clock_class
= NULL
, *writer_clock_class
= NULL
;
641 struct bt_ctf_clock_value
*clock_value
= NULL
, *writer_clock_value
= NULL
;
644 struct bt_ctf_field
*writer_event_header
= NULL
;
647 clock_class
= event_get_clock_class(err
, event
);
649 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
654 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
657 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
662 ret
= bt_ctf_clock_value_get_value(clock_value
, &value
);
665 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
670 writer_clock_class
= event_get_clock_class(err
, writer_event
);
671 if (!writer_clock_class
) {
672 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
677 writer_clock_value
= bt_ctf_clock_value_create(writer_clock_class
, value
);
678 BT_PUT(writer_clock_class
);
679 if (!writer_clock_value
) {
680 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
685 ret
= bt_ctf_event_set_clock_value(writer_event
, writer_clock_value
);
686 BT_PUT(writer_clock_value
);
688 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
693 writer_event_header
= bt_ctf_field_copy(event_header
);
694 if (!writer_event_header
) {
695 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
700 ret
= bt_ctf_event_set_header(writer_event
, writer_event_header
);
701 BT_PUT(writer_event_header
);
703 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
719 struct bt_ctf_event
*ctf_copy_event(FILE *err
, struct bt_ctf_event
*event
,
720 struct bt_ctf_event_class
*writer_event_class
,
723 struct bt_ctf_event
*writer_event
= NULL
;
724 struct bt_ctf_field
*field
= NULL
, *copy_field
= NULL
;
727 writer_event
= bt_ctf_event_create(writer_event_class
);
729 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
734 field
= bt_ctf_event_get_header(event
);
736 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
742 * If override_ts64, we override all integer fields mapped to a clock
743 * to a uint64_t field type, otherwise, we just copy it as is.
746 copy_field
= bt_ctf_event_get_header(writer_event
);
748 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
753 ret
= copy_override_field(err
, event
, writer_event
, field
,
756 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
762 ret
= ctf_copy_event_header(err
, event
, writer_event_class
,
763 writer_event
, field
);
765 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
772 /* Optional field, so it can fail silently. */
773 field
= bt_ctf_event_get_stream_event_context(event
);
774 copy_field
= bt_ctf_field_copy(field
);
776 ret
= bt_ctf_event_set_stream_event_context(writer_event
,
779 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
787 /* Optional field, so it can fail silently. */
788 field
= bt_ctf_event_get_event_context(event
);
789 copy_field
= bt_ctf_field_copy(field
);
791 ret
= bt_ctf_event_set_event_context(writer_event
, copy_field
);
793 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
801 field
= bt_ctf_event_get_event_payload(event
);
803 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
807 copy_field
= bt_ctf_field_copy(field
);
809 ret
= bt_ctf_event_set_event_payload(writer_event
, copy_field
);
811 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
822 BT_PUT(writer_event
);
830 enum bt_component_status
ctf_copy_trace(FILE *err
, struct bt_ctf_trace
*trace
,
831 struct bt_ctf_trace
*writer_trace
)
833 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
834 int field_count
, i
, int_ret
;
835 struct bt_ctf_field_type
*header_type
= NULL
;
836 enum bt_ctf_byte_order order
;
838 field_count
= bt_ctf_trace_get_environment_field_count(trace
);
839 for (i
= 0; i
< field_count
; i
++) {
842 struct bt_value
*value
= NULL
;
844 name
= bt_ctf_trace_get_environment_field_name_by_index(
847 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
849 ret
= BT_COMPONENT_STATUS_ERROR
;
852 value
= bt_ctf_trace_get_environment_field_value_by_index(
855 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
857 ret
= BT_COMPONENT_STATUS_ERROR
;
861 ret_int
= bt_ctf_trace_set_environment_field(writer_trace
,
865 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
867 fprintf(err
, "[error] Unable to set environment field %s\n",
869 ret
= BT_COMPONENT_STATUS_ERROR
;
874 order
= bt_ctf_trace_get_native_byte_order(trace
);
875 if (order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
876 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
877 ret
= BT_COMPONENT_STATUS_ERROR
;
881 ret
= bt_ctf_trace_set_native_byte_order(writer_trace
, order
);
883 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
884 ret
= BT_COMPONENT_STATUS_ERROR
;
888 header_type
= bt_ctf_trace_get_packet_header_type(writer_trace
);
890 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
891 ret
= BT_COMPONENT_STATUS_ERROR
;
895 int_ret
= bt_ctf_trace_set_packet_header_type(writer_trace
, header_type
);
898 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
899 ret
= BT_COMPONENT_STATUS_ERROR
;