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