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