| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation |
| 5 | * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com> |
| 6 | * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 7 | * |
| 8 | * Babeltrace - Mapping of IR metadata and data object between input and output |
| 9 | * trace |
| 10 | */ |
| 11 | |
| 12 | #define BT_COMP_LOG_SELF_COMP (ir_maps->self_comp) |
| 13 | #define BT_LOG_OUTPUT_LEVEL (ir_maps->log_level) |
| 14 | #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-MAPPING" |
| 15 | #include "logging/comp-logging.h" |
| 16 | |
| 17 | #include "common/assert.h" |
| 18 | #include <babeltrace2/babeltrace.h> |
| 19 | |
| 20 | #include "debug-info.h" |
| 21 | #include "trace-ir-data-copy.h" |
| 22 | #include "trace-ir-mapping.h" |
| 23 | #include "trace-ir-metadata-copy.h" |
| 24 | |
| 25 | static |
| 26 | bt_trace_class *create_new_mapped_trace_class(struct trace_ir_maps *ir_maps, |
| 27 | const bt_trace_class *in_trace_class) |
| 28 | { |
| 29 | bt_self_component *self_comp = ir_maps->self_comp; |
| 30 | enum debug_info_trace_ir_mapping_status status; |
| 31 | struct trace_ir_metadata_maps *metadata_maps; |
| 32 | |
| 33 | BT_COMP_LOGD("Creating new mapped trace class: in-tc-addr=%p", in_trace_class); |
| 34 | |
| 35 | BT_ASSERT(ir_maps); |
| 36 | BT_ASSERT(in_trace_class); |
| 37 | |
| 38 | metadata_maps = borrow_metadata_maps_from_input_trace_class(ir_maps, |
| 39 | in_trace_class); |
| 40 | |
| 41 | BT_ASSERT(!metadata_maps->output_trace_class); |
| 42 | |
| 43 | /* Create the ouput trace class. */ |
| 44 | metadata_maps->output_trace_class = |
| 45 | bt_trace_class_create(ir_maps->self_comp); |
| 46 | if (!metadata_maps->output_trace_class) { |
| 47 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 48 | "Error create output trace class"); |
| 49 | goto error; |
| 50 | } |
| 51 | |
| 52 | /* Copy the content over and add to the mapping. */ |
| 53 | status = copy_trace_class_content(ir_maps, in_trace_class, |
| 54 | metadata_maps->output_trace_class, ir_maps->log_level, |
| 55 | ir_maps->self_comp); |
| 56 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 57 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 58 | "Error copy content to output trace class" |
| 59 | "in-tc-addr=%p, out-tc-addr=%p", in_trace_class, |
| 60 | metadata_maps->output_trace_class); |
| 61 | goto error; |
| 62 | } |
| 63 | |
| 64 | BT_COMP_LOGD("Created new mapped trace class: " |
| 65 | "in-tc-addr=%p, out-tc-addr=%p", |
| 66 | in_trace_class, metadata_maps->output_trace_class); |
| 67 | |
| 68 | goto end; |
| 69 | error: |
| 70 | BT_TRACE_CLASS_PUT_REF_AND_RESET(metadata_maps->output_trace_class); |
| 71 | end: |
| 72 | return metadata_maps->output_trace_class; |
| 73 | } |
| 74 | |
| 75 | static |
| 76 | bt_trace *create_new_mapped_trace(struct trace_ir_maps *ir_maps, |
| 77 | const bt_trace *in_trace) |
| 78 | { |
| 79 | bt_self_component *self_comp = ir_maps->self_comp; |
| 80 | enum debug_info_trace_ir_mapping_status status; |
| 81 | struct trace_ir_metadata_maps *metadata_maps; |
| 82 | const bt_trace_class *in_trace_class; |
| 83 | bt_trace *out_trace; |
| 84 | |
| 85 | BT_COMP_LOGD("Creating new mapped trace: in-t-addr=%p", in_trace); |
| 86 | BT_ASSERT(ir_maps); |
| 87 | BT_ASSERT(in_trace); |
| 88 | |
| 89 | in_trace_class = bt_trace_borrow_class_const(in_trace); |
| 90 | metadata_maps = borrow_metadata_maps_from_input_trace_class(ir_maps, |
| 91 | in_trace_class); |
| 92 | |
| 93 | if (!metadata_maps->output_trace_class) { |
| 94 | /* |
| 95 | * If there is no output trace class yet, create a one and add |
| 96 | * it to the mapping. |
| 97 | */ |
| 98 | metadata_maps->output_trace_class = |
| 99 | create_new_mapped_trace_class(ir_maps, in_trace_class); |
| 100 | if (!metadata_maps->output_trace_class) { |
| 101 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 102 | "Error create output trace class"); |
| 103 | out_trace = NULL; |
| 104 | goto end; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* Create the output trace from the output trace class. */ |
| 109 | out_trace = bt_trace_create(metadata_maps->output_trace_class); |
| 110 | if (!out_trace) { |
| 111 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 112 | "Error create output trace"); |
| 113 | goto end; |
| 114 | } |
| 115 | |
| 116 | /* Copy the content over to the output trace. */ |
| 117 | status = copy_trace_content(in_trace, out_trace, ir_maps->log_level, |
| 118 | ir_maps->self_comp); |
| 119 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 120 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 121 | "Error copy content to output trace" |
| 122 | "in-t-addr=%p, out-t-addr=%p", in_trace, out_trace); |
| 123 | goto error; |
| 124 | } |
| 125 | |
| 126 | BT_COMP_LOGD("Created new mapped trace: in-t-addr=%p, out-t-addr=%p", |
| 127 | in_trace, out_trace); |
| 128 | goto end; |
| 129 | |
| 130 | error: |
| 131 | BT_TRACE_PUT_REF_AND_RESET(out_trace); |
| 132 | end: |
| 133 | return out_trace; |
| 134 | } |
| 135 | |
| 136 | BT_HIDDEN |
| 137 | bt_stream_class *trace_ir_mapping_borrow_mapped_stream_class( |
| 138 | struct trace_ir_maps *ir_maps, |
| 139 | const bt_stream_class *in_stream_class) |
| 140 | { |
| 141 | BT_ASSERT_DBG(ir_maps); |
| 142 | BT_ASSERT_DBG(in_stream_class); |
| 143 | |
| 144 | struct trace_ir_metadata_maps *md_maps = |
| 145 | borrow_metadata_maps_from_input_stream_class(ir_maps, |
| 146 | in_stream_class); |
| 147 | return g_hash_table_lookup(md_maps->stream_class_map, |
| 148 | (gpointer) in_stream_class); |
| 149 | } |
| 150 | |
| 151 | BT_HIDDEN |
| 152 | bt_stream_class *trace_ir_mapping_create_new_mapped_stream_class( |
| 153 | struct trace_ir_maps *ir_maps, |
| 154 | const bt_stream_class *in_stream_class) |
| 155 | { |
| 156 | bt_self_component *self_comp = ir_maps->self_comp; |
| 157 | enum debug_info_trace_ir_mapping_status status; |
| 158 | struct trace_ir_metadata_maps *md_maps; |
| 159 | bt_stream_class *out_stream_class; |
| 160 | |
| 161 | BT_COMP_LOGD("Creating new mapped stream class: in-sc-addr=%p", |
| 162 | in_stream_class); |
| 163 | |
| 164 | BT_ASSERT(ir_maps); |
| 165 | BT_ASSERT(in_stream_class); |
| 166 | BT_ASSERT(!trace_ir_mapping_borrow_mapped_stream_class(ir_maps, |
| 167 | in_stream_class)); |
| 168 | |
| 169 | md_maps = borrow_metadata_maps_from_input_stream_class(ir_maps, |
| 170 | in_stream_class); |
| 171 | |
| 172 | BT_ASSERT(md_maps); |
| 173 | |
| 174 | /* Create the output stream class. */ |
| 175 | out_stream_class = bt_stream_class_create_with_id( |
| 176 | md_maps->output_trace_class, |
| 177 | bt_stream_class_get_id(in_stream_class)); |
| 178 | if (!out_stream_class) { |
| 179 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 180 | "Error create output stream class"); |
| 181 | goto end; |
| 182 | } |
| 183 | |
| 184 | /* Add it to the mapping. The mapping now owns out_stream_class. */ |
| 185 | g_hash_table_insert(md_maps->stream_class_map, |
| 186 | (gpointer) in_stream_class, out_stream_class); |
| 187 | |
| 188 | /* Copy the content over to the output stream class. */ |
| 189 | status = copy_stream_class_content(ir_maps, in_stream_class, |
| 190 | out_stream_class); |
| 191 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 192 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 193 | "Error copy content to output stream class: " |
| 194 | "in-sc-addr=%p, out-sc-addr=%p", in_stream_class, |
| 195 | out_stream_class); |
| 196 | goto error; |
| 197 | } |
| 198 | |
| 199 | BT_COMP_LOGD("Created new mapped stream class: " |
| 200 | "in-sc-addr=%p, out-sc-addr=%p", |
| 201 | in_stream_class, out_stream_class); |
| 202 | |
| 203 | goto end; |
| 204 | error: |
| 205 | out_stream_class = NULL; |
| 206 | end: |
| 207 | return out_stream_class; |
| 208 | } |
| 209 | |
| 210 | static |
| 211 | bt_stream *borrow_mapped_stream(struct trace_ir_data_maps *d_maps, |
| 212 | const bt_stream *in_stream) |
| 213 | { |
| 214 | BT_ASSERT_DBG(d_maps); |
| 215 | BT_ASSERT_DBG(in_stream); |
| 216 | |
| 217 | return g_hash_table_lookup(d_maps->stream_map, (gpointer) in_stream); |
| 218 | } |
| 219 | |
| 220 | BT_HIDDEN |
| 221 | bt_stream *trace_ir_mapping_create_new_mapped_stream( |
| 222 | struct trace_ir_maps *ir_maps, const bt_stream *in_stream) |
| 223 | { |
| 224 | bt_self_component *self_comp = ir_maps->self_comp; |
| 225 | enum debug_info_trace_ir_mapping_status status; |
| 226 | struct trace_ir_data_maps *d_maps; |
| 227 | const bt_stream_class *in_stream_class; |
| 228 | const bt_trace *in_trace; |
| 229 | bt_stream_class *out_stream_class; |
| 230 | bt_stream *out_stream = NULL; |
| 231 | |
| 232 | BT_ASSERT(ir_maps); |
| 233 | BT_ASSERT(in_stream); |
| 234 | BT_COMP_LOGD("Creating new mapped stream: in-s-addr=%p", in_stream); |
| 235 | |
| 236 | in_trace = bt_stream_borrow_trace_const(in_stream); |
| 237 | |
| 238 | d_maps = borrow_data_maps_from_input_trace(ir_maps, in_trace); |
| 239 | if (!d_maps->output_trace) { |
| 240 | /* Create the output trace for this input trace. */ |
| 241 | d_maps->output_trace = create_new_mapped_trace(ir_maps, in_trace); |
| 242 | if (!d_maps->output_trace) { |
| 243 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 244 | "Error creating mapped trace"); |
| 245 | goto error; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | BT_ASSERT(d_maps->output_trace); |
| 250 | BT_ASSERT(!borrow_mapped_stream(d_maps, in_stream)); |
| 251 | |
| 252 | in_stream_class = bt_stream_borrow_class_const(in_stream); |
| 253 | out_stream_class = trace_ir_mapping_borrow_mapped_stream_class(ir_maps, |
| 254 | in_stream_class); |
| 255 | |
| 256 | if (!out_stream_class) { |
| 257 | /* Create the output stream class for this input stream class. */ |
| 258 | out_stream_class = trace_ir_mapping_create_new_mapped_stream_class( |
| 259 | ir_maps, in_stream_class); |
| 260 | if (!out_stream_class) { |
| 261 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 262 | "Error creating mapped stream class"); |
| 263 | goto error; |
| 264 | } |
| 265 | } |
| 266 | BT_ASSERT(out_stream_class); |
| 267 | |
| 268 | /* Create the output stream for this input stream. */ |
| 269 | out_stream = bt_stream_create_with_id(out_stream_class, |
| 270 | d_maps->output_trace, bt_stream_get_id(in_stream)); |
| 271 | if (!out_stream) { |
| 272 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 273 | "Error creating output stream"); |
| 274 | goto error; |
| 275 | } |
| 276 | |
| 277 | /* Add it to the mapping. The mapping now owns out_stream.*/ |
| 278 | g_hash_table_insert(d_maps->stream_map, (gpointer) in_stream, |
| 279 | out_stream); |
| 280 | |
| 281 | /* Copy the content over to the output stream. */ |
| 282 | status = copy_stream_content(in_stream, out_stream, ir_maps->log_level, |
| 283 | ir_maps->self_comp); |
| 284 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 285 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 286 | "Error copy content to output stream: " |
| 287 | "in-s-addr=%p, out-s-addr=%p", in_stream, out_stream); |
| 288 | goto error; |
| 289 | } |
| 290 | |
| 291 | BT_COMP_LOGD("Created new mapped stream: in-s-addr=%p, out-s-addr=%p", |
| 292 | in_stream, out_stream); |
| 293 | |
| 294 | goto end; |
| 295 | error: |
| 296 | out_stream = NULL; |
| 297 | end: |
| 298 | return out_stream; |
| 299 | } |
| 300 | |
| 301 | BT_HIDDEN |
| 302 | bt_stream *trace_ir_mapping_borrow_mapped_stream(struct trace_ir_maps *ir_maps, |
| 303 | const bt_stream *in_stream) |
| 304 | { |
| 305 | struct trace_ir_data_maps *d_maps; |
| 306 | |
| 307 | BT_ASSERT_DBG(ir_maps); |
| 308 | BT_ASSERT_DBG(in_stream); |
| 309 | |
| 310 | d_maps = borrow_data_maps_from_input_stream(ir_maps, in_stream); |
| 311 | /* Return the mapped stream. */ |
| 312 | return borrow_mapped_stream(d_maps, in_stream); |
| 313 | } |
| 314 | |
| 315 | static inline |
| 316 | bt_event_class *borrow_mapped_event_class(struct trace_ir_metadata_maps *md_maps, |
| 317 | const bt_event_class *in_event_class) |
| 318 | { |
| 319 | return g_hash_table_lookup(md_maps->event_class_map, |
| 320 | (gpointer) in_event_class); |
| 321 | } |
| 322 | |
| 323 | BT_HIDDEN |
| 324 | bt_event_class *trace_ir_mapping_create_new_mapped_event_class( |
| 325 | struct trace_ir_maps *ir_maps, |
| 326 | const bt_event_class *in_event_class) |
| 327 | { |
| 328 | bt_self_component *self_comp = ir_maps->self_comp; |
| 329 | enum debug_info_trace_ir_mapping_status status; |
| 330 | struct trace_ir_metadata_maps *md_maps; |
| 331 | const bt_stream_class *in_stream_class; |
| 332 | bt_stream_class *out_stream_class; |
| 333 | bt_event_class *out_event_class; |
| 334 | |
| 335 | BT_COMP_LOGD("Creating new mapped event class: in-ec-addr=%p", |
| 336 | in_event_class); |
| 337 | |
| 338 | BT_ASSERT(ir_maps); |
| 339 | BT_ASSERT(in_event_class); |
| 340 | |
| 341 | in_stream_class = bt_event_class_borrow_stream_class_const(in_event_class); |
| 342 | |
| 343 | BT_ASSERT(in_stream_class); |
| 344 | |
| 345 | md_maps = borrow_metadata_maps_from_input_stream_class(ir_maps, |
| 346 | in_stream_class); |
| 347 | |
| 348 | BT_ASSERT(md_maps); |
| 349 | BT_ASSERT(!borrow_mapped_event_class(md_maps, in_event_class)); |
| 350 | |
| 351 | /* Get the right output stream class to add the new event class to it. */ |
| 352 | out_stream_class = trace_ir_mapping_borrow_mapped_stream_class( |
| 353 | ir_maps, in_stream_class); |
| 354 | BT_ASSERT(out_stream_class); |
| 355 | |
| 356 | /* Create an output event class. */ |
| 357 | out_event_class = bt_event_class_create_with_id(out_stream_class, |
| 358 | bt_event_class_get_id(in_event_class)); |
| 359 | if (!out_event_class) { |
| 360 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 361 | "Error creating output event class"); |
| 362 | goto end; |
| 363 | } |
| 364 | |
| 365 | /* Add it to the mapping. The mapping now owns out_event_class. */ |
| 366 | g_hash_table_insert(md_maps->event_class_map, (gpointer) in_event_class, |
| 367 | out_event_class); |
| 368 | |
| 369 | /* Copy the content over to the output event class. */ |
| 370 | status = copy_event_class_content(ir_maps, in_event_class, |
| 371 | out_event_class); |
| 372 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 373 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 374 | "Error copy content to output event class: " |
| 375 | "in-ec-addr=%p, out-ec-addr=%p", in_event_class, |
| 376 | out_event_class); |
| 377 | goto error; |
| 378 | } |
| 379 | |
| 380 | BT_COMP_LOGD("Created new mapped event class: in-ec-addr=%p, out-ec-addr=%p", |
| 381 | in_event_class, out_event_class); |
| 382 | |
| 383 | goto end; |
| 384 | error: |
| 385 | out_event_class = NULL; |
| 386 | end: |
| 387 | return out_event_class; |
| 388 | } |
| 389 | |
| 390 | BT_HIDDEN |
| 391 | bt_event_class *trace_ir_mapping_borrow_mapped_event_class( |
| 392 | struct trace_ir_maps *ir_maps, |
| 393 | const bt_event_class *in_event_class) |
| 394 | { |
| 395 | struct trace_ir_metadata_maps *md_maps; |
| 396 | |
| 397 | BT_ASSERT_DBG(ir_maps); |
| 398 | BT_ASSERT_DBG(in_event_class); |
| 399 | |
| 400 | md_maps = borrow_metadata_maps_from_input_event_class(ir_maps, |
| 401 | in_event_class); |
| 402 | |
| 403 | /* Return the mapped event_class. */ |
| 404 | return borrow_mapped_event_class(md_maps, in_event_class); |
| 405 | } |
| 406 | |
| 407 | static inline |
| 408 | bt_packet *borrow_mapped_packet(struct trace_ir_data_maps *d_maps, |
| 409 | const bt_packet *in_packet) |
| 410 | { |
| 411 | BT_ASSERT_DBG(d_maps); |
| 412 | BT_ASSERT_DBG(in_packet); |
| 413 | |
| 414 | return g_hash_table_lookup(d_maps->packet_map, (gpointer) in_packet); |
| 415 | } |
| 416 | |
| 417 | BT_HIDDEN |
| 418 | bt_packet *trace_ir_mapping_create_new_mapped_packet( |
| 419 | struct trace_ir_maps *ir_maps, |
| 420 | const bt_packet *in_packet) |
| 421 | { |
| 422 | bt_self_component *self_comp = ir_maps->self_comp; |
| 423 | enum debug_info_trace_ir_mapping_status status; |
| 424 | struct trace_ir_data_maps *d_maps; |
| 425 | const bt_stream *in_stream; |
| 426 | const bt_trace *in_trace; |
| 427 | bt_packet *out_packet; |
| 428 | bt_stream *out_stream; |
| 429 | |
| 430 | BT_COMP_LOGD("Creating new mapped packet: in-p-addr=%p", in_packet); |
| 431 | |
| 432 | in_stream = bt_packet_borrow_stream_const(in_packet); |
| 433 | in_trace = bt_stream_borrow_trace_const(in_stream); |
| 434 | d_maps = borrow_data_maps_from_input_trace(ir_maps, in_trace); |
| 435 | |
| 436 | /* There should never be a mapped packet already. */ |
| 437 | BT_ASSERT(!borrow_mapped_packet(d_maps, in_packet)); |
| 438 | BT_ASSERT(in_stream); |
| 439 | |
| 440 | /* Get output stream corresponding to this input stream. */ |
| 441 | out_stream = borrow_mapped_stream(d_maps, in_stream); |
| 442 | BT_ASSERT(out_stream); |
| 443 | |
| 444 | /* Create the output packet. */ |
| 445 | out_packet = bt_packet_create(out_stream); |
| 446 | if (!out_packet) { |
| 447 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 448 | "Error create output packet"); |
| 449 | goto error; |
| 450 | } |
| 451 | |
| 452 | /* Add it to the mapping. The mapping now owns out_packet. */ |
| 453 | g_hash_table_insert(d_maps->packet_map, (gpointer) in_packet, |
| 454 | out_packet); |
| 455 | |
| 456 | /* Copy the content over to the output packet. */ |
| 457 | status = copy_packet_content(in_packet, out_packet, ir_maps->log_level, |
| 458 | ir_maps->self_comp); |
| 459 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 460 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 461 | "Error copy content to output packet: " |
| 462 | "in-p-addr=%p, out-p-addr=%p", in_packet, out_packet); |
| 463 | goto error; |
| 464 | } |
| 465 | |
| 466 | BT_COMP_LOGD("Created new mapped packet: in-p-addr=%p, out-p-addr=%p", |
| 467 | in_packet, out_packet); |
| 468 | |
| 469 | goto end; |
| 470 | error: |
| 471 | out_packet = NULL; |
| 472 | end: |
| 473 | return out_packet; |
| 474 | } |
| 475 | |
| 476 | BT_HIDDEN |
| 477 | bt_packet *trace_ir_mapping_borrow_mapped_packet(struct trace_ir_maps *ir_maps, |
| 478 | const bt_packet *in_packet) |
| 479 | { |
| 480 | struct trace_ir_data_maps *d_maps; |
| 481 | BT_ASSERT_DBG(ir_maps); |
| 482 | BT_ASSERT_DBG(in_packet); |
| 483 | |
| 484 | d_maps = borrow_data_maps_from_input_packet(ir_maps, in_packet); |
| 485 | |
| 486 | return borrow_mapped_packet(d_maps, in_packet); |
| 487 | } |
| 488 | |
| 489 | BT_HIDDEN |
| 490 | void trace_ir_mapping_remove_mapped_packet(struct trace_ir_maps *ir_maps, |
| 491 | const bt_packet *in_packet) |
| 492 | { |
| 493 | struct trace_ir_data_maps *d_maps; |
| 494 | gboolean ret; |
| 495 | |
| 496 | BT_ASSERT(ir_maps); |
| 497 | BT_ASSERT(in_packet); |
| 498 | |
| 499 | d_maps = borrow_data_maps_from_input_packet(ir_maps, in_packet); |
| 500 | |
| 501 | ret = g_hash_table_remove(d_maps->packet_map, in_packet); |
| 502 | |
| 503 | BT_ASSERT(ret); |
| 504 | } |
| 505 | |
| 506 | BT_HIDDEN |
| 507 | void trace_ir_mapping_remove_mapped_stream(struct trace_ir_maps *ir_maps, |
| 508 | const bt_stream *in_stream) |
| 509 | { |
| 510 | struct trace_ir_data_maps *d_maps; |
| 511 | gboolean ret; |
| 512 | |
| 513 | BT_ASSERT(ir_maps); |
| 514 | BT_ASSERT(in_stream); |
| 515 | |
| 516 | d_maps = borrow_data_maps_from_input_stream(ir_maps, in_stream); |
| 517 | |
| 518 | ret = g_hash_table_remove(d_maps->stream_map, in_stream); |
| 519 | |
| 520 | BT_ASSERT(ret); |
| 521 | } |
| 522 | |
| 523 | static |
| 524 | void trace_ir_metadata_maps_remove_func(const bt_trace_class *in_trace_class, |
| 525 | void *data) |
| 526 | { |
| 527 | struct trace_ir_maps *maps = (struct trace_ir_maps *) data; |
| 528 | if (maps->metadata_maps) { |
| 529 | gboolean ret; |
| 530 | ret = g_hash_table_remove(maps->metadata_maps, |
| 531 | (gpointer) in_trace_class); |
| 532 | BT_ASSERT(ret); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | static |
| 537 | void trace_ir_data_maps_remove_func(const bt_trace *in_trace, void *data) |
| 538 | { |
| 539 | struct trace_ir_maps *maps = (struct trace_ir_maps *) data; |
| 540 | if (maps->data_maps) { |
| 541 | gboolean ret; |
| 542 | ret = g_hash_table_remove(maps->data_maps, (gpointer) in_trace); |
| 543 | BT_ASSERT(ret); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | struct trace_ir_data_maps *trace_ir_data_maps_create(struct trace_ir_maps *ir_maps, |
| 548 | const bt_trace *in_trace) |
| 549 | { |
| 550 | bt_self_component *self_comp = ir_maps->self_comp; |
| 551 | bt_trace_add_listener_status add_listener_status; |
| 552 | struct trace_ir_data_maps *d_maps = g_new0(struct trace_ir_data_maps, 1); |
| 553 | |
| 554 | if (!d_maps) { |
| 555 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 556 | "Error allocating trace_ir_maps"); |
| 557 | goto error; |
| 558 | } |
| 559 | |
| 560 | d_maps->log_level = ir_maps->log_level; |
| 561 | d_maps->self_comp = ir_maps->self_comp; |
| 562 | d_maps->input_trace = in_trace; |
| 563 | |
| 564 | /* Create the hashtables used to map data objects. */ |
| 565 | d_maps->stream_map = g_hash_table_new_full(g_direct_hash, |
| 566 | g_direct_equal, NULL,(GDestroyNotify) bt_stream_put_ref); |
| 567 | d_maps->packet_map = g_hash_table_new_full(g_direct_hash, |
| 568 | g_direct_equal, NULL,(GDestroyNotify) bt_packet_put_ref); |
| 569 | |
| 570 | add_listener_status = bt_trace_add_destruction_listener( |
| 571 | in_trace, trace_ir_data_maps_remove_func, |
| 572 | ir_maps, &d_maps->destruction_listener_id); |
| 573 | BT_ASSERT(add_listener_status == BT_TRACE_ADD_LISTENER_STATUS_OK); |
| 574 | |
| 575 | error: |
| 576 | return d_maps; |
| 577 | } |
| 578 | |
| 579 | struct trace_ir_metadata_maps *trace_ir_metadata_maps_create( |
| 580 | struct trace_ir_maps *ir_maps, |
| 581 | const bt_trace_class *in_trace_class) |
| 582 | { |
| 583 | bt_self_component *self_comp = ir_maps->self_comp; |
| 584 | bt_trace_class_add_listener_status add_listener_status; |
| 585 | struct trace_ir_metadata_maps *md_maps = |
| 586 | g_new0(struct trace_ir_metadata_maps, 1); |
| 587 | |
| 588 | if (!md_maps) { |
| 589 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 590 | "Error allocating trace_ir_maps"); |
| 591 | goto error; |
| 592 | } |
| 593 | |
| 594 | md_maps->log_level = ir_maps->log_level; |
| 595 | md_maps->self_comp = ir_maps->self_comp; |
| 596 | md_maps->input_trace_class = in_trace_class; |
| 597 | /* |
| 598 | * Create the field class resolving context. This is needed to keep |
| 599 | * track of the field class already copied in order to do the field |
| 600 | * path resolution correctly. |
| 601 | */ |
| 602 | md_maps->fc_resolving_ctx = |
| 603 | g_new0(struct field_class_resolving_context, 1); |
| 604 | if (!md_maps->fc_resolving_ctx) { |
| 605 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 606 | "Error allocating field_class_resolving_context"); |
| 607 | goto error; |
| 608 | } |
| 609 | |
| 610 | /* Create the hashtables used to map metadata objects. */ |
| 611 | md_maps->stream_class_map = g_hash_table_new_full(g_direct_hash, |
| 612 | g_direct_equal, NULL, (GDestroyNotify) bt_stream_class_put_ref); |
| 613 | md_maps->event_class_map = g_hash_table_new_full(g_direct_hash, |
| 614 | g_direct_equal, NULL, (GDestroyNotify) bt_event_class_put_ref); |
| 615 | md_maps->field_class_map = g_hash_table_new_full(g_direct_hash, |
| 616 | g_direct_equal, NULL, (GDestroyNotify) bt_field_class_put_ref); |
| 617 | md_maps->clock_class_map = g_hash_table_new_full(g_direct_hash, |
| 618 | g_direct_equal, NULL, (GDestroyNotify) bt_clock_class_put_ref); |
| 619 | |
| 620 | add_listener_status = bt_trace_class_add_destruction_listener( |
| 621 | in_trace_class, trace_ir_metadata_maps_remove_func, |
| 622 | ir_maps, &md_maps->destruction_listener_id); |
| 623 | BT_ASSERT(add_listener_status == BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK); |
| 624 | |
| 625 | error: |
| 626 | return md_maps; |
| 627 | } |
| 628 | |
| 629 | BT_HIDDEN |
| 630 | void trace_ir_data_maps_destroy(struct trace_ir_data_maps *maps) |
| 631 | { |
| 632 | bt_trace_remove_listener_status status; |
| 633 | |
| 634 | if (!maps) { |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | if (maps->packet_map) { |
| 639 | g_hash_table_destroy(maps->packet_map); |
| 640 | } |
| 641 | |
| 642 | if (maps->stream_map) { |
| 643 | g_hash_table_destroy(maps->stream_map); |
| 644 | } |
| 645 | |
| 646 | if (maps->output_trace) { |
| 647 | bt_trace_put_ref(maps->output_trace); |
| 648 | } |
| 649 | |
| 650 | status = bt_trace_remove_destruction_listener(maps->input_trace, |
| 651 | maps->destruction_listener_id); |
| 652 | if (status != BT_TRACE_REMOVE_LISTENER_STATUS_OK) { |
| 653 | BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, maps->log_level, |
| 654 | maps->self_comp, |
| 655 | "Trace destruction listener removal failed."); |
| 656 | bt_current_thread_clear_error(); |
| 657 | } |
| 658 | |
| 659 | g_free(maps); |
| 660 | } |
| 661 | |
| 662 | BT_HIDDEN |
| 663 | void trace_ir_metadata_maps_destroy(struct trace_ir_metadata_maps *maps) |
| 664 | { |
| 665 | bt_trace_class_remove_listener_status status; |
| 666 | |
| 667 | if (!maps) { |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | if (maps->stream_class_map) { |
| 672 | g_hash_table_destroy(maps->stream_class_map); |
| 673 | } |
| 674 | |
| 675 | if (maps->event_class_map) { |
| 676 | g_hash_table_destroy(maps->event_class_map); |
| 677 | } |
| 678 | |
| 679 | if (maps->field_class_map) { |
| 680 | g_hash_table_destroy(maps->field_class_map); |
| 681 | } |
| 682 | |
| 683 | if (maps->clock_class_map) { |
| 684 | g_hash_table_destroy(maps->clock_class_map); |
| 685 | } |
| 686 | |
| 687 | g_free(maps->fc_resolving_ctx); |
| 688 | |
| 689 | if (maps->output_trace_class) { |
| 690 | bt_trace_class_put_ref(maps->output_trace_class); |
| 691 | } |
| 692 | |
| 693 | status = bt_trace_class_remove_destruction_listener( |
| 694 | maps->input_trace_class, maps->destruction_listener_id); |
| 695 | if (status != BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK) { |
| 696 | BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, maps->log_level, |
| 697 | maps->self_comp, |
| 698 | "Trace destruction listener removal failed."); |
| 699 | bt_current_thread_clear_error(); |
| 700 | } |
| 701 | |
| 702 | g_free(maps); |
| 703 | } |
| 704 | |
| 705 | void trace_ir_maps_clear(struct trace_ir_maps *maps) |
| 706 | { |
| 707 | if (maps->data_maps) { |
| 708 | g_hash_table_remove_all(maps->data_maps); |
| 709 | } |
| 710 | |
| 711 | if (maps->metadata_maps) { |
| 712 | g_hash_table_remove_all(maps->metadata_maps); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | BT_HIDDEN |
| 717 | void trace_ir_maps_destroy(struct trace_ir_maps *maps) |
| 718 | { |
| 719 | if (!maps) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | g_free(maps->debug_info_field_class_name); |
| 724 | |
| 725 | if (maps->data_maps) { |
| 726 | g_hash_table_destroy(maps->data_maps); |
| 727 | maps->data_maps = NULL; |
| 728 | } |
| 729 | |
| 730 | if (maps->metadata_maps) { |
| 731 | g_hash_table_destroy(maps->metadata_maps); |
| 732 | maps->metadata_maps = NULL; |
| 733 | } |
| 734 | |
| 735 | g_free(maps); |
| 736 | } |
| 737 | |
| 738 | BT_HIDDEN |
| 739 | struct trace_ir_maps *trace_ir_maps_create(bt_self_component *self_comp, |
| 740 | const char *debug_info_field_name, bt_logging_level log_level) |
| 741 | { |
| 742 | struct trace_ir_maps *ir_maps = g_new0(struct trace_ir_maps, 1); |
| 743 | if (!ir_maps) { |
| 744 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, |
| 745 | "Error allocating trace_ir_maps"); |
| 746 | goto error; |
| 747 | } |
| 748 | |
| 749 | ir_maps->log_level = log_level; |
| 750 | ir_maps->self_comp = self_comp; |
| 751 | |
| 752 | /* Copy debug info field name received from the user. */ |
| 753 | ir_maps->debug_info_field_class_name = g_strdup(debug_info_field_name); |
| 754 | if (!ir_maps->debug_info_field_class_name) { |
| 755 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 756 | "Cannot copy debug info field name"); |
| 757 | goto error; |
| 758 | } |
| 759 | |
| 760 | ir_maps->self_comp = self_comp; |
| 761 | |
| 762 | ir_maps->data_maps = g_hash_table_new_full(g_direct_hash, |
| 763 | g_direct_equal, (GDestroyNotify) NULL, |
| 764 | (GDestroyNotify) trace_ir_data_maps_destroy); |
| 765 | |
| 766 | ir_maps->metadata_maps = g_hash_table_new_full(g_direct_hash, |
| 767 | g_direct_equal, (GDestroyNotify) NULL, |
| 768 | (GDestroyNotify) trace_ir_metadata_maps_destroy); |
| 769 | |
| 770 | goto end; |
| 771 | error: |
| 772 | trace_ir_maps_destroy(ir_maps); |
| 773 | ir_maps = NULL; |
| 774 | end: |
| 775 | return ir_maps; |
| 776 | } |