| 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 output 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_stream_class *trace_ir_mapping_borrow_mapped_stream_class( |
| 137 | struct trace_ir_maps *ir_maps, |
| 138 | const bt_stream_class *in_stream_class) |
| 139 | { |
| 140 | BT_ASSERT_DBG(ir_maps); |
| 141 | BT_ASSERT_DBG(in_stream_class); |
| 142 | |
| 143 | struct trace_ir_metadata_maps *md_maps = |
| 144 | borrow_metadata_maps_from_input_stream_class(ir_maps, |
| 145 | in_stream_class); |
| 146 | return g_hash_table_lookup(md_maps->stream_class_map, |
| 147 | (gpointer) in_stream_class); |
| 148 | } |
| 149 | |
| 150 | bt_stream_class *trace_ir_mapping_create_new_mapped_stream_class( |
| 151 | struct trace_ir_maps *ir_maps, |
| 152 | const bt_stream_class *in_stream_class) |
| 153 | { |
| 154 | bt_self_component *self_comp = ir_maps->self_comp; |
| 155 | enum debug_info_trace_ir_mapping_status status; |
| 156 | struct trace_ir_metadata_maps *md_maps; |
| 157 | bt_stream_class *out_stream_class; |
| 158 | |
| 159 | BT_COMP_LOGD("Creating new mapped stream class: in-sc-addr=%p", |
| 160 | in_stream_class); |
| 161 | |
| 162 | BT_ASSERT(ir_maps); |
| 163 | BT_ASSERT(in_stream_class); |
| 164 | BT_ASSERT(!trace_ir_mapping_borrow_mapped_stream_class(ir_maps, |
| 165 | in_stream_class)); |
| 166 | |
| 167 | md_maps = borrow_metadata_maps_from_input_stream_class(ir_maps, |
| 168 | in_stream_class); |
| 169 | |
| 170 | BT_ASSERT(md_maps); |
| 171 | |
| 172 | /* Create the output stream class. */ |
| 173 | out_stream_class = bt_stream_class_create_with_id( |
| 174 | md_maps->output_trace_class, |
| 175 | bt_stream_class_get_id(in_stream_class)); |
| 176 | if (!out_stream_class) { |
| 177 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 178 | "Error create output stream class"); |
| 179 | goto end; |
| 180 | } |
| 181 | |
| 182 | /* Add it to the mapping. The mapping now owns out_stream_class. */ |
| 183 | g_hash_table_insert(md_maps->stream_class_map, |
| 184 | (gpointer) in_stream_class, out_stream_class); |
| 185 | |
| 186 | /* Copy the content over to the output stream class. */ |
| 187 | status = copy_stream_class_content(ir_maps, in_stream_class, |
| 188 | out_stream_class); |
| 189 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 190 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 191 | "Error copy content to output stream class: " |
| 192 | "in-sc-addr=%p, out-sc-addr=%p", in_stream_class, |
| 193 | out_stream_class); |
| 194 | goto error; |
| 195 | } |
| 196 | |
| 197 | BT_COMP_LOGD("Created new mapped stream class: " |
| 198 | "in-sc-addr=%p, out-sc-addr=%p", |
| 199 | in_stream_class, out_stream_class); |
| 200 | |
| 201 | goto end; |
| 202 | error: |
| 203 | out_stream_class = NULL; |
| 204 | end: |
| 205 | return out_stream_class; |
| 206 | } |
| 207 | |
| 208 | static |
| 209 | bt_stream *borrow_mapped_stream(struct trace_ir_data_maps *d_maps, |
| 210 | const bt_stream *in_stream) |
| 211 | { |
| 212 | BT_ASSERT_DBG(d_maps); |
| 213 | BT_ASSERT_DBG(in_stream); |
| 214 | |
| 215 | return g_hash_table_lookup(d_maps->stream_map, (gpointer) in_stream); |
| 216 | } |
| 217 | |
| 218 | bt_stream *trace_ir_mapping_create_new_mapped_stream( |
| 219 | struct trace_ir_maps *ir_maps, const bt_stream *in_stream) |
| 220 | { |
| 221 | bt_self_component *self_comp = ir_maps->self_comp; |
| 222 | enum debug_info_trace_ir_mapping_status status; |
| 223 | struct trace_ir_data_maps *d_maps; |
| 224 | const bt_stream_class *in_stream_class; |
| 225 | const bt_trace *in_trace; |
| 226 | bt_stream_class *out_stream_class; |
| 227 | bt_stream *out_stream = NULL; |
| 228 | |
| 229 | BT_ASSERT(ir_maps); |
| 230 | BT_ASSERT(in_stream); |
| 231 | BT_COMP_LOGD("Creating new mapped stream: in-s-addr=%p", in_stream); |
| 232 | |
| 233 | in_trace = bt_stream_borrow_trace_const(in_stream); |
| 234 | |
| 235 | d_maps = borrow_data_maps_from_input_trace(ir_maps, in_trace); |
| 236 | if (!d_maps->output_trace) { |
| 237 | /* Create the output trace for this input trace. */ |
| 238 | d_maps->output_trace = create_new_mapped_trace(ir_maps, in_trace); |
| 239 | if (!d_maps->output_trace) { |
| 240 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 241 | "Error creating mapped trace"); |
| 242 | goto error; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | BT_ASSERT(d_maps->output_trace); |
| 247 | BT_ASSERT(!borrow_mapped_stream(d_maps, in_stream)); |
| 248 | |
| 249 | in_stream_class = bt_stream_borrow_class_const(in_stream); |
| 250 | out_stream_class = trace_ir_mapping_borrow_mapped_stream_class(ir_maps, |
| 251 | in_stream_class); |
| 252 | |
| 253 | if (!out_stream_class) { |
| 254 | /* Create the output stream class for this input stream class. */ |
| 255 | out_stream_class = trace_ir_mapping_create_new_mapped_stream_class( |
| 256 | ir_maps, in_stream_class); |
| 257 | if (!out_stream_class) { |
| 258 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 259 | "Error creating mapped stream class"); |
| 260 | goto error; |
| 261 | } |
| 262 | } |
| 263 | BT_ASSERT(out_stream_class); |
| 264 | |
| 265 | /* Create the output stream for this input stream. */ |
| 266 | out_stream = bt_stream_create_with_id(out_stream_class, |
| 267 | d_maps->output_trace, bt_stream_get_id(in_stream)); |
| 268 | if (!out_stream) { |
| 269 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 270 | "Error creating output stream"); |
| 271 | goto error; |
| 272 | } |
| 273 | |
| 274 | /* Add it to the mapping. The mapping now owns out_stream.*/ |
| 275 | g_hash_table_insert(d_maps->stream_map, (gpointer) in_stream, |
| 276 | out_stream); |
| 277 | |
| 278 | /* Copy the content over to the output stream. */ |
| 279 | status = copy_stream_content(in_stream, out_stream, ir_maps->log_level, |
| 280 | ir_maps->self_comp); |
| 281 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 282 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 283 | "Error copy content to output stream: " |
| 284 | "in-s-addr=%p, out-s-addr=%p", in_stream, out_stream); |
| 285 | goto error; |
| 286 | } |
| 287 | |
| 288 | BT_COMP_LOGD("Created new mapped stream: in-s-addr=%p, out-s-addr=%p", |
| 289 | in_stream, out_stream); |
| 290 | |
| 291 | goto end; |
| 292 | error: |
| 293 | out_stream = NULL; |
| 294 | end: |
| 295 | return out_stream; |
| 296 | } |
| 297 | |
| 298 | bt_stream *trace_ir_mapping_borrow_mapped_stream(struct trace_ir_maps *ir_maps, |
| 299 | const bt_stream *in_stream) |
| 300 | { |
| 301 | struct trace_ir_data_maps *d_maps; |
| 302 | |
| 303 | BT_ASSERT_DBG(ir_maps); |
| 304 | BT_ASSERT_DBG(in_stream); |
| 305 | |
| 306 | d_maps = borrow_data_maps_from_input_stream(ir_maps, in_stream); |
| 307 | /* Return the mapped stream. */ |
| 308 | return borrow_mapped_stream(d_maps, in_stream); |
| 309 | } |
| 310 | |
| 311 | static inline |
| 312 | bt_event_class *borrow_mapped_event_class(struct trace_ir_metadata_maps *md_maps, |
| 313 | const bt_event_class *in_event_class) |
| 314 | { |
| 315 | return g_hash_table_lookup(md_maps->event_class_map, |
| 316 | (gpointer) in_event_class); |
| 317 | } |
| 318 | |
| 319 | bt_event_class *trace_ir_mapping_create_new_mapped_event_class( |
| 320 | struct trace_ir_maps *ir_maps, |
| 321 | const bt_event_class *in_event_class) |
| 322 | { |
| 323 | bt_self_component *self_comp = ir_maps->self_comp; |
| 324 | enum debug_info_trace_ir_mapping_status status; |
| 325 | struct trace_ir_metadata_maps *md_maps; |
| 326 | const bt_stream_class *in_stream_class; |
| 327 | bt_stream_class *out_stream_class; |
| 328 | bt_event_class *out_event_class; |
| 329 | |
| 330 | BT_COMP_LOGD("Creating new mapped event class: in-ec-addr=%p", |
| 331 | in_event_class); |
| 332 | |
| 333 | BT_ASSERT(ir_maps); |
| 334 | BT_ASSERT(in_event_class); |
| 335 | |
| 336 | in_stream_class = bt_event_class_borrow_stream_class_const(in_event_class); |
| 337 | |
| 338 | BT_ASSERT(in_stream_class); |
| 339 | |
| 340 | md_maps = borrow_metadata_maps_from_input_stream_class(ir_maps, |
| 341 | in_stream_class); |
| 342 | |
| 343 | BT_ASSERT(md_maps); |
| 344 | BT_ASSERT(!borrow_mapped_event_class(md_maps, in_event_class)); |
| 345 | |
| 346 | /* Get the right output stream class to add the new event class to it. */ |
| 347 | out_stream_class = trace_ir_mapping_borrow_mapped_stream_class( |
| 348 | ir_maps, in_stream_class); |
| 349 | BT_ASSERT(out_stream_class); |
| 350 | |
| 351 | /* Create an output event class. */ |
| 352 | out_event_class = bt_event_class_create_with_id(out_stream_class, |
| 353 | bt_event_class_get_id(in_event_class)); |
| 354 | if (!out_event_class) { |
| 355 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 356 | "Error creating output event class"); |
| 357 | goto end; |
| 358 | } |
| 359 | |
| 360 | /* Add it to the mapping. The mapping now owns out_event_class. */ |
| 361 | g_hash_table_insert(md_maps->event_class_map, (gpointer) in_event_class, |
| 362 | out_event_class); |
| 363 | |
| 364 | /* Copy the content over to the output event class. */ |
| 365 | status = copy_event_class_content(ir_maps, in_event_class, |
| 366 | out_event_class); |
| 367 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 368 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 369 | "Error copy content to output event class: " |
| 370 | "in-ec-addr=%p, out-ec-addr=%p", in_event_class, |
| 371 | out_event_class); |
| 372 | goto error; |
| 373 | } |
| 374 | |
| 375 | BT_COMP_LOGD("Created new mapped event class: in-ec-addr=%p, out-ec-addr=%p", |
| 376 | in_event_class, out_event_class); |
| 377 | |
| 378 | goto end; |
| 379 | error: |
| 380 | out_event_class = NULL; |
| 381 | end: |
| 382 | return out_event_class; |
| 383 | } |
| 384 | |
| 385 | bt_event_class *trace_ir_mapping_borrow_mapped_event_class( |
| 386 | struct trace_ir_maps *ir_maps, |
| 387 | const bt_event_class *in_event_class) |
| 388 | { |
| 389 | struct trace_ir_metadata_maps *md_maps; |
| 390 | |
| 391 | BT_ASSERT_DBG(ir_maps); |
| 392 | BT_ASSERT_DBG(in_event_class); |
| 393 | |
| 394 | md_maps = borrow_metadata_maps_from_input_event_class(ir_maps, |
| 395 | in_event_class); |
| 396 | |
| 397 | /* Return the mapped event_class. */ |
| 398 | return borrow_mapped_event_class(md_maps, in_event_class); |
| 399 | } |
| 400 | |
| 401 | static inline |
| 402 | bt_packet *borrow_mapped_packet(struct trace_ir_data_maps *d_maps, |
| 403 | const bt_packet *in_packet) |
| 404 | { |
| 405 | BT_ASSERT_DBG(d_maps); |
| 406 | BT_ASSERT_DBG(in_packet); |
| 407 | |
| 408 | return g_hash_table_lookup(d_maps->packet_map, (gpointer) in_packet); |
| 409 | } |
| 410 | |
| 411 | bt_packet *trace_ir_mapping_create_new_mapped_packet( |
| 412 | struct trace_ir_maps *ir_maps, |
| 413 | const bt_packet *in_packet) |
| 414 | { |
| 415 | bt_self_component *self_comp = ir_maps->self_comp; |
| 416 | enum debug_info_trace_ir_mapping_status status; |
| 417 | struct trace_ir_data_maps *d_maps; |
| 418 | const bt_stream *in_stream; |
| 419 | const bt_trace *in_trace; |
| 420 | bt_packet *out_packet; |
| 421 | bt_stream *out_stream; |
| 422 | |
| 423 | BT_COMP_LOGD("Creating new mapped packet: in-p-addr=%p", in_packet); |
| 424 | |
| 425 | in_stream = bt_packet_borrow_stream_const(in_packet); |
| 426 | in_trace = bt_stream_borrow_trace_const(in_stream); |
| 427 | d_maps = borrow_data_maps_from_input_trace(ir_maps, in_trace); |
| 428 | |
| 429 | /* There should never be a mapped packet already. */ |
| 430 | BT_ASSERT(!borrow_mapped_packet(d_maps, in_packet)); |
| 431 | BT_ASSERT(in_stream); |
| 432 | |
| 433 | /* Get output stream corresponding to this input stream. */ |
| 434 | out_stream = borrow_mapped_stream(d_maps, in_stream); |
| 435 | BT_ASSERT(out_stream); |
| 436 | |
| 437 | /* Create the output packet. */ |
| 438 | out_packet = bt_packet_create(out_stream); |
| 439 | if (!out_packet) { |
| 440 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 441 | "Error create output packet"); |
| 442 | goto error; |
| 443 | } |
| 444 | |
| 445 | /* Add it to the mapping. The mapping now owns out_packet. */ |
| 446 | g_hash_table_insert(d_maps->packet_map, (gpointer) in_packet, |
| 447 | out_packet); |
| 448 | |
| 449 | /* Copy the content over to the output packet. */ |
| 450 | status = copy_packet_content(in_packet, out_packet, ir_maps->log_level, |
| 451 | ir_maps->self_comp); |
| 452 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 453 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 454 | "Error copy content to output packet: " |
| 455 | "in-p-addr=%p, out-p-addr=%p", in_packet, out_packet); |
| 456 | goto error; |
| 457 | } |
| 458 | |
| 459 | BT_COMP_LOGD("Created new mapped packet: in-p-addr=%p, out-p-addr=%p", |
| 460 | in_packet, out_packet); |
| 461 | |
| 462 | goto end; |
| 463 | error: |
| 464 | out_packet = NULL; |
| 465 | end: |
| 466 | return out_packet; |
| 467 | } |
| 468 | |
| 469 | bt_packet *trace_ir_mapping_borrow_mapped_packet(struct trace_ir_maps *ir_maps, |
| 470 | const bt_packet *in_packet) |
| 471 | { |
| 472 | struct trace_ir_data_maps *d_maps; |
| 473 | BT_ASSERT_DBG(ir_maps); |
| 474 | BT_ASSERT_DBG(in_packet); |
| 475 | |
| 476 | d_maps = borrow_data_maps_from_input_packet(ir_maps, in_packet); |
| 477 | |
| 478 | return borrow_mapped_packet(d_maps, in_packet); |
| 479 | } |
| 480 | |
| 481 | void trace_ir_mapping_remove_mapped_packet(struct trace_ir_maps *ir_maps, |
| 482 | const bt_packet *in_packet) |
| 483 | { |
| 484 | struct trace_ir_data_maps *d_maps; |
| 485 | gboolean ret; |
| 486 | |
| 487 | BT_ASSERT(ir_maps); |
| 488 | BT_ASSERT(in_packet); |
| 489 | |
| 490 | d_maps = borrow_data_maps_from_input_packet(ir_maps, in_packet); |
| 491 | |
| 492 | ret = g_hash_table_remove(d_maps->packet_map, in_packet); |
| 493 | |
| 494 | BT_ASSERT(ret); |
| 495 | } |
| 496 | |
| 497 | void trace_ir_mapping_remove_mapped_stream(struct trace_ir_maps *ir_maps, |
| 498 | const bt_stream *in_stream) |
| 499 | { |
| 500 | struct trace_ir_data_maps *d_maps; |
| 501 | gboolean ret; |
| 502 | |
| 503 | BT_ASSERT(ir_maps); |
| 504 | BT_ASSERT(in_stream); |
| 505 | |
| 506 | d_maps = borrow_data_maps_from_input_stream(ir_maps, in_stream); |
| 507 | |
| 508 | ret = g_hash_table_remove(d_maps->stream_map, in_stream); |
| 509 | |
| 510 | BT_ASSERT(ret); |
| 511 | } |
| 512 | |
| 513 | static |
| 514 | void trace_ir_metadata_maps_remove_func(const bt_trace_class *in_trace_class, |
| 515 | void *data) |
| 516 | { |
| 517 | struct trace_ir_maps *maps = (struct trace_ir_maps *) data; |
| 518 | if (maps->metadata_maps) { |
| 519 | gboolean ret; |
| 520 | ret = g_hash_table_remove(maps->metadata_maps, |
| 521 | (gpointer) in_trace_class); |
| 522 | BT_ASSERT(ret); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | static |
| 527 | void trace_ir_data_maps_remove_func(const bt_trace *in_trace, void *data) |
| 528 | { |
| 529 | struct trace_ir_maps *maps = (struct trace_ir_maps *) data; |
| 530 | if (maps->data_maps) { |
| 531 | gboolean ret; |
| 532 | ret = g_hash_table_remove(maps->data_maps, (gpointer) in_trace); |
| 533 | BT_ASSERT(ret); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | struct trace_ir_data_maps *trace_ir_data_maps_create(struct trace_ir_maps *ir_maps, |
| 538 | const bt_trace *in_trace) |
| 539 | { |
| 540 | bt_self_component *self_comp = ir_maps->self_comp; |
| 541 | bt_trace_add_listener_status add_listener_status; |
| 542 | struct trace_ir_data_maps *d_maps = g_new0(struct trace_ir_data_maps, 1); |
| 543 | |
| 544 | if (!d_maps) { |
| 545 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 546 | "Error allocating trace_ir_maps"); |
| 547 | goto error; |
| 548 | } |
| 549 | |
| 550 | d_maps->log_level = ir_maps->log_level; |
| 551 | d_maps->self_comp = ir_maps->self_comp; |
| 552 | d_maps->input_trace = in_trace; |
| 553 | |
| 554 | /* Create the hashtables used to map data objects. */ |
| 555 | d_maps->stream_map = g_hash_table_new_full(g_direct_hash, |
| 556 | g_direct_equal, NULL,(GDestroyNotify) bt_stream_put_ref); |
| 557 | d_maps->packet_map = g_hash_table_new_full(g_direct_hash, |
| 558 | g_direct_equal, NULL,(GDestroyNotify) bt_packet_put_ref); |
| 559 | |
| 560 | add_listener_status = bt_trace_add_destruction_listener( |
| 561 | in_trace, trace_ir_data_maps_remove_func, |
| 562 | ir_maps, &d_maps->destruction_listener_id); |
| 563 | BT_ASSERT(add_listener_status == BT_TRACE_ADD_LISTENER_STATUS_OK); |
| 564 | |
| 565 | error: |
| 566 | return d_maps; |
| 567 | } |
| 568 | |
| 569 | struct trace_ir_metadata_maps *trace_ir_metadata_maps_create( |
| 570 | struct trace_ir_maps *ir_maps, |
| 571 | const bt_trace_class *in_trace_class) |
| 572 | { |
| 573 | bt_self_component *self_comp = ir_maps->self_comp; |
| 574 | bt_trace_class_add_listener_status add_listener_status; |
| 575 | struct trace_ir_metadata_maps *md_maps = |
| 576 | g_new0(struct trace_ir_metadata_maps, 1); |
| 577 | |
| 578 | if (!md_maps) { |
| 579 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 580 | "Error allocating trace_ir_maps"); |
| 581 | goto error; |
| 582 | } |
| 583 | |
| 584 | md_maps->log_level = ir_maps->log_level; |
| 585 | md_maps->self_comp = ir_maps->self_comp; |
| 586 | md_maps->input_trace_class = in_trace_class; |
| 587 | /* |
| 588 | * Create the field class resolving context. This is needed to keep |
| 589 | * track of the field class already copied in order to do the field |
| 590 | * path resolution correctly. |
| 591 | */ |
| 592 | md_maps->fc_resolving_ctx = |
| 593 | g_new0(struct field_class_resolving_context, 1); |
| 594 | if (!md_maps->fc_resolving_ctx) { |
| 595 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 596 | "Error allocating field_class_resolving_context"); |
| 597 | goto error; |
| 598 | } |
| 599 | |
| 600 | /* Create the hashtables used to map metadata objects. */ |
| 601 | md_maps->stream_class_map = g_hash_table_new_full(g_direct_hash, |
| 602 | g_direct_equal, NULL, (GDestroyNotify) bt_stream_class_put_ref); |
| 603 | md_maps->event_class_map = g_hash_table_new_full(g_direct_hash, |
| 604 | g_direct_equal, NULL, (GDestroyNotify) bt_event_class_put_ref); |
| 605 | md_maps->field_class_map = g_hash_table_new_full(g_direct_hash, |
| 606 | g_direct_equal, NULL, (GDestroyNotify) bt_field_class_put_ref); |
| 607 | md_maps->clock_class_map = g_hash_table_new_full(g_direct_hash, |
| 608 | g_direct_equal, NULL, (GDestroyNotify) bt_clock_class_put_ref); |
| 609 | |
| 610 | add_listener_status = bt_trace_class_add_destruction_listener( |
| 611 | in_trace_class, trace_ir_metadata_maps_remove_func, |
| 612 | ir_maps, &md_maps->destruction_listener_id); |
| 613 | BT_ASSERT(add_listener_status == BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK); |
| 614 | |
| 615 | error: |
| 616 | return md_maps; |
| 617 | } |
| 618 | |
| 619 | void trace_ir_data_maps_destroy(struct trace_ir_data_maps *maps) |
| 620 | { |
| 621 | bt_trace_remove_listener_status status; |
| 622 | |
| 623 | if (!maps) { |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | if (maps->packet_map) { |
| 628 | g_hash_table_destroy(maps->packet_map); |
| 629 | } |
| 630 | |
| 631 | if (maps->stream_map) { |
| 632 | g_hash_table_destroy(maps->stream_map); |
| 633 | } |
| 634 | |
| 635 | if (maps->output_trace) { |
| 636 | bt_trace_put_ref(maps->output_trace); |
| 637 | } |
| 638 | |
| 639 | status = bt_trace_remove_destruction_listener(maps->input_trace, |
| 640 | maps->destruction_listener_id); |
| 641 | if (status != BT_TRACE_REMOVE_LISTENER_STATUS_OK) { |
| 642 | BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, maps->log_level, |
| 643 | maps->self_comp, |
| 644 | "Trace destruction listener removal failed."); |
| 645 | bt_current_thread_clear_error(); |
| 646 | } |
| 647 | |
| 648 | g_free(maps); |
| 649 | } |
| 650 | |
| 651 | void trace_ir_metadata_maps_destroy(struct trace_ir_metadata_maps *maps) |
| 652 | { |
| 653 | bt_trace_class_remove_listener_status status; |
| 654 | |
| 655 | if (!maps) { |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | if (maps->stream_class_map) { |
| 660 | g_hash_table_destroy(maps->stream_class_map); |
| 661 | } |
| 662 | |
| 663 | if (maps->event_class_map) { |
| 664 | g_hash_table_destroy(maps->event_class_map); |
| 665 | } |
| 666 | |
| 667 | if (maps->field_class_map) { |
| 668 | g_hash_table_destroy(maps->field_class_map); |
| 669 | } |
| 670 | |
| 671 | if (maps->clock_class_map) { |
| 672 | g_hash_table_destroy(maps->clock_class_map); |
| 673 | } |
| 674 | |
| 675 | g_free(maps->fc_resolving_ctx); |
| 676 | |
| 677 | if (maps->output_trace_class) { |
| 678 | bt_trace_class_put_ref(maps->output_trace_class); |
| 679 | } |
| 680 | |
| 681 | status = bt_trace_class_remove_destruction_listener( |
| 682 | maps->input_trace_class, maps->destruction_listener_id); |
| 683 | if (status != BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK) { |
| 684 | BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, maps->log_level, |
| 685 | maps->self_comp, |
| 686 | "Trace destruction listener removal failed."); |
| 687 | bt_current_thread_clear_error(); |
| 688 | } |
| 689 | |
| 690 | g_free(maps); |
| 691 | } |
| 692 | |
| 693 | void trace_ir_maps_clear(struct trace_ir_maps *maps) |
| 694 | { |
| 695 | if (maps->data_maps) { |
| 696 | g_hash_table_remove_all(maps->data_maps); |
| 697 | } |
| 698 | |
| 699 | if (maps->metadata_maps) { |
| 700 | g_hash_table_remove_all(maps->metadata_maps); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | void trace_ir_maps_destroy(struct trace_ir_maps *maps) |
| 705 | { |
| 706 | if (!maps) { |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | g_free(maps->debug_info_field_class_name); |
| 711 | |
| 712 | if (maps->data_maps) { |
| 713 | g_hash_table_destroy(maps->data_maps); |
| 714 | maps->data_maps = NULL; |
| 715 | } |
| 716 | |
| 717 | if (maps->metadata_maps) { |
| 718 | g_hash_table_destroy(maps->metadata_maps); |
| 719 | maps->metadata_maps = NULL; |
| 720 | } |
| 721 | |
| 722 | g_free(maps); |
| 723 | } |
| 724 | |
| 725 | struct trace_ir_maps *trace_ir_maps_create(bt_self_component *self_comp, |
| 726 | const char *debug_info_field_name, bt_logging_level log_level) |
| 727 | { |
| 728 | struct trace_ir_maps *ir_maps = g_new0(struct trace_ir_maps, 1); |
| 729 | if (!ir_maps) { |
| 730 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, |
| 731 | "Error allocating trace_ir_maps"); |
| 732 | goto error; |
| 733 | } |
| 734 | |
| 735 | ir_maps->log_level = log_level; |
| 736 | ir_maps->self_comp = self_comp; |
| 737 | |
| 738 | /* Copy debug info field name received from the user. */ |
| 739 | ir_maps->debug_info_field_class_name = g_strdup(debug_info_field_name); |
| 740 | if (!ir_maps->debug_info_field_class_name) { |
| 741 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 742 | "Cannot copy debug info field name"); |
| 743 | goto error; |
| 744 | } |
| 745 | |
| 746 | ir_maps->self_comp = self_comp; |
| 747 | |
| 748 | ir_maps->data_maps = g_hash_table_new_full(g_direct_hash, |
| 749 | g_direct_equal, (GDestroyNotify) NULL, |
| 750 | (GDestroyNotify) trace_ir_data_maps_destroy); |
| 751 | |
| 752 | ir_maps->metadata_maps = g_hash_table_new_full(g_direct_hash, |
| 753 | g_direct_equal, (GDestroyNotify) NULL, |
| 754 | (GDestroyNotify) trace_ir_metadata_maps_destroy); |
| 755 | |
| 756 | goto end; |
| 757 | error: |
| 758 | trace_ir_maps_destroy(ir_maps); |
| 759 | ir_maps = NULL; |
| 760 | end: |
| 761 | return ir_maps; |
| 762 | } |