2 * Babeltrace - Trace IR metadata object copy
4 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #define BT_COMP_LOG_SELF_COMP self_comp
28 #define BT_LOG_OUTPUT_LEVEL log_level
29 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-META-COPY"
30 #include "logging/comp-logging.h"
35 #include "common/assert.h"
37 #include "trace-ir-metadata-copy.h"
38 #include "trace-ir-metadata-field-class-copy.h"
42 int copy_trace_class_content(struct trace_ir_maps
*ir_maps
,
43 const bt_trace_class
*in_trace_class
, bt_trace_class
*out_trace_class
,
44 bt_logging_level log_level
, bt_self_component
*self_comp
)
46 uint64_t sc_number
, sc_idx
;
47 BT_COMP_LOGD("Copying content of trace class: in-tc-addr=%p, out-tc-addr=%p",
48 in_trace_class
, out_trace_class
);
51 * Safe to use the same value object because it's frozen at this
54 bt_trace_class_set_user_attributes(out_trace_class
,
55 bt_trace_class_borrow_user_attributes_const(in_trace_class
));
57 /* Use the same stream class ids as in the origin trace class. */
58 bt_trace_class_set_assigns_automatic_stream_class_id(out_trace_class
,
61 /* Copy stream classes contained in the trace class. */
62 sc_number
= bt_trace_class_get_stream_class_count(in_trace_class
);
63 for (sc_idx
= 0; sc_idx
< sc_number
; sc_idx
++) {
64 bt_stream_class
*out_stream_class
;
65 const bt_stream_class
*in_stream_class
=
66 bt_trace_class_borrow_stream_class_by_index_const(
67 in_trace_class
, sc_idx
);
69 out_stream_class
= trace_ir_mapping_borrow_mapped_stream_class(
70 ir_maps
, in_stream_class
);
71 if (!out_stream_class
) {
73 * We don't need the new stream_class yet. We simply
74 * want to create it and keep it in the map.
76 (void) trace_ir_mapping_create_new_mapped_stream_class(
77 ir_maps
, in_stream_class
);
81 BT_COMP_LOGD("Copied content of trace class: in-tc-addr=%p, out-tc-addr=%p",
82 in_trace_class
, out_trace_class
);
87 int copy_clock_class_content(const bt_clock_class
*in_clock_class
,
88 bt_clock_class
*out_clock_class
, bt_logging_level log_level
,
89 bt_self_component
*self_comp
)
91 const char *clock_class_name
, *clock_class_description
;
97 BT_COMP_LOGD("Copying content of clock class: in-cc-addr=%p, out-cc-addr=%p",
98 in_clock_class
, out_clock_class
);
100 clock_class_name
= bt_clock_class_get_name(in_clock_class
);
102 if (clock_class_name
) {
103 if (bt_clock_class_set_name(out_clock_class
, clock_class_name
)
104 != BT_CLOCK_CLASS_SET_NAME_STATUS_OK
) {
105 BT_COMP_LOGE("Error setting clock class' name cc-addr=%p, name=%p",
106 out_clock_class
, clock_class_name
);
107 out_clock_class
= NULL
;
114 * Safe to use the same value object because it's frozen at this
117 bt_clock_class_set_user_attributes(out_clock_class
,
118 bt_clock_class_borrow_user_attributes_const(in_clock_class
));
120 clock_class_description
= bt_clock_class_get_description(in_clock_class
);
122 if (clock_class_description
) {
123 if (bt_clock_class_set_description(out_clock_class
,
124 clock_class_description
) !=
125 BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_OK
) {
126 BT_COMP_LOGE("Error setting clock class' description cc-addr=%p, "
127 "name=%p", out_clock_class
, clock_class_description
);
128 out_clock_class
= NULL
;
134 in_uuid
= bt_clock_class_get_uuid(in_clock_class
);
136 bt_clock_class_set_uuid(out_clock_class
, in_uuid
);
139 bt_clock_class_set_frequency(out_clock_class
,
140 bt_clock_class_get_frequency(in_clock_class
));
141 bt_clock_class_set_precision(out_clock_class
,
142 bt_clock_class_get_precision(in_clock_class
));
143 bt_clock_class_get_offset(in_clock_class
, &seconds
, &cycles
);
144 bt_clock_class_set_offset(out_clock_class
, seconds
, cycles
);
145 bt_clock_class_set_origin_is_unix_epoch(out_clock_class
,
146 bt_clock_class_origin_is_unix_epoch(in_clock_class
));
148 BT_COMP_LOGD("Copied content of clock class: in-cc-addr=%p, out-cc-addr=%p",
149 in_clock_class
, out_clock_class
);
156 bt_clock_class
*borrow_mapped_clock_class(
157 struct trace_ir_metadata_maps
*md_maps
,
158 const bt_clock_class
*in_clock_class
)
160 BT_ASSERT_DBG(md_maps
);
161 BT_ASSERT_DBG(in_clock_class
);
163 return g_hash_table_lookup(md_maps
->clock_class_map
,
164 (gpointer
) in_clock_class
);
168 bt_clock_class
*create_new_mapped_clock_class(bt_self_component
*self_comp
,
169 struct trace_ir_metadata_maps
*md_maps
,
170 const bt_clock_class
*in_clock_class
)
172 bt_clock_class
*out_clock_class
;
173 bt_logging_level log_level
= md_maps
->log_level
;
176 BT_COMP_LOGD("Creating new mapped clock class: in-cc-addr=%p",
180 BT_ASSERT(in_clock_class
);
182 BT_ASSERT(!borrow_mapped_clock_class(md_maps
, in_clock_class
));
184 out_clock_class
= bt_clock_class_create(self_comp
);
185 if (!out_clock_class
) {
186 BT_COMP_LOGE_STR("Cannot create clock class");
189 /* If not, create a new one and add it to the mapping. */
190 ret
= copy_clock_class_content(in_clock_class
, out_clock_class
,
191 log_level
, self_comp
);
193 BT_COMP_LOGE_STR("Cannot copy clock class");
197 g_hash_table_insert(md_maps
->clock_class_map
,
198 (gpointer
) in_clock_class
, out_clock_class
);
200 BT_COMP_LOGD("Created new mapped clock class: in-cc-addr=%p, out-cc-addr=%p",
201 in_clock_class
, out_clock_class
);
203 return out_clock_class
;
207 int copy_stream_class_content(struct trace_ir_maps
*ir_maps
,
208 const bt_stream_class
*in_stream_class
,
209 bt_stream_class
*out_stream_class
)
211 struct trace_ir_metadata_maps
*md_maps
;
212 const bt_clock_class
*in_clock_class
;
213 bt_clock_class
*out_clock_class
;
214 const bt_field_class
*in_packet_context_fc
, *in_common_context_fc
;
215 bt_field_class
*out_packet_context_fc
, *out_common_context_fc
;
217 uint64_t ec_number
, ec_idx
;
218 bt_logging_level log_level
= ir_maps
->log_level
;
219 bt_self_component
*self_comp
= ir_maps
->self_comp
;
222 BT_COMP_LOGD("Copying content of stream class: in-sc-addr=%p, out-sc-addr=%p",
223 in_stream_class
, out_stream_class
);
225 md_maps
= borrow_metadata_maps_from_input_stream_class(ir_maps
, in_stream_class
);
226 in_clock_class
= bt_stream_class_borrow_default_clock_class_const(
229 if (in_clock_class
) {
230 /* Copy the clock class. */
231 out_clock_class
= borrow_mapped_clock_class(md_maps
,
233 if (!out_clock_class
) {
234 out_clock_class
= create_new_mapped_clock_class(
235 ir_maps
->self_comp
, md_maps
, in_clock_class
);
237 bt_stream_class_set_default_clock_class(out_stream_class
,
242 * Safe to use the same value object because it's frozen at this
245 bt_stream_class_set_user_attributes(out_stream_class
,
246 bt_stream_class_borrow_user_attributes_const(in_stream_class
));
248 bt_stream_class_set_supports_packets(
250 bt_stream_class_supports_packets(in_stream_class
),
251 bt_stream_class_packets_have_beginning_default_clock_snapshot(
253 bt_stream_class_packets_have_end_default_clock_snapshot(
255 bt_stream_class_set_supports_discarded_events(
257 bt_stream_class_supports_discarded_events(in_stream_class
),
258 bt_stream_class_discarded_events_have_default_clock_snapshots(
260 bt_stream_class_set_supports_discarded_packets(
262 bt_stream_class_supports_discarded_packets(in_stream_class
),
263 bt_stream_class_discarded_packets_have_default_clock_snapshots(
266 in_name
= bt_stream_class_get_name(in_stream_class
);
268 if (bt_stream_class_set_name(out_stream_class
, in_name
) !=
269 BT_STREAM_CLASS_SET_NAME_STATUS_OK
) {
270 BT_COMP_LOGE("Error set stream class name: out-sc-addr=%p, "
271 "name=%s", out_stream_class
, in_name
);
277 bt_stream_class_set_assigns_automatic_stream_id(out_stream_class
,
279 bt_stream_class_set_assigns_automatic_event_class_id(out_stream_class
,
283 * Add the input packet context field class to the context to
284 * resolution in the further steps.
286 in_packet_context_fc
=
287 bt_stream_class_borrow_packet_context_field_class_const(
289 md_maps
->fc_resolving_ctx
->packet_context
=
290 in_packet_context_fc
;
292 if (in_packet_context_fc
) {
293 /* Copy packet context. */
294 out_packet_context_fc
= create_field_class_copy(md_maps
,
295 in_packet_context_fc
);
297 ret
= copy_field_class_content(md_maps
, in_packet_context_fc
,
298 out_packet_context_fc
);
304 if (bt_stream_class_set_packet_context_field_class(
305 out_stream_class
, out_packet_context_fc
) !=
306 BT_STREAM_CLASS_SET_FIELD_CLASS_STATUS_OK
) {
307 BT_COMP_LOGE("Error setting stream class' packet context "
308 "field class: sc-addr=%p, packet-fc-addr=%p",
309 out_stream_class
, out_packet_context_fc
);
316 * Add the input common context field class to the context to
317 * resolution in the further steps.
319 in_common_context_fc
=
320 bt_stream_class_borrow_event_common_context_field_class_const(
322 md_maps
->fc_resolving_ctx
->event_common_context
= in_common_context_fc
;
324 if (in_common_context_fc
) {
325 /* Copy common context. */
326 /* TODO: I find it a bit awkward to have this special function
327 * here to add the debug-info field class. I would like to
329 out_common_context_fc
= create_field_class_copy(md_maps
,
330 in_common_context_fc
);
332 ret
= copy_event_common_context_field_class_content(md_maps
,
333 ir_maps
->debug_info_field_class_name
,
334 in_common_context_fc
, out_common_context_fc
);
339 if (bt_stream_class_set_event_common_context_field_class(
340 out_stream_class
, out_common_context_fc
) !=
341 BT_STREAM_CLASS_SET_FIELD_CLASS_STATUS_OK
) {
342 BT_COMP_LOGE("Error setting stream class' packet context "
343 "field class: sc-addr=%p, packet-fc-addr=%p",
344 out_stream_class
, out_common_context_fc
);
350 /* Copy event classes contained in the stream class. */
351 ec_number
= bt_stream_class_get_event_class_count(in_stream_class
);
352 for (ec_idx
= 0; ec_idx
< ec_number
; ec_idx
++) {
353 bt_event_class
*out_event_class
;
354 const bt_event_class
*in_event_class
=
355 bt_stream_class_borrow_event_class_by_id_const(
356 in_stream_class
, ec_idx
);
357 out_event_class
= trace_ir_mapping_borrow_mapped_event_class(
358 ir_maps
, in_event_class
);
359 if (!out_event_class
) {
361 * We don't need the new event_class yet. We simply
362 * want to create it and keep it in the map.
364 (void) trace_ir_mapping_create_new_mapped_event_class(
365 ir_maps
, in_event_class
);
369 BT_COMP_LOGD("Copied content of stream class: in-sc-addr=%p, out-sc-addr=%p",
370 in_stream_class
, out_stream_class
);
376 int copy_event_class_content(struct trace_ir_maps
*ir_maps
,
377 const bt_event_class
*in_event_class
,
378 bt_event_class
*out_event_class
)
380 struct trace_ir_metadata_maps
*md_maps
;
381 const char *in_event_class_name
, *in_emf_uri
;
382 bt_property_availability prop_avail
;
383 bt_event_class_log_level ec_log_level
;
384 bt_field_class
*out_specific_context_fc
, *out_payload_fc
;
385 const bt_field_class
*in_event_specific_context
, *in_event_payload
;
387 bt_logging_level log_level
= ir_maps
->log_level
;
388 bt_self_component
*self_comp
= ir_maps
->self_comp
;
390 BT_COMP_LOGD("Copying content of event class: in-ec-addr=%p, out-ec-addr=%p",
391 in_event_class
, out_event_class
);
393 /* Copy event class name. */
394 in_event_class_name
= bt_event_class_get_name(in_event_class
);
395 if (in_event_class_name
) {
396 if (bt_event_class_set_name(out_event_class
,
397 in_event_class_name
) !=
398 BT_EVENT_CLASS_SET_NAME_STATUS_OK
) {
399 BT_COMP_LOGE("Error setting event class' name: ec-addr=%p, "
400 "name=%s", out_event_class
, in_event_class_name
);
407 * Safe to use the same value object because it's frozen at this
410 bt_event_class_set_user_attributes(out_event_class
,
411 bt_event_class_borrow_user_attributes_const(in_event_class
));
413 /* Copy event class loglevel. */
414 prop_avail
= bt_event_class_get_log_level(in_event_class
,
416 if (prop_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
417 bt_event_class_set_log_level(out_event_class
, ec_log_level
);
420 /* Copy event class emf uri. */
421 in_emf_uri
= bt_event_class_get_emf_uri(in_event_class
);
423 if (bt_event_class_set_emf_uri(out_event_class
, in_emf_uri
) !=
424 BT_EVENT_CLASS_SET_EMF_URI_STATUS_OK
) {
425 BT_COMP_LOGE("Error setting event class' emf uri: ec-addr=%p, "
426 "emf uri=%s", out_event_class
, in_emf_uri
);
432 md_maps
= borrow_metadata_maps_from_input_event_class(ir_maps
,
435 * Add the input event class' specific ctx to te
438 in_event_specific_context
=
439 bt_event_class_borrow_specific_context_field_class_const(
442 md_maps
->fc_resolving_ctx
->event_specific_context
=
443 in_event_specific_context
;
445 if (in_event_specific_context
) {
446 /* Copy the specific context of this event class. */
447 out_specific_context_fc
= create_field_class_copy(md_maps
,
448 in_event_specific_context
);
450 ret
= copy_field_class_content(md_maps
,
451 in_event_specific_context
, out_specific_context_fc
);
456 * Add the output specific context to the output event
459 if (bt_event_class_set_specific_context_field_class(
460 out_event_class
, out_specific_context_fc
) !=
461 BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK
) {
462 BT_COMP_LOGE("Error setting event class' specific context "
463 "field class: ec-addr=%p, ctx-fc-addr=%p",
464 out_event_class
, out_specific_context_fc
);
471 * Add the input event class' payload field class to
474 in_event_payload
= bt_event_class_borrow_payload_field_class_const(
477 md_maps
->fc_resolving_ctx
->event_payload
= in_event_payload
;
479 if (in_event_payload
) {
480 /* Copy the payload of this event class. */
481 out_payload_fc
= create_field_class_copy(md_maps
,
483 ret
= copy_field_class_content(md_maps
, in_event_payload
,
489 /* Add the output payload to the output event class. */
490 if (bt_event_class_set_payload_field_class(out_event_class
,
491 out_payload_fc
) != BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK
) {
492 BT_COMP_LOGE("Error setting event class' payload "
493 "field class: ec-addr=%p, payload-fc-addr=%p",
494 out_event_class
, out_payload_fc
);
500 BT_COMP_LOGD("Copied content of event class: in-ec-addr=%p, out-ec-addr=%p",
501 in_event_class
, out_event_class
);
507 int copy_event_common_context_field_class_content(
508 struct trace_ir_metadata_maps
*md_maps
,
509 const char *debug_info_fc_name
,
510 const bt_field_class
*in_field_class
,
511 bt_field_class
*out_field_class
)
513 bt_field_class
*debug_field_class
= NULL
, *bin_field_class
= NULL
,
514 *func_field_class
= NULL
, *src_field_class
= NULL
;
515 bt_logging_level log_level
= md_maps
->log_level
;
516 bt_self_component
*self_comp
= md_maps
->self_comp
;
519 BT_COMP_LOGD("Copying content of event common context field class: "
520 "in-fc-addr=%p, out-fc-addr=%p", in_field_class
, out_field_class
);
522 /* Copy the content of the input common context. */
523 ret
= copy_field_class_content(md_maps
, in_field_class
, out_field_class
);
529 * If this event common context has the necessary fields to compute the
530 * debug information append the debug-info field class to the event
533 if (is_event_common_ctx_dbg_info_compatible(in_field_class
, debug_info_fc_name
)) {
535 * The struct field and 3 sub-fields are not stored in the
536 * field class map because they don't have input equivalent.
537 * We need to put our reference each of these field classes
538 * once they are added to their respective containing field
541 debug_field_class
= bt_field_class_structure_create(
542 md_maps
->output_trace_class
);
543 if (!debug_field_class
) {
544 BT_COMP_LOGE_STR("Failed to create debug_info structure.");
549 bin_field_class
= bt_field_class_string_create(
550 md_maps
->output_trace_class
);
551 if (!bin_field_class
) {
552 BT_COMP_LOGE_STR("Failed to create string for field=bin.");
557 func_field_class
= bt_field_class_string_create(
558 md_maps
->output_trace_class
);
559 if (!func_field_class
) {
560 BT_COMP_LOGE_STR("Failed to create string for field=func.");
565 src_field_class
= bt_field_class_string_create(
566 md_maps
->output_trace_class
);
567 if (!src_field_class
) {
568 BT_COMP_LOGE_STR("Failed to create string for field=src.");
573 if (bt_field_class_structure_append_member(debug_field_class
,
574 "bin", bin_field_class
) !=
575 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK
) {
576 BT_COMP_LOGE_STR("Failed to add a field to debug_info "
577 "struct: field=bin.");
581 BT_FIELD_CLASS_PUT_REF_AND_RESET(bin_field_class
);
583 if (bt_field_class_structure_append_member(debug_field_class
,
584 "func", func_field_class
) !=
585 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK
) {
586 BT_COMP_LOGE_STR("Failed to add a field to debug_info "
587 "struct: field=func.");
591 BT_FIELD_CLASS_PUT_REF_AND_RESET(func_field_class
);
593 if (bt_field_class_structure_append_member(debug_field_class
,
594 "src", src_field_class
) !=
595 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK
) {
596 BT_COMP_LOGE_STR("Failed to add a field to debug_info "
597 "struct: field=src.");
601 BT_FIELD_CLASS_PUT_REF_AND_RESET(src_field_class
);
603 /*Add the filled debug-info field class to the common context. */
604 if (bt_field_class_structure_append_member(out_field_class
,
605 debug_info_fc_name
, debug_field_class
) !=
606 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK
) {
607 BT_COMP_LOGE_STR("Failed to add debug_info field to "
608 "event common context.");
612 BT_FIELD_CLASS_PUT_REF_AND_RESET(debug_field_class
);
614 BT_COMP_LOGD("Copied content of event common context field class: "
615 "in-fc-addr=%p, out-fc-addr=%p", in_field_class
, out_field_class
);
619 if (debug_field_class
) {
620 bt_field_class_put_ref(debug_field_class
);
622 if (bin_field_class
) {
623 bt_field_class_put_ref(bin_field_class
);
625 if (func_field_class
) {
626 bt_field_class_put_ref(func_field_class
);
628 if (src_field_class
) {
629 bt_field_class_put_ref(src_field_class
);
636 bt_field_class
*create_field_class_copy(struct trace_ir_metadata_maps
*md_maps
,
637 const bt_field_class
*in_field_class
)
639 return create_field_class_copy_internal(md_maps
, in_field_class
);
643 int copy_field_class_content(struct trace_ir_metadata_maps
*md_maps
,
644 const bt_field_class
*in_field_class
,
645 bt_field_class
*out_field_class
)
647 return copy_field_class_content_internal(md_maps
, in_field_class
,