Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[babeltrace.git] / src / plugins / lttng-utils / debug-info / trace-ir-mapping.c
CommitLineData
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 32
578e048b 33#include "common/assert.h"
3fadfbc0 34#include <babeltrace2/babeltrace.h>
ca9f27f3 35/* For bt_property_availability */
3fadfbc0 36#include <babeltrace2/property.h>
ca9f27f3
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
43static
44bt_trace_class *create_new_mapped_trace_class(struct trace_ir_maps *ir_maps,
45 const bt_trace_class *in_trace_class)
46{
3b34b490
FD
47 bt_self_component *self_comp = ir_maps->self_comp;
48 enum debug_info_trace_ir_mapping_status status;
db5d746d 49 struct trace_ir_metadata_maps *metadata_maps;
ca9f27f3 50
91bc8451 51 BT_COMP_LOGD("Creating new mapped trace class: in-tc-addr=%p", in_trace_class);
ca9f27f3
FD
52
53 BT_ASSERT(ir_maps);
54 BT_ASSERT(in_trace_class);
55
db5d746d
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
ca9f27f3 61 /* Create the ouput trace class. */
db5d746d
FD
62 metadata_maps->output_trace_class =
63 bt_trace_class_create(ir_maps->self_comp);
64 if (!metadata_maps->output_trace_class) {
3b34b490
FD
65 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
66 "Error create output trace class");
67 goto error;
ca9f27f3
FD
68 }
69
db5d746d 70 /* Copy the content over and add to the mapping. */
3b34b490 71 status = copy_trace_class_content(ir_maps, in_trace_class,
db5d746d
FD
72 metadata_maps->output_trace_class, ir_maps->log_level,
73 ir_maps->self_comp);
3b34b490
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;
ca9f27f3
FD
80 }
81
3b34b490
FD
82 BT_COMP_LOGD("Created new mapped trace class: "
83 "in-tc-addr=%p, out-tc-addr=%p",
db5d746d 84 in_trace_class, metadata_maps->output_trace_class);
ca9f27f3 85
3b34b490
FD
86 goto end;
87error:
88 BT_TRACE_CLASS_PUT_REF_AND_RESET(metadata_maps->output_trace_class);
ca9f27f3 89end:
db5d746d 90 return metadata_maps->output_trace_class;
ca9f27f3
FD
91}
92
93static
94bt_trace *create_new_mapped_trace(struct trace_ir_maps *ir_maps,
95 const bt_trace *in_trace)
96{
3b34b490
FD
97 bt_self_component *self_comp = ir_maps->self_comp;
98 enum debug_info_trace_ir_mapping_status status;
ca9f27f3 99 struct trace_ir_metadata_maps *metadata_maps;
8aca077e
FD
100 const bt_trace_class *in_trace_class;
101 bt_trace *out_trace;
ca9f27f3 102
91bc8451 103 BT_COMP_LOGD("Creating new mapped trace: in-t-addr=%p", in_trace);
ca9f27f3
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,
bc463d34 109 in_trace_class);
ca9f27f3
FD
110
111 if (!metadata_maps->output_trace_class) {
8aca077e
FD
112 /*
113 * If there is no output trace class yet, create a one and add
114 * it to the mapping.
115 */
ca9f27f3
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) {
3b34b490
FD
119 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
120 "Error create output trace class");
ca9f27f3
FD
121 out_trace = NULL;
122 goto end;
123 }
124 }
125
8aca077e 126 /* Create the output trace from the output trace class. */
ca9f27f3
FD
127 out_trace = bt_trace_create(metadata_maps->output_trace_class);
128 if (!out_trace) {
3b34b490
FD
129 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
130 "Error create output trace");
ca9f27f3
FD
131 goto end;
132 }
133
8aca077e 134 /* Copy the content over to the output trace. */
3b34b490 135 status = copy_trace_content(in_trace, out_trace, ir_maps->log_level,
91bc8451 136 ir_maps->self_comp);
3b34b490
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 }
ca9f27f3 143
91bc8451 144 BT_COMP_LOGD("Created new mapped trace: in-t-addr=%p, out-t-addr=%p",
bc463d34 145 in_trace, out_trace);
3b34b490
FD
146 goto end;
147
148error:
149 BT_TRACE_PUT_REF_AND_RESET(out_trace);
ca9f27f3
FD
150end:
151 return out_trace;
152}
153
db5d746d
FD
154BT_HIDDEN
155bt_stream_class *trace_ir_mapping_borrow_mapped_stream_class(
156 struct trace_ir_maps *ir_maps,
ca9f27f3
FD
157 const bt_stream_class *in_stream_class)
158{
db5d746d 159 BT_ASSERT_DBG(ir_maps);
98b15851 160 BT_ASSERT_DBG(in_stream_class);
ca9f27f3 161
db5d746d
FD
162 struct trace_ir_metadata_maps *md_maps =
163 borrow_metadata_maps_from_input_stream_class(ir_maps,
164 in_stream_class);
ca9f27f3 165 return g_hash_table_lookup(md_maps->stream_class_map,
bc463d34 166 (gpointer) in_stream_class);
ca9f27f3
FD
167}
168
db5d746d
FD
169BT_HIDDEN
170bt_stream_class *trace_ir_mapping_create_new_mapped_stream_class(
171 struct trace_ir_maps *ir_maps,
ca9f27f3
FD
172 const bt_stream_class *in_stream_class)
173{
3b34b490
FD
174 bt_self_component *self_comp = ir_maps->self_comp;
175 enum debug_info_trace_ir_mapping_status status;
ca9f27f3 176 struct trace_ir_metadata_maps *md_maps;
8aca077e 177 bt_stream_class *out_stream_class;
ca9f27f3 178
91bc8451 179 BT_COMP_LOGD("Creating new mapped stream class: in-sc-addr=%p",
bc463d34 180 in_stream_class);
ca9f27f3 181
8aca077e
FD
182 BT_ASSERT(ir_maps);
183 BT_ASSERT(in_stream_class);
db5d746d
FD
184 BT_ASSERT(!trace_ir_mapping_borrow_mapped_stream_class(ir_maps,
185 in_stream_class));
8aca077e 186
ca9f27f3 187 md_maps = borrow_metadata_maps_from_input_stream_class(ir_maps,
bc463d34 188 in_stream_class);
ca9f27f3
FD
189
190 BT_ASSERT(md_maps);
ca9f27f3 191
8aca077e 192 /* Create the output stream class. */
ca9f27f3 193 out_stream_class = bt_stream_class_create_with_id(
bc463d34
FD
194 md_maps->output_trace_class,
195 bt_stream_class_get_id(in_stream_class));
ca9f27f3 196 if (!out_stream_class) {
3b34b490
FD
197 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
198 "Error create output stream class");
ca9f27f3
FD
199 goto end;
200 }
201
3b34b490 202 /* Add it to the mapping. The mapping now owns out_stream_class. */
db5d746d
FD
203 g_hash_table_insert(md_maps->stream_class_map,
204 (gpointer) in_stream_class, out_stream_class);
205
8aca077e 206 /* Copy the content over to the output stream class. */
3b34b490 207 status = copy_stream_class_content(ir_maps, in_stream_class,
bc463d34 208 out_stream_class);
3b34b490
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;
ca9f27f3
FD
215 }
216
3b34b490
FD
217 BT_COMP_LOGD("Created new mapped stream class: "
218 "in-sc-addr=%p, out-sc-addr=%p",
bc463d34 219 in_stream_class, out_stream_class);
ca9f27f3 220
3b34b490
FD
221 goto end;
222error:
223 out_stream_class = NULL;
ca9f27f3
FD
224end:
225 return out_stream_class;
226}
227
228static
229bt_stream *borrow_mapped_stream(struct trace_ir_data_maps *d_maps,
230 const bt_stream *in_stream)
231{
98b15851
PP
232 BT_ASSERT_DBG(d_maps);
233 BT_ASSERT_DBG(in_stream);
ca9f27f3
FD
234
235 return g_hash_table_lookup(d_maps->stream_map, (gpointer) in_stream);
236}
237
238BT_HIDDEN
239bt_stream *trace_ir_mapping_create_new_mapped_stream(
8aca077e 240 struct trace_ir_maps *ir_maps, const bt_stream *in_stream)
ca9f27f3 241{
3b34b490
FD
242 bt_self_component *self_comp = ir_maps->self_comp;
243 enum debug_info_trace_ir_mapping_status status;
ca9f27f3 244 struct trace_ir_data_maps *d_maps;
ca9f27f3
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
ca9f27f3
FD
250 BT_ASSERT(ir_maps);
251 BT_ASSERT(in_stream);
91bc8451 252 BT_COMP_LOGD("Creating new mapped stream: in-s-addr=%p", in_stream);
ca9f27f3
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) {
8aca077e 258 /* Create the output trace for this input trace. */
ca9f27f3
FD
259 d_maps->output_trace = create_new_mapped_trace(ir_maps, in_trace);
260 if (!d_maps->output_trace) {
3b34b490
FD
261 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
262 "Error creating mapped trace");
263 goto error;
ca9f27f3
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);
db5d746d
FD
271 out_stream_class = trace_ir_mapping_borrow_mapped_stream_class(ir_maps,
272 in_stream_class);
273
ca9f27f3 274 if (!out_stream_class) {
8aca077e 275 /* Create the output stream class for this input stream class. */
db5d746d
FD
276 out_stream_class = trace_ir_mapping_create_new_mapped_stream_class(
277 ir_maps, in_stream_class);
ca9f27f3 278 if (!out_stream_class) {
3b34b490
FD
279 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
280 "Error creating mapped stream class");
281 goto error;
ca9f27f3
FD
282 }
283 }
284 BT_ASSERT(out_stream_class);
285
8aca077e 286 /* Create the output stream for this input stream. */
ca9f27f3 287 out_stream = bt_stream_create_with_id(out_stream_class,
8aca077e 288 d_maps->output_trace, bt_stream_get_id(in_stream));
ca9f27f3 289 if (!out_stream) {
3b34b490
FD
290 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
291 "Error creating output stream");
292 goto error;
ca9f27f3 293 }
ca9f27f3 294
3b34b490 295 /* Add it to the mapping. The mapping now owns out_stream.*/
ca9f27f3 296 g_hash_table_insert(d_maps->stream_map, (gpointer) in_stream,
bc463d34 297 out_stream);
ca9f27f3 298
db5d746d 299 /* Copy the content over to the output stream. */
3b34b490 300 status = copy_stream_content(in_stream, out_stream, ir_maps->log_level,
db5d746d 301 ir_maps->self_comp);
3b34b490
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 }
db5d746d 308
91bc8451 309 BT_COMP_LOGD("Created new mapped stream: in-s-addr=%p, out-s-addr=%p",
bc463d34 310 in_stream, out_stream);
ca9f27f3 311
3b34b490
FD
312 goto end;
313error:
314 out_stream = NULL;
ca9f27f3
FD
315end:
316 return out_stream;
317}
318
319BT_HIDDEN
320bt_stream *trace_ir_mapping_borrow_mapped_stream(struct trace_ir_maps *ir_maps,
321 const bt_stream *in_stream)
322{
8aca077e
FD
323 struct trace_ir_data_maps *d_maps;
324
98b15851
PP
325 BT_ASSERT_DBG(ir_maps);
326 BT_ASSERT_DBG(in_stream);
ca9f27f3
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
333static inline
334bt_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,
bc463d34 338 (gpointer) in_event_class);
ca9f27f3
FD
339}
340
341BT_HIDDEN
342bt_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{
3b34b490
FD
346 bt_self_component *self_comp = ir_maps->self_comp;
347 enum debug_info_trace_ir_mapping_status status;
8aca077e 348 struct trace_ir_metadata_maps *md_maps;
ca9f27f3
FD
349 const bt_stream_class *in_stream_class;
350 bt_stream_class *out_stream_class;
8aca077e 351 bt_event_class *out_event_class;
ca9f27f3 352
8aca077e
FD
353 BT_COMP_LOGD("Creating new mapped event class: in-ec-addr=%p",
354 in_event_class);
355
ca9f27f3
FD
356 BT_ASSERT(ir_maps);
357 BT_ASSERT(in_event_class);
358
8aca077e 359 in_stream_class = bt_event_class_borrow_stream_class_const(in_event_class);
ca9f27f3 360
8aca077e 361 BT_ASSERT(in_stream_class);
ca9f27f3 362
8aca077e
FD
363 md_maps = borrow_metadata_maps_from_input_stream_class(ir_maps,
364 in_stream_class);
ca9f27f3 365
8aca077e
FD
366 BT_ASSERT(md_maps);
367 BT_ASSERT(!borrow_mapped_event_class(md_maps, in_event_class));
ca9f27f3 368
8aca077e 369 /* Get the right output stream class to add the new event class to it. */
db5d746d
FD
370 out_stream_class = trace_ir_mapping_borrow_mapped_stream_class(
371 ir_maps, in_stream_class);
ca9f27f3
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,
bc463d34 376 bt_event_class_get_id(in_event_class));
ca9f27f3 377 if (!out_event_class) {
3b34b490
FD
378 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
379 "Error creating output event class");
ca9f27f3
FD
380 goto end;
381 }
382
3b34b490
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);
db5d746d 386
8aca077e 387 /* Copy the content over to the output event class. */
3b34b490 388 status = copy_event_class_content(ir_maps, in_event_class,
bc463d34 389 out_event_class);
3b34b490
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;
ca9f27f3
FD
396 }
397
91bc8451 398 BT_COMP_LOGD("Created new mapped event class: in-ec-addr=%p, out-ec-addr=%p",
bc463d34 399 in_event_class, out_event_class);
ca9f27f3 400
3b34b490
FD
401 goto end;
402error:
403 out_event_class = NULL;
ca9f27f3
FD
404end:
405 return out_event_class;
406}
407
408BT_HIDDEN
409bt_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
98b15851
PP
415 BT_ASSERT_DBG(ir_maps);
416 BT_ASSERT_DBG(in_event_class);
ca9f27f3 417
bc463d34
FD
418 md_maps = borrow_metadata_maps_from_input_event_class(ir_maps,
419 in_event_class);
ca9f27f3
FD
420
421 /* Return the mapped event_class. */
422 return borrow_mapped_event_class(md_maps, in_event_class);
423}
424
425static inline
426bt_packet *borrow_mapped_packet(struct trace_ir_data_maps *d_maps,
427 const bt_packet *in_packet)
428{
98b15851
PP
429 BT_ASSERT_DBG(d_maps);
430 BT_ASSERT_DBG(in_packet);
ca9f27f3 431
bc463d34 432 return g_hash_table_lookup(d_maps->packet_map, (gpointer) in_packet);
ca9f27f3
FD
433}
434
435BT_HIDDEN
436bt_packet *trace_ir_mapping_create_new_mapped_packet(
437 struct trace_ir_maps *ir_maps,
438 const bt_packet *in_packet)
439{
3b34b490
FD
440 bt_self_component *self_comp = ir_maps->self_comp;
441 enum debug_info_trace_ir_mapping_status status;
ca9f27f3 442 struct trace_ir_data_maps *d_maps;
ca9f27f3 443 const bt_stream *in_stream;
8aca077e 444 const bt_trace *in_trace;
ca9f27f3
FD
445 bt_packet *out_packet;
446 bt_stream *out_stream;
447
91bc8451 448 BT_COMP_LOGD("Creating new mapped packet: in-p-addr=%p", in_packet);
ca9f27f3
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
8aca077e 454 /* There should never be a mapped packet already. */
ca9f27f3 455 BT_ASSERT(!borrow_mapped_packet(d_maps, in_packet));
ca9f27f3
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) {
3b34b490
FD
465 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
466 "Error create output packet");
467 goto error;
ca9f27f3
FD
468 }
469
3b34b490
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);
ca9f27f3 473
db5d746d 474 /* Copy the content over to the output packet. */
3b34b490 475 status = copy_packet_content(in_packet, out_packet, ir_maps->log_level,
db5d746d 476 ir_maps->self_comp);
3b34b490
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 }
db5d746d 483
91bc8451 484 BT_COMP_LOGD("Created new mapped packet: in-p-addr=%p, out-p-addr=%p",
bc463d34 485 in_packet, out_packet);
ca9f27f3 486
3b34b490
FD
487 goto end;
488error:
489 out_packet = NULL;
ca9f27f3
FD
490end:
491 return out_packet;
492}
493
494BT_HIDDEN
495bt_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;
98b15851
PP
499 BT_ASSERT_DBG(ir_maps);
500 BT_ASSERT_DBG(in_packet);
ca9f27f3
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
507BT_HIDDEN
508void trace_ir_mapping_remove_mapped_packet(struct trace_ir_maps *ir_maps,
509 const bt_packet *in_packet)
510{
8aca077e 511 struct trace_ir_data_maps *d_maps;
ca9f27f3
FD
512 gboolean ret;
513
ca9f27f3
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
524BT_HIDDEN
525void trace_ir_mapping_remove_mapped_stream(struct trace_ir_maps *ir_maps,
526 const bt_stream *in_stream)
527{
ca9f27f3 528 struct trace_ir_data_maps *d_maps;
8aca077e 529 gboolean ret;
ca9f27f3
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
541static
542void 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,
bc463d34 549 (gpointer) in_trace_class);
ca9f27f3
FD
550 BT_ASSERT(ret);
551 }
552}
553
554static
555void 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
565struct trace_ir_data_maps *trace_ir_data_maps_create(struct trace_ir_maps *ir_maps,
566 const bt_trace *in_trace)
567{
3b34b490 568 bt_self_component *self_comp = ir_maps->self_comp;
b80991f6 569 bt_trace_add_listener_status add_listener_status;
8aca077e 570 struct trace_ir_data_maps *d_maps = g_new0(struct trace_ir_data_maps, 1);
b80991f6 571
ca9f27f3 572 if (!d_maps) {
3b34b490
FD
573 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
574 "Error allocating trace_ir_maps");
ca9f27f3
FD
575 goto error;
576 }
577
3a3d15f3 578 d_maps->log_level = ir_maps->log_level;
91bc8451 579 d_maps->self_comp = ir_maps->self_comp;
ca9f27f3
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,
8aca077e 584 g_direct_equal, NULL,(GDestroyNotify) bt_stream_put_ref);
ca9f27f3 585 d_maps->packet_map = g_hash_table_new_full(g_direct_hash,
8aca077e 586 g_direct_equal, NULL,(GDestroyNotify) bt_packet_put_ref);
ca9f27f3 587
b80991f6
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
ca9f27f3
FD
593error:
594 return d_maps;
595}
596
597struct 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{
3b34b490 601 bt_self_component *self_comp = ir_maps->self_comp;
8aca077e 602 bt_trace_class_add_listener_status add_listener_status;
ca9f27f3
FD
603 struct trace_ir_metadata_maps *md_maps =
604 g_new0(struct trace_ir_metadata_maps, 1);
b80991f6 605
ca9f27f3 606 if (!md_maps) {
3b34b490
FD
607 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
608 "Error allocating trace_ir_maps");
ca9f27f3
FD
609 goto error;
610 }
611
3a3d15f3 612 md_maps->log_level = ir_maps->log_level;
91bc8451 613 md_maps->self_comp = ir_maps->self_comp;
ca9f27f3
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) {
3b34b490
FD
623 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
624 "Error allocating field_class_resolving_context");
ca9f27f3
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,
bc463d34 630 g_direct_equal, NULL, (GDestroyNotify) bt_stream_class_put_ref);
ca9f27f3 631 md_maps->event_class_map = g_hash_table_new_full(g_direct_hash,
bc463d34 632 g_direct_equal, NULL, (GDestroyNotify) bt_event_class_put_ref);
ca9f27f3 633 md_maps->field_class_map = g_hash_table_new_full(g_direct_hash,
bc463d34 634 g_direct_equal, NULL, (GDestroyNotify) bt_field_class_put_ref);
ca9f27f3 635 md_maps->clock_class_map = g_hash_table_new_full(g_direct_hash,
bc463d34 636 g_direct_equal, NULL, (GDestroyNotify) bt_clock_class_put_ref);
ca9f27f3 637
b80991f6 638 add_listener_status = bt_trace_class_add_destruction_listener(
bc463d34
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);
b80991f6 642
ca9f27f3
FD
643error:
644 return md_maps;
645}
646
647BT_HIDDEN
648void trace_ir_data_maps_destroy(struct trace_ir_data_maps *maps)
649{
d24d5663
PP
650 bt_trace_remove_listener_status status;
651
ca9f27f3
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,
d24d5663
PP
669 maps->destruction_listener_id);
670 if (status != BT_TRACE_REMOVE_LISTENER_STATUS_OK) {
91bc8451
PP
671 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, maps->log_level,
672 maps->self_comp,
3a3d15f3 673 "Trace destruction listener removal failed.");
b80991f6 674 bt_current_thread_clear_error();
3b40fbf9 675 }
ca9f27f3
FD
676
677 g_free(maps);
678}
679
680BT_HIDDEN
681void trace_ir_metadata_maps_destroy(struct trace_ir_metadata_maps *maps)
682{
d24d5663
PP
683 bt_trace_class_remove_listener_status status;
684
ca9f27f3
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
19bbdc9b 705 g_free(maps->fc_resolving_ctx);
ca9f27f3
FD
706
707 if (maps->output_trace_class) {
708 bt_trace_class_put_ref(maps->output_trace_class);
709 }
710
d24d5663 711 status = bt_trace_class_remove_destruction_listener(
bc463d34 712 maps->input_trace_class, maps->destruction_listener_id);
d24d5663 713 if (status != BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK) {
91bc8451
PP
714 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, maps->log_level,
715 maps->self_comp,
3a3d15f3 716 "Trace destruction listener removal failed.");
b80991f6 717 bt_current_thread_clear_error();
3b40fbf9 718 }
ca9f27f3
FD
719
720 g_free(maps);
721}
722
723void 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
734BT_HIDDEN
735void trace_ir_maps_destroy(struct trace_ir_maps *maps)
736{
737 if (!maps) {
738 return;
739 }
740
19bbdc9b 741 g_free(maps->debug_info_field_class_name);
ca9f27f3
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
756BT_HIDDEN
757struct trace_ir_maps *trace_ir_maps_create(bt_self_component *self_comp,
3a3d15f3 758 const char *debug_info_field_name, bt_logging_level log_level)
ca9f27f3 759{
8aca077e 760 struct trace_ir_maps *ir_maps = g_new0(struct trace_ir_maps, 1);
3a3d15f3 761 if (!ir_maps) {
91bc8451 762 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
3a3d15f3 763 "Error allocating trace_ir_maps");
ca9f27f3
FD
764 goto error;
765 }
766
3a3d15f3 767 ir_maps->log_level = log_level;
91bc8451 768 ir_maps->self_comp = self_comp;
3a3d15f3 769
ca9f27f3 770 /* Copy debug info field name received from the user. */
8aca077e 771 ir_maps->debug_info_field_class_name = g_strdup(debug_info_field_name);
3a3d15f3 772 if (!ir_maps->debug_info_field_class_name) {
3b34b490
FD
773 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
774 "Cannot copy debug info field name");
ca9f27f3
FD
775 goto error;
776 }
777
3a3d15f3 778 ir_maps->self_comp = self_comp;
ca9f27f3 779
3a3d15f3 780 ir_maps->data_maps = g_hash_table_new_full(g_direct_hash,
bc463d34
FD
781 g_direct_equal, (GDestroyNotify) NULL,
782 (GDestroyNotify) trace_ir_data_maps_destroy);
ca9f27f3 783
3a3d15f3 784 ir_maps->metadata_maps = g_hash_table_new_full(g_direct_hash,
bc463d34
FD
785 g_direct_equal, (GDestroyNotify) NULL,
786 (GDestroyNotify) trace_ir_metadata_maps_destroy);
ca9f27f3
FD
787
788 goto end;
789error:
3a3d15f3
PP
790 trace_ir_maps_destroy(ir_maps);
791 ir_maps = NULL;
ca9f27f3 792end:
3a3d15f3 793 return ir_maps;
ca9f27f3 794}
This page took 0.080422 seconds and 4 git commands to generate.