Cleanup: flt.lttng-utils.debug-info: coding style fixes
[babeltrace.git] / src / plugins / lttng-utils / debug-info / trace-ir-mapping.c
CommitLineData
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
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
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
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{
58f6d595 84 struct trace_ir_metadata_maps *metadata_maps;
bb88d099
FD
85 const bt_trace_class *in_trace_class;
86 bt_trace *out_trace;
58f6d595 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) {
bb88d099
FD
97 /*
98 * If there is no output trace class yet, create a one and add
99 * it to the mapping.
100 */
58f6d595
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
bb88d099 109 /* Create the output trace from the output trace class. */
58f6d595
FD
110 out_trace = bt_trace_create(metadata_maps->output_trace_class);
111 if (!out_trace) {
475e740e 112 BT_COMP_LOGE_STR("Error create output trace");
58f6d595
FD
113 goto end;
114 }
115
bb88d099 116 /* Copy the content over to the output trace. */
475e740e
PP
117 copy_trace_content(in_trace, out_trace, ir_maps->log_level,
118 ir_maps->self_comp);
58f6d595 119
475e740e 120 BT_COMP_LOGD("Created new mapped trace: in-t-addr=%p, out-t-addr=%p",
9a51bfb2 121 in_trace, out_trace);
58f6d595
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{
130 BT_ASSERT(md_maps);
131 BT_ASSERT(in_stream_class);
132
133 return g_hash_table_lookup(md_maps->stream_class_map,
9a51bfb2 134 (gpointer) in_stream_class);
58f6d595
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{
58f6d595 141 struct trace_ir_metadata_maps *md_maps;
bb88d099
FD
142 bt_stream_class *out_stream_class;
143 int ret;
58f6d595 144
475e740e 145 BT_COMP_LOGD("Creating new mapped stream class: in-sc-addr=%p",
9a51bfb2 146 in_stream_class);
58f6d595 147
bb88d099
FD
148 BT_ASSERT(ir_maps);
149 BT_ASSERT(in_stream_class);
150
58f6d595 151 md_maps = borrow_metadata_maps_from_input_stream_class(ir_maps,
9a51bfb2 152 in_stream_class);
58f6d595
FD
153
154 BT_ASSERT(md_maps);
58f6d595
FD
155 BT_ASSERT(!borrow_mapped_stream_class(md_maps, in_stream_class));
156
bb88d099 157 /* Create the output stream class. */
58f6d595 158 out_stream_class = bt_stream_class_create_with_id(
9a51bfb2
FD
159 md_maps->output_trace_class,
160 bt_stream_class_get_id(in_stream_class));
58f6d595 161 if (!out_stream_class) {
475e740e 162 BT_COMP_LOGE_STR("Error create output stream class");
58f6d595
FD
163 goto end;
164 }
165
bb88d099 166 /* Copy the content over to the output stream class. */
58f6d595 167 ret = copy_stream_class_content(ir_maps, in_stream_class,
9a51bfb2 168 out_stream_class);
58f6d595 169 if (ret) {
475e740e 170 BT_COMP_LOGE_STR("Error copy content to output stream class");
58f6d595
FD
171 out_stream_class = NULL;
172 goto end;
173 }
174
bb88d099 175 /* Add it to the mapping. */
58f6d595 176 g_hash_table_insert(md_maps->stream_class_map,
9a51bfb2 177 (gpointer) in_stream_class, out_stream_class);
58f6d595 178
475e740e 179 BT_COMP_LOGD("Created new mapped stream class: in-sc-addr=%p, out-sc-addr=%p",
9a51bfb2 180 in_stream_class, out_stream_class);
58f6d595
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{
190 BT_ASSERT(d_maps);
191 BT_ASSERT(in_stream);
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(
bb88d099 198 struct trace_ir_maps *ir_maps, const bt_stream *in_stream)
58f6d595
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
58f6d595
FD
207 BT_ASSERT(ir_maps);
208 BT_ASSERT(in_stream);
475e740e 209 BT_COMP_LOGD("Creating new mapped stream: in-s-addr=%p", in_stream);
58f6d595
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) {
bb88d099 215 /* Create the output trace for this input trace. */
58f6d595
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);
58f6d595
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) {
bb88d099 229 /* Create the output stream class for this input stream class. */
58f6d595 230 out_stream_class = create_new_mapped_stream_class(ir_maps,
9a51bfb2 231 in_stream_class);
58f6d595
FD
232 if (!out_stream_class) {
233 goto end;
234 }
235 }
236 BT_ASSERT(out_stream_class);
237
bb88d099 238 /* Create the output stream for this input stream. */
58f6d595 239 out_stream = bt_stream_create_with_id(out_stream_class,
bb88d099 240 d_maps->output_trace, bt_stream_get_id(in_stream));
58f6d595 241 if (!out_stream) {
475e740e 242 BT_COMP_LOGE_STR("Error creating output stream");
58f6d595
FD
243 goto end;
244 }
58f6d595 245
bb88d099 246 /* Copy the content over to the output stream. */
475e740e
PP
247 copy_stream_content(in_stream, out_stream, ir_maps->log_level,
248 ir_maps->self_comp);
58f6d595 249
bb88d099 250 /* Add it to the mapping. */
58f6d595 251 g_hash_table_insert(d_maps->stream_map, (gpointer) in_stream,
9a51bfb2 252 out_stream);
58f6d595 253
475e740e 254 BT_COMP_LOGD("Created new mapped stream: in-s-addr=%p, out-s-addr=%p",
9a51bfb2 255 in_stream, out_stream);
58f6d595
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{
bb88d099
FD
265 struct trace_ir_data_maps *d_maps;
266
58f6d595
FD
267 BT_ASSERT(ir_maps);
268 BT_ASSERT(in_stream);
58f6d595
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,
9a51bfb2 280 (gpointer) in_event_class);
58f6d595
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{
bb88d099 288 struct trace_ir_metadata_maps *md_maps;
58f6d595
FD
289 const bt_stream_class *in_stream_class;
290 bt_stream_class *out_stream_class;
bb88d099 291 bt_event_class *out_event_class;
58f6d595
FD
292 int ret;
293
bb88d099
FD
294 BT_COMP_LOGD("Creating new mapped event class: in-ec-addr=%p",
295 in_event_class);
296
58f6d595
FD
297 BT_ASSERT(ir_maps);
298 BT_ASSERT(in_event_class);
299
bb88d099 300 in_stream_class = bt_event_class_borrow_stream_class_const(in_event_class);
58f6d595 301
bb88d099 302 BT_ASSERT(in_stream_class);
58f6d595 303
bb88d099
FD
304 md_maps = borrow_metadata_maps_from_input_stream_class(ir_maps,
305 in_stream_class);
58f6d595 306
bb88d099
FD
307 BT_ASSERT(md_maps);
308 BT_ASSERT(!borrow_mapped_event_class(md_maps, in_event_class));
58f6d595 309
bb88d099 310 /* Get the right output stream class to add the new event class to it. */
58f6d595
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,
9a51bfb2 316 bt_event_class_get_id(in_event_class));
58f6d595 317 if (!out_event_class) {
475e740e 318 BT_COMP_LOGE_STR("Error creating output event class");
58f6d595
FD
319 goto end;
320 }
321
bb88d099 322 /* Copy the content over to the output event class. */
58f6d595 323 ret = copy_event_class_content(ir_maps, in_event_class,
9a51bfb2 324 out_event_class);
58f6d595 325 if (ret) {
475e740e 326 BT_COMP_LOGE_STR("Error copy content to output event class");
58f6d595
FD
327 out_event_class = NULL;
328 goto end;
329 }
330
bb88d099 331 /* Add it to the mapping. */
58f6d595 332 g_hash_table_insert(md_maps->event_class_map,
9a51bfb2 333 (gpointer) in_event_class, out_event_class);
58f6d595 334
475e740e 335 BT_COMP_LOGD("Created new mapped event class: in-ec-addr=%p, out-ec-addr=%p",
9a51bfb2 336 in_event_class, out_event_class);
58f6d595
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
349 BT_ASSERT(ir_maps);
350 BT_ASSERT(in_event_class);
351
9a51bfb2
FD
352 md_maps = borrow_metadata_maps_from_input_event_class(ir_maps,
353 in_event_class);
58f6d595
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{
363 BT_ASSERT(d_maps);
364 BT_ASSERT(in_packet);
365
9a51bfb2 366 return g_hash_table_lookup(d_maps->packet_map, (gpointer) in_packet);
58f6d595
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;
58f6d595 375 const bt_stream *in_stream;
bb88d099 376 const bt_trace *in_trace;
58f6d595
FD
377 bt_packet *out_packet;
378 bt_stream *out_stream;
379
475e740e 380 BT_COMP_LOGD("Creating new mapped packet: in-p-addr=%p", in_packet);
58f6d595
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
bb88d099 386 /* There should never be a mapped packet already. */
58f6d595 387 BT_ASSERT(!borrow_mapped_packet(d_maps, in_packet));
58f6d595
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) {
475e740e 397 BT_COMP_LOGE_STR("Error create output packet");
58f6d595
FD
398 goto end;
399 }
400
bb88d099 401 /* Copy the content over to the output packet. */
475e740e
PP
402 copy_packet_content(in_packet, out_packet, ir_maps->log_level,
403 ir_maps->self_comp);
58f6d595 404
bb88d099 405 /* Add it to the mapping. */
58f6d595 406 g_hash_table_insert(d_maps->packet_map,
9a51bfb2 407 (gpointer) in_packet, out_packet);
58f6d595 408
475e740e 409 BT_COMP_LOGD("Created new mapped packet: in-p-addr=%p, out-p-addr=%p",
9a51bfb2 410 in_packet, out_packet);
58f6d595
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;
421 BT_ASSERT(ir_maps);
422 BT_ASSERT(in_packet);
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{
bb88d099 433 struct trace_ir_data_maps *d_maps;
58f6d595
FD
434 gboolean ret;
435
58f6d595
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{
58f6d595 450 struct trace_ir_data_maps *d_maps;
bb88d099 451 gboolean ret;
58f6d595
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,
9a51bfb2 471 (gpointer) in_trace_class);
58f6d595
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{
0fb60f5d 490 bt_trace_add_listener_status add_listener_status;
bb88d099 491 struct trace_ir_data_maps *d_maps = g_new0(struct trace_ir_data_maps, 1);
0fb60f5d 492
58f6d595 493 if (!d_maps) {
475e740e 494 BT_COMP_LOGE_STR("Error allocating trace_ir_maps");
58f6d595
FD
495 goto error;
496 }
497
a07aba4f 498 d_maps->log_level = ir_maps->log_level;
475e740e 499 d_maps->self_comp = ir_maps->self_comp;
58f6d595
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,
bb88d099 504 g_direct_equal, NULL,(GDestroyNotify) bt_stream_put_ref);
58f6d595 505 d_maps->packet_map = g_hash_table_new_full(g_direct_hash,
bb88d099 506 g_direct_equal, NULL,(GDestroyNotify) bt_packet_put_ref);
58f6d595 507
0fb60f5d
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
58f6d595
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{
bb88d099 521 bt_trace_class_add_listener_status add_listener_status;
58f6d595
FD
522 struct trace_ir_metadata_maps *md_maps =
523 g_new0(struct trace_ir_metadata_maps, 1);
0fb60f5d 524
58f6d595 525 if (!md_maps) {
475e740e 526 BT_COMP_LOGE_STR("Error allocating trace_ir_maps");
58f6d595
FD
527 goto error;
528 }
529
a07aba4f 530 md_maps->log_level = ir_maps->log_level;
475e740e 531 md_maps->self_comp = ir_maps->self_comp;
58f6d595
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) {
475e740e 541 BT_COMP_LOGE_STR("Error allocating field_class_resolving_context");
58f6d595
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,
9a51bfb2 547 g_direct_equal, NULL, (GDestroyNotify) bt_stream_class_put_ref);
58f6d595 548 md_maps->event_class_map = g_hash_table_new_full(g_direct_hash,
9a51bfb2 549 g_direct_equal, NULL, (GDestroyNotify) bt_event_class_put_ref);
58f6d595 550 md_maps->field_class_map = g_hash_table_new_full(g_direct_hash,
9a51bfb2 551 g_direct_equal, NULL, (GDestroyNotify) bt_field_class_put_ref);
58f6d595 552 md_maps->clock_class_map = g_hash_table_new_full(g_direct_hash,
9a51bfb2 553 g_direct_equal, NULL, (GDestroyNotify) bt_clock_class_put_ref);
58f6d595 554
0fb60f5d 555 add_listener_status = bt_trace_class_add_destruction_listener(
9a51bfb2
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);
0fb60f5d 559
58f6d595
FD
560error:
561 return md_maps;
562}
563
564BT_HIDDEN
565void trace_ir_data_maps_destroy(struct trace_ir_data_maps *maps)
566{
fb25b9e3
PP
567 bt_trace_remove_listener_status status;
568
58f6d595
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,
fb25b9e3
PP
586 maps->destruction_listener_id);
587 if (status != BT_TRACE_REMOVE_LISTENER_STATUS_OK) {
475e740e
PP
588 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, maps->log_level,
589 maps->self_comp,
a07aba4f 590 "Trace destruction listener removal failed.");
0fb60f5d 591 bt_current_thread_clear_error();
c5ec1c0b 592 }
58f6d595
FD
593
594 g_free(maps);
595}
596
597BT_HIDDEN
598void trace_ir_metadata_maps_destroy(struct trace_ir_metadata_maps *maps)
599{
fb25b9e3
PP
600 bt_trace_class_remove_listener_status status;
601
58f6d595
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
be1b2e7e 622 g_free(maps->fc_resolving_ctx);
58f6d595
FD
623
624 if (maps->output_trace_class) {
625 bt_trace_class_put_ref(maps->output_trace_class);
626 }
627
fb25b9e3 628 status = bt_trace_class_remove_destruction_listener(
9a51bfb2 629 maps->input_trace_class, maps->destruction_listener_id);
fb25b9e3 630 if (status != BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK) {
475e740e
PP
631 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, maps->log_level,
632 maps->self_comp,
a07aba4f 633 "Trace destruction listener removal failed.");
0fb60f5d 634 bt_current_thread_clear_error();
c5ec1c0b 635 }
58f6d595
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
be1b2e7e 658 g_free(maps->debug_info_field_class_name);
58f6d595
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,
a07aba4f 675 const char *debug_info_field_name, bt_logging_level log_level)
58f6d595 676{
bb88d099 677 struct trace_ir_maps *ir_maps = g_new0(struct trace_ir_maps, 1);
a07aba4f 678 if (!ir_maps) {
475e740e 679 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
a07aba4f 680 "Error allocating trace_ir_maps");
58f6d595
FD
681 goto error;
682 }
683
a07aba4f 684 ir_maps->log_level = log_level;
475e740e 685 ir_maps->self_comp = self_comp;
a07aba4f 686
58f6d595 687 /* Copy debug info field name received from the user. */
bb88d099 688 ir_maps->debug_info_field_class_name = g_strdup(debug_info_field_name);
a07aba4f 689 if (!ir_maps->debug_info_field_class_name) {
475e740e 690 BT_COMP_LOGE_STR("Cannot copy debug info field name");
58f6d595
FD
691 goto error;
692 }
693
a07aba4f 694 ir_maps->self_comp = self_comp;
58f6d595 695
a07aba4f 696 ir_maps->data_maps = g_hash_table_new_full(g_direct_hash,
9a51bfb2
FD
697 g_direct_equal, (GDestroyNotify) NULL,
698 (GDestroyNotify) trace_ir_data_maps_destroy);
58f6d595 699
a07aba4f 700 ir_maps->metadata_maps = g_hash_table_new_full(g_direct_hash,
9a51bfb2
FD
701 g_direct_equal, (GDestroyNotify) NULL,
702 (GDestroyNotify) trace_ir_metadata_maps_destroy);
58f6d595
FD
703
704 goto end;
705error:
a07aba4f
PP
706 trace_ir_maps_destroy(ir_maps);
707 ir_maps = NULL;
58f6d595 708end:
a07aba4f 709 return ir_maps;
58f6d595 710}
This page took 0.069026 seconds and 4 git commands to generate.