debug-info filter plugin
[babeltrace.git] / plugins / debug-info / plugin.c
1 /*
2 * plugin.c
3 *
4 * Babeltrace Debug Info Plug-in
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/plugin/plugin-dev.h>
30 #include <babeltrace/component/component.h>
31 #include <babeltrace/component/component-filter.h>
32 #include <babeltrace/component/port.h>
33 #include <babeltrace/component/connection.h>
34 #include <babeltrace/component/notification/notification.h>
35 #include <babeltrace/component/notification/iterator.h>
36 #include <babeltrace/component/notification/event.h>
37 #include <babeltrace/component/notification/packet.h>
38 #include <babeltrace/component/notification/stream.h>
39 #include <plugins-common.h>
40 #include <assert.h>
41 #include "debug-info.h"
42 #include "copy.h"
43
44 static
45 void destroy_debug_info_data(struct debug_info_component *debug_info)
46 {
47 free(debug_info->arg_debug_info_field_name);
48 g_free(debug_info);
49 }
50
51 static
52 void destroy_debug_info_component(struct bt_component *component)
53 {
54 void *data = bt_component_get_private_data(component);
55 destroy_debug_info_data(data);
56 }
57
58 static
59 struct debug_info_component *create_debug_info_component_data(void)
60 {
61 struct debug_info_component *debug_info;
62
63 debug_info = g_new0(struct debug_info_component, 1);
64 if (!debug_info) {
65 goto end;
66 }
67 debug_info->err = stderr;
68
69 end:
70 return debug_info;
71 }
72
73 static
74 void unref_debug_info(struct debug_info *debug_info)
75 {
76 debug_info_destroy(debug_info);
77 }
78
79 static
80 void unref_trace(struct bt_ctf_trace *trace)
81 {
82 bt_put(trace);
83 }
84
85 static
86 void unref_stream(struct bt_ctf_stream *stream)
87 {
88 bt_put(stream);
89 }
90
91 static
92 void unref_packet(struct bt_ctf_packet *packet)
93 {
94 bt_put(packet);
95 }
96
97 static
98 void unref_stream_class(struct bt_ctf_stream_class *stream_class)
99 {
100 bt_put(stream_class);
101 }
102
103 static
104 void debug_info_iterator_destroy(struct bt_notification_iterator *it)
105 {
106 struct debug_info_iterator *it_data;
107
108 it_data = bt_notification_iterator_get_private_data(it);
109 assert(it_data);
110
111 if (it_data->input_iterator_group) {
112 g_ptr_array_free(it_data->input_iterator_group, TRUE);
113 }
114 bt_put(it_data->current_notification);
115 bt_put(it_data->input_iterator);
116 g_hash_table_destroy(it_data->trace_map);
117 g_hash_table_destroy(it_data->stream_map);
118 g_hash_table_destroy(it_data->stream_class_map);
119 g_hash_table_destroy(it_data->packet_map);
120 g_hash_table_destroy(it_data->trace_debug_map);
121 g_free(it_data);
122 }
123
124 static
125 struct bt_notification *handle_notification(FILE *err,
126 struct debug_info_iterator *debug_it,
127 struct bt_notification *notification)
128 {
129 struct bt_notification *new_notification = NULL;
130
131 switch (bt_notification_get_type(notification)) {
132 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
133 {
134 struct bt_ctf_packet *packet =
135 bt_notification_packet_begin_get_packet(notification);
136 struct bt_ctf_packet *writer_packet;
137
138 if (!packet) {
139 goto end;
140 }
141
142 writer_packet = debug_info_new_packet(debug_it, packet);
143 assert(writer_packet);
144 new_notification = bt_notification_packet_begin_create(
145 writer_packet);
146 assert(new_notification);
147 bt_put(packet);
148 break;
149 }
150 case BT_NOTIFICATION_TYPE_PACKET_END:
151 {
152 struct bt_ctf_packet *packet =
153 bt_notification_packet_end_get_packet(notification);
154 struct bt_ctf_packet *writer_packet;
155
156 if (!packet) {
157 goto end;
158 }
159
160 writer_packet = debug_info_close_packet(debug_it, packet);
161 assert(writer_packet);
162 new_notification = bt_notification_packet_end_create(
163 writer_packet);
164 assert(new_notification);
165 bt_put(packet);
166 bt_put(writer_packet);
167 break;
168 }
169 case BT_NOTIFICATION_TYPE_EVENT:
170 {
171 struct bt_ctf_event *event = bt_notification_event_get_event(
172 notification);
173 struct bt_ctf_event *writer_event;
174 struct bt_clock_class_priority_map *cc_prio_map =
175 bt_notification_event_get_clock_class_priority_map(
176 notification);
177
178 if (!event) {
179 goto end;
180 }
181 writer_event = debug_info_output_event(debug_it, event);
182 assert(writer_event);
183 new_notification = bt_notification_event_create(writer_event,
184 cc_prio_map);
185 bt_put(cc_prio_map);
186 assert(new_notification);
187 bt_put(event);
188 bt_put(writer_event);
189 break;
190 }
191 case BT_NOTIFICATION_TYPE_STREAM_END:
192 {
193 struct bt_ctf_stream *stream =
194 bt_notification_stream_end_get_stream(notification);
195 struct bt_ctf_stream *writer_stream;
196
197 if (!stream) {
198 goto end;
199 }
200
201 writer_stream = debug_info_stream_end(debug_it, stream);
202 assert(writer_stream);
203 new_notification = bt_notification_stream_end_create(
204 writer_stream);
205 assert(new_notification);
206 bt_put(stream);
207 bt_put(writer_stream);
208 break;
209 }
210 default:
211 puts("Unhandled notification type");
212 }
213
214 end:
215 return new_notification;
216 }
217
218 static
219 enum bt_notification_iterator_status debug_info_iterator_next(
220 struct bt_notification_iterator *iterator)
221 {
222 struct debug_info_iterator *debug_it = NULL;
223 struct bt_component *component = NULL;
224 struct debug_info_component *debug_info = NULL;
225 struct bt_notification_iterator *source_it = NULL;
226 enum bt_notification_iterator_status ret =
227 BT_NOTIFICATION_ITERATOR_STATUS_OK;
228 struct bt_notification *notification, *new_notification;
229
230 debug_it = bt_notification_iterator_get_private_data(iterator);
231 assert(debug_it);
232
233 component = bt_notification_iterator_get_component(iterator);
234 assert(component);
235 debug_info = bt_component_get_private_data(component);
236 assert(debug_info);
237
238 source_it = debug_it->input_iterator;
239
240 ret = bt_notification_iterator_next(source_it);
241 if (ret != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
242 goto end;
243 }
244
245 notification = bt_notification_iterator_get_notification(
246 source_it);
247 if (!notification) {
248 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
249 goto end;
250 }
251
252 new_notification = handle_notification(debug_info->err, debug_it,
253 notification);
254 assert(new_notification);
255 bt_put(notification);
256
257 BT_MOVE(debug_it->current_notification, new_notification);
258
259 end:
260 bt_put(component);
261 return ret;
262 }
263
264 static
265 struct bt_notification *debug_info_iterator_get(
266 struct bt_notification_iterator *iterator)
267 {
268 struct debug_info_iterator *debug_it;
269
270 debug_it = bt_notification_iterator_get_private_data(iterator);
271 assert(debug_it);
272
273 if (!debug_it->current_notification) {
274 enum bt_notification_iterator_status it_ret;
275
276 it_ret = debug_info_iterator_next(iterator);
277 if (it_ret) {
278 goto end;
279 }
280 }
281
282 end:
283 return bt_get(debug_it->current_notification);
284 }
285
286 static
287 enum bt_notification_iterator_status debug_info_iterator_seek_time(
288 struct bt_notification_iterator *iterator, int64_t time)
289 {
290 enum bt_notification_iterator_status ret;
291
292 ret = BT_NOTIFICATION_ITERATOR_STATUS_OK;
293
294 return ret;
295 }
296
297 static
298 enum bt_notification_iterator_status debug_info_iterator_init(struct bt_component *component,
299 struct bt_notification_iterator *iterator,
300 UNUSED_VAR void *init_method_data)
301 {
302 enum bt_notification_iterator_status ret =
303 BT_NOTIFICATION_ITERATOR_STATUS_OK;
304 enum bt_notification_iterator_status it_ret;
305 struct bt_port *input_port = NULL;
306 struct bt_connection *connection = NULL;
307 struct debug_info_iterator *it_data = g_new0(struct debug_info_iterator, 1);
308
309 if (!it_data) {
310 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
311 goto end;
312 }
313
314 /* Create a new iterator on the upstream component. */
315 input_port = bt_component_filter_get_default_input_port(component);
316 assert(input_port);
317 connection = bt_port_get_connection(input_port, 0);
318 assert(connection);
319
320 it_data->input_iterator = bt_connection_create_notification_iterator(
321 connection);
322 if (!it_data->input_iterator) {
323 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
324 goto end;
325 }
326 it_data->debug_info_component = (struct debug_info_component *)
327 bt_component_get_private_data(component);
328
329 it_data->err = it_data->debug_info_component->err;
330 it_data->trace_map = g_hash_table_new_full(g_direct_hash,
331 g_direct_equal, NULL, (GDestroyNotify) unref_trace);
332 it_data->stream_map = g_hash_table_new_full(g_direct_hash,
333 g_direct_equal, NULL, (GDestroyNotify) unref_stream);
334 it_data->stream_class_map = g_hash_table_new_full(g_direct_hash,
335 g_direct_equal, NULL, (GDestroyNotify) unref_stream_class);
336 it_data->packet_map = g_hash_table_new_full(g_direct_hash,
337 g_direct_equal, NULL, (GDestroyNotify) unref_packet);
338 it_data->trace_debug_map = g_hash_table_new_full(g_direct_hash,
339 g_direct_equal, NULL, (GDestroyNotify) unref_debug_info);
340
341 it_ret = bt_notification_iterator_set_private_data(iterator, it_data);
342 if (it_ret) {
343 goto end;
344 }
345
346 end:
347 bt_put(connection);
348 bt_put(input_port);
349 return ret;
350 }
351
352 static
353 enum bt_component_status init_from_params(
354 struct debug_info_component *debug_info_component,
355 struct bt_value *params)
356 {
357 struct bt_value *value = NULL;
358 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
359
360 assert(params);
361
362 value = bt_value_map_get(params, "debug-info-field-name");
363 if (value) {
364 enum bt_value_status value_ret;
365 const char *tmp;
366
367 value_ret = bt_value_string_get(value, &tmp);
368 if (value_ret) {
369 ret = BT_COMPONENT_STATUS_INVALID;
370 printf_error("Failed to retrieve debug-info-field-name value. "
371 "Expecting a string");
372 }
373 strcpy(debug_info_component->arg_debug_info_field_name, tmp);
374 bt_put(value);
375 } else {
376 debug_info_component->arg_debug_info_field_name =
377 malloc(strlen("debug_info") + 1);
378 if (!debug_info_component->arg_debug_info_field_name) {
379 ret = BT_COMPONENT_STATUS_NOMEM;
380 printf_error();
381 }
382 sprintf(debug_info_component->arg_debug_info_field_name,
383 "debug_info");
384 }
385 if (ret != BT_COMPONENT_STATUS_OK) {
386 goto end;
387 }
388
389 value = bt_value_map_get(params, "debug-dir");
390 if (value) {
391 enum bt_value_status value_ret;
392
393 value_ret = bt_value_string_get(value,
394 &debug_info_component->arg_debug_dir);
395 if (value_ret) {
396 ret = BT_COMPONENT_STATUS_INVALID;
397 printf_error("Failed to retrieve debug-dir value. "
398 "Expecting a string");
399 }
400 }
401 bt_put(value);
402 if (ret != BT_COMPONENT_STATUS_OK) {
403 goto end;
404 }
405
406 value = bt_value_map_get(params, "target-prefix");
407 if (value) {
408 enum bt_value_status value_ret;
409
410 value_ret = bt_value_string_get(value,
411 &debug_info_component->arg_target_prefix);
412 if (value_ret) {
413 ret = BT_COMPONENT_STATUS_INVALID;
414 printf_error("Failed to retrieve target-prefix value. "
415 "Expecting a string");
416 }
417 }
418 bt_put(value);
419 if (ret != BT_COMPONENT_STATUS_OK) {
420 goto end;
421 }
422
423 value = bt_value_map_get(params, "full-path");
424 if (value) {
425 enum bt_value_status value_ret;
426
427 value_ret = bt_value_bool_get(value,
428 &debug_info_component->arg_full_path);
429 if (value_ret) {
430 ret = BT_COMPONENT_STATUS_INVALID;
431 printf_error("Failed to retrieve full-path value. "
432 "Expecting a boolean");
433 }
434 }
435 bt_put(value);
436 if (ret != BT_COMPONENT_STATUS_OK) {
437 goto end;
438 }
439
440 end:
441 return ret;
442 }
443
444 enum bt_component_status debug_info_component_init(
445 struct bt_component *component, struct bt_value *params,
446 UNUSED_VAR void *init_method_data)
447 {
448 enum bt_component_status ret;
449 struct debug_info_component *debug_info = create_debug_info_component_data();
450
451 if (!debug_info) {
452 ret = BT_COMPONENT_STATUS_NOMEM;
453 goto end;
454 }
455
456 ret = bt_component_set_private_data(component, debug_info);
457 if (ret != BT_COMPONENT_STATUS_OK) {
458 goto error;
459 }
460
461 ret = init_from_params(debug_info, params);
462 end:
463 return ret;
464 error:
465 destroy_debug_info_data(debug_info);
466 return ret;
467 }
468
469 /* Initialize plug-in entry points. */
470 BT_PLUGIN(debug_info);
471 BT_PLUGIN_DESCRIPTION("Babeltrace Debug Informations Plug-In.");
472 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
473 BT_PLUGIN_LICENSE("MIT");
474
475 BT_PLUGIN_FILTER_COMPONENT_CLASS(debug_info, debug_info_iterator_get,
476 debug_info_iterator_next);
477 BT_PLUGIN_FILTER_COMPONENT_CLASS_DESCRIPTION(debug_info,
478 "Add the debug information to events if possible.");
479 BT_PLUGIN_FILTER_COMPONENT_CLASS_INIT_METHOD(debug_info, debug_info_component_init);
480 BT_PLUGIN_FILTER_COMPONENT_CLASS_DESTROY_METHOD(debug_info, destroy_debug_info_component);
481 BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_INIT_METHOD(debug_info,
482 debug_info_iterator_init);
483 BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_DESTROY_METHOD(debug_info,
484 debug_info_iterator_destroy);
485 BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_SEEK_TIME_METHOD(debug_info,
486 debug_info_iterator_seek_time);
This page took 0.038839 seconds and 4 git commands to generate.