Fix writer: leak of the event classes
[babeltrace.git] / plugins / writer / writer.c
1 /*
2 * writer.c
3 *
4 * Babeltrace CTF Writer Output Plugin
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/plugin/plugin-dev.h>
31 #include <babeltrace/graph/component.h>
32 #include <babeltrace/graph/private-component.h>
33 #include <babeltrace/graph/component-sink.h>
34 #include <babeltrace/graph/private-component-sink.h>
35 #include <babeltrace/graph/private-port.h>
36 #include <babeltrace/graph/private-connection.h>
37 #include <babeltrace/graph/notification.h>
38 #include <babeltrace/graph/notification-iterator.h>
39 #include <babeltrace/graph/notification-event.h>
40 #include <babeltrace/graph/notification-packet.h>
41 #include <plugins-common.h>
42 #include <stdio.h>
43 #include <stdbool.h>
44 #include <glib.h>
45 #include "writer.h"
46 #include <assert.h>
47
48 static
49 void destroy_writer_component_data(struct writer_component *writer_component)
50 {
51 bt_put(writer_component->input_iterator);
52 g_hash_table_destroy(writer_component->stream_map);
53 g_hash_table_destroy(writer_component->stream_class_map);
54 g_hash_table_destroy(writer_component->trace_map);
55 g_string_free(writer_component->base_path, true);
56 g_string_free(writer_component->trace_name_base, true);
57 }
58
59 static
60 void finalize_writer_component(struct bt_private_component *component)
61 {
62 struct writer_component *writer_component = (struct writer_component *)
63 bt_private_component_get_user_data(component);
64
65 destroy_writer_component_data(writer_component);
66 g_free(writer_component);
67 }
68
69 static
70 void unref_stream_class(struct bt_ctf_stream_class *writer_stream_class)
71 {
72 return;
73 }
74
75 static
76 void unref_stream(struct bt_ctf_stream_class *writer_stream)
77 {
78 bt_put(writer_stream);
79 }
80
81 static
82 void unref_trace(struct bt_ctf_writer *writer)
83 {
84 return;
85 }
86
87 static
88 struct writer_component *create_writer_component(void)
89 {
90 struct writer_component *writer_component;
91
92 writer_component = g_new0(struct writer_component, 1);
93 if (!writer_component) {
94 goto end;
95 }
96
97 writer_component->err = stderr;
98 writer_component->trace_id = 0;
99 writer_component->trace_name_base = g_string_new("trace");
100 if (!writer_component->trace_name_base) {
101 g_free(writer_component);
102 writer_component = NULL;
103 goto end;
104 }
105
106 /*
107 * Reader to writer corresponding structures.
108 */
109 writer_component->trace_map = g_hash_table_new_full(g_direct_hash,
110 g_direct_equal, NULL, (GDestroyNotify) unref_trace);
111 writer_component->stream_class_map = g_hash_table_new_full(g_direct_hash,
112 g_direct_equal, NULL, (GDestroyNotify) unref_stream_class);
113 writer_component->stream_map = g_hash_table_new_full(g_direct_hash,
114 g_direct_equal, NULL, (GDestroyNotify) unref_stream);
115
116 end:
117 return writer_component;
118 }
119
120 static
121 enum bt_component_status handle_notification(
122 struct writer_component *writer_component,
123 struct bt_notification *notification)
124 {
125 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
126
127 if (!writer_component) {
128 ret = BT_COMPONENT_STATUS_ERROR;
129 goto end;
130 }
131
132 switch (bt_notification_get_type(notification)) {
133 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
134 {
135 struct bt_ctf_packet *packet =
136 bt_notification_packet_begin_get_packet(notification);
137
138 if (!packet) {
139 ret = BT_COMPONENT_STATUS_ERROR;
140 goto end;
141 }
142
143 ret = writer_new_packet(writer_component, packet);
144 bt_put(packet);
145 break;
146 }
147 case BT_NOTIFICATION_TYPE_PACKET_END:
148 {
149 struct bt_ctf_packet *packet =
150 bt_notification_packet_end_get_packet(notification);
151
152 if (!packet) {
153 ret = BT_COMPONENT_STATUS_ERROR;
154 goto end;
155 }
156 ret = writer_close_packet(writer_component, packet);
157 bt_put(packet);
158 break;
159 }
160 case BT_NOTIFICATION_TYPE_EVENT:
161 {
162 struct bt_ctf_event *event = bt_notification_event_get_event(
163 notification);
164
165 if (!event) {
166 ret = BT_COMPONENT_STATUS_ERROR;
167 goto end;
168 }
169 ret = BT_COMPONENT_STATUS_OK;
170 ret = writer_output_event(writer_component, event);
171 bt_put(event);
172 if (ret != BT_COMPONENT_STATUS_OK) {
173 goto end;
174 }
175 break;
176 }
177 case BT_NOTIFICATION_TYPE_STREAM_END:
178 break;
179 default:
180 puts("Unhandled notification type");
181 }
182 end:
183 return ret;
184 }
185
186 static
187 enum bt_component_status writer_component_accept_port_connection(
188 struct bt_private_component *component,
189 struct bt_private_port *self_port,
190 struct bt_port *other_port)
191 {
192 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
193 struct bt_private_connection *connection;
194 struct writer_component *writer;
195
196 writer = bt_private_component_get_user_data(component);
197 assert(writer);
198 assert(!writer->input_iterator);
199 connection = bt_private_port_get_private_connection(self_port);
200 assert(connection);
201 writer->input_iterator =
202 bt_private_connection_create_notification_iterator(connection);
203
204 if (!writer->input_iterator) {
205 ret = BT_COMPONENT_STATUS_ERROR;
206 }
207 bt_put(connection);
208 return ret;
209 }
210
211 static
212 enum bt_component_status run(struct bt_private_component *component)
213 {
214 enum bt_component_status ret;
215 enum bt_notification_iterator_status it_status;
216 struct bt_notification *notification = NULL;
217 struct bt_notification_iterator *it;
218 struct writer_component *writer_component =
219 bt_private_component_get_user_data(component);
220
221 it = writer_component->input_iterator;
222 assert(it);
223
224 notification = bt_notification_iterator_get_notification(it);
225 if (!notification) {
226 ret = BT_COMPONENT_STATUS_ERROR;
227 goto end;
228 }
229
230 it_status = bt_notification_iterator_next(it);
231 if (it_status != BT_COMPONENT_STATUS_OK) {
232 ret = BT_COMPONENT_STATUS_ERROR;
233 goto end;
234 }
235
236 ret = handle_notification(writer_component, notification);
237 end:
238 bt_put(it);
239 bt_put(notification);
240 return ret;
241 }
242
243 static
244 enum bt_component_status writer_component_init(
245 struct bt_private_component *component, struct bt_value *params,
246 UNUSED_VAR void *init_method_data)
247 {
248 enum bt_component_status ret;
249 enum bt_value_status value_ret;
250 struct writer_component *writer_component = create_writer_component();
251 struct bt_value *value = NULL;
252 const char *path;
253
254 if (!writer_component) {
255 ret = BT_COMPONENT_STATUS_NOMEM;
256 goto end;
257 }
258
259 value = bt_value_map_get(params, "path");
260 if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
261 fprintf(writer_component->err,
262 "[error] output path parameter required\n");
263 ret = BT_COMPONENT_STATUS_INVALID;
264 goto error;
265 }
266
267 value_ret = bt_value_string_get(value, &path);
268 if (value_ret != BT_VALUE_STATUS_OK) {
269 ret = BT_COMPONENT_STATUS_INVALID;
270 goto error;
271 }
272 bt_put(value);
273
274 writer_component->base_path = g_string_new(path);
275 if (!writer_component) {
276 ret = BT_COMPONENT_STATUS_ERROR;
277 goto error;
278 }
279
280 ret = bt_private_component_set_user_data(component, writer_component);
281 if (ret != BT_COMPONENT_STATUS_OK) {
282 goto error;
283 }
284
285 end:
286 return ret;
287 error:
288 destroy_writer_component_data(writer_component);
289 g_free(writer_component);
290 return ret;
291 }
292
293 /* Initialize plug-in entry points. */
294 BT_PLUGIN(writer);
295 BT_PLUGIN_DESCRIPTION("Babeltrace CTF-Writer output plug-in.");
296 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
297 BT_PLUGIN_LICENSE("MIT");
298 BT_PLUGIN_SINK_COMPONENT_CLASS(writer, run);
299 BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(writer, writer_component_init);
300 BT_PLUGIN_SINK_COMPONENT_CLASS_ACCEPT_PORT_CONNECTION_METHOD(writer,
301 writer_component_accept_port_connection);
302 BT_PLUGIN_SINK_COMPONENT_CLASS_FINALIZE_METHOD(writer, finalize_writer_component);
303 BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(writer, "Formats CTF-IR to CTF.");
This page took 0.036401 seconds and 4 git commands to generate.