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