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