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