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