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
;
48 BT_LOGD("Copying content of trace class: in-tc-addr=%p, out-tc-addr=%p",
49 in_trace_class
, out_trace_class
);
51 /* Use the same stream class ids as in the origin trace class. */
52 bt_trace_class_set_assigns_automatic_stream_class_id(out_trace_class
,
55 in_trace_class_name
= bt_trace_class_get_name(in_trace_class
);
56 if (in_trace_class_name
) {
57 bt_trace_class_set_name(out_trace_class
, in_trace_class_name
);
60 in_uuid
= bt_trace_class_get_uuid(in_trace_class
);
62 bt_trace_class_set_uuid(out_trace_class
, in_uuid
);
66 * Go over all the entries in the environment section of the trace class
67 * and copy the content to the new trace class.
69 env_field_count
= bt_trace_class_get_environment_entry_count(in_trace_class
);
70 for (i
= 0; i
< env_field_count
; i
++) {
71 const char *value_name
;
72 const bt_value
*value
= NULL
;
73 bt_trace_class_status trace_class_status
;
75 bt_trace_class_borrow_environment_entry_by_index_const(
76 in_trace_class
, i
, &value_name
, &value
);
78 BT_LOGD("Copying trace class environnement entry: "
79 "index=%" PRId64
", value-addr=%p, value-name=%s",
80 i
, value
, value_name
);
82 BT_ASSERT(value_name
);
85 if (bt_value_is_integer(value
)) {
87 bt_trace_class_set_environment_entry_integer(
88 out_trace_class
, value_name
,
89 bt_value_integer_get(value
));
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 in_name
= bt_stream_class_get_name(in_stream_class
);
260 status
= bt_stream_class_set_name(out_stream_class
, in_name
);
261 if (status
!= BT_STREAM_CLASS_STATUS_OK
) {
262 BT_LOGE("Error set stream class name: out-sc-addr=%p, "
263 "name=%s", out_stream_class
, in_name
);
269 bt_stream_class_set_assigns_automatic_stream_id(out_stream_class
,
271 bt_stream_class_set_assigns_automatic_event_class_id(out_stream_class
,
275 * Add the input packet context field class to the context to
276 * resolution in the further steps.
278 in_packet_context_fc
=
279 bt_stream_class_borrow_packet_context_field_class_const(
281 md_maps
->fc_resolving_ctx
->packet_context
=
282 in_packet_context_fc
;
284 if (in_packet_context_fc
) {
285 /* Copy packet context. */
286 out_packet_context_fc
= create_field_class_copy(
287 md_maps
, in_packet_context_fc
);
289 ret
= copy_field_class_content(md_maps
,
290 in_packet_context_fc
, out_packet_context_fc
);
296 status
= bt_stream_class_set_packet_context_field_class(
297 out_stream_class
, out_packet_context_fc
);
298 if (status
!= BT_STREAM_CLASS_STATUS_OK
) {
299 BT_LOGE("Error setting stream class' packet context "
300 "field class: sc-addr=%p, packet-fc-addr=%p",
301 out_stream_class
, out_packet_context_fc
);
308 * Add the input common context field class to the context to
309 * resolution in the further steps.
311 in_common_context_fc
=
312 bt_stream_class_borrow_event_common_context_field_class_const(
314 md_maps
->fc_resolving_ctx
->event_common_context
=
315 in_common_context_fc
;
317 if (in_common_context_fc
) {
318 /* Copy common context. */
319 /* TODO: I find it a bit awkward to have this special function
320 * here to add the debug-info field class. I would like to
322 out_common_context_fc
= create_field_class_copy(
323 md_maps
, in_common_context_fc
);
325 ret
= copy_event_common_context_field_class_content(
326 md_maps
, ir_maps
->debug_info_field_class_name
,
327 in_common_context_fc
, out_common_context_fc
);
332 status
= bt_stream_class_set_event_common_context_field_class(
333 out_stream_class
, out_common_context_fc
);
334 if (status
!= BT_STREAM_CLASS_STATUS_OK
) {
335 BT_LOGE("Error setting stream class' packet context "
336 "field class: sc-addr=%p, packet-fc-addr=%p",
337 out_stream_class
, out_common_context_fc
);
343 /* Set packet snapshot boolean fields. */
344 BT_LOGD("Copied content of stream class: in-sc-addr=%p, out-sc-addr=%p",
345 in_stream_class
, out_stream_class
);
351 int copy_event_class_content(struct trace_ir_maps
*ir_maps
,
352 const bt_event_class
*in_event_class
,
353 bt_event_class
*out_event_class
)
355 struct trace_ir_metadata_maps
*md_maps
;
356 const char *in_event_class_name
, *in_emf_uri
;
357 bt_property_availability prop_avail
;
358 bt_event_class_log_level log_level
;
359 bt_event_class_status status
;
360 bt_field_class
*out_specific_context_fc
, *out_payload_fc
;
361 const bt_field_class
*in_event_specific_context
, *in_event_payload
;
364 BT_LOGD("Copying content of event class: in-ec-addr=%p, out-ec-addr=%p",
365 in_event_class
, out_event_class
);
367 /* Copy event class name. */
368 in_event_class_name
= bt_event_class_get_name(in_event_class
);
369 if (in_event_class_name
) {
370 status
= bt_event_class_set_name(out_event_class
, in_event_class_name
);
371 if (status
!= BT_EVENT_CLASS_STATUS_OK
) {
372 BT_LOGE("Error setting event class' name: ec-addr=%p, "
373 "name=%s", out_event_class
, in_event_class_name
);
379 /* Copy event class loglevel. */
380 prop_avail
= bt_event_class_get_log_level(in_event_class
, &log_level
);
381 if (prop_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
382 bt_event_class_set_log_level(out_event_class
,
386 /* Copy event class emf uri. */
387 in_emf_uri
= bt_event_class_get_emf_uri(in_event_class
);
389 status
= bt_event_class_set_emf_uri(out_event_class
, in_emf_uri
);
390 if (status
!= BT_EVENT_CLASS_STATUS_OK
) {
391 BT_LOGE("Error setting event class' emf uri: ec-addr=%p, "
392 "emf uri=%s", out_event_class
, in_emf_uri
);
398 md_maps
= borrow_metadata_maps_from_input_event_class(ir_maps
, in_event_class
);
400 * Add the input event class' specific ctx to te
403 in_event_specific_context
=
404 bt_event_class_borrow_specific_context_field_class_const(
407 md_maps
->fc_resolving_ctx
->event_specific_context
=
408 in_event_specific_context
;
410 if (in_event_specific_context
) {
411 /* Copy the specific context of this event class. */
412 out_specific_context_fc
= create_field_class_copy(md_maps
,
413 in_event_specific_context
);
415 copy_field_class_content(md_maps
,
416 in_event_specific_context
, out_specific_context_fc
);
421 * Add the output specific context to the output event
424 status
= bt_event_class_set_specific_context_field_class(
425 out_event_class
, out_specific_context_fc
);
426 if (status
!= BT_EVENT_CLASS_STATUS_OK
) {
427 BT_LOGE("Error setting event class' specific context "
428 "field class: ec-addr=%p, ctx-fc-addr=%p",
429 out_event_class
, out_specific_context_fc
);
436 * Add the input event class' payload field class to
439 in_event_payload
= bt_event_class_borrow_payload_field_class_const(
442 md_maps
->fc_resolving_ctx
->event_payload
= in_event_payload
;
444 if (in_event_payload
) {
445 /* Copy the payload of this event class. */
446 out_payload_fc
= create_field_class_copy(md_maps
,
448 copy_field_class_content(md_maps
,
449 in_event_payload
, out_payload_fc
);
454 /* Add the output payload to the output event class. */
455 status
= bt_event_class_set_payload_field_class(
456 out_event_class
, out_payload_fc
);
457 if (status
!= BT_EVENT_CLASS_STATUS_OK
) {
458 BT_LOGE("Error setting event class' payload "
459 "field class: ec-addr=%p, payload-fc-addr=%p",
460 out_event_class
, out_payload_fc
);
466 BT_LOGD("Copied content of event class: in-ec-addr=%p, out-ec-addr=%p",
467 in_event_class
, out_event_class
);
473 int copy_event_common_context_field_class_content(
474 struct trace_ir_metadata_maps
*md_maps
,
475 const char *debug_info_fc_name
,
476 const bt_field_class
*in_field_class
,
477 bt_field_class
*out_field_class
)
479 bt_field_class_status status
;
480 bt_field_class
*debug_field_class
= NULL
, *bin_field_class
= NULL
,
481 *func_field_class
= NULL
, *src_field_class
= NULL
;
484 BT_LOGD("Copying content of event common context field class: "
485 "in-fc-addr=%p, out-fc-addr=%p", in_field_class
, out_field_class
);
487 /* Copy the content of the input common context. */
488 ret
= copy_field_class_content(md_maps
, in_field_class
, out_field_class
);
494 * If this event common context has the necessary fields to compute the
495 * debug information append the debug-info field class to the event
498 if (is_event_common_ctx_dbg_info_compatible(in_field_class
, debug_info_fc_name
)) {
500 * The struct field and 3 sub-fields are not stored in the
501 * field class map because they don't have input equivalent.
502 * We need to put our reference each of these field classes
503 * once they are added to their respective containing field
506 debug_field_class
= bt_field_class_structure_create(
507 md_maps
->output_trace_class
);
508 if (!debug_field_class
) {
509 BT_LOGE_STR("Failed to create debug_info structure.");
514 bin_field_class
= bt_field_class_string_create(
515 md_maps
->output_trace_class
);
516 if (!bin_field_class
) {
517 BT_LOGE_STR("Failed to create string for field=bin.");
522 func_field_class
= bt_field_class_string_create(
523 md_maps
->output_trace_class
);
524 if (!func_field_class
) {
525 BT_LOGE_STR("Failed to create string for field=func.");
530 src_field_class
= bt_field_class_string_create(
531 md_maps
->output_trace_class
);
532 if (!src_field_class
) {
533 BT_LOGE_STR("Failed to create string for field=src.");
538 status
= bt_field_class_structure_append_member(
539 debug_field_class
, "bin", bin_field_class
);
540 if (status
!= BT_FIELD_CLASS_STATUS_OK
) {
541 BT_LOGE_STR("Failed to add a field to debug_info "
542 "struct: field=bin.");
546 BT_FIELD_CLASS_PUT_REF_AND_RESET(bin_field_class
);
548 status
= bt_field_class_structure_append_member(
549 debug_field_class
, "func", func_field_class
);
550 if (status
!= BT_FIELD_CLASS_STATUS_OK
) {
551 BT_LOGE_STR("Failed to add a field to debug_info "
552 "struct: field=func.");
556 BT_FIELD_CLASS_PUT_REF_AND_RESET(func_field_class
);
558 status
= bt_field_class_structure_append_member(
559 debug_field_class
, "src", src_field_class
);
560 if (status
!= BT_FIELD_CLASS_STATUS_OK
) {
561 BT_LOGE_STR("Failed to add a field to debug_info "
562 "struct: field=src.");
566 BT_FIELD_CLASS_PUT_REF_AND_RESET(src_field_class
);
568 /*Add the filled debug-info field class to the common context. */
569 status
= bt_field_class_structure_append_member(out_field_class
,
572 if (status
!= BT_FIELD_CLASS_STATUS_OK
) {
573 BT_LOGE_STR("Failed to add debug_info field to "
574 "event common context.");
578 BT_FIELD_CLASS_PUT_REF_AND_RESET(debug_field_class
);
580 BT_LOGD("Copied content of event common context field class: "
581 "in-fc-addr=%p, out-fc-addr=%p", in_field_class
, out_field_class
);
585 if (debug_field_class
) {
586 bt_field_class_put_ref(debug_field_class
);
588 if (bin_field_class
) {
589 bt_field_class_put_ref(bin_field_class
);
591 if (func_field_class
) {
592 bt_field_class_put_ref(func_field_class
);
594 if (src_field_class
) {
595 bt_field_class_put_ref(src_field_class
);
602 bt_field_class
*create_field_class_copy(struct trace_ir_metadata_maps
*md_maps
,
603 const bt_field_class
*in_field_class
)
605 return create_field_class_copy_internal(md_maps
, in_field_class
);
609 int copy_field_class_content(struct trace_ir_metadata_maps
*md_maps
,
610 const bt_field_class
*in_field_class
,
611 bt_field_class
*out_field_class
)
613 return copy_field_class_content_internal(md_maps
, in_field_class
,