4 * Babeltrace CTF Writer Output Plugin
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/packet.h>
30 #include <babeltrace/plugin/plugin-dev.h>
31 #include <babeltrace/component/component.h>
32 #include <babeltrace/component/component-sink.h>
33 #include <babeltrace/component/port.h>
34 #include <babeltrace/component/connection.h>
35 #include <babeltrace/component/notification/notification.h>
36 #include <babeltrace/component/notification/iterator.h>
37 #include <babeltrace/component/notification/event.h>
38 #include <babeltrace/component/notification/packet.h>
39 #include <plugins-common.h>
47 void destroy_writer_component_data(struct writer_component
*writer_component
)
49 bt_put(writer_component
->input_iterator
);
50 g_hash_table_destroy(writer_component
->stream_map
);
51 g_hash_table_destroy(writer_component
->stream_class_map
);
52 g_hash_table_destroy(writer_component
->trace_map
);
53 g_string_free(writer_component
->base_path
, true);
54 g_string_free(writer_component
->trace_name_base
, true);
58 void destroy_writer_component(struct bt_component
*component
)
60 struct writer_component
*writer_component
= (struct writer_component
*)
61 bt_component_get_private_data(component
);
63 destroy_writer_component_data(writer_component
);
64 g_free(writer_component
);
68 void unref_stream_class(struct bt_ctf_stream_class
*writer_stream_class
)
70 BT_PUT(writer_stream_class
);
71 g_free(writer_stream_class
);
75 void unref_stream(struct bt_ctf_stream_class
*writer_stream
)
77 BT_PUT(writer_stream
);
78 g_free(writer_stream
);
82 void unref_trace(struct bt_ctf_writer
*writer
)
89 struct writer_component
*create_writer_component(void)
91 struct writer_component
*writer_component
;
93 writer_component
= g_new0(struct writer_component
, 1);
94 if (!writer_component
) {
98 writer_component
->err
= stderr
;
99 writer_component
->trace_id
= 0;
100 writer_component
->trace_name_base
= g_string_new("trace");
101 if (!writer_component
->trace_name_base
) {
102 g_free(writer_component
);
103 writer_component
= NULL
;
108 * Reader to writer corresponding structures.
110 writer_component
->trace_map
= g_hash_table_new_full(g_direct_hash
,
111 g_direct_equal
, NULL
, (GDestroyNotify
) unref_trace
);
112 writer_component
->stream_class_map
= g_hash_table_new_full(g_direct_hash
,
113 g_direct_equal
, NULL
, (GDestroyNotify
) unref_stream_class
);
114 writer_component
->stream_map
= g_hash_table_new_full(g_direct_hash
,
115 g_direct_equal
, NULL
, (GDestroyNotify
) unref_stream
);
118 return writer_component
;
122 enum bt_component_status
handle_notification(
123 struct writer_component
*writer_component
,
124 struct bt_notification
*notification
)
126 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
128 if (!writer_component
) {
129 ret
= BT_COMPONENT_STATUS_ERROR
;
133 switch (bt_notification_get_type(notification
)) {
134 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
136 struct bt_ctf_packet
*packet
=
137 bt_notification_packet_begin_get_packet(notification
);
140 ret
= BT_COMPONENT_STATUS_ERROR
;
144 ret
= writer_new_packet(writer_component
, packet
);
148 case BT_NOTIFICATION_TYPE_PACKET_END
:
150 struct bt_ctf_packet
*packet
=
151 bt_notification_packet_end_get_packet(notification
);
154 ret
= BT_COMPONENT_STATUS_ERROR
;
157 ret
= writer_close_packet(writer_component
, packet
);
161 case BT_NOTIFICATION_TYPE_EVENT
:
163 struct bt_ctf_event
*event
= bt_notification_event_get_event(
167 ret
= BT_COMPONENT_STATUS_ERROR
;
170 ret
= BT_COMPONENT_STATUS_OK
;
171 ret
= writer_output_event(writer_component
, event
);
173 if (ret
!= BT_COMPONENT_STATUS_OK
) {
178 case BT_NOTIFICATION_TYPE_STREAM_END
:
181 puts("Unhandled notification type");
188 enum bt_component_status
writer_component_new_connection(
189 struct bt_port
*own_port
, struct bt_connection
*connection
)
191 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
192 struct bt_component
*component
;
193 struct writer_component
*writer
;
195 component
= bt_port_get_component(own_port
);
197 writer
= bt_component_get_private_data(component
);
199 assert(!writer
->input_iterator
);
200 writer
->input_iterator
= bt_connection_create_notification_iterator(
203 if (!writer
->input_iterator
) {
204 ret
= BT_COMPONENT_STATUS_ERROR
;
211 enum bt_component_status
run(struct bt_component
*component
)
213 enum bt_component_status ret
;
214 struct bt_notification
*notification
= NULL
;
215 struct bt_notification_iterator
*it
;
216 struct writer_component
*writer_component
=
217 bt_component_get_private_data(component
);
219 it
= writer_component
->input_iterator
;
222 notification
= bt_notification_iterator_get_notification(it
);
224 ret
= BT_COMPONENT_STATUS_ERROR
;
228 ret
= bt_notification_iterator_next(it
);
229 if (ret
!= BT_COMPONENT_STATUS_OK
) {
233 ret
= handle_notification(writer_component
, notification
);
236 bt_put(notification
);
241 enum bt_component_status
writer_component_init(
242 struct bt_component
*component
, struct bt_value
*params
,
243 UNUSED_VAR
void *init_method_data
)
245 enum bt_component_status ret
;
246 enum bt_value_status value_ret
;
247 struct writer_component
*writer_component
= create_writer_component();
248 struct bt_value
*value
= NULL
;
251 if (!writer_component
) {
252 ret
= BT_COMPONENT_STATUS_NOMEM
;
256 value
= bt_value_map_get(params
, "path");
257 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
258 fprintf(writer_component
->err
,
259 "[error] output path parameter required\n");
260 ret
= BT_COMPONENT_STATUS_INVALID
;
264 value_ret
= bt_value_string_get(value
, &path
);
265 if (value_ret
!= BT_VALUE_STATUS_OK
) {
266 ret
= BT_COMPONENT_STATUS_INVALID
;
270 writer_component
->base_path
= g_string_new(path
);
271 if (!writer_component
) {
272 ret
= BT_COMPONENT_STATUS_ERROR
;
276 ret
= bt_component_set_private_data(component
, writer_component
);
277 if (ret
!= BT_COMPONENT_STATUS_OK
) {
284 destroy_writer_component_data(writer_component
);
285 g_free(writer_component
);
289 /* Initialize plug-in entry points. */
291 BT_PLUGIN_DESCRIPTION("Babeltrace CTF-Writer output plug-in.");
292 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
293 BT_PLUGIN_LICENSE("MIT");
294 BT_PLUGIN_SINK_COMPONENT_CLASS(writer
, run
);
295 BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(writer
, writer_component_init
);
296 BT_PLUGIN_SINK_COMPONENT_CLASS_NEW_CONNECTION_METHOD(writer
,
297 writer_component_new_connection
);
298 BT_PLUGIN_SINK_COMPONENT_CLASS_DESTROY_METHOD(writer
, destroy_writer_component
);
299 BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(writer
, "Formats CTF-IR to CTF.");