| 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-connection-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_connection_private_notification_iterator *it) |
| 99 | { |
| 100 | struct debug_info_iterator *it_data; |
| 101 | |
| 102 | it_data = bt_private_connection_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_method_return debug_info_iterator_next( |
| 236 | struct bt_private_connection_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_method_return ret = { |
| 244 | .status = BT_NOTIFICATION_ITERATOR_STATUS_OK, |
| 245 | .notification = NULL, |
| 246 | }; |
| 247 | |
| 248 | debug_it = bt_private_connection_private_notification_iterator_get_user_data(iterator); |
| 249 | assert(debug_it); |
| 250 | |
| 251 | component = bt_private_connection_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_connection_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_connection_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_connection_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 | goto end; |
| 368 | } |
| 369 | sprintf(debug_info_component->arg_debug_info_field_name, |
| 370 | "debug_info"); |
| 371 | } |
| 372 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 373 | goto end; |
| 374 | } |
| 375 | |
| 376 | value = bt_value_map_get(params, "debug-dir"); |
| 377 | if (value) { |
| 378 | enum bt_value_status value_ret; |
| 379 | |
| 380 | value_ret = bt_value_string_get(value, |
| 381 | &debug_info_component->arg_debug_dir); |
| 382 | if (value_ret) { |
| 383 | ret = BT_COMPONENT_STATUS_INVALID; |
| 384 | BT_LOGE_STR("Failed to retrieve debug-dir value. " |
| 385 | "Expecting a string."); |
| 386 | } |
| 387 | } |
| 388 | bt_put(value); |
| 389 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 390 | goto end; |
| 391 | } |
| 392 | |
| 393 | value = bt_value_map_get(params, "target-prefix"); |
| 394 | if (value) { |
| 395 | enum bt_value_status value_ret; |
| 396 | |
| 397 | value_ret = bt_value_string_get(value, |
| 398 | &debug_info_component->arg_target_prefix); |
| 399 | if (value_ret) { |
| 400 | ret = BT_COMPONENT_STATUS_INVALID; |
| 401 | BT_LOGE_STR("Failed to retrieve target-prefix value. " |
| 402 | "Expecting a string."); |
| 403 | } |
| 404 | } |
| 405 | bt_put(value); |
| 406 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 407 | goto end; |
| 408 | } |
| 409 | |
| 410 | value = bt_value_map_get(params, "full-path"); |
| 411 | if (value) { |
| 412 | enum bt_value_status value_ret; |
| 413 | bt_bool bool_val; |
| 414 | |
| 415 | value_ret = bt_value_bool_get(value, |
| 416 | &bool_val); |
| 417 | if (value_ret) { |
| 418 | ret = BT_COMPONENT_STATUS_INVALID; |
| 419 | BT_LOGE_STR("Failed to retrieve full-path value. " |
| 420 | "Expecting a boolean."); |
| 421 | } |
| 422 | |
| 423 | debug_info_component->arg_full_path = bool_val; |
| 424 | } |
| 425 | bt_put(value); |
| 426 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 427 | goto end; |
| 428 | } |
| 429 | |
| 430 | end: |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | enum bt_component_status debug_info_component_init( |
| 435 | struct bt_private_component *component, struct bt_value *params, |
| 436 | UNUSED_VAR void *init_method_data) |
| 437 | { |
| 438 | enum bt_component_status ret; |
| 439 | struct debug_info_component *debug_info = create_debug_info_component_data(); |
| 440 | |
| 441 | if (!debug_info) { |
| 442 | ret = BT_COMPONENT_STATUS_NOMEM; |
| 443 | goto end; |
| 444 | } |
| 445 | |
| 446 | ret = bt_private_component_set_user_data(component, debug_info); |
| 447 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 448 | goto error; |
| 449 | } |
| 450 | |
| 451 | ret = bt_private_component_filter_add_input_private_port( |
| 452 | component, "in", NULL, NULL); |
| 453 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 454 | goto end; |
| 455 | } |
| 456 | |
| 457 | ret = bt_private_component_filter_add_output_private_port( |
| 458 | component, "out", NULL, NULL); |
| 459 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 460 | goto end; |
| 461 | } |
| 462 | |
| 463 | ret = init_from_params(debug_info, params); |
| 464 | end: |
| 465 | return ret; |
| 466 | error: |
| 467 | destroy_debug_info_data(debug_info); |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | #ifndef BT_BUILT_IN_PLUGINS |
| 472 | BT_PLUGIN_MODULE(); |
| 473 | #endif |
| 474 | |
| 475 | /* Initialize plug-in entry points. */ |
| 476 | BT_PLUGIN_WITH_ID(lttng_utils, "lttng-utils"); |
| 477 | BT_PLUGIN_DESCRIPTION_WITH_ID(lttng_utils, "LTTng utilities"); |
| 478 | BT_PLUGIN_AUTHOR_WITH_ID(lttng_utils, "Julien Desfossez"); |
| 479 | BT_PLUGIN_LICENSE_WITH_ID(lttng_utils, "MIT"); |
| 480 | |
| 481 | BT_PLUGIN_FILTER_COMPONENT_CLASS_WITH_ID(lttng_utils, debug_info, "debug-info", |
| 482 | debug_info_iterator_next); |
| 483 | BT_PLUGIN_FILTER_COMPONENT_CLASS_DESCRIPTION_WITH_ID(lttng_utils, debug_info, |
| 484 | "Augment compatible events with debugging information."); |
| 485 | BT_PLUGIN_FILTER_COMPONENT_CLASS_INIT_METHOD_WITH_ID(lttng_utils, |
| 486 | debug_info, debug_info_component_init); |
| 487 | BT_PLUGIN_FILTER_COMPONENT_CLASS_FINALIZE_METHOD_WITH_ID(lttng_utils, |
| 488 | debug_info, destroy_debug_info_component); |
| 489 | BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_INIT_METHOD_WITH_ID( |
| 490 | lttng_utils, debug_info, debug_info_iterator_init); |
| 491 | BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_FINALIZE_METHOD_WITH_ID( |
| 492 | lttng_utils, debug_info, debug_info_iterator_destroy); |