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"
41 struct bt_ctf_clock_class
*ctf_copy_clock_class(FILE *err
,
42 struct bt_ctf_clock_class
*clock_class
)
44 int64_t offset
, offset_s
;
47 const char *name
, *description
;
48 struct bt_ctf_clock_class
*writer_clock_class
= NULL
;
50 assert(err
&& clock_class
);
52 name
= bt_ctf_clock_class_get_name(clock_class
);
54 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
59 writer_clock_class
= bt_ctf_clock_class_create(name
);
60 if (!writer_clock_class
) {
61 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
66 description
= bt_ctf_clock_class_get_description(clock_class
);
68 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
73 int_ret
= bt_ctf_clock_class_set_description(writer_clock_class
,
76 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
81 u64_ret
= bt_ctf_clock_class_get_frequency(clock_class
);
82 if (u64_ret
== -1ULL) {
83 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
87 int_ret
= bt_ctf_clock_class_set_frequency(writer_clock_class
, u64_ret
);
89 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
94 u64_ret
= bt_ctf_clock_class_get_precision(clock_class
);
95 if (u64_ret
== -1ULL) {
96 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
100 int_ret
= bt_ctf_clock_class_set_precision(writer_clock_class
,
103 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
108 int_ret
= bt_ctf_clock_class_get_offset_s(clock_class
, &offset_s
);
110 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
115 int_ret
= bt_ctf_clock_class_set_offset_s(writer_clock_class
, offset_s
);
117 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
122 int_ret
= bt_ctf_clock_class_get_offset_cycles(clock_class
, &offset
);
124 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
129 int_ret
= bt_ctf_clock_class_set_offset_cycles(writer_clock_class
, offset
);
131 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
136 int_ret
= bt_ctf_clock_class_get_is_absolute(clock_class
);
138 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
143 int_ret
= bt_ctf_clock_class_set_is_absolute(writer_clock_class
, int_ret
);
145 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
153 BT_PUT(writer_clock_class
);
155 return writer_clock_class
;
158 enum bt_component_status
ctf_copy_clock_classes(FILE *err
,
159 struct bt_ctf_trace
*writer_trace
,
160 struct bt_ctf_stream_class
*writer_stream_class
,
161 struct bt_ctf_trace
*trace
)
163 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 writer and the stream_class.
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
)
360 struct bt_ctf_field_type
*type
;
361 struct bt_ctf_stream_class
*writer_stream_class
;
363 const char *name
= bt_ctf_stream_class_get_name(stream_class
);
365 if (strlen(name
) == 0) {
369 writer_stream_class
= bt_ctf_stream_class_create(name
);
370 if (!writer_stream_class
) {
371 fprintf(err
, "[error] %s in %s:%d\n",
372 __func__
, __FILE__
, __LINE__
);
376 type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
378 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
383 ret_int
= bt_ctf_stream_class_set_packet_context_type(
384 writer_stream_class
, type
);
387 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
392 type
= bt_ctf_stream_class_get_event_header_type(stream_class
);
394 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
399 ret_int
= bt_ctf_stream_class_set_event_header_type(
400 writer_stream_class
, type
);
402 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
407 type
= bt_ctf_stream_class_get_event_context_type(stream_class
);
409 ret_int
= bt_ctf_stream_class_set_event_context_type(
410 writer_stream_class
, type
);
413 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
422 BT_PUT(writer_stream_class
);
424 return writer_stream_class
;
427 enum bt_component_status
ctf_copy_packet_context_field(FILE *err
,
428 struct bt_ctf_field
*field
, const char *field_name
,
429 struct bt_ctf_field
*writer_packet_context
,
430 struct bt_ctf_field_type
*writer_packet_context_type
)
432 enum bt_component_status ret
;
433 struct bt_ctf_field
*writer_field
;
434 struct bt_ctf_field_type
*field_type
;
438 field_type
= bt_ctf_field_get_type(field
);
440 ret
= BT_COMPONENT_STATUS_ERROR
;
441 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
446 * Only support for integers for now.
448 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_TYPE_ID_INTEGER
) {
449 fprintf(err
, "[error] Unsupported packet context field type\n");
451 ret
= BT_COMPONENT_STATUS_ERROR
;
457 * TODO: handle the special case of the first/last packet that might
458 * be trimmed. In these cases, the timestamp_begin/end need to be
459 * explicitely set to the first/last event timestamps.
461 writer_field
= bt_ctf_field_structure_get_field(writer_packet_context
,
464 ret
= BT_COMPONENT_STATUS_ERROR
;
465 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
471 int_ret
= bt_ctf_field_unsigned_integer_get_value(field
, &value
);
473 fprintf(err
, "[error] Wrong packet_context field type\n");
474 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
476 ret
= BT_COMPONENT_STATUS_ERROR
;
477 goto end_put_writer_field
;
480 int_ret
= bt_ctf_field_unsigned_integer_set_value(writer_field
, value
);
482 ret
= BT_COMPONENT_STATUS_ERROR
;
483 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
485 goto end_put_writer_field
;
488 ret
= BT_COMPONENT_STATUS_OK
;
490 end_put_writer_field
:
491 bt_put(writer_field
);
496 enum bt_component_status
ctf_copy_packet_context(FILE *err
,
497 struct bt_ctf_packet
*packet
,
498 struct bt_ctf_stream
*writer_stream
)
500 enum bt_component_status ret
;
501 struct bt_ctf_field
*packet_context
, *writer_packet_context
;
502 struct bt_ctf_field_type
*struct_type
, *writer_packet_context_type
;
503 struct bt_ctf_stream_class
*writer_stream_class
;
504 int nr_fields
, i
, int_ret
;
506 packet_context
= bt_ctf_packet_get_context(packet
);
507 if (!packet_context
) {
508 ret
= BT_COMPONENT_STATUS_ERROR
;
509 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
514 writer_stream_class
= bt_ctf_stream_get_class(writer_stream
);
515 if (!writer_stream_class
) {
516 ret
= BT_COMPONENT_STATUS_ERROR
;
517 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
519 goto end_put_packet_context
;
522 writer_packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
523 writer_stream_class
);
524 if (!writer_packet_context_type
) {
525 ret
= BT_COMPONENT_STATUS_ERROR
;
526 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
528 goto end_put_writer_stream_class
;
531 struct_type
= bt_ctf_field_get_type(packet_context
);
533 ret
= BT_COMPONENT_STATUS_ERROR
;
534 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
536 goto end_put_writer_packet_context_type
;
539 writer_packet_context
= bt_ctf_field_create(writer_packet_context_type
);
540 if (!writer_packet_context
) {
541 ret
= BT_COMPONENT_STATUS_ERROR
;
542 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
544 goto end_put_struct_type
;
547 nr_fields
= bt_ctf_field_type_structure_get_field_count(struct_type
);
548 for (i
= 0; i
< nr_fields
; i
++) {
549 struct bt_ctf_field
*field
;
550 struct bt_ctf_field_type
*field_type
;
551 const char *field_name
;
553 field
= bt_ctf_field_structure_get_field_by_index(
556 ret
= BT_COMPONENT_STATUS_ERROR
;
557 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
559 goto end_put_writer_packet_context
;
561 if (bt_ctf_field_type_structure_get_field(struct_type
,
562 &field_name
, &field_type
, i
) < 0) {
563 ret
= BT_COMPONENT_STATUS_ERROR
;
565 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
567 goto end_put_writer_packet_context
;
569 if (!strncmp(field_name
, "content_size", strlen("content_size")) ||
570 !strncmp(field_name
, "packet_size",
571 strlen("packet_size"))) {
577 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_TYPE_ID_INTEGER
) {
578 fprintf(err
, "[error] Unexpected packet context field type\n");
580 ret
= BT_COMPONENT_STATUS_ERROR
;
581 goto end_put_writer_packet_context
;
584 ret
= ctf_copy_packet_context_field(err
, field
, field_name
,
585 writer_packet_context
, writer_packet_context_type
);
588 if (ret
!= BT_COMPONENT_STATUS_OK
) {
589 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
591 goto end_put_writer_packet_context
;
595 int_ret
= bt_ctf_stream_set_packet_context(writer_stream
,
596 writer_packet_context
);
598 ret
= BT_COMPONENT_STATUS_ERROR
;
599 goto end_put_writer_packet_context
;
602 end_put_writer_packet_context
:
603 bt_put(writer_packet_context
);
606 end_put_writer_packet_context_type
:
607 bt_put(writer_packet_context_type
);
608 end_put_writer_stream_class
:
609 bt_put(writer_stream_class
);
610 end_put_packet_context
:
611 bt_put(packet_context
);
616 struct bt_ctf_event
*ctf_copy_event(FILE *err
, struct bt_ctf_event
*event
,
617 struct bt_ctf_event_class
*writer_event_class
)
619 struct bt_ctf_event
*writer_event
;
620 struct bt_ctf_field
*field
, *copy_field
;
623 writer_event
= bt_ctf_event_create(writer_event_class
);
625 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
630 field
= bt_ctf_event_get_header(event
);
632 BT_PUT(writer_event
);
633 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
638 copy_field
= bt_ctf_field_copy(field
);
641 ret
= bt_ctf_event_set_header(writer_event
, copy_field
);
643 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
650 /* Optional field, so it can fail silently. */
651 field
= bt_ctf_event_get_stream_event_context(event
);
652 copy_field
= bt_ctf_field_copy(field
);
655 ret
= bt_ctf_event_set_stream_event_context(writer_event
,
659 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
665 /* Optional field, so it can fail silently. */
666 field
= bt_ctf_event_get_event_context(event
);
667 copy_field
= bt_ctf_field_copy(field
);
670 ret
= bt_ctf_event_set_event_context(writer_event
, copy_field
);
673 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
679 field
= bt_ctf_event_get_payload_field(event
);
681 BT_PUT(writer_event
);
682 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
686 copy_field
= bt_ctf_field_copy(field
);
689 ret
= bt_ctf_event_set_payload_field(writer_event
, copy_field
);
692 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
700 BT_PUT(writer_event
);
705 enum bt_component_status
ctf_copy_trace(FILE *err
, struct bt_ctf_trace
*trace
,
706 struct bt_ctf_trace
*writer_trace
)
708 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
709 int field_count
, i
, int_ret
;
710 struct bt_ctf_field_type
*header_type
;
712 field_count
= bt_ctf_trace_get_environment_field_count(trace
);
713 for (i
= 0; i
< field_count
; i
++) {
716 struct bt_value
*value
;
718 name
= bt_ctf_trace_get_environment_field_name(trace
, i
);
720 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
722 ret
= BT_COMPONENT_STATUS_ERROR
;
723 goto end_put_writer_trace
;
725 value
= bt_ctf_trace_get_environment_field_value(trace
, i
);
727 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
729 ret
= BT_COMPONENT_STATUS_ERROR
;
730 goto end_put_writer_trace
;
733 ret_int
= bt_ctf_trace_set_environment_field(writer_trace
,
737 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
739 fprintf(err
, "[error] Unable to set environment field %s\n",
741 ret
= BT_COMPONENT_STATUS_ERROR
;
742 goto end_put_writer_trace
;
746 header_type
= bt_ctf_trace_get_packet_header_type(writer_trace
);
748 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
749 ret
= BT_COMPONENT_STATUS_ERROR
;
750 goto end_put_writer_trace
;
753 int_ret
= bt_ctf_trace_set_packet_header_type(writer_trace
, header_type
);
755 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
756 ret
= BT_COMPONENT_STATUS_ERROR
;
757 goto end_put_header_type
;
762 end_put_writer_trace
: