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