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 | ||
b4565e8b PP |
29 | #define BT_LOG_TAG "PLUGIN-CTF-LTTNG-UTILS-DEBUG-INFO-FLT" |
30 | #include "logging.h" | |
31 | ||
9d408fca | 32 | #include <babeltrace/babeltrace.h> |
4f45f9bb | 33 | #include <plugins-common.h> |
f6ccaed9 | 34 | #include <babeltrace/assert-internal.h> |
4f45f9bb JD |
35 | #include "debug-info.h" |
36 | #include "copy.h" | |
37 | ||
1c78e839 JD |
38 | static |
39 | gboolean empty_trace_map(gpointer key, gpointer value, gpointer user_data) | |
40 | { | |
41 | struct debug_info_trace *di_trace = value; | |
42 | ||
43 | di_trace->trace_static = 1; | |
44 | debug_info_close_trace(di_trace->debug_it, di_trace); | |
45 | ||
46 | return TRUE; | |
47 | } | |
48 | ||
4f45f9bb JD |
49 | static |
50 | void destroy_debug_info_data(struct debug_info_component *debug_info) | |
51 | { | |
52 | free(debug_info->arg_debug_info_field_name); | |
53 | g_free(debug_info); | |
54 | } | |
55 | ||
56 | static | |
b19ff26f | 57 | void destroy_debug_info_component(bt_self_component *component) |
4f45f9bb | 58 | { |
d94d92ac | 59 | void *data = bt_self_component_get_user_data(component); |
4f45f9bb JD |
60 | destroy_debug_info_data(data); |
61 | } | |
62 | ||
63 | static | |
64 | struct debug_info_component *create_debug_info_component_data(void) | |
65 | { | |
66 | struct debug_info_component *debug_info; | |
67 | ||
68 | debug_info = g_new0(struct debug_info_component, 1); | |
69 | if (!debug_info) { | |
70 | goto end; | |
71 | } | |
72 | debug_info->err = stderr; | |
73 | ||
74 | end: | |
75 | return debug_info; | |
76 | } | |
77 | ||
78 | static | |
504db471 | 79 | void unref_trace(struct debug_info_trace *di_trace) |
4f45f9bb | 80 | { |
c5b9b441 | 81 | bt_trace_put_ref(di_trace->writer_trace); |
1c78e839 | 82 | g_free(di_trace); |
4f45f9bb JD |
83 | } |
84 | ||
85 | static | |
d6e69534 | 86 | void debug_info_iterator_destroy(bt_self_message_iterator *it) |
4f45f9bb JD |
87 | { |
88 | struct debug_info_iterator *it_data; | |
89 | ||
d6e69534 | 90 | it_data = bt_self_message_iterator_get_user_data(it); |
f6ccaed9 | 91 | BT_ASSERT(it_data); |
4f45f9bb JD |
92 | |
93 | if (it_data->input_iterator_group) { | |
94 | g_ptr_array_free(it_data->input_iterator_group, TRUE); | |
95 | } | |
1c78e839 JD |
96 | |
97 | g_hash_table_foreach_remove(it_data->trace_map, | |
98 | empty_trace_map, it_data); | |
99 | g_hash_table_destroy(it_data->trace_map); | |
100 | ||
d6e69534 | 101 | bt_message_put_ref(it_data->current_message); |
65300d60 | 102 | bt_object_put_ref(it_data->input_iterator); |
1c78e839 | 103 | |
4f45f9bb JD |
104 | g_free(it_data); |
105 | } | |
106 | ||
107 | static | |
d6e69534 | 108 | const bt_message *handle_message(FILE *err, |
4f45f9bb | 109 | struct debug_info_iterator *debug_it, |
d6e69534 | 110 | const bt_message *message) |
4f45f9bb | 111 | { |
d6e69534 | 112 | const bt_message *new_message = NULL; |
4f45f9bb | 113 | |
d6e69534 PP |
114 | switch (bt_message_get_type(message)) { |
115 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
4f45f9bb | 116 | { |
b19ff26f | 117 | const bt_packet *packet = |
d6e69534 | 118 | bt_message_packet_beginning_get_packet(message); |
b19ff26f | 119 | const bt_packet *writer_packet; |
4f45f9bb JD |
120 | |
121 | if (!packet) { | |
122 | goto end; | |
123 | } | |
124 | ||
125 | writer_packet = debug_info_new_packet(debug_it, packet); | |
f6ccaed9 | 126 | BT_ASSERT(writer_packet); |
d6e69534 | 127 | new_message = bt_message_packet_beginning_create( |
4f45f9bb | 128 | writer_packet); |
d6e69534 | 129 | BT_ASSERT(new_message); |
c5b9b441 PP |
130 | bt_packet_put_ref(packet); |
131 | bt_packet_put_ref(writer_packet); | |
4f45f9bb JD |
132 | break; |
133 | } | |
d6e69534 | 134 | case BT_MESSAGE_TYPE_PACKET_END: |
4f45f9bb | 135 | { |
b19ff26f | 136 | const bt_packet *packet = |
d6e69534 | 137 | bt_message_packet_end_get_packet(message); |
b19ff26f | 138 | const bt_packet *writer_packet; |
4f45f9bb JD |
139 | |
140 | if (!packet) { | |
141 | goto end; | |
142 | } | |
143 | ||
144 | writer_packet = debug_info_close_packet(debug_it, packet); | |
f6ccaed9 | 145 | BT_ASSERT(writer_packet); |
d6e69534 | 146 | new_message = bt_message_packet_end_create( |
4f45f9bb | 147 | writer_packet); |
d6e69534 | 148 | BT_ASSERT(new_message); |
c5b9b441 PP |
149 | bt_packet_put_ref(packet); |
150 | bt_packet_put_ref(writer_packet); | |
4f45f9bb JD |
151 | break; |
152 | } | |
d6e69534 | 153 | case BT_MESSAGE_TYPE_EVENT: |
4f45f9bb | 154 | { |
d6e69534 PP |
155 | const bt_event *event = bt_message_event_get_event( |
156 | message); | |
b19ff26f PP |
157 | const bt_event *writer_event; |
158 | bt_clock_class_priority_map *cc_prio_map = | |
d6e69534 PP |
159 | bt_message_event_get_clock_class_priority_map( |
160 | message); | |
4f45f9bb JD |
161 | |
162 | if (!event) { | |
163 | goto end; | |
164 | } | |
165 | writer_event = debug_info_output_event(debug_it, event); | |
f6ccaed9 | 166 | BT_ASSERT(writer_event); |
d6e69534 | 167 | new_message = bt_message_event_create(writer_event, |
4f45f9bb | 168 | cc_prio_map); |
65300d60 | 169 | bt_object_put_ref(cc_prio_map); |
d6e69534 | 170 | BT_ASSERT(new_message); |
65300d60 PP |
171 | bt_object_put_ref(event); |
172 | bt_object_put_ref(writer_event); | |
4f45f9bb JD |
173 | break; |
174 | } | |
d6e69534 | 175 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
504db471 | 176 | { |
b19ff26f | 177 | const bt_stream *stream = |
d6e69534 | 178 | bt_message_stream_beginning_get_stream(message); |
b19ff26f | 179 | const bt_stream *writer_stream; |
504db471 JD |
180 | |
181 | if (!stream) { | |
182 | goto end; | |
183 | } | |
184 | ||
185 | writer_stream = debug_info_stream_begin(debug_it, stream); | |
f6ccaed9 | 186 | BT_ASSERT(writer_stream); |
d6e69534 | 187 | new_message = bt_message_stream_beginning_create( |
504db471 | 188 | writer_stream); |
d6e69534 | 189 | BT_ASSERT(new_message); |
c5b9b441 PP |
190 | bt_stream_put_ref(stream); |
191 | bt_stream_put_ref(writer_stream); | |
504db471 JD |
192 | break; |
193 | } | |
d6e69534 | 194 | case BT_MESSAGE_TYPE_STREAM_END: |
4f45f9bb | 195 | { |
b19ff26f | 196 | const bt_stream *stream = |
d6e69534 | 197 | bt_message_stream_end_get_stream(message); |
b19ff26f | 198 | const bt_stream *writer_stream; |
4f45f9bb JD |
199 | |
200 | if (!stream) { | |
201 | goto end; | |
202 | } | |
203 | ||
204 | writer_stream = debug_info_stream_end(debug_it, stream); | |
f6ccaed9 | 205 | BT_ASSERT(writer_stream); |
d6e69534 | 206 | new_message = bt_message_stream_end_create( |
4f45f9bb | 207 | writer_stream); |
d6e69534 | 208 | BT_ASSERT(new_message); |
c5b9b441 PP |
209 | bt_stream_put_ref(stream); |
210 | bt_stream_put_ref(writer_stream); | |
4f45f9bb JD |
211 | break; |
212 | } | |
617dd674 | 213 | default: |
d6e69534 | 214 | new_message = bt_message_get_ref(message); |
779fc95d JD |
215 | break; |
216 | } | |
4f45f9bb JD |
217 | |
218 | end: | |
d6e69534 | 219 | return new_message; |
4f45f9bb JD |
220 | } |
221 | ||
222 | static | |
d6e69534 PP |
223 | bt_message_iterator_next_method_return debug_info_iterator_next( |
224 | bt_self_message_iterator *iterator) | |
4f45f9bb JD |
225 | { |
226 | struct debug_info_iterator *debug_it = NULL; | |
b19ff26f | 227 | bt_self_component *component = NULL; |
4f45f9bb | 228 | struct debug_info_component *debug_info = NULL; |
d6e69534 PP |
229 | bt_message_iterator *source_it = NULL; |
230 | const bt_message *message; | |
231 | bt_message_iterator_next_method_return ret = { | |
232 | .status = BT_MESSAGE_ITERATOR_STATUS_OK, | |
233 | .message = NULL, | |
7b077a9d | 234 | }; |
4f45f9bb | 235 | |
d6e69534 | 236 | debug_it = bt_self_message_iterator_get_user_data(iterator); |
f6ccaed9 | 237 | BT_ASSERT(debug_it); |
4f45f9bb | 238 | |
d6e69534 | 239 | component = bt_self_message_iterator_get_private_component(iterator); |
f6ccaed9 | 240 | BT_ASSERT(component); |
d94d92ac | 241 | debug_info = bt_self_component_get_user_data(component); |
f6ccaed9 | 242 | BT_ASSERT(debug_info); |
4f45f9bb JD |
243 | |
244 | source_it = debug_it->input_iterator; | |
245 | ||
d6e69534 PP |
246 | ret.status = bt_message_iterator_next(source_it); |
247 | if (ret.status != BT_MESSAGE_ITERATOR_STATUS_OK) { | |
4f45f9bb JD |
248 | goto end; |
249 | } | |
250 | ||
d6e69534 | 251 | message = bt_message_iterator_get_message( |
4f45f9bb | 252 | source_it); |
d6e69534 PP |
253 | if (!message) { |
254 | ret.status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
4f45f9bb JD |
255 | goto end; |
256 | } | |
257 | ||
d6e69534 PP |
258 | ret.message = handle_message(debug_info->err, debug_it, |
259 | message); | |
260 | BT_ASSERT(ret.message); | |
261 | bt_message_put_ref(message); | |
4f45f9bb | 262 | |
4f45f9bb | 263 | end: |
65300d60 | 264 | bt_object_put_ref(component); |
4f45f9bb JD |
265 | return ret; |
266 | } | |
267 | ||
4f45f9bb | 268 | static |
d6e69534 PP |
269 | enum bt_message_iterator_status debug_info_iterator_init( |
270 | bt_self_message_iterator *iterator, | |
7b077a9d | 271 | struct bt_private_port *port) |
4f45f9bb | 272 | { |
d6e69534 PP |
273 | enum bt_message_iterator_status ret = |
274 | BT_MESSAGE_ITERATOR_STATUS_OK; | |
275 | enum bt_message_iterator_status it_ret; | |
73d5c1ad | 276 | enum bt_connection_status conn_status; |
7b077a9d | 277 | struct bt_private_connection *connection = NULL; |
b19ff26f | 278 | bt_self_component *component = |
d6e69534 | 279 | bt_self_message_iterator_get_private_component(iterator); |
4f45f9bb | 280 | struct debug_info_iterator *it_data = g_new0(struct debug_info_iterator, 1); |
779fc95d | 281 | struct bt_private_port *input_port; |
4f45f9bb JD |
282 | |
283 | if (!it_data) { | |
d6e69534 | 284 | ret = BT_MESSAGE_ITERATOR_STATUS_NOMEM; |
4f45f9bb JD |
285 | goto end; |
286 | } | |
287 | ||
d94d92ac | 288 | input_port = bt_self_component_filter_get_input_port_by_name( |
779fc95d JD |
289 | component, "in"); |
290 | if (!input_port) { | |
d6e69534 | 291 | ret = BT_MESSAGE_ITERATOR_STATUS_ERROR; |
779fc95d JD |
292 | goto end; |
293 | } | |
294 | ||
28e6ca8b | 295 | connection = bt_private_port_get_connection(input_port); |
65300d60 | 296 | bt_object_put_ref(input_port); |
779fc95d | 297 | if (!connection) { |
d6e69534 | 298 | ret = BT_MESSAGE_ITERATOR_STATUS_ERROR; |
779fc95d JD |
299 | goto end; |
300 | } | |
4f45f9bb | 301 | |
d6e69534 | 302 | conn_status = bt_private_connection_create_message_iterator( |
f42867e2 | 303 | connection, &it_data->input_iterator); |
73d5c1ad | 304 | if (conn_status != BT_CONNECTION_STATUS_OK) { |
d6e69534 | 305 | ret = BT_MESSAGE_ITERATOR_STATUS_ERROR; |
4f45f9bb JD |
306 | goto end; |
307 | } | |
779fc95d | 308 | |
4f45f9bb | 309 | it_data->debug_info_component = (struct debug_info_component *) |
d94d92ac | 310 | bt_self_component_get_user_data(component); |
4f45f9bb JD |
311 | it_data->err = it_data->debug_info_component->err; |
312 | it_data->trace_map = g_hash_table_new_full(g_direct_hash, | |
313 | g_direct_equal, NULL, (GDestroyNotify) unref_trace); | |
4f45f9bb | 314 | |
d6e69534 | 315 | it_ret = bt_self_message_iterator_set_user_data(iterator, it_data); |
4f45f9bb JD |
316 | if (it_ret) { |
317 | goto end; | |
318 | } | |
319 | ||
320 | end: | |
65300d60 PP |
321 | bt_object_put_ref(connection); |
322 | bt_object_put_ref(component); | |
4f45f9bb JD |
323 | return ret; |
324 | } | |
325 | ||
326 | static | |
327 | enum bt_component_status init_from_params( | |
328 | struct debug_info_component *debug_info_component, | |
b19ff26f | 329 | bt_value *params) |
4f45f9bb | 330 | { |
b19ff26f | 331 | bt_value *value = NULL; |
4f45f9bb JD |
332 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
333 | ||
f6ccaed9 | 334 | BT_ASSERT(params); |
4f45f9bb JD |
335 | |
336 | value = bt_value_map_get(params, "debug-info-field-name"); | |
337 | if (value) { | |
338 | enum bt_value_status value_ret; | |
339 | const char *tmp; | |
340 | ||
601b0d3c | 341 | tmp = bt_value_string_get(value); |
4f45f9bb | 342 | strcpy(debug_info_component->arg_debug_info_field_name, tmp); |
c5b9b441 | 343 | bt_value_put_ref(value); |
4f45f9bb JD |
344 | } else { |
345 | debug_info_component->arg_debug_info_field_name = | |
346 | malloc(strlen("debug_info") + 1); | |
347 | if (!debug_info_component->arg_debug_info_field_name) { | |
348 | ret = BT_COMPONENT_STATUS_NOMEM; | |
b4565e8b | 349 | BT_LOGE_STR("Missing field name."); |
6caae98c | 350 | goto end; |
4f45f9bb JD |
351 | } |
352 | sprintf(debug_info_component->arg_debug_info_field_name, | |
353 | "debug_info"); | |
354 | } | |
355 | if (ret != BT_COMPONENT_STATUS_OK) { | |
356 | goto end; | |
357 | } | |
358 | ||
4cd687b9 | 359 | value = bt_value_map_get(params, "debug-info-dir"); |
4f45f9bb JD |
360 | if (value) { |
361 | enum bt_value_status value_ret; | |
362 | ||
601b0d3c | 363 | debug_info_component->arg_debug_dir = bt_value_string_get(value); |
4f45f9bb | 364 | } |
c5b9b441 | 365 | bt_value_put_ref(value); |
4f45f9bb JD |
366 | if (ret != BT_COMPONENT_STATUS_OK) { |
367 | goto end; | |
368 | } | |
369 | ||
370 | value = bt_value_map_get(params, "target-prefix"); | |
371 | if (value) { | |
372 | enum bt_value_status value_ret; | |
373 | ||
601b0d3c | 374 | debug_info_component->arg_target_prefix = bt_value_string_get(value); |
4f45f9bb | 375 | } |
c5b9b441 | 376 | bt_value_put_ref(value); |
4f45f9bb JD |
377 | if (ret != BT_COMPONENT_STATUS_OK) { |
378 | goto end; | |
379 | } | |
380 | ||
381 | value = bt_value_map_get(params, "full-path"); | |
382 | if (value) { | |
383 | enum bt_value_status value_ret; | |
c55a9f58 | 384 | bt_bool bool_val; |
4f45f9bb | 385 | |
601b0d3c | 386 | bool_val = bt_value_bool_get(value); |
c55a9f58 PP |
387 | |
388 | debug_info_component->arg_full_path = bool_val; | |
4f45f9bb | 389 | } |
c5b9b441 | 390 | bt_value_put_ref(value); |
4f45f9bb JD |
391 | if (ret != BT_COMPONENT_STATUS_OK) { |
392 | goto end; | |
393 | } | |
394 | ||
395 | end: | |
396 | return ret; | |
397 | } | |
398 | ||
399 | enum bt_component_status debug_info_component_init( | |
b19ff26f | 400 | bt_self_component *component, bt_value *params, |
4f45f9bb JD |
401 | UNUSED_VAR void *init_method_data) |
402 | { | |
403 | enum bt_component_status ret; | |
404 | struct debug_info_component *debug_info = create_debug_info_component_data(); | |
405 | ||
406 | if (!debug_info) { | |
407 | ret = BT_COMPONENT_STATUS_NOMEM; | |
408 | goto end; | |
409 | } | |
410 | ||
d94d92ac | 411 | ret = bt_self_component_set_user_data(component, debug_info); |
4f45f9bb JD |
412 | if (ret != BT_COMPONENT_STATUS_OK) { |
413 | goto error; | |
414 | } | |
415 | ||
d94d92ac | 416 | ret = bt_self_component_filter_add_input_port( |
147337a3 PP |
417 | component, "in", NULL, NULL); |
418 | if (ret != BT_COMPONENT_STATUS_OK) { | |
779fc95d JD |
419 | goto end; |
420 | } | |
779fc95d | 421 | |
d94d92ac | 422 | ret = bt_self_component_filter_add_output_port( |
147337a3 PP |
423 | component, "out", NULL, NULL); |
424 | if (ret != BT_COMPONENT_STATUS_OK) { | |
779fc95d JD |
425 | goto end; |
426 | } | |
779fc95d | 427 | |
4f45f9bb JD |
428 | ret = init_from_params(debug_info, params); |
429 | end: | |
430 | return ret; | |
431 | error: | |
432 | destroy_debug_info_data(debug_info); | |
433 | return ret; | |
434 | } | |
435 | ||
52238017 MJ |
436 | #ifndef BT_BUILT_IN_PLUGINS |
437 | BT_PLUGIN_MODULE(); | |
438 | #endif | |
439 | ||
4f45f9bb | 440 | /* Initialize plug-in entry points. */ |
456a4476 PP |
441 | BT_PLUGIN_WITH_ID(lttng_utils, "lttng-utils"); |
442 | BT_PLUGIN_DESCRIPTION_WITH_ID(lttng_utils, "LTTng utilities"); | |
443 | BT_PLUGIN_AUTHOR_WITH_ID(lttng_utils, "Julien Desfossez"); | |
444 | BT_PLUGIN_LICENSE_WITH_ID(lttng_utils, "MIT"); | |
445 | ||
446 | BT_PLUGIN_FILTER_COMPONENT_CLASS_WITH_ID(lttng_utils, debug_info, "debug-info", | |
447 | debug_info_iterator_next); | |
448 | BT_PLUGIN_FILTER_COMPONENT_CLASS_DESCRIPTION_WITH_ID(lttng_utils, debug_info, | |
449 | "Augment compatible events with debugging information."); | |
450 | BT_PLUGIN_FILTER_COMPONENT_CLASS_INIT_METHOD_WITH_ID(lttng_utils, | |
451 | debug_info, debug_info_component_init); | |
452 | BT_PLUGIN_FILTER_COMPONENT_CLASS_FINALIZE_METHOD_WITH_ID(lttng_utils, | |
453 | debug_info, destroy_debug_info_component); | |
d6e69534 | 454 | BT_PLUGIN_FILTER_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_WITH_ID( |
456a4476 | 455 | lttng_utils, debug_info, debug_info_iterator_init); |
d6e69534 | 456 | BT_PLUGIN_FILTER_COMPONENT_CLASS_MESSAGE_ITERATOR_FINALIZE_METHOD_WITH_ID( |
456a4476 | 457 | lttng_utils, debug_info, debug_info_iterator_destroy); |