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