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_LOG_TAG "PLUGIN-LTTNG-UTILS-DEBUG-INFO-TRACE-IR-METADATA-COPY"
33 #include <babeltrace/assert-internal.h>
35 #include "trace-ir-metadata-copy.h"
36 #include "trace-ir-metadata-field-class-copy.h"
40 int copy_trace_class_content(const bt_trace_class
*in_trace_class
,
41 bt_trace_class
*out_trace_class
)
44 uint64_t i
, env_field_count
;
45 const char *in_trace_class_name
;
47 BT_LOGD("Copying content of trace class: in-tc-addr=%p, out-tc-addr=%p",
48 in_trace_class
, out_trace_class
);
50 /* Use the same stream class ids as in the origin trace class. */
51 bt_trace_class_set_assigns_automatic_stream_class_id(out_trace_class
,
54 in_trace_class_name
= bt_trace_class_get_name(in_trace_class
);
55 if (in_trace_class_name
) {
56 bt_trace_class_set_name(out_trace_class
, in_trace_class_name
);
60 * Do not copy the trace class UUID as it may be modified and should no
61 * longer have the same UUID.
65 * Go over all the entries in the environment section of the trace class
66 * and copy the content to the new trace class.
68 env_field_count
= bt_trace_class_get_environment_entry_count(in_trace_class
);
69 for (i
= 0; i
< env_field_count
; i
++) {
70 const char *value_name
;
71 const bt_value
*value
= NULL
;
72 bt_trace_class_status trace_class_status
;
74 bt_trace_class_borrow_environment_entry_by_index_const(
75 in_trace_class
, i
, &value_name
, &value
);
77 BT_LOGD("Copying trace class environnement entry: "
78 "index=%" PRId64
", value-addr=%p, value-name=%s",
79 i
, value
, value_name
);
81 BT_ASSERT(value_name
);
84 if (bt_value_is_signed_integer(value
)) {
86 bt_trace_class_set_environment_entry_integer(
87 out_trace_class
, value_name
,
88 bt_value_signed_integer_get(
90 } else if (bt_value_is_string(value
)) {
92 bt_trace_class_set_environment_entry_string(
93 out_trace_class
, value_name
,
94 bt_value_string_get(value
));
99 if (trace_class_status
!= BT_TRACE_CLASS_STATUS_OK
) {
105 BT_LOGD("Copied content of trace class: in-tc-addr=%p, out-tc-addr=%p",
106 in_trace_class
, out_trace_class
);
112 int copy_clock_class_content(const bt_clock_class
*in_clock_class
,
113 bt_clock_class
*out_clock_class
)
115 bt_clock_class_status status
;
116 const char *clock_class_name
, *clock_class_description
;
122 BT_LOGD("Copying content of clock class: in-cc-addr=%p, out-cc-addr=%p",
123 in_clock_class
, out_clock_class
);
125 clock_class_name
= bt_clock_class_get_name(in_clock_class
);
127 if (clock_class_name
) {
128 status
= bt_clock_class_set_name(out_clock_class
, clock_class_name
);
129 if (status
!= BT_CLOCK_CLASS_STATUS_OK
) {
130 BT_LOGE("Error setting clock class' name cc-addr=%p, name=%p",
131 out_clock_class
, clock_class_name
);
132 out_clock_class
= NULL
;
138 clock_class_description
= bt_clock_class_get_description(in_clock_class
);
140 if (clock_class_description
) {
141 status
= bt_clock_class_set_description(out_clock_class
,
142 clock_class_description
);
143 if (status
!= BT_CLOCK_CLASS_STATUS_OK
) {
144 BT_LOGE("Error setting clock class' description cc-addr=%p, "
145 "name=%p", out_clock_class
, clock_class_description
);
146 out_clock_class
= NULL
;
152 in_uuid
= bt_clock_class_get_uuid(in_clock_class
);
154 bt_clock_class_set_uuid(out_clock_class
, in_uuid
);
157 bt_clock_class_set_frequency(out_clock_class
,
158 bt_clock_class_get_frequency(in_clock_class
));
159 bt_clock_class_set_precision(out_clock_class
,
160 bt_clock_class_get_precision(in_clock_class
));
161 bt_clock_class_get_offset(in_clock_class
, &seconds
, &cycles
);
162 bt_clock_class_set_offset(out_clock_class
, seconds
, cycles
);
163 bt_clock_class_set_origin_is_unix_epoch(out_clock_class
,
164 bt_clock_class_origin_is_unix_epoch(in_clock_class
));
166 BT_LOGD("Copied content of clock class: in-cc-addr=%p, out-cc-addr=%p",
167 in_clock_class
, out_clock_class
);
174 bt_clock_class
*borrow_mapped_clock_class(
175 struct trace_ir_metadata_maps
*md_maps
,
176 const bt_clock_class
*in_clock_class
)
179 BT_ASSERT(in_clock_class
);
181 return g_hash_table_lookup(md_maps
->clock_class_map
,
182 (gpointer
) in_clock_class
);
186 bt_clock_class
*create_new_mapped_clock_class(
187 bt_self_component
*self_comp
,
188 struct trace_ir_metadata_maps
*md_maps
,
189 const bt_clock_class
*in_clock_class
)
191 bt_clock_class
*out_clock_class
;
194 BT_LOGD("Creating new mapped clock class: in-cc-addr=%p",
198 BT_ASSERT(in_clock_class
);
200 BT_ASSERT(!borrow_mapped_clock_class(md_maps
, in_clock_class
));
202 out_clock_class
= bt_clock_class_create(self_comp
);
203 if (!out_clock_class
) {
204 BT_LOGE_STR("Cannot create clock class");
207 /* If not, create a new one and add it to the mapping. */
208 ret
= copy_clock_class_content(in_clock_class
, out_clock_class
);
210 BT_LOGE_STR("Cannot copy clock class");
214 g_hash_table_insert(md_maps
->clock_class_map
,
215 (gpointer
) in_clock_class
, out_clock_class
);
217 BT_LOGD("Created new mapped clock class: in-cc-addr=%p, out-cc-addr=%p",
218 in_clock_class
, out_clock_class
);
220 return out_clock_class
;
224 int copy_stream_class_content(struct trace_ir_maps
*ir_maps
,
225 const bt_stream_class
*in_stream_class
,
226 bt_stream_class
*out_stream_class
)
228 struct trace_ir_metadata_maps
*md_maps
;
229 const bt_clock_class
*in_clock_class
;
230 bt_clock_class
*out_clock_class
;
231 const bt_field_class
*in_packet_context_fc
, *in_common_context_fc
;
232 bt_field_class
*out_packet_context_fc
, *out_common_context_fc
;
233 bt_stream_class_status status
;
237 BT_LOGD("Copying content of stream class: in-sc-addr=%p, out-sc-addr=%p",
238 in_stream_class
, out_stream_class
);
240 md_maps
= borrow_metadata_maps_from_input_stream_class(ir_maps
, in_stream_class
);
241 in_clock_class
= bt_stream_class_borrow_default_clock_class_const(
244 if (in_clock_class
) {
245 /* Copy the clock class. */
247 borrow_mapped_clock_class(md_maps
, in_clock_class
);
248 if (!out_clock_class
) {
249 out_clock_class
= create_new_mapped_clock_class(
250 ir_maps
->self_comp
, md_maps
,
253 bt_stream_class_set_default_clock_class(out_stream_class
,
258 bt_stream_class_set_packets_have_beginning_default_clock_snapshot(
260 bt_stream_class_packets_have_beginning_default_clock_snapshot(
262 bt_stream_class_set_packets_have_end_default_clock_snapshot(
264 bt_stream_class_packets_have_end_default_clock_snapshot(
266 bt_stream_class_set_supports_discarded_events(
268 bt_stream_class_supports_discarded_events(in_stream_class
),
269 bt_stream_class_discarded_events_have_default_clock_snapshots(
271 bt_stream_class_set_supports_discarded_packets(
273 bt_stream_class_supports_discarded_packets(in_stream_class
),
274 bt_stream_class_discarded_packets_have_default_clock_snapshots(
277 in_name
= bt_stream_class_get_name(in_stream_class
);
279 status
= bt_stream_class_set_name(out_stream_class
, in_name
);
280 if (status
!= BT_STREAM_CLASS_STATUS_OK
) {
281 BT_LOGE("Error set stream class name: out-sc-addr=%p, "
282 "name=%s", out_stream_class
, in_name
);
288 bt_stream_class_set_assigns_automatic_stream_id(out_stream_class
,
290 bt_stream_class_set_assigns_automatic_event_class_id(out_stream_class
,
294 * Add the input packet context field class to the context to
295 * resolution in the further steps.
297 in_packet_context_fc
=
298 bt_stream_class_borrow_packet_context_field_class_const(
300 md_maps
->fc_resolving_ctx
->packet_context
=
301 in_packet_context_fc
;
303 if (in_packet_context_fc
) {
304 /* Copy packet context. */
305 out_packet_context_fc
= create_field_class_copy(
306 md_maps
, in_packet_context_fc
);
308 ret
= copy_field_class_content(md_maps
,
309 in_packet_context_fc
, out_packet_context_fc
);
315 status
= bt_stream_class_set_packet_context_field_class(
316 out_stream_class
, out_packet_context_fc
);
317 if (status
!= BT_STREAM_CLASS_STATUS_OK
) {
318 BT_LOGE("Error setting stream class' packet context "
319 "field class: sc-addr=%p, packet-fc-addr=%p",
320 out_stream_class
, out_packet_context_fc
);
327 * Add the input common context field class to the context to
328 * resolution in the further steps.
330 in_common_context_fc
=
331 bt_stream_class_borrow_event_common_context_field_class_const(
333 md_maps
->fc_resolving_ctx
->event_common_context
=
334 in_common_context_fc
;
336 if (in_common_context_fc
) {
337 /* Copy common context. */
338 /* TODO: I find it a bit awkward to have this special function
339 * here to add the debug-info field class. I would like to
341 out_common_context_fc
= create_field_class_copy(
342 md_maps
, in_common_context_fc
);
344 ret
= copy_event_common_context_field_class_content(
345 md_maps
, ir_maps
->debug_info_field_class_name
,
346 in_common_context_fc
, out_common_context_fc
);
351 status
= bt_stream_class_set_event_common_context_field_class(
352 out_stream_class
, out_common_context_fc
);
353 if (status
!= BT_STREAM_CLASS_STATUS_OK
) {
354 BT_LOGE("Error setting stream class' packet context "
355 "field class: sc-addr=%p, packet-fc-addr=%p",
356 out_stream_class
, out_common_context_fc
);
362 /* Set packet snapshot boolean fields. */
363 BT_LOGD("Copied content of stream class: in-sc-addr=%p, out-sc-addr=%p",
364 in_stream_class
, out_stream_class
);
370 int copy_event_class_content(struct trace_ir_maps
*ir_maps
,
371 const bt_event_class
*in_event_class
,
372 bt_event_class
*out_event_class
)
374 struct trace_ir_metadata_maps
*md_maps
;
375 const char *in_event_class_name
, *in_emf_uri
;
376 bt_property_availability prop_avail
;
377 bt_event_class_log_level log_level
;
378 bt_event_class_status status
;
379 bt_field_class
*out_specific_context_fc
, *out_payload_fc
;
380 const bt_field_class
*in_event_specific_context
, *in_event_payload
;
383 BT_LOGD("Copying content of event class: in-ec-addr=%p, out-ec-addr=%p",
384 in_event_class
, out_event_class
);
386 /* Copy event class name. */
387 in_event_class_name
= bt_event_class_get_name(in_event_class
);
388 if (in_event_class_name
) {
389 status
= bt_event_class_set_name(out_event_class
, in_event_class_name
);
390 if (status
!= BT_EVENT_CLASS_STATUS_OK
) {
391 BT_LOGE("Error setting event class' name: ec-addr=%p, "
392 "name=%s", out_event_class
, in_event_class_name
);
398 /* Copy event class loglevel. */
399 prop_avail
= bt_event_class_get_log_level(in_event_class
, &log_level
);
400 if (prop_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
401 bt_event_class_set_log_level(out_event_class
,
405 /* Copy event class emf uri. */
406 in_emf_uri
= bt_event_class_get_emf_uri(in_event_class
);
408 status
= bt_event_class_set_emf_uri(out_event_class
, in_emf_uri
);
409 if (status
!= BT_EVENT_CLASS_STATUS_OK
) {
410 BT_LOGE("Error setting event class' emf uri: ec-addr=%p, "
411 "emf uri=%s", out_event_class
, in_emf_uri
);
417 md_maps
= borrow_metadata_maps_from_input_event_class(ir_maps
, in_event_class
);
419 * Add the input event class' specific ctx to te
422 in_event_specific_context
=
423 bt_event_class_borrow_specific_context_field_class_const(
426 md_maps
->fc_resolving_ctx
->event_specific_context
=
427 in_event_specific_context
;
429 if (in_event_specific_context
) {
430 /* Copy the specific context of this event class. */
431 out_specific_context_fc
= create_field_class_copy(md_maps
,
432 in_event_specific_context
);
434 ret
= copy_field_class_content(md_maps
,
435 in_event_specific_context
, out_specific_context_fc
);
440 * Add the output specific context to the output event
443 status
= bt_event_class_set_specific_context_field_class(
444 out_event_class
, out_specific_context_fc
);
445 if (status
!= BT_EVENT_CLASS_STATUS_OK
) {
446 BT_LOGE("Error setting event class' specific context "
447 "field class: ec-addr=%p, ctx-fc-addr=%p",
448 out_event_class
, out_specific_context_fc
);
455 * Add the input event class' payload field class to
458 in_event_payload
= bt_event_class_borrow_payload_field_class_const(
461 md_maps
->fc_resolving_ctx
->event_payload
= in_event_payload
;
463 if (in_event_payload
) {
464 /* Copy the payload of this event class. */
465 out_payload_fc
= create_field_class_copy(md_maps
,
467 ret
= copy_field_class_content(md_maps
,
468 in_event_payload
, out_payload_fc
);
473 /* Add the output payload to the output event class. */
474 status
= bt_event_class_set_payload_field_class(
475 out_event_class
, out_payload_fc
);
476 if (status
!= BT_EVENT_CLASS_STATUS_OK
) {
477 BT_LOGE("Error setting event class' payload "
478 "field class: ec-addr=%p, payload-fc-addr=%p",
479 out_event_class
, out_payload_fc
);
485 BT_LOGD("Copied content of event class: in-ec-addr=%p, out-ec-addr=%p",
486 in_event_class
, out_event_class
);
492 int copy_event_common_context_field_class_content(
493 struct trace_ir_metadata_maps
*md_maps
,
494 const char *debug_info_fc_name
,
495 const bt_field_class
*in_field_class
,
496 bt_field_class
*out_field_class
)
498 bt_field_class_status status
;
499 bt_field_class
*debug_field_class
= NULL
, *bin_field_class
= NULL
,
500 *func_field_class
= NULL
, *src_field_class
= NULL
;
503 BT_LOGD("Copying content of event common context field class: "
504 "in-fc-addr=%p, out-fc-addr=%p", in_field_class
, out_field_class
);
506 /* Copy the content of the input common context. */
507 ret
= copy_field_class_content(md_maps
, in_field_class
, out_field_class
);
513 * If this event common context has the necessary fields to compute the
514 * debug information append the debug-info field class to the event
517 if (is_event_common_ctx_dbg_info_compatible(in_field_class
, debug_info_fc_name
)) {
519 * The struct field and 3 sub-fields are not stored in the
520 * field class map because they don't have input equivalent.
521 * We need to put our reference each of these field classes
522 * once they are added to their respective containing field
525 debug_field_class
= bt_field_class_structure_create(
526 md_maps
->output_trace_class
);
527 if (!debug_field_class
) {
528 BT_LOGE_STR("Failed to create debug_info structure.");
533 bin_field_class
= bt_field_class_string_create(
534 md_maps
->output_trace_class
);
535 if (!bin_field_class
) {
536 BT_LOGE_STR("Failed to create string for field=bin.");
541 func_field_class
= bt_field_class_string_create(
542 md_maps
->output_trace_class
);
543 if (!func_field_class
) {
544 BT_LOGE_STR("Failed to create string for field=func.");
549 src_field_class
= bt_field_class_string_create(
550 md_maps
->output_trace_class
);
551 if (!src_field_class
) {
552 BT_LOGE_STR("Failed to create string for field=src.");
557 status
= bt_field_class_structure_append_member(
558 debug_field_class
, "bin", bin_field_class
);
559 if (status
!= BT_FIELD_CLASS_STATUS_OK
) {
560 BT_LOGE_STR("Failed to add a field to debug_info "
561 "struct: field=bin.");
565 BT_FIELD_CLASS_PUT_REF_AND_RESET(bin_field_class
);
567 status
= bt_field_class_structure_append_member(
568 debug_field_class
, "func", func_field_class
);
569 if (status
!= BT_FIELD_CLASS_STATUS_OK
) {
570 BT_LOGE_STR("Failed to add a field to debug_info "
571 "struct: field=func.");
575 BT_FIELD_CLASS_PUT_REF_AND_RESET(func_field_class
);
577 status
= bt_field_class_structure_append_member(
578 debug_field_class
, "src", src_field_class
);
579 if (status
!= BT_FIELD_CLASS_STATUS_OK
) {
580 BT_LOGE_STR("Failed to add a field to debug_info "
581 "struct: field=src.");
585 BT_FIELD_CLASS_PUT_REF_AND_RESET(src_field_class
);
587 /*Add the filled debug-info field class to the common context. */
588 status
= bt_field_class_structure_append_member(out_field_class
,
591 if (status
!= BT_FIELD_CLASS_STATUS_OK
) {
592 BT_LOGE_STR("Failed to add debug_info field to "
593 "event common context.");
597 BT_FIELD_CLASS_PUT_REF_AND_RESET(debug_field_class
);
599 BT_LOGD("Copied content of event common context field class: "
600 "in-fc-addr=%p, out-fc-addr=%p", in_field_class
, out_field_class
);
604 if (debug_field_class
) {
605 bt_field_class_put_ref(debug_field_class
);
607 if (bin_field_class
) {
608 bt_field_class_put_ref(bin_field_class
);
610 if (func_field_class
) {
611 bt_field_class_put_ref(func_field_class
);
613 if (src_field_class
) {
614 bt_field_class_put_ref(src_field_class
);
621 bt_field_class
*create_field_class_copy(struct trace_ir_metadata_maps
*md_maps
,
622 const bt_field_class
*in_field_class
)
624 return create_field_class_copy_internal(md_maps
, in_field_class
);
628 int copy_field_class_content(struct trace_ir_metadata_maps
*md_maps
,
629 const bt_field_class
*in_field_class
,
630 bt_field_class
*out_field_class
)
632 return copy_field_class_content_internal(md_maps
, in_field_class
,