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
,
516 int skip_content_size
)
518 enum bt_component_status ret
;
519 struct bt_ctf_field
*packet_context
= NULL
, *writer_packet_context
= NULL
;
520 struct bt_ctf_field_type
*struct_type
= NULL
, *writer_packet_context_type
= NULL
;
521 struct bt_ctf_stream_class
*writer_stream_class
= NULL
;
522 struct bt_ctf_field
*field
= NULL
;
523 struct bt_ctf_field_type
*field_type
= NULL
;
526 packet_context
= bt_ctf_packet_get_context(packet
);
527 if (!packet_context
) {
531 writer_stream_class
= bt_ctf_stream_get_class(writer_stream
);
532 if (!writer_stream_class
) {
533 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
538 writer_packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
539 writer_stream_class
);
540 BT_PUT(writer_stream_class
);
541 if (!writer_packet_context_type
) {
542 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
547 struct_type
= bt_ctf_field_get_type(packet_context
);
549 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
554 writer_packet_context
= bt_ctf_field_create(writer_packet_context_type
);
555 if (!writer_packet_context
) {
556 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
561 nr_fields
= bt_ctf_field_type_structure_get_field_count(struct_type
);
562 for (i
= 0; i
< nr_fields
; i
++) {
563 const char *field_name
;
565 field
= bt_ctf_field_structure_get_field_by_index(
568 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
572 if (bt_ctf_field_type_structure_get_field_by_index(struct_type
,
573 &field_name
, &field_type
, i
) < 0) {
574 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
578 if (skip_content_size
&&
579 (!strncmp(field_name
, "content_size", strlen("content_size")) ||
580 !strncmp(field_name
, "packet_size", strlen("packet_size")))) {
586 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
587 fprintf(err
, "[error] Unexpected packet context field type\n");
591 ret
= ctf_copy_packet_context_field(err
, field
, field_name
,
592 writer_packet_context
, writer_packet_context_type
);
595 if (ret
!= BT_COMPONENT_STATUS_OK
) {
596 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
605 BT_PUT(writer_packet_context
);
610 bt_put(writer_packet_context_type
);
611 bt_put(writer_stream_class
);
612 bt_put(packet_context
);
613 return writer_packet_context
;
617 int ctf_copy_event_header(FILE *err
, struct bt_ctf_event
*event
,
618 struct bt_ctf_event_class
*writer_event_class
,
619 struct bt_ctf_event
*writer_event
,
620 struct bt_ctf_field
*event_header
)
622 struct bt_ctf_clock_class
*clock_class
= NULL
, *writer_clock_class
= NULL
;
623 struct bt_ctf_clock_value
*clock_value
= NULL
, *writer_clock_value
= NULL
;
626 struct bt_ctf_field
*writer_event_header
= NULL
;
629 clock_class
= event_get_clock_class(err
, event
);
631 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
636 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
639 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
644 ret
= bt_ctf_clock_value_get_value(clock_value
, &value
);
647 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
652 writer_clock_class
= event_get_clock_class(err
, writer_event
);
653 if (!writer_clock_class
) {
654 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
659 writer_clock_value
= bt_ctf_clock_value_create(writer_clock_class
, value
);
660 BT_PUT(writer_clock_class
);
661 if (!writer_clock_value
) {
662 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
667 ret
= bt_ctf_event_set_clock_value(writer_event
, writer_clock_value
);
668 BT_PUT(writer_clock_value
);
670 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
675 writer_event_header
= bt_ctf_field_copy(event_header
);
676 if (!writer_event_header
) {
677 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
682 ret
= bt_ctf_event_set_header(writer_event
, writer_event_header
);
683 BT_PUT(writer_event_header
);
685 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
701 struct bt_ctf_event
*ctf_copy_event(FILE *err
, struct bt_ctf_event
*event
,
702 struct bt_ctf_event_class
*writer_event_class
,
705 struct bt_ctf_event
*writer_event
= NULL
;
706 struct bt_ctf_field
*field
= NULL
, *copy_field
= NULL
;
709 writer_event
= bt_ctf_event_create(writer_event_class
);
711 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
716 field
= bt_ctf_event_get_header(event
);
718 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
724 * If override_ts64, we override all integer fields mapped to a clock
725 * to a uint64_t field type, otherwise, we just copy it as is.
728 copy_field
= bt_ctf_event_get_header(writer_event
);
730 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
735 ret
= copy_override_field(err
, event
, writer_event
, field
,
738 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
744 ret
= ctf_copy_event_header(err
, event
, writer_event_class
,
745 writer_event
, field
);
747 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
754 /* Optional field, so it can fail silently. */
755 field
= bt_ctf_event_get_stream_event_context(event
);
756 copy_field
= bt_ctf_field_copy(field
);
758 ret
= bt_ctf_event_set_stream_event_context(writer_event
,
761 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
769 /* Optional field, so it can fail silently. */
770 field
= bt_ctf_event_get_event_context(event
);
771 copy_field
= bt_ctf_field_copy(field
);
773 ret
= bt_ctf_event_set_event_context(writer_event
, copy_field
);
775 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
783 field
= bt_ctf_event_get_event_payload(event
);
785 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
789 copy_field
= bt_ctf_field_copy(field
);
791 ret
= bt_ctf_event_set_event_payload(writer_event
, copy_field
);
793 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
804 BT_PUT(writer_event
);
812 enum bt_component_status
ctf_copy_trace(FILE *err
, struct bt_ctf_trace
*trace
,
813 struct bt_ctf_trace
*writer_trace
)
815 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
816 int field_count
, i
, int_ret
;
817 struct bt_ctf_field_type
*header_type
= NULL
;
818 enum bt_ctf_byte_order order
;
820 field_count
= bt_ctf_trace_get_environment_field_count(trace
);
821 for (i
= 0; i
< field_count
; i
++) {
824 struct bt_value
*value
= NULL
;
826 name
= bt_ctf_trace_get_environment_field_name_by_index(
829 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
831 ret
= BT_COMPONENT_STATUS_ERROR
;
834 value
= bt_ctf_trace_get_environment_field_value_by_index(
837 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
839 ret
= BT_COMPONENT_STATUS_ERROR
;
843 ret_int
= bt_ctf_trace_set_environment_field(writer_trace
,
847 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
849 fprintf(err
, "[error] Unable to set environment field %s\n",
851 ret
= BT_COMPONENT_STATUS_ERROR
;
856 order
= bt_ctf_trace_get_native_byte_order(trace
);
857 if (order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
858 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
859 ret
= BT_COMPONENT_STATUS_ERROR
;
864 * Only explicitly set the writer trace's native byte order if
865 * the original trace has a specific one. Otherwise leave what
866 * the CTF writer object chooses, which is the machine's native
869 if (order
!= BT_CTF_BYTE_ORDER_NONE
) {
870 ret
= bt_ctf_trace_set_native_byte_order(writer_trace
, order
);
872 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
873 ret
= BT_COMPONENT_STATUS_ERROR
;
878 header_type
= bt_ctf_trace_get_packet_header_type(writer_trace
);
880 int_ret
= bt_ctf_trace_set_packet_header_type(writer_trace
, header_type
);
883 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
884 ret
= BT_COMPONENT_STATUS_ERROR
;