| 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-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_notification_iterator *it) |
| 99 | { |
| 100 | struct debug_info_iterator *it_data; |
| 101 | |
| 102 | it_data = bt_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 | case BT_NOTIFICATION_TYPE_INACTIVITY: |
| 226 | { |
| 227 | new_notification = bt_get(notification); |
| 228 | break; |
| 229 | } |
| 230 | default: |
| 231 | puts("Unhandled notification type"); |
| 232 | } |
| 233 | |
| 234 | end: |
| 235 | return new_notification; |
| 236 | } |
| 237 | |
| 238 | static |
| 239 | struct bt_notification_iterator_next_return debug_info_iterator_next( |
| 240 | struct bt_private_notification_iterator *iterator) |
| 241 | { |
| 242 | struct debug_info_iterator *debug_it = NULL; |
| 243 | struct bt_private_component *component = NULL; |
| 244 | struct debug_info_component *debug_info = NULL; |
| 245 | struct bt_notification_iterator *source_it = NULL; |
| 246 | struct bt_notification *notification; |
| 247 | struct bt_notification_iterator_next_return ret = { |
| 248 | .status = BT_NOTIFICATION_ITERATOR_STATUS_OK, |
| 249 | .notification = NULL, |
| 250 | }; |
| 251 | |
| 252 | debug_it = bt_private_notification_iterator_get_user_data(iterator); |
| 253 | assert(debug_it); |
| 254 | |
| 255 | component = bt_private_notification_iterator_get_private_component(iterator); |
| 256 | assert(component); |
| 257 | debug_info = bt_private_component_get_user_data(component); |
| 258 | assert(debug_info); |
| 259 | |
| 260 | source_it = debug_it->input_iterator; |
| 261 | |
| 262 | ret.status = bt_notification_iterator_next(source_it); |
| 263 | if (ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { |
| 264 | goto end; |
| 265 | } |
| 266 | |
| 267 | notification = bt_notification_iterator_get_notification( |
| 268 | source_it); |
| 269 | if (!notification) { |
| 270 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 271 | goto end; |
| 272 | } |
| 273 | |
| 274 | ret.notification = handle_notification(debug_info->err, debug_it, |
| 275 | notification); |
| 276 | assert(ret.notification); |
| 277 | bt_put(notification); |
| 278 | |
| 279 | end: |
| 280 | bt_put(component); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | static |
| 285 | enum bt_notification_iterator_status debug_info_iterator_init( |
| 286 | struct bt_private_notification_iterator *iterator, |
| 287 | struct bt_private_port *port) |
| 288 | { |
| 289 | enum bt_notification_iterator_status ret = |
| 290 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 291 | enum bt_notification_iterator_status it_ret; |
| 292 | enum bt_connection_status conn_status; |
| 293 | struct bt_private_connection *connection = NULL; |
| 294 | struct bt_private_component *component = |
| 295 | bt_private_notification_iterator_get_private_component(iterator); |
| 296 | struct debug_info_iterator *it_data = g_new0(struct debug_info_iterator, 1); |
| 297 | struct bt_private_port *input_port; |
| 298 | static const enum bt_notification_type notif_types[] = { |
| 299 | BT_NOTIFICATION_TYPE_EVENT, |
| 300 | BT_NOTIFICATION_TYPE_STREAM_BEGIN, |
| 301 | BT_NOTIFICATION_TYPE_STREAM_END, |
| 302 | BT_NOTIFICATION_TYPE_PACKET_BEGIN, |
| 303 | BT_NOTIFICATION_TYPE_PACKET_END, |
| 304 | BT_NOTIFICATION_TYPE_SENTINEL, |
| 305 | }; |
| 306 | |
| 307 | if (!it_data) { |
| 308 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
| 309 | goto end; |
| 310 | } |
| 311 | |
| 312 | input_port = bt_private_component_filter_get_input_private_port_by_name( |
| 313 | component, "in"); |
| 314 | if (!input_port) { |
| 315 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 316 | goto end; |
| 317 | } |
| 318 | |
| 319 | connection = bt_private_port_get_private_connection(input_port); |
| 320 | bt_put(input_port); |
| 321 | if (!connection) { |
| 322 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 323 | goto end; |
| 324 | } |
| 325 | |
| 326 | conn_status = bt_private_connection_create_notification_iterator( |
| 327 | connection, notif_types, &it_data->input_iterator); |
| 328 | if (conn_status != BT_CONNECTION_STATUS_OK) { |
| 329 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 330 | goto end; |
| 331 | } |
| 332 | |
| 333 | it_data->debug_info_component = (struct debug_info_component *) |
| 334 | bt_private_component_get_user_data(component); |
| 335 | it_data->err = it_data->debug_info_component->err; |
| 336 | it_data->trace_map = g_hash_table_new_full(g_direct_hash, |
| 337 | g_direct_equal, NULL, (GDestroyNotify) unref_trace); |
| 338 | |
| 339 | it_ret = bt_private_notification_iterator_set_user_data(iterator, it_data); |
| 340 | if (it_ret) { |
| 341 | goto end; |
| 342 | } |
| 343 | |
| 344 | end: |
| 345 | bt_put(connection); |
| 346 | bt_put(component); |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | static |
| 351 | enum bt_component_status init_from_params( |
| 352 | struct debug_info_component *debug_info_component, |
| 353 | struct bt_value *params) |
| 354 | { |
| 355 | struct bt_value *value = NULL; |
| 356 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 357 | |
| 358 | assert(params); |
| 359 | |
| 360 | value = bt_value_map_get(params, "debug-info-field-name"); |
| 361 | if (value) { |
| 362 | enum bt_value_status value_ret; |
| 363 | const char *tmp; |
| 364 | |
| 365 | value_ret = bt_value_string_get(value, &tmp); |
| 366 | if (value_ret) { |
| 367 | ret = BT_COMPONENT_STATUS_INVALID; |
| 368 | BT_LOGE_STR("Failed to retrieve debug-info-field-name value. " |
| 369 | "Expecting a string"); |
| 370 | } |
| 371 | strcpy(debug_info_component->arg_debug_info_field_name, tmp); |
| 372 | bt_put(value); |
| 373 | } else { |
| 374 | debug_info_component->arg_debug_info_field_name = |
| 375 | malloc(strlen("debug_info") + 1); |
| 376 | if (!debug_info_component->arg_debug_info_field_name) { |
| 377 | ret = BT_COMPONENT_STATUS_NOMEM; |
| 378 | BT_LOGE_STR("Missing field name."); |
| 379 | } |
| 380 | sprintf(debug_info_component->arg_debug_info_field_name, |
| 381 | "debug_info"); |
| 382 | } |
| 383 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 384 | goto end; |
| 385 | } |
| 386 | |
| 387 | value = bt_value_map_get(params, "debug-dir"); |
| 388 | if (value) { |
| 389 | enum bt_value_status value_ret; |
| 390 | |
| 391 | value_ret = bt_value_string_get(value, |
| 392 | &debug_info_component->arg_debug_dir); |
| 393 | if (value_ret) { |
| 394 | ret = BT_COMPONENT_STATUS_INVALID; |
| 395 | BT_LOGE_STR("Failed to retrieve debug-dir value. " |
| 396 | "Expecting a string"); |
| 397 | } |
| 398 | } |
| 399 | bt_put(value); |
| 400 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 401 | goto end; |
| 402 | } |
| 403 | |
| 404 | value = bt_value_map_get(params, "target-prefix"); |
| 405 | if (value) { |
| 406 | enum bt_value_status value_ret; |
| 407 | |
| 408 | value_ret = bt_value_string_get(value, |
| 409 | &debug_info_component->arg_target_prefix); |
| 410 | if (value_ret) { |
| 411 | ret = BT_COMPONENT_STATUS_INVALID; |
| 412 | BT_LOGE_STR("Failed to retrieve target-prefix 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, "full-path"); |
| 422 | if (value) { |
| 423 | enum bt_value_status value_ret; |
| 424 | bt_bool bool_val; |
| 425 | |
| 426 | value_ret = bt_value_bool_get(value, |
| 427 | &bool_val); |
| 428 | if (value_ret) { |
| 429 | ret = BT_COMPONENT_STATUS_INVALID; |
| 430 | BT_LOGE_STR("Failed to retrieve full-path value. " |
| 431 | "Expecting a boolean"); |
| 432 | } |
| 433 | |
| 434 | debug_info_component->arg_full_path = bool_val; |
| 435 | } |
| 436 | bt_put(value); |
| 437 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 438 | goto end; |
| 439 | } |
| 440 | |
| 441 | end: |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | enum bt_component_status debug_info_component_init( |
| 446 | struct bt_private_component *component, struct bt_value *params, |
| 447 | UNUSED_VAR void *init_method_data) |
| 448 | { |
| 449 | enum bt_component_status ret; |
| 450 | struct debug_info_component *debug_info = create_debug_info_component_data(); |
| 451 | |
| 452 | if (!debug_info) { |
| 453 | ret = BT_COMPONENT_STATUS_NOMEM; |
| 454 | goto end; |
| 455 | } |
| 456 | |
| 457 | ret = bt_private_component_set_user_data(component, debug_info); |
| 458 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 459 | goto error; |
| 460 | } |
| 461 | |
| 462 | ret = bt_private_component_filter_add_input_private_port( |
| 463 | component, "in", NULL, NULL); |
| 464 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 465 | goto end; |
| 466 | } |
| 467 | |
| 468 | ret = bt_private_component_filter_add_output_private_port( |
| 469 | component, "out", NULL, NULL); |
| 470 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 471 | goto end; |
| 472 | } |
| 473 | |
| 474 | ret = init_from_params(debug_info, params); |
| 475 | end: |
| 476 | return ret; |
| 477 | error: |
| 478 | destroy_debug_info_data(debug_info); |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | /* Initialize plug-in entry points. */ |
| 483 | BT_PLUGIN_WITH_ID(lttng_utils, "lttng-utils"); |
| 484 | BT_PLUGIN_DESCRIPTION_WITH_ID(lttng_utils, "LTTng utilities"); |
| 485 | BT_PLUGIN_AUTHOR_WITH_ID(lttng_utils, "Julien Desfossez"); |
| 486 | BT_PLUGIN_LICENSE_WITH_ID(lttng_utils, "MIT"); |
| 487 | |
| 488 | BT_PLUGIN_FILTER_COMPONENT_CLASS_WITH_ID(lttng_utils, debug_info, "debug-info", |
| 489 | debug_info_iterator_next); |
| 490 | BT_PLUGIN_FILTER_COMPONENT_CLASS_DESCRIPTION_WITH_ID(lttng_utils, debug_info, |
| 491 | "Augment compatible events with debugging information."); |
| 492 | BT_PLUGIN_FILTER_COMPONENT_CLASS_INIT_METHOD_WITH_ID(lttng_utils, |
| 493 | debug_info, debug_info_component_init); |
| 494 | BT_PLUGIN_FILTER_COMPONENT_CLASS_FINALIZE_METHOD_WITH_ID(lttng_utils, |
| 495 | debug_info, destroy_debug_info_component); |
| 496 | BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_INIT_METHOD_WITH_ID( |
| 497 | lttng_utils, debug_info, debug_info_iterator_init); |
| 498 | BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_FINALIZE_METHOD_WITH_ID( |
| 499 | lttng_utils, debug_info, debug_info_iterator_destroy); |