4 * Babeltrace CTF Writer Output Plugin Event Handling
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@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.h>
35 #include <babeltrace/ctf-ir/fields.h>
36 #include <babeltrace/ctf-writer/stream-class.h>
37 #include <babeltrace/ctf-writer/stream.h>
42 enum bt_component_status
copy_clock(FILE *err
, struct bt_ctf_writer
*writer
,
43 struct bt_ctf_stream_class
*writer_stream_class
,
44 struct bt_ctf_clock
*clock
)
46 int64_t offset
, offset_s
;
49 const char *name
, *description
;
50 struct bt_ctf_clock
*writer_clock
= NULL
;
51 enum bt_component_status ret
;
53 name
= bt_ctf_clock_get_name(clock
);
55 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
57 ret
= BT_COMPONENT_STATUS_ERROR
;
61 writer_clock
= bt_ctf_clock_create(name
);
63 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
65 ret
= BT_COMPONENT_STATUS_ERROR
;
69 description
= bt_ctf_clock_get_description(clock
);
71 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
73 ret
= BT_COMPONENT_STATUS_ERROR
;
77 int_ret
= bt_ctf_clock_set_description(writer_clock
,
80 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
82 ret
= BT_COMPONENT_STATUS_ERROR
;
86 u64_ret
= bt_ctf_clock_get_frequency(clock
);
87 if (u64_ret
== -1ULL) {
88 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
90 ret
= BT_COMPONENT_STATUS_ERROR
;
93 int_ret
= bt_ctf_clock_set_frequency(writer_clock
, u64_ret
);
95 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
97 ret
= BT_COMPONENT_STATUS_ERROR
;
101 u64_ret
= bt_ctf_clock_get_precision(clock
);
102 if (u64_ret
== -1ULL) {
103 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
105 ret
= BT_COMPONENT_STATUS_ERROR
;
108 int_ret
= bt_ctf_clock_set_precision(writer_clock
, u64_ret
);
110 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
112 ret
= BT_COMPONENT_STATUS_ERROR
;
116 int_ret
= bt_ctf_clock_get_offset_s(clock
, &offset_s
);
118 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
120 ret
= BT_COMPONENT_STATUS_ERROR
;
124 int_ret
= bt_ctf_clock_set_offset_s(writer_clock
, offset_s
);
126 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
128 ret
= BT_COMPONENT_STATUS_ERROR
;
132 int_ret
= bt_ctf_clock_get_offset(clock
, &offset
);
134 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
136 ret
= BT_COMPONENT_STATUS_ERROR
;
140 int_ret
= bt_ctf_clock_set_offset(writer_clock
, offset
);
142 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
144 ret
= BT_COMPONENT_STATUS_ERROR
;
148 int_ret
= bt_ctf_clock_get_is_absolute(clock
);
150 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
152 ret
= BT_COMPONENT_STATUS_ERROR
;
156 int_ret
= bt_ctf_clock_set_is_absolute(writer_clock
, int_ret
);
158 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
160 ret
= BT_COMPONENT_STATUS_ERROR
;
164 int_ret
= bt_ctf_writer_add_clock(writer
, writer_clock
);
166 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
168 ret
= BT_COMPONENT_STATUS_ERROR
;
173 * Ownership transferred to the writer and the stream_class.
175 bt_put(writer_clock
);
176 ret
= BT_COMPONENT_STATUS_OK
;
181 BT_PUT(writer_clock
);
187 struct bt_ctf_event_class
*copy_event_class(FILE *err
, struct bt_ctf_event_class
*event_class
)
189 struct bt_ctf_event_class
*writer_event_class
= NULL
;
191 struct bt_ctf_field_type
*context
;
194 name
= bt_ctf_event_class_get_name(event_class
);
196 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
201 writer_event_class
= bt_ctf_event_class_create(name
);
202 if (!writer_event_class
) {
203 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
208 count
= bt_ctf_event_class_get_attribute_count(event_class
);
209 for (i
= 0; i
< count
; i
++) {
210 const char *attr_name
;
211 struct bt_value
*attr_value
;
214 attr_name
= bt_ctf_event_class_get_attribute_name(event_class
, i
);
216 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
218 BT_PUT(writer_event_class
);
221 attr_value
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
223 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
225 BT_PUT(writer_event_class
);
229 ret
= bt_ctf_event_class_set_attribute(writer_event_class
,
230 attr_name
, attr_value
);
232 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
234 BT_PUT(writer_event_class
);
239 context
= bt_ctf_event_class_get_context_type(event_class
);
241 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
246 ret
= bt_ctf_event_class_set_context_type(writer_event_class
, context
);
248 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
253 count
= bt_ctf_event_class_get_field_count(event_class
);
254 for (i
= 0; i
< count
; i
++) {
255 const char *field_name
;
256 struct bt_ctf_field_type
*field_type
;
259 ret
= bt_ctf_event_class_get_field(event_class
, &field_name
,
262 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
263 BT_PUT(writer_event_class
);
267 ret
= bt_ctf_event_class_add_field(writer_event_class
, field_type
,
270 fprintf(err
, "[error] Cannot add field %s\n", field_name
);
271 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
273 BT_PUT(writer_event_class
);
280 return writer_event_class
;
284 enum bt_component_status
copy_event_classes(FILE *err
,
285 struct bt_ctf_writer
*writer
,
286 struct bt_ctf_stream_class
*stream_class
,
287 struct bt_ctf_stream_class
*writer_stream_class
)
289 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
292 count
= bt_ctf_stream_class_get_event_class_count(stream_class
);
294 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
299 for (i
= 0; i
< count
; i
++) {
300 struct bt_ctf_event_class
*event_class
, *writer_event_class
;
303 event_class
= bt_ctf_stream_class_get_event_class(
306 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
308 ret
= BT_COMPONENT_STATUS_ERROR
;
312 writer_event_class
= copy_event_class(err
, event_class
);
313 if (!writer_event_class
) {
314 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
316 ret
= BT_COMPONENT_STATUS_ERROR
;
320 int_ret
= bt_ctf_stream_class_add_event_class(writer_stream_class
,
323 fprintf(err
, "[error] Failed to add event class\n");
324 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
326 ret
= BT_COMPONENT_STATUS_ERROR
;
338 enum bt_component_status
copy_stream_class(FILE *err
,
339 struct bt_ctf_writer
*writer
,
340 struct bt_ctf_stream_class
*stream_class
,
341 struct bt_ctf_stream_class
*writer_stream_class
)
343 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
344 struct bt_ctf_field_type
*type
;
345 int ret_int
, clock_count
, i
;
346 struct bt_ctf_trace
*trace
;
348 trace
= bt_ctf_stream_class_get_trace(stream_class
);
350 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
352 ret
= BT_COMPONENT_STATUS_ERROR
;
356 clock_count
= bt_ctf_trace_get_clock_count(trace
);
358 for (i
= 0; i
< clock_count
; i
++) {
359 struct bt_ctf_clock
*clock
= bt_ctf_trace_get_clock(trace
, i
);
362 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
364 ret
= BT_COMPONENT_STATUS_ERROR
;
368 ret
= copy_clock(err
, writer
, writer_stream_class
, clock
);
370 if (ret
!= BT_COMPONENT_STATUS_OK
) {
371 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
377 type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
379 ret
= BT_COMPONENT_STATUS_ERROR
;
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
);
388 ret
= BT_COMPONENT_STATUS_ERROR
;
389 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
394 type
= bt_ctf_stream_class_get_event_header_type(stream_class
);
396 ret
= BT_COMPONENT_STATUS_ERROR
;
397 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
402 ret_int
= bt_ctf_stream_class_set_event_header_type(
403 writer_stream_class
, type
);
405 ret
= BT_COMPONENT_STATUS_ERROR
;
406 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
411 type
= bt_ctf_stream_class_get_event_context_type(stream_class
);
413 ret
= BT_COMPONENT_STATUS_ERROR
;
414 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
419 ret_int
= bt_ctf_stream_class_set_event_context_type(
420 writer_stream_class
, type
);
422 ret
= BT_COMPONENT_STATUS_ERROR
;
423 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
428 ret
= copy_event_classes(err
, writer
, stream_class
, writer_stream_class
);
429 if (ret
!= BT_COMPONENT_STATUS_OK
) {
430 fprintf(err
, "[error] Failed to copy event classes\n");
431 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
443 enum bt_component_status
copy_trace(FILE *err
, struct bt_ctf_writer
*ctf_writer
,
444 struct bt_ctf_trace
*trace
)
446 struct bt_ctf_trace
*writer_trace
;
447 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
448 int field_count
, i
, int_ret
;
449 struct bt_ctf_field_type
*header_type
;
451 writer_trace
= bt_ctf_writer_get_trace(ctf_writer
);
453 ret
= BT_COMPONENT_STATUS_ERROR
;
454 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
459 field_count
= bt_ctf_trace_get_environment_field_count(trace
);
460 for (i
= 0; i
< field_count
; i
++) {
463 struct bt_value
*value
;
465 name
= bt_ctf_trace_get_environment_field_name(trace
, i
);
467 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
469 ret
= BT_COMPONENT_STATUS_ERROR
;
470 goto end_put_writer_trace
;
472 value
= bt_ctf_trace_get_environment_field_value(trace
, i
);
474 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
476 ret
= BT_COMPONENT_STATUS_ERROR
;
477 goto end_put_writer_trace
;
480 ret_int
= bt_ctf_trace_set_environment_field(writer_trace
,
483 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
485 fprintf(err
, "[error] Unable to set environment field %s\n",
487 ret
= BT_COMPONENT_STATUS_ERROR
;
488 goto end_put_writer_trace
;
492 header_type
= bt_ctf_trace_get_packet_header_type(writer_trace
);
494 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
495 ret
= BT_COMPONENT_STATUS_ERROR
;
496 goto end_put_writer_trace
;
499 int_ret
= bt_ctf_trace_set_packet_header_type(writer_trace
, header_type
);
501 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
502 ret
= BT_COMPONENT_STATUS_ERROR
;
503 goto end_put_header_type
;
508 end_put_writer_trace
:
509 bt_put(writer_trace
);
515 struct bt_ctf_stream_class
*insert_new_stream_class(
516 struct writer_component
*writer_component
,
517 struct bt_ctf_writer
*ctf_writer
,
518 struct bt_ctf_stream_class
*stream_class
)
520 struct bt_ctf_stream_class
*writer_stream_class
;
521 const char *name
= bt_ctf_stream_class_get_name(stream_class
);
522 enum bt_component_status ret
;
524 if (strlen(name
) == 0) {
528 writer_stream_class
= bt_ctf_stream_class_create(name
);
529 if (!writer_stream_class
) {
530 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
531 __func__
, __FILE__
, __LINE__
);
535 ret
= copy_stream_class(writer_component
->err
,
536 ctf_writer
, stream_class
, writer_stream_class
);
537 if (ret
!= BT_COMPONENT_STATUS_OK
) {
538 fprintf(writer_component
->err
, "[error] Failed to copy stream class\n");
539 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
540 __func__
, __FILE__
, __LINE__
);
541 BT_PUT(writer_stream_class
);
544 g_hash_table_insert(writer_component
->stream_class_map
,
545 (gpointer
) stream_class
, writer_stream_class
);
548 return writer_stream_class
;
552 struct bt_ctf_stream
*insert_new_stream(
553 struct writer_component
*writer_component
,
554 struct bt_ctf_writer
*ctf_writer
,
555 struct bt_ctf_stream_class
*stream_class
,
556 struct bt_ctf_stream
*stream
)
558 struct bt_ctf_stream
*writer_stream
;
559 struct bt_ctf_stream_class
*writer_stream_class
;
561 writer_stream_class
= g_hash_table_lookup(
562 writer_component
->stream_class_map
,
563 (gpointer
) stream_class
);
564 if (writer_stream_class
) {
565 if (!bt_get(writer_stream_class
)) {
566 writer_stream
= NULL
;
567 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
568 __func__
, __FILE__
, __LINE__
);
572 writer_stream_class
= insert_new_stream_class(
573 writer_component
, ctf_writer
, stream_class
);
574 if (!writer_stream_class
) {
575 writer_stream
= NULL
;
576 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
577 __func__
, __FILE__
, __LINE__
);
582 writer_stream
= bt_ctf_writer_create_stream(ctf_writer
,
583 writer_stream_class
);
584 if (!writer_stream
) {
585 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
586 __func__
, __FILE__
, __LINE__
);
590 g_hash_table_insert(writer_component
->stream_map
, (gpointer
) stream
,
593 bt_ctf_writer_flush_metadata(ctf_writer
);
596 bt_put(writer_stream_class
);
598 return writer_stream
;
602 struct bt_ctf_stream
*lookup_stream(struct writer_component
*writer_component
,
603 struct bt_ctf_stream
*stream
)
605 return (struct bt_ctf_stream
*) g_hash_table_lookup(
606 writer_component
->stream_map
,
611 struct bt_ctf_event_class
*get_event_class(struct writer_component
*writer_component
,
612 struct bt_ctf_stream_class
*writer_stream_class
,
613 struct bt_ctf_event_class
*event_class
)
615 return bt_ctf_stream_class_get_event_class_by_name(writer_stream_class
,
616 bt_ctf_event_class_get_name(event_class
));
619 struct bt_ctf_writer
*insert_new_writer(
620 struct writer_component
*writer_component
,
621 struct bt_ctf_trace
*trace
)
623 struct bt_ctf_writer
*ctf_writer
;
624 char trace_name
[PATH_MAX
];
625 enum bt_component_status ret
;
627 snprintf(trace_name
, PATH_MAX
, "%s/%s_%03d",
628 writer_component
->base_path
->str
,
629 writer_component
->trace_name_base
->str
,
630 writer_component
->trace_id
++);
631 printf_verbose("CTF-Writer creating trace in %s\n", trace_name
);
633 ctf_writer
= bt_ctf_writer_create(trace_name
);
635 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
636 __func__
, __FILE__
, __LINE__
);
640 ret
= copy_trace(writer_component
->err
, ctf_writer
, trace
);
641 if (ret
!= BT_COMPONENT_STATUS_OK
) {
642 fprintf(writer_component
->err
, "[error] Failed to copy trace\n");
643 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
644 __func__
, __FILE__
, __LINE__
);
649 g_hash_table_insert(writer_component
->trace_map
, (gpointer
) trace
,
657 struct bt_ctf_writer
*get_writer(struct writer_component
*writer_component
,
658 struct bt_ctf_stream_class
*stream_class
)
660 struct bt_ctf_trace
*trace
;
661 struct bt_ctf_writer
*ctf_writer
;
663 trace
= bt_ctf_stream_class_get_trace(stream_class
);
666 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
667 __func__
, __FILE__
, __LINE__
);
671 ctf_writer
= g_hash_table_lookup(writer_component
->trace_map
,
674 if (!bt_get(ctf_writer
)) {
676 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
677 __func__
, __FILE__
, __LINE__
);
681 ctf_writer
= insert_new_writer(writer_component
, trace
);
690 struct bt_ctf_stream
*get_writer_stream(
691 struct writer_component
*writer_component
,
692 struct bt_ctf_packet
*packet
, struct bt_ctf_stream
*stream
)
694 struct bt_ctf_stream_class
*stream_class
;
695 struct bt_ctf_writer
*ctf_writer
;
696 struct bt_ctf_stream
*writer_stream
;
698 stream_class
= bt_ctf_stream_get_class(stream
);
700 writer_stream
= NULL
;
701 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
702 __func__
, __FILE__
, __LINE__
);
706 ctf_writer
= get_writer(writer_component
, stream_class
);
708 writer_stream
= NULL
;
709 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
710 __func__
, __FILE__
, __LINE__
);
711 goto end_put_stream_class
;
714 writer_stream
= lookup_stream(writer_component
, stream
);
717 if (!bt_get(writer_stream
)) {
718 writer_stream
= NULL
;
719 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
720 __func__
, __FILE__
, __LINE__
);
721 goto end_put_stream_class
;
724 writer_stream
= insert_new_stream(writer_component
, ctf_writer
,
725 stream_class
, stream
);
729 end_put_stream_class
:
730 bt_put(stream_class
);
732 return writer_stream
;
736 enum bt_component_status
writer_new_packet(
737 struct writer_component
*writer_component
,
738 struct bt_ctf_packet
*packet
)
740 struct bt_ctf_stream
*stream
, *writer_stream
;
741 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
743 stream
= bt_ctf_packet_get_stream(packet
);
745 ret
= BT_COMPONENT_STATUS_ERROR
;
746 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
747 __func__
, __FILE__
, __LINE__
);
751 /* TODO: copy values for event discarded and packet_seq_num */
752 writer_stream
= get_writer_stream(writer_component
, packet
, stream
);
753 if (!writer_stream
) {
754 ret
= BT_COMPONENT_STATUS_ERROR
;
755 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
756 __func__
, __FILE__
, __LINE__
);
760 bt_put(writer_stream
);
769 enum bt_component_status
copy_packet_context_field(FILE *err
,
770 struct bt_ctf_field
*field
, const char *field_name
,
771 struct bt_ctf_field
*writer_packet_context
,
772 struct bt_ctf_field_type
*writer_packet_context_type
)
774 enum bt_component_status ret
;
775 struct bt_ctf_field
*writer_field
;
780 * TODO: handle the special case of the first/last packet that might
781 * be trimmed. In these cases, the timestamp_begin/end need to be
782 * explicitely set to the first/last event timestamps.
785 writer_field
= bt_ctf_field_structure_get_field(writer_packet_context
,
788 ret
= BT_COMPONENT_STATUS_ERROR
;
789 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
794 int_ret
= bt_ctf_field_unsigned_integer_get_value(field
, &value
);
796 fprintf(err
, "[error] Wrong packet_context field type\n");
797 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
799 ret
= BT_COMPONENT_STATUS_ERROR
;
800 goto end_put_writer_field
;
803 int_ret
= bt_ctf_field_unsigned_integer_set_value(writer_field
, value
);
805 ret
= BT_COMPONENT_STATUS_ERROR
;
806 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
808 goto end_put_writer_field
;
811 ret
= BT_COMPONENT_STATUS_OK
;
813 end_put_writer_field
:
814 bt_put(writer_field
);
820 enum bt_component_status
copy_packet_context(FILE *err
,
821 struct bt_ctf_packet
*packet
,
822 struct bt_ctf_stream
*writer_stream
)
824 enum bt_component_status ret
;
825 struct bt_ctf_field
*packet_context
, *writer_packet_context
;
826 struct bt_ctf_field_type
*struct_type
, *writer_packet_context_type
;
827 struct bt_ctf_stream_class
*writer_stream_class
;
828 int nr_fields
, i
, int_ret
;
830 packet_context
= bt_ctf_packet_get_context(packet
);
831 if (!packet_context
) {
832 ret
= BT_COMPONENT_STATUS_ERROR
;
833 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
838 writer_stream_class
= bt_ctf_stream_get_class(writer_stream
);
839 if (!writer_stream_class
) {
840 ret
= BT_COMPONENT_STATUS_ERROR
;
841 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
843 goto end_put_packet_context
;
846 writer_packet_context_type
= bt_ctf_stream_class_get_packet_context_type(
847 writer_stream_class
);
848 if (!writer_packet_context_type
) {
849 ret
= BT_COMPONENT_STATUS_ERROR
;
850 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
852 goto end_put_writer_stream_class
;
855 struct_type
= bt_ctf_field_get_type(packet_context
);
857 ret
= BT_COMPONENT_STATUS_ERROR
;
858 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
860 goto end_put_writer_packet_context_type
;
863 writer_packet_context
= bt_ctf_field_create(writer_packet_context_type
);
864 if (!writer_packet_context
) {
865 ret
= BT_COMPONENT_STATUS_ERROR
;
866 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
,
868 goto end_put_struct_type
;
871 nr_fields
= bt_ctf_field_type_structure_get_field_count(struct_type
);
872 for (i
= 0; i
< nr_fields
; i
++) {
873 struct bt_ctf_field
*field
;
874 struct bt_ctf_field_type
*field_type
;
875 const char *field_name
;
877 field
= bt_ctf_field_structure_get_field_by_index(
880 ret
= BT_COMPONENT_STATUS_ERROR
;
881 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
883 goto end_put_writer_packet_context
;
885 if (bt_ctf_field_type_structure_get_field(struct_type
,
886 &field_name
, &field_type
, i
) < 0) {
887 ret
= BT_COMPONENT_STATUS_ERROR
;
889 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
891 goto end_put_writer_packet_context
;
894 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_TYPE_ID_INTEGER
) {
895 fprintf(err
, "[error] Unexpected packet context field type\n");
897 ret
= BT_COMPONENT_STATUS_ERROR
;
898 goto end_put_writer_packet_context
;
901 ret
= copy_packet_context_field(err
, field
, field_name
,
902 writer_packet_context
, writer_packet_context_type
);
905 if (ret
!= BT_COMPONENT_STATUS_OK
) {
906 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
908 goto end_put_writer_packet_context
;
912 int_ret
= bt_ctf_stream_set_packet_context(writer_stream
,
913 writer_packet_context
);
915 ret
= BT_COMPONENT_STATUS_ERROR
;
916 goto end_put_writer_packet_context
;
919 end_put_writer_packet_context
:
920 bt_put(writer_packet_context
);
923 end_put_writer_packet_context_type
:
924 bt_put(writer_packet_context_type
);
925 end_put_writer_stream_class
:
926 bt_put(writer_stream_class
);
927 end_put_packet_context
:
928 bt_put(packet_context
);
934 enum bt_component_status
writer_close_packet(
935 struct writer_component
*writer_component
,
936 struct bt_ctf_packet
*packet
)
938 struct bt_ctf_stream
*stream
, *writer_stream
;
939 enum bt_component_status ret
;
941 stream
= bt_ctf_packet_get_stream(packet
);
943 ret
= BT_COMPONENT_STATUS_ERROR
;
944 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
945 __func__
, __FILE__
, __LINE__
);
949 writer_stream
= lookup_stream(writer_component
, stream
);
950 if (!writer_stream
) {
951 ret
= BT_COMPONENT_STATUS_ERROR
;
952 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
953 __func__
, __FILE__
, __LINE__
);
957 if (!bt_get(writer_stream
)) {
958 fprintf(writer_component
->err
,
959 "[error] Failed to get reference on writer stream\n");
960 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
961 __func__
, __FILE__
, __LINE__
);
962 ret
= BT_COMPONENT_STATUS_ERROR
;
966 ret
= copy_packet_context(writer_component
->err
, packet
, writer_stream
);
967 if (ret
!= BT_COMPONENT_STATUS_OK
) {
968 ret
= BT_COMPONENT_STATUS_ERROR
;
969 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
970 __func__
, __FILE__
, __LINE__
);
974 ret
= bt_ctf_stream_flush(writer_stream
);
976 fprintf(writer_component
->err
,
977 "[error] Failed to flush packet\n");
978 ret
= BT_COMPONENT_STATUS_ERROR
;
981 ret
= BT_COMPONENT_STATUS_OK
;
983 bt_put(writer_stream
);
992 struct bt_ctf_event
*copy_event(FILE *err
, struct bt_ctf_event
*event
,
993 struct bt_ctf_event_class
*writer_event_class
)
995 struct bt_ctf_event
*writer_event
;
996 struct bt_ctf_field
*field
;
999 writer_event
= bt_ctf_event_create(writer_event_class
);
1000 if (!writer_event
) {
1001 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
1005 field
= bt_ctf_event_get_header(event
);
1006 ret
= bt_ctf_event_set_header(writer_event
,
1007 bt_ctf_field_copy(field
));
1009 BT_PUT(writer_event
);
1010 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
1014 field
= bt_ctf_event_get_stream_event_context(event
);
1015 ret
= bt_ctf_event_set_stream_event_context(writer_event
,
1016 bt_ctf_field_copy(field
));
1018 BT_PUT(writer_event
);
1019 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
1023 field
= bt_ctf_event_get_event_context(event
);
1024 ret
= bt_ctf_event_set_event_context(writer_event
,
1025 bt_ctf_field_copy(field
));
1027 BT_PUT(writer_event
);
1028 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
1032 field
= bt_ctf_event_get_payload_field(event
);
1033 ret
= bt_ctf_event_set_payload_field(writer_event
,
1034 bt_ctf_field_copy(field
));
1036 BT_PUT(writer_event
);
1037 fprintf(err
, "[error] %s in %s:%d\n", __func__
, __FILE__
, __LINE__
);
1042 return writer_event
;
1046 enum bt_component_status
writer_output_event(
1047 struct writer_component
*writer_component
,
1048 struct bt_ctf_event
*event
)
1050 enum bt_component_status ret
;
1051 struct bt_ctf_event_class
*event_class
, *writer_event_class
;
1052 struct bt_ctf_stream
*stream
, *writer_stream
;
1053 struct bt_ctf_stream_class
*stream_class
, *writer_stream_class
;
1054 struct bt_ctf_event
*writer_event
;
1055 const char *event_name
;
1058 event_class
= bt_ctf_event_get_class(event
);
1060 ret
= BT_COMPONENT_STATUS_ERROR
;
1061 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
1062 __FILE__
, __LINE__
);
1066 event_name
= bt_ctf_event_class_get_name(event_class
);
1068 ret
= BT_COMPONENT_STATUS_ERROR
;
1069 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
1070 __FILE__
, __LINE__
);
1071 goto end_put_event_class
;
1074 stream
= bt_ctf_event_get_stream(event
);
1076 ret
= BT_COMPONENT_STATUS_ERROR
;
1077 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
1078 __FILE__
, __LINE__
);
1079 goto end_put_event_class
;
1082 writer_stream
= lookup_stream(writer_component
, stream
);
1083 if (!writer_stream
|| !bt_get(writer_stream
)) {
1084 ret
= BT_COMPONENT_STATUS_ERROR
;
1085 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
1086 __FILE__
, __LINE__
);
1087 goto end_put_stream
;
1090 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1091 if (!stream_class
) {
1092 ret
= BT_COMPONENT_STATUS_ERROR
;
1093 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
1094 __FILE__
, __LINE__
);
1095 goto end_put_writer_stream
;
1098 writer_stream_class
= g_hash_table_lookup(
1099 writer_component
->stream_class_map
,
1100 (gpointer
) stream_class
);
1101 if (!writer_stream_class
|| !bt_get(writer_stream_class
)) {
1102 ret
= BT_COMPONENT_STATUS_ERROR
;
1103 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
1104 __FILE__
, __LINE__
);
1105 goto end_put_stream_class
;
1108 writer_event_class
= get_event_class(writer_component
,
1109 writer_stream_class
, event_class
);
1110 if (!writer_event_class
) {
1111 ret
= BT_COMPONENT_STATUS_ERROR
;
1112 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
1113 __FILE__
, __LINE__
);
1114 goto end_put_writer_stream_class
;
1117 writer_event
= copy_event(writer_component
->err
, event
, writer_event_class
);
1118 if (!writer_event
) {
1119 ret
= BT_COMPONENT_STATUS_ERROR
;
1120 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
1121 __FILE__
, __LINE__
);
1122 fprintf(writer_component
->err
, "[error] Failed to copy event %s\n",
1123 bt_ctf_event_class_get_name(writer_event_class
));
1124 goto end_put_writer_event_class
;
1127 int_ret
= bt_ctf_stream_append_event(writer_stream
, writer_event
);
1129 ret
= BT_COMPONENT_STATUS_ERROR
;
1130 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
1131 __FILE__
, __LINE__
);
1132 fprintf(writer_component
->err
, "[error] Failed to append event %s\n",
1133 bt_ctf_event_class_get_name(writer_event_class
));
1134 goto end_put_writer_event
;
1137 ret
= BT_COMPONENT_STATUS_OK
;
1139 end_put_writer_event
:
1140 bt_put(writer_event
);
1141 end_put_writer_event_class
:
1142 bt_put(writer_event_class
);
1143 end_put_writer_stream_class
:
1144 bt_put(writer_stream_class
);
1145 end_put_stream_class
:
1146 bt_put(stream_class
);
1147 end_put_writer_stream
:
1148 bt_put(writer_stream
);
1151 end_put_event_class
:
1152 bt_put(event_class
);