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