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