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 int_ret
= bt_ctf_clock_class_set_description(writer_clock_class
,
73 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
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__
,
85 int_ret
= bt_ctf_clock_class_set_frequency(writer_clock_class
, u64_ret
);
87 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
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__
,
98 int_ret
= bt_ctf_clock_class_set_precision(writer_clock_class
,
101 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
106 int_ret
= bt_ctf_clock_class_get_offset_s(clock_class
, &offset_s
);
108 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
113 int_ret
= bt_ctf_clock_class_set_offset_s(writer_clock_class
, offset_s
);
115 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
120 int_ret
= bt_ctf_clock_class_get_offset_cycles(clock_class
, &offset
);
122 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
127 int_ret
= bt_ctf_clock_class_set_offset_cycles(writer_clock_class
, offset
);
129 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
134 int_ret
= bt_ctf_clock_class_is_absolute(clock_class
);
136 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
141 int_ret
= bt_ctf_clock_class_set_is_absolute(writer_clock_class
, int_ret
);
143 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
151 BT_PUT(writer_clock_class
);
153 return writer_clock_class
;
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
)
162 enum bt_component_status ret
;
163 int int_ret
, clock_class_count
, i
;
165 clock_class_count
= bt_ctf_trace_get_clock_class_count(trace
);
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
);
173 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
175 ret
= BT_COMPONENT_STATUS_ERROR
;
179 writer_clock_class
= ctf_copy_clock_class(err
, clock_class
);
181 if (!writer_clock_class
) {
182 fprintf(err
, "Failed to copy clock class");
183 ret
= BT_COMPONENT_STATUS_ERROR
;
187 int_ret
= bt_ctf_trace_add_clock_class(writer_trace
, writer_clock_class
);
189 BT_PUT(writer_clock_class
);
190 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
192 ret
= BT_COMPONENT_STATUS_ERROR
;
197 * Ownership transferred to the trace.
199 bt_put(writer_clock_class
);
202 ret
= BT_COMPONENT_STATUS_OK
;
209 struct bt_ctf_event_class
*ctf_copy_event_class(FILE *err
,
210 struct bt_ctf_event_class
*event_class
)
212 struct bt_ctf_event_class
*writer_event_class
= NULL
;
213 struct bt_ctf_field_type
*context
, *payload_type
;
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_by_index(
240 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
244 attr_value
= bt_ctf_event_class_get_attribute_value_by_index(
247 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
252 ret
= bt_ctf_event_class_set_attribute(writer_event_class
,
253 attr_name
, attr_value
);
256 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
262 payload_type
= bt_ctf_event_class_get_payload_type(event_class
);
264 ret
= bt_ctf_event_class_set_payload_type(writer_event_class
,
267 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
271 BT_PUT(payload_type
);
274 context
= bt_ctf_event_class_get_context_type(event_class
);
276 ret
= bt_ctf_event_class_set_context_type(
277 writer_event_class
, context
);
280 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
289 BT_PUT(writer_event_class
);
291 return writer_event_class
;
295 enum bt_component_status
ctf_copy_event_classes(FILE *err
,
296 struct bt_ctf_stream_class
*stream_class
,
297 struct bt_ctf_stream_class
*writer_stream_class
)
299 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
300 struct bt_ctf_event_class
*event_class
= NULL
, *writer_event_class
= NULL
;
303 count
= bt_ctf_stream_class_get_event_class_count(stream_class
);
305 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
310 for (i
= 0; i
< count
; i
++) {
313 event_class
= bt_ctf_stream_class_get_event_class_by_index(
316 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
318 ret
= BT_COMPONENT_STATUS_ERROR
;
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__
,
325 ret
= BT_COMPONENT_STATUS_ERROR
;
329 int_ret
= bt_ctf_stream_class_add_event_class(writer_stream_class
,
332 fprintf(err
, "[error] Failed to add event class\n");
333 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
335 ret
= BT_COMPONENT_STATUS_ERROR
;
338 BT_PUT(writer_event_class
);
346 bt_put(writer_event_class
);
352 struct bt_ctf_stream_class
*ctf_copy_stream_class(FILE *err
,
353 struct bt_ctf_stream_class
*stream_class
,
354 struct bt_ctf_trace
*writer_trace
,
357 struct bt_ctf_field_type
*type
= NULL
;
358 struct bt_ctf_stream_class
*writer_stream_class
= NULL
;
360 const char *name
= bt_ctf_stream_class_get_name(stream_class
);
362 if (strlen(name
) == 0) {
366 writer_stream_class
= bt_ctf_stream_class_create(name
);
367 if (!writer_stream_class
) {
368 fprintf(err
, "[error] %s in %s:%d\n",
369 __func__
, __FILE__
, __LINE__
);
373 type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
375 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
380 ret_int
= bt_ctf_stream_class_set_packet_context_type(
381 writer_stream_class
, type
);
383 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
389 type
= bt_ctf_stream_class_get_event_header_type(stream_class
);
391 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
397 struct bt_ctf_field_type
*new_event_header_type
;
399 new_event_header_type
= override_header_type(err
, type
,
401 if (!new_event_header_type
) {
402 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
406 ret_int
= bt_ctf_stream_class_set_event_header_type(
407 writer_stream_class
, new_event_header_type
);
408 BT_PUT(new_event_header_type
);
410 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
415 ret_int
= bt_ctf_stream_class_set_event_header_type(
416 writer_stream_class
, type
);
418 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
425 type
= bt_ctf_stream_class_get_event_context_type(stream_class
);
427 ret_int
= bt_ctf_stream_class_set_event_context_type(
428 writer_stream_class
, type
);
430 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
440 BT_PUT(writer_stream_class
);
443 return writer_stream_class
;
447 enum bt_component_status
ctf_copy_packet_context_field(FILE *err
,
448 struct bt_ctf_field
*field
, const char *field_name
,
449 struct bt_ctf_field
*writer_packet_context
,
450 struct bt_ctf_field_type
*writer_packet_context_type
)
452 enum bt_component_status ret
;
453 struct bt_ctf_field
*writer_field
= NULL
;
454 struct bt_ctf_field_type
*field_type
= NULL
;
458 field_type
= bt_ctf_field_get_type(field
);
460 ret
= BT_COMPONENT_STATUS_ERROR
;
461 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
466 * Only support for integers for now.
468 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
469 fprintf(err
, "[error] Unsupported packet context field type\n");
470 ret
= BT_COMPONENT_STATUS_ERROR
;
475 writer_field
= bt_ctf_field_structure_get_field_by_name(
476 writer_packet_context
, field_name
);
478 ret
= BT_COMPONENT_STATUS_ERROR
;
479 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
484 int_ret
= bt_ctf_field_unsigned_integer_get_value(field
, &value
);
486 fprintf(err
, "[error] Wrong packet_context field type\n");
487 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
489 ret
= BT_COMPONENT_STATUS_ERROR
;
493 int_ret
= bt_ctf_field_unsigned_integer_set_value(writer_field
, value
);
495 ret
= BT_COMPONENT_STATUS_ERROR
;
496 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
501 ret
= BT_COMPONENT_STATUS_OK
;
508 bt_put(writer_field
);
513 struct bt_ctf_field
*ctf_copy_packet_context(FILE *err
,
514 struct bt_ctf_packet
*packet
,
515 struct bt_ctf_stream
*writer_stream
)
517 enum bt_component_status ret
;
518 struct bt_ctf_field
*packet_context
= NULL
, *writer_packet_context
= NULL
;
519 struct bt_ctf_field_type
*struct_type
= NULL
, *writer_packet_context_type
= NULL
;
520 struct bt_ctf_stream_class
*writer_stream_class
= NULL
;
521 struct bt_ctf_field
*field
= NULL
;
522 struct bt_ctf_field_type
*field_type
= NULL
;
525 packet_context
= bt_ctf_packet_get_context(packet
);
526 if (!packet_context
) {
530 writer_stream_class
= bt_ctf_stream_get_class(writer_stream
);
531 if (!writer_stream_class
) {
532 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
537 writer_packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
538 writer_stream_class
);
539 BT_PUT(writer_stream_class
);
540 if (!writer_packet_context_type
) {
541 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
546 struct_type
= bt_ctf_field_get_type(packet_context
);
548 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
553 writer_packet_context
= bt_ctf_field_create(writer_packet_context_type
);
554 if (!writer_packet_context
) {
555 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
560 nr_fields
= bt_ctf_field_type_structure_get_field_count(struct_type
);
561 for (i
= 0; i
< nr_fields
; i
++) {
562 const char *field_name
;
564 field
= bt_ctf_field_structure_get_field_by_index(
567 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
571 if (bt_ctf_field_type_structure_get_field_by_index(struct_type
,
572 &field_name
, &field_type
, i
) < 0) {
573 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
578 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
579 fprintf(err
, "[error] Unexpected packet context field type\n");
583 ret
= ctf_copy_packet_context_field(err
, field
, field_name
,
584 writer_packet_context
, writer_packet_context_type
);
587 if (ret
!= BT_COMPONENT_STATUS_OK
) {
588 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
597 BT_PUT(writer_packet_context
);
602 bt_put(writer_packet_context_type
);
603 bt_put(writer_stream_class
);
604 bt_put(packet_context
);
605 return writer_packet_context
;
609 int ctf_copy_event_header(FILE *err
, struct bt_ctf_event
*event
,
610 struct bt_ctf_event_class
*writer_event_class
,
611 struct bt_ctf_event
*writer_event
,
612 struct bt_ctf_field
*event_header
)
614 struct bt_ctf_clock_class
*clock_class
= NULL
, *writer_clock_class
= NULL
;
615 struct bt_ctf_clock_value
*clock_value
= NULL
, *writer_clock_value
= NULL
;
618 struct bt_ctf_field
*writer_event_header
= NULL
;
621 clock_class
= event_get_clock_class(err
, event
);
623 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
628 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
631 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
636 ret
= bt_ctf_clock_value_get_value(clock_value
, &value
);
639 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
644 writer_clock_class
= event_get_clock_class(err
, writer_event
);
645 if (!writer_clock_class
) {
646 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
651 writer_clock_value
= bt_ctf_clock_value_create(writer_clock_class
, value
);
652 BT_PUT(writer_clock_class
);
653 if (!writer_clock_value
) {
654 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
659 ret
= bt_ctf_event_set_clock_value(writer_event
, writer_clock_value
);
660 BT_PUT(writer_clock_value
);
662 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
667 writer_event_header
= bt_ctf_field_copy(event_header
);
668 if (!writer_event_header
) {
669 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
674 ret
= bt_ctf_event_set_header(writer_event
, writer_event_header
);
675 BT_PUT(writer_event_header
);
677 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
693 struct bt_ctf_event
*ctf_copy_event(FILE *err
, struct bt_ctf_event
*event
,
694 struct bt_ctf_event_class
*writer_event_class
,
697 struct bt_ctf_event
*writer_event
= NULL
;
698 struct bt_ctf_field
*field
= NULL
, *copy_field
= NULL
;
701 writer_event
= bt_ctf_event_create(writer_event_class
);
703 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
708 field
= bt_ctf_event_get_header(event
);
710 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
716 * If override_ts64, we override all integer fields mapped to a clock
717 * to a uint64_t field type, otherwise, we just copy it as is.
720 copy_field
= bt_ctf_event_get_header(writer_event
);
722 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
727 ret
= copy_override_field(err
, event
, writer_event
, field
,
730 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
736 ret
= ctf_copy_event_header(err
, event
, writer_event_class
,
737 writer_event
, field
);
739 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
746 /* Optional field, so it can fail silently. */
747 field
= bt_ctf_event_get_stream_event_context(event
);
748 copy_field
= bt_ctf_field_copy(field
);
750 ret
= bt_ctf_event_set_stream_event_context(writer_event
,
753 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
761 /* Optional field, so it can fail silently. */
762 field
= bt_ctf_event_get_event_context(event
);
763 copy_field
= bt_ctf_field_copy(field
);
765 ret
= bt_ctf_event_set_event_context(writer_event
, copy_field
);
767 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
775 field
= bt_ctf_event_get_event_payload(event
);
777 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
781 copy_field
= bt_ctf_field_copy(field
);
783 ret
= bt_ctf_event_set_event_payload(writer_event
, copy_field
);
785 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
796 BT_PUT(writer_event
);
804 enum bt_component_status
ctf_copy_trace(FILE *err
, struct bt_ctf_trace
*trace
,
805 struct bt_ctf_trace
*writer_trace
)
807 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
808 int field_count
, i
, int_ret
;
809 struct bt_ctf_field_type
*header_type
= NULL
;
810 enum bt_ctf_byte_order order
;
811 const char *trace_name
;
813 field_count
= bt_ctf_trace_get_environment_field_count(trace
);
814 for (i
= 0; i
< field_count
; i
++) {
817 struct bt_value
*value
= NULL
;
819 name
= bt_ctf_trace_get_environment_field_name_by_index(
822 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
824 ret
= BT_COMPONENT_STATUS_ERROR
;
827 value
= bt_ctf_trace_get_environment_field_value_by_index(
830 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
832 ret
= BT_COMPONENT_STATUS_ERROR
;
836 ret_int
= bt_ctf_trace_set_environment_field(writer_trace
,
840 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
842 fprintf(err
, "[error] Unable to set environment field %s\n",
844 ret
= BT_COMPONENT_STATUS_ERROR
;
849 order
= bt_ctf_trace_get_native_byte_order(trace
);
850 if (order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
851 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
852 ret
= BT_COMPONENT_STATUS_ERROR
;
857 * Only explicitly set the writer trace's native byte order if
858 * the original trace has a specific one. Otherwise leave what
859 * the CTF writer object chooses, which is the machine's native
862 if (order
!= BT_CTF_BYTE_ORDER_NONE
) {
863 ret
= bt_ctf_trace_set_native_byte_order(writer_trace
, order
);
865 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
866 ret
= BT_COMPONENT_STATUS_ERROR
;
871 header_type
= bt_ctf_trace_get_packet_header_type(writer_trace
);
873 int_ret
= bt_ctf_trace_set_packet_header_type(writer_trace
, header_type
);
876 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
877 ret
= BT_COMPONENT_STATUS_ERROR
;
882 trace_name
= bt_ctf_trace_get_name(trace
);
884 int_ret
= bt_ctf_trace_set_name(writer_trace
, trace_name
);
886 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
887 ret
= BT_COMPONENT_STATUS_ERROR
;