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__
,
241 BT_PUT(writer_event_class
);
244 attr_value
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
246 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
248 BT_PUT(writer_event_class
);
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__
,
258 BT_PUT(writer_event_class
);
263 count
= bt_ctf_event_class_get_field_count(event_class
);
264 for (i
= 0; i
< count
; i
++) {
265 const char *field_name
;
266 struct bt_ctf_field_type
*field_type
;
269 ret
= bt_ctf_event_class_get_field(event_class
, &field_name
,
272 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
273 BT_PUT(writer_event_class
);
277 ret
= bt_ctf_event_class_add_field(writer_event_class
, field_type
,
280 fprintf(err
, "[error] Cannot add field %s\n", field_name
);
281 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
283 BT_PUT(writer_event_class
);
290 return writer_event_class
;
293 enum bt_component_status
ctf_copy_event_classes(FILE *err
,
294 struct bt_ctf_stream_class
*stream_class
,
295 struct bt_ctf_stream_class
*writer_stream_class
)
297 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
300 count
= bt_ctf_stream_class_get_event_class_count(stream_class
);
302 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
307 for (i
= 0; i
< count
; i
++) {
308 struct bt_ctf_event_class
*event_class
, *writer_event_class
;
309 struct bt_ctf_field_type
*context
;
312 event_class
= bt_ctf_stream_class_get_event_class(
315 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
317 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
;
330 context
= bt_ctf_event_class_get_context_type(event_class
);
331 ret
= bt_ctf_event_class_set_context_type(writer_event_class
, context
);
334 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
339 int_ret
= bt_ctf_stream_class_add_event_class(writer_stream_class
,
342 fprintf(err
, "[error] Failed to add event class\n");
343 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
345 ret
= BT_COMPONENT_STATUS_ERROR
;
350 bt_put(writer_event_class
);
357 struct bt_ctf_stream_class
*ctf_copy_stream_class(FILE *err
,
358 struct bt_ctf_stream_class
*stream_class
,
359 struct bt_ctf_trace
*writer_trace
,
362 struct bt_ctf_field_type
*type
, *new_event_header_type
;
363 struct bt_ctf_stream_class
*writer_stream_class
;
365 const char *name
= bt_ctf_stream_class_get_name(stream_class
);
367 if (strlen(name
) == 0) {
371 writer_stream_class
= bt_ctf_stream_class_create(name
);
372 if (!writer_stream_class
) {
373 fprintf(err
, "[error] %s in %s:%d\n",
374 __func__
, __FILE__
, __LINE__
);
378 type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
380 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
385 ret_int
= bt_ctf_stream_class_set_packet_context_type(
386 writer_stream_class
, type
);
389 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
394 type
= bt_ctf_stream_class_get_event_header_type(stream_class
);
396 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
402 new_event_header_type
= override_header_type(err
, type
,
405 if (!new_event_header_type
) {
406 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
410 ret_int
= bt_ctf_stream_class_set_event_header_type(
411 writer_stream_class
, new_event_header_type
);
412 bt_put(new_event_header_type
);
414 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
419 ret_int
= bt_ctf_stream_class_set_event_header_type(
420 writer_stream_class
, type
);
423 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
429 type
= bt_ctf_stream_class_get_event_context_type(stream_class
);
431 ret_int
= bt_ctf_stream_class_set_event_context_type(
432 writer_stream_class
, type
);
435 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
444 BT_PUT(writer_stream_class
);
446 return writer_stream_class
;
449 enum bt_component_status
ctf_copy_packet_context_field(FILE *err
,
450 struct bt_ctf_field
*field
, const char *field_name
,
451 struct bt_ctf_field
*writer_packet_context
,
452 struct bt_ctf_field_type
*writer_packet_context_type
)
454 enum bt_component_status ret
;
455 struct bt_ctf_field
*writer_field
;
456 struct bt_ctf_field_type
*field_type
;
460 field_type
= bt_ctf_field_get_type(field
);
462 ret
= BT_COMPONENT_STATUS_ERROR
;
463 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
468 * Only support for integers for now.
470 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_TYPE_ID_INTEGER
) {
471 fprintf(err
, "[error] Unsupported packet context field type\n");
473 ret
= BT_COMPONENT_STATUS_ERROR
;
479 * TODO: handle the special case of the first/last packet that might
480 * be trimmed. In these cases, the timestamp_begin/end need to be
481 * explicitely set to the first/last event timestamps.
483 writer_field
= bt_ctf_field_structure_get_field(writer_packet_context
,
486 ret
= BT_COMPONENT_STATUS_ERROR
;
487 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
493 int_ret
= bt_ctf_field_unsigned_integer_get_value(field
, &value
);
495 fprintf(err
, "[error] Wrong packet_context field type\n");
496 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
498 ret
= BT_COMPONENT_STATUS_ERROR
;
499 goto end_put_writer_field
;
502 int_ret
= bt_ctf_field_unsigned_integer_set_value(writer_field
, value
);
504 ret
= BT_COMPONENT_STATUS_ERROR
;
505 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
507 goto end_put_writer_field
;
510 ret
= BT_COMPONENT_STATUS_OK
;
512 end_put_writer_field
:
513 bt_put(writer_field
);
518 struct bt_ctf_field
*ctf_copy_packet_context(FILE *err
,
519 struct bt_ctf_packet
*packet
,
520 struct bt_ctf_stream
*writer_stream
)
522 enum bt_component_status ret
;
523 struct bt_ctf_field
*packet_context
, *writer_packet_context
= NULL
;
524 struct bt_ctf_field_type
*struct_type
, *writer_packet_context_type
;
525 struct bt_ctf_stream_class
*writer_stream_class
;
528 packet_context
= bt_ctf_packet_get_context(packet
);
529 if (!packet_context
) {
530 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
535 writer_stream_class
= bt_ctf_stream_get_class(writer_stream
);
536 if (!writer_stream_class
) {
537 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
539 goto end_put_packet_context
;
542 writer_packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
543 writer_stream_class
);
544 if (!writer_packet_context_type
) {
545 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
547 goto end_put_writer_stream_class
;
550 struct_type
= bt_ctf_field_get_type(packet_context
);
552 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
554 goto end_put_writer_packet_context_type
;
557 writer_packet_context
= bt_ctf_field_create(writer_packet_context_type
);
558 if (!writer_packet_context
) {
559 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
561 goto end_put_struct_type
;
564 nr_fields
= bt_ctf_field_type_structure_get_field_count(struct_type
);
565 for (i
= 0; i
< nr_fields
; i
++) {
566 struct bt_ctf_field
*field
;
567 struct bt_ctf_field_type
*field_type
;
568 const char *field_name
;
570 field
= bt_ctf_field_structure_get_field_by_index(
573 BT_PUT(writer_packet_context
);
574 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
576 goto end_put_struct_type
;
578 if (bt_ctf_field_type_structure_get_field(struct_type
,
579 &field_name
, &field_type
, i
) < 0) {
581 BT_PUT(writer_packet_context
);
582 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
584 goto end_put_struct_type
;
586 if (!strncmp(field_name
, "content_size", strlen("content_size")) ||
587 !strncmp(field_name
, "packet_size",
588 strlen("packet_size"))) {
594 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_TYPE_ID_INTEGER
) {
595 fprintf(err
, "[error] Unexpected packet context field type\n");
597 BT_PUT(writer_packet_context
);
598 goto end_put_struct_type
;
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 BT_PUT(writer_packet_context
);
607 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
609 goto end_put_struct_type
;
615 end_put_writer_packet_context_type
:
616 bt_put(writer_packet_context_type
);
617 end_put_writer_stream_class
:
618 bt_put(writer_stream_class
);
619 end_put_packet_context
:
620 bt_put(packet_context
);
622 return writer_packet_context
;
625 int ctf_copy_event_header(FILE *err
, struct bt_ctf_event
*event
,
626 struct bt_ctf_event_class
*writer_event_class
,
627 struct bt_ctf_event
*writer_event
,
628 struct bt_ctf_field
*event_header
)
630 struct bt_ctf_clock_class
*clock_class
, *writer_clock_class
;
631 struct bt_ctf_clock_value
*clock_value
, *writer_clock_value
;
634 struct bt_ctf_field
*writer_event_header
= NULL
;
637 clock_class
= event_get_clock_class(err
, event
);
639 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
644 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
647 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
652 ret
= bt_ctf_clock_value_get_value(clock_value
, &value
);
655 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
660 writer_clock_class
= event_get_clock_class(err
, writer_event
);
661 if (!writer_clock_class
) {
662 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
667 writer_clock_value
= bt_ctf_clock_value_create(writer_clock_class
, value
);
668 bt_put(writer_clock_class
);
669 if (!writer_clock_value
) {
670 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
675 ret
= bt_ctf_event_set_clock_value(writer_event
, writer_clock_value
);
676 bt_put(writer_clock_value
);
678 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
683 writer_event_header
= bt_ctf_field_copy(event_header
);
684 if (!writer_event_header
) {
685 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__
,
704 BT_PUT(writer_event_header
);
710 struct bt_ctf_event
*ctf_copy_event(FILE *err
, struct bt_ctf_event
*event
,
711 struct bt_ctf_event_class
*writer_event_class
,
714 struct bt_ctf_event
*writer_event
;
715 struct bt_ctf_field
*field
, *copy_field
;
718 writer_event
= bt_ctf_event_create(writer_event_class
);
720 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
725 field
= bt_ctf_event_get_header(event
);
727 BT_PUT(writer_event
);
728 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
734 * If override_ts64, we override all integer fields mapped to a clock
735 * to a uint64_t field type, otherwise, we just copy it as is.
738 copy_field
= bt_ctf_event_get_header(writer_event
);
740 BT_PUT(writer_event
);
742 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
747 ret
= copy_override_field(err
, event
, writer_event
, field
,
751 BT_PUT(writer_event
);
753 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
758 ret
= ctf_copy_event_header(err
, event
, writer_event_class
,
759 writer_event
, field
);
761 BT_PUT(writer_event
);
762 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
768 /* Optional field, so it can fail silently. */
769 field
= bt_ctf_event_get_stream_event_context(event
);
770 copy_field
= bt_ctf_field_copy(field
);
773 ret
= bt_ctf_event_set_stream_event_context(writer_event
,
777 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
783 /* Optional field, so it can fail silently. */
784 field
= bt_ctf_event_get_event_context(event
);
785 copy_field
= bt_ctf_field_copy(field
);
788 ret
= bt_ctf_event_set_event_context(writer_event
, copy_field
);
791 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
797 field
= bt_ctf_event_get_payload_field(event
);
799 BT_PUT(writer_event
);
800 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
804 copy_field
= bt_ctf_field_copy(field
);
807 ret
= bt_ctf_event_set_payload_field(writer_event
, copy_field
);
810 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
818 BT_PUT(writer_event
);
823 enum bt_component_status
ctf_copy_trace(FILE *err
, struct bt_ctf_trace
*trace
,
824 struct bt_ctf_trace
*writer_trace
)
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
;
830 field_count
= bt_ctf_trace_get_environment_field_count(trace
);
831 for (i
= 0; i
< field_count
; i
++) {
834 struct bt_value
*value
;
836 name
= bt_ctf_trace_get_environment_field_name(trace
, i
);
838 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
840 ret
= BT_COMPONENT_STATUS_ERROR
;
841 goto end_put_writer_trace
;
843 value
= bt_ctf_trace_get_environment_field_value(trace
, i
);
845 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
847 ret
= BT_COMPONENT_STATUS_ERROR
;
848 goto end_put_writer_trace
;
851 ret_int
= bt_ctf_trace_set_environment_field(writer_trace
,
855 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
857 fprintf(err
, "[error] Unable to set environment field %s\n",
859 ret
= BT_COMPONENT_STATUS_ERROR
;
860 goto end_put_writer_trace
;
864 header_type
= bt_ctf_trace_get_packet_header_type(writer_trace
);
866 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
867 ret
= BT_COMPONENT_STATUS_ERROR
;
868 goto end_put_writer_trace
;
871 int_ret
= bt_ctf_trace_set_packet_header_type(writer_trace
, header_type
);
873 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
874 ret
= BT_COMPONENT_STATUS_ERROR
;
875 goto end_put_header_type
;
880 end_put_writer_trace
: