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 "plugins/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(const bt_trace_class
*in_trace_class
,
43 bt_trace_class
*out_trace_class
, bt_logging_level log_level
,
44 bt_self_component
*self_comp
)
47 uint64_t i
, env_field_count
;
48 const char *in_trace_class_name
;
50 BT_COMP_LOGD("Copying content of trace class: in-tc-addr=%p, out-tc-addr=%p",
51 in_trace_class
, out_trace_class
);
53 /* Use the same stream class ids as in the origin trace class. */
54 bt_trace_class_set_assigns_automatic_stream_class_id(out_trace_class
,
57 in_trace_class_name
= bt_trace_class_get_name(in_trace_class
);
58 if (in_trace_class_name
) {
59 bt_trace_class_set_name(out_trace_class
, in_trace_class_name
);
63 * Do not copy the trace class UUID as it may be modified and should no
64 * longer have the same UUID.
68 * Go over all the entries in the environment section of the trace class
69 * and copy the content to the new trace class.
71 env_field_count
= bt_trace_class_get_environment_entry_count(in_trace_class
);
72 for (i
= 0; i
< env_field_count
; i
++) {
73 const char *value_name
;
74 const bt_value
*value
= NULL
;
75 bt_trace_class_set_environment_entry_status set_env_status
;
77 bt_trace_class_borrow_environment_entry_by_index_const(
78 in_trace_class
, i
, &value_name
, &value
);
80 BT_COMP_LOGD("Copying trace class environnement entry: "
81 "index=%" PRId64
", value-addr=%p, value-name=%s",
82 i
, value
, value_name
);
84 BT_ASSERT(value_name
);
87 if (bt_value_is_signed_integer(value
)) {
89 bt_trace_class_set_environment_entry_integer(
90 out_trace_class
, value_name
,
91 bt_value_signed_integer_get(
93 } else if (bt_value_is_string(value
)) {
95 bt_trace_class_set_environment_entry_string(
96 out_trace_class
, value_name
,
97 bt_value_string_get(value
));
102 if (set_env_status
!=
103 BT_TRACE_CLASS_SET_ENVIRONMENT_ENTRY_STATUS_OK
) {
109 BT_COMP_LOGD("Copied content of trace class: in-tc-addr=%p, out-tc-addr=%p",
110 in_trace_class
, out_trace_class
);
116 int copy_clock_class_content(const bt_clock_class
*in_clock_class
,
117 bt_clock_class
*out_clock_class
, bt_logging_level log_level
,
118 bt_self_component
*self_comp
)
120 const char *clock_class_name
, *clock_class_description
;
126 BT_COMP_LOGD("Copying content of clock class: in-cc-addr=%p, out-cc-addr=%p",
127 in_clock_class
, out_clock_class
);
129 clock_class_name
= bt_clock_class_get_name(in_clock_class
);
131 if (clock_class_name
) {
132 if (bt_clock_class_set_name(out_clock_class
, clock_class_name
)
133 != BT_CLOCK_CLASS_SET_NAME_STATUS_OK
) {
134 BT_COMP_LOGE("Error setting clock class' name cc-addr=%p, name=%p",
135 out_clock_class
, clock_class_name
);
136 out_clock_class
= NULL
;
142 clock_class_description
= bt_clock_class_get_description(in_clock_class
);
144 if (clock_class_description
) {
145 if (bt_clock_class_set_description(out_clock_class
,
146 clock_class_description
) !=
147 BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_OK
) {
148 BT_COMP_LOGE("Error setting clock class' description cc-addr=%p, "
149 "name=%p", out_clock_class
, clock_class_description
);
150 out_clock_class
= NULL
;
156 in_uuid
= bt_clock_class_get_uuid(in_clock_class
);
158 bt_clock_class_set_uuid(out_clock_class
, in_uuid
);
161 bt_clock_class_set_frequency(out_clock_class
,
162 bt_clock_class_get_frequency(in_clock_class
));
163 bt_clock_class_set_precision(out_clock_class
,
164 bt_clock_class_get_precision(in_clock_class
));
165 bt_clock_class_get_offset(in_clock_class
, &seconds
, &cycles
);
166 bt_clock_class_set_offset(out_clock_class
, seconds
, cycles
);
167 bt_clock_class_set_origin_is_unix_epoch(out_clock_class
,
168 bt_clock_class_origin_is_unix_epoch(in_clock_class
));
170 BT_COMP_LOGD("Copied content of clock class: in-cc-addr=%p, out-cc-addr=%p",
171 in_clock_class
, out_clock_class
);
178 bt_clock_class
*borrow_mapped_clock_class(
179 struct trace_ir_metadata_maps
*md_maps
,
180 const bt_clock_class
*in_clock_class
)
183 BT_ASSERT(in_clock_class
);
185 return g_hash_table_lookup(md_maps
->clock_class_map
,
186 (gpointer
) in_clock_class
);
190 bt_clock_class
*create_new_mapped_clock_class(
191 bt_self_component
*self_comp
,
192 struct trace_ir_metadata_maps
*md_maps
,
193 const bt_clock_class
*in_clock_class
)
195 bt_clock_class
*out_clock_class
;
197 bt_logging_level log_level
= md_maps
->log_level
;
199 BT_COMP_LOGD("Creating new mapped clock class: in-cc-addr=%p",
203 BT_ASSERT(in_clock_class
);
205 BT_ASSERT(!borrow_mapped_clock_class(md_maps
, in_clock_class
));
207 out_clock_class
= bt_clock_class_create(self_comp
);
208 if (!out_clock_class
) {
209 BT_COMP_LOGE_STR("Cannot create clock class");
212 /* If not, create a new one and add it to the mapping. */
213 ret
= copy_clock_class_content(in_clock_class
, out_clock_class
,
214 log_level
, self_comp
);
216 BT_COMP_LOGE_STR("Cannot copy clock class");
220 g_hash_table_insert(md_maps
->clock_class_map
,
221 (gpointer
) in_clock_class
, out_clock_class
);
223 BT_COMP_LOGD("Created new mapped clock class: in-cc-addr=%p, out-cc-addr=%p",
224 in_clock_class
, out_clock_class
);
226 return out_clock_class
;
230 int copy_stream_class_content(struct trace_ir_maps
*ir_maps
,
231 const bt_stream_class
*in_stream_class
,
232 bt_stream_class
*out_stream_class
)
234 struct trace_ir_metadata_maps
*md_maps
;
235 const bt_clock_class
*in_clock_class
;
236 bt_clock_class
*out_clock_class
;
237 const bt_field_class
*in_packet_context_fc
, *in_common_context_fc
;
238 bt_field_class
*out_packet_context_fc
, *out_common_context_fc
;
241 bt_logging_level log_level
= ir_maps
->log_level
;
242 bt_self_component
*self_comp
= ir_maps
->self_comp
;
244 BT_COMP_LOGD("Copying content of stream class: in-sc-addr=%p, out-sc-addr=%p",
245 in_stream_class
, out_stream_class
);
247 md_maps
= borrow_metadata_maps_from_input_stream_class(ir_maps
, in_stream_class
);
248 in_clock_class
= bt_stream_class_borrow_default_clock_class_const(
251 if (in_clock_class
) {
252 /* Copy the clock class. */
254 borrow_mapped_clock_class(md_maps
, in_clock_class
);
255 if (!out_clock_class
) {
256 out_clock_class
= create_new_mapped_clock_class(
257 ir_maps
->self_comp
, md_maps
,
260 bt_stream_class_set_default_clock_class(out_stream_class
,
265 bt_stream_class_set_packets_have_beginning_default_clock_snapshot(
267 bt_stream_class_packets_have_beginning_default_clock_snapshot(
269 bt_stream_class_set_packets_have_end_default_clock_snapshot(
271 bt_stream_class_packets_have_end_default_clock_snapshot(
273 bt_stream_class_set_supports_discarded_events(
275 bt_stream_class_supports_discarded_events(in_stream_class
),
276 bt_stream_class_discarded_events_have_default_clock_snapshots(
278 bt_stream_class_set_supports_discarded_packets(
280 bt_stream_class_supports_discarded_packets(in_stream_class
),
281 bt_stream_class_discarded_packets_have_default_clock_snapshots(
284 in_name
= bt_stream_class_get_name(in_stream_class
);
286 if (bt_stream_class_set_name(out_stream_class
, in_name
) !=
287 BT_STREAM_CLASS_SET_NAME_STATUS_OK
) {
288 BT_COMP_LOGE("Error set stream class name: out-sc-addr=%p, "
289 "name=%s", out_stream_class
, in_name
);
295 bt_stream_class_set_assigns_automatic_stream_id(out_stream_class
,
297 bt_stream_class_set_assigns_automatic_event_class_id(out_stream_class
,
301 * Add the input packet context field class to the context to
302 * resolution in the further steps.
304 in_packet_context_fc
=
305 bt_stream_class_borrow_packet_context_field_class_const(
307 md_maps
->fc_resolving_ctx
->packet_context
=
308 in_packet_context_fc
;
310 if (in_packet_context_fc
) {
311 /* Copy packet context. */
312 out_packet_context_fc
= create_field_class_copy(
313 md_maps
, in_packet_context_fc
);
315 ret
= copy_field_class_content(md_maps
,
316 in_packet_context_fc
, out_packet_context_fc
);
322 if (bt_stream_class_set_packet_context_field_class(
323 out_stream_class
, out_packet_context_fc
) !=
324 BT_STREAM_CLASS_SET_FIELD_CLASS_STATUS_OK
) {
325 BT_COMP_LOGE("Error setting stream class' packet context "
326 "field class: sc-addr=%p, packet-fc-addr=%p",
327 out_stream_class
, out_packet_context_fc
);
334 * Add the input common context field class to the context to
335 * resolution in the further steps.
337 in_common_context_fc
=
338 bt_stream_class_borrow_event_common_context_field_class_const(
340 md_maps
->fc_resolving_ctx
->event_common_context
=
341 in_common_context_fc
;
343 if (in_common_context_fc
) {
344 /* Copy common context. */
345 /* TODO: I find it a bit awkward to have this special function
346 * here to add the debug-info field class. I would like to
348 out_common_context_fc
= create_field_class_copy(
349 md_maps
, in_common_context_fc
);
351 ret
= copy_event_common_context_field_class_content(
352 md_maps
, ir_maps
->debug_info_field_class_name
,
353 in_common_context_fc
, out_common_context_fc
);
358 if (bt_stream_class_set_event_common_context_field_class(
359 out_stream_class
, out_common_context_fc
) !=
360 BT_STREAM_CLASS_SET_FIELD_CLASS_STATUS_OK
) {
361 BT_COMP_LOGE("Error setting stream class' packet context "
362 "field class: sc-addr=%p, packet-fc-addr=%p",
363 out_stream_class
, out_common_context_fc
);
369 /* Set packet snapshot boolean fields. */
370 BT_COMP_LOGD("Copied content of stream class: in-sc-addr=%p, out-sc-addr=%p",
371 in_stream_class
, out_stream_class
);
377 int copy_event_class_content(struct trace_ir_maps
*ir_maps
,
378 const bt_event_class
*in_event_class
,
379 bt_event_class
*out_event_class
)
381 struct trace_ir_metadata_maps
*md_maps
;
382 const char *in_event_class_name
, *in_emf_uri
;
383 bt_property_availability prop_avail
;
384 bt_event_class_log_level ec_log_level
;
385 bt_field_class
*out_specific_context_fc
, *out_payload_fc
;
386 const bt_field_class
*in_event_specific_context
, *in_event_payload
;
388 bt_logging_level log_level
= ir_maps
->log_level
;
389 bt_self_component
*self_comp
= ir_maps
->self_comp
;
391 BT_COMP_LOGD("Copying content of event class: in-ec-addr=%p, out-ec-addr=%p",
392 in_event_class
, out_event_class
);
394 /* Copy event class name. */
395 in_event_class_name
= bt_event_class_get_name(in_event_class
);
396 if (in_event_class_name
) {
397 if (bt_event_class_set_name(out_event_class
,
398 in_event_class_name
) !=
399 BT_EVENT_CLASS_SET_NAME_STATUS_OK
) {
400 BT_COMP_LOGE("Error setting event class' name: ec-addr=%p, "
401 "name=%s", out_event_class
, in_event_class_name
);
407 /* Copy event class loglevel. */
408 prop_avail
= bt_event_class_get_log_level(in_event_class
,
410 if (prop_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
411 bt_event_class_set_log_level(out_event_class
,
415 /* Copy event class emf uri. */
416 in_emf_uri
= bt_event_class_get_emf_uri(in_event_class
);
418 if (bt_event_class_set_emf_uri(out_event_class
, in_emf_uri
) !=
419 BT_EVENT_CLASS_SET_EMF_URI_STATUS_OK
) {
420 BT_COMP_LOGE("Error setting event class' emf uri: ec-addr=%p, "
421 "emf uri=%s", out_event_class
, in_emf_uri
);
427 md_maps
= borrow_metadata_maps_from_input_event_class(ir_maps
, in_event_class
);
429 * Add the input event class' specific ctx to te
432 in_event_specific_context
=
433 bt_event_class_borrow_specific_context_field_class_const(
436 md_maps
->fc_resolving_ctx
->event_specific_context
=
437 in_event_specific_context
;
439 if (in_event_specific_context
) {
440 /* Copy the specific context of this event class. */
441 out_specific_context_fc
= create_field_class_copy(md_maps
,
442 in_event_specific_context
);
444 ret
= copy_field_class_content(md_maps
,
445 in_event_specific_context
, out_specific_context_fc
);
450 * Add the output specific context to the output event
453 if (bt_event_class_set_specific_context_field_class(
454 out_event_class
, out_specific_context_fc
) !=
455 BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK
) {
456 BT_COMP_LOGE("Error setting event class' specific context "
457 "field class: ec-addr=%p, ctx-fc-addr=%p",
458 out_event_class
, out_specific_context_fc
);
465 * Add the input event class' payload field class to
468 in_event_payload
= bt_event_class_borrow_payload_field_class_const(
471 md_maps
->fc_resolving_ctx
->event_payload
= in_event_payload
;
473 if (in_event_payload
) {
474 /* Copy the payload of this event class. */
475 out_payload_fc
= create_field_class_copy(md_maps
,
477 ret
= copy_field_class_content(md_maps
,
478 in_event_payload
, out_payload_fc
);
483 /* Add the output payload to the output event class. */
484 if (bt_event_class_set_payload_field_class(
485 out_event_class
, out_payload_fc
) !=
486 BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK
) {
487 BT_COMP_LOGE("Error setting event class' payload "
488 "field class: ec-addr=%p, payload-fc-addr=%p",
489 out_event_class
, out_payload_fc
);
495 BT_COMP_LOGD("Copied content of event class: in-ec-addr=%p, out-ec-addr=%p",
496 in_event_class
, out_event_class
);
502 int copy_event_common_context_field_class_content(
503 struct trace_ir_metadata_maps
*md_maps
,
504 const char *debug_info_fc_name
,
505 const bt_field_class
*in_field_class
,
506 bt_field_class
*out_field_class
)
508 bt_field_class
*debug_field_class
= NULL
, *bin_field_class
= NULL
,
509 *func_field_class
= NULL
, *src_field_class
= NULL
;
511 bt_logging_level log_level
= md_maps
->log_level
;
512 bt_self_component
*self_comp
= md_maps
->self_comp
;
514 BT_COMP_LOGD("Copying content of event common context field class: "
515 "in-fc-addr=%p, out-fc-addr=%p", in_field_class
, out_field_class
);
517 /* Copy the content of the input common context. */
518 ret
= copy_field_class_content(md_maps
, in_field_class
, out_field_class
);
524 * If this event common context has the necessary fields to compute the
525 * debug information append the debug-info field class to the event
528 if (is_event_common_ctx_dbg_info_compatible(in_field_class
, debug_info_fc_name
)) {
530 * The struct field and 3 sub-fields are not stored in the
531 * field class map because they don't have input equivalent.
532 * We need to put our reference each of these field classes
533 * once they are added to their respective containing field
536 debug_field_class
= bt_field_class_structure_create(
537 md_maps
->output_trace_class
);
538 if (!debug_field_class
) {
539 BT_COMP_LOGE_STR("Failed to create debug_info structure.");
544 bin_field_class
= bt_field_class_string_create(
545 md_maps
->output_trace_class
);
546 if (!bin_field_class
) {
547 BT_COMP_LOGE_STR("Failed to create string for field=bin.");
552 func_field_class
= bt_field_class_string_create(
553 md_maps
->output_trace_class
);
554 if (!func_field_class
) {
555 BT_COMP_LOGE_STR("Failed to create string for field=func.");
560 src_field_class
= bt_field_class_string_create(
561 md_maps
->output_trace_class
);
562 if (!src_field_class
) {
563 BT_COMP_LOGE_STR("Failed to create string for field=src.");
568 if (bt_field_class_structure_append_member(
569 debug_field_class
, "bin", bin_field_class
) !=
570 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK
) {
571 BT_COMP_LOGE_STR("Failed to add a field to debug_info "
572 "struct: field=bin.");
576 BT_FIELD_CLASS_PUT_REF_AND_RESET(bin_field_class
);
578 if (bt_field_class_structure_append_member(
579 debug_field_class
, "func", func_field_class
) !=
580 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK
) {
581 BT_COMP_LOGE_STR("Failed to add a field to debug_info "
582 "struct: field=func.");
586 BT_FIELD_CLASS_PUT_REF_AND_RESET(func_field_class
);
588 if (bt_field_class_structure_append_member(
589 debug_field_class
, "src", src_field_class
) !=
590 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK
) {
591 BT_COMP_LOGE_STR("Failed to add a field to debug_info "
592 "struct: field=src.");
596 BT_FIELD_CLASS_PUT_REF_AND_RESET(src_field_class
);
598 /*Add the filled debug-info field class to the common context. */
599 if (bt_field_class_structure_append_member(out_field_class
,
600 debug_info_fc_name
, debug_field_class
) !=
601 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK
) {
602 BT_COMP_LOGE_STR("Failed to add debug_info field to "
603 "event common context.");
607 BT_FIELD_CLASS_PUT_REF_AND_RESET(debug_field_class
);
609 BT_COMP_LOGD("Copied content of event common context field class: "
610 "in-fc-addr=%p, out-fc-addr=%p", in_field_class
, out_field_class
);
614 if (debug_field_class
) {
615 bt_field_class_put_ref(debug_field_class
);
617 if (bin_field_class
) {
618 bt_field_class_put_ref(bin_field_class
);
620 if (func_field_class
) {
621 bt_field_class_put_ref(func_field_class
);
623 if (src_field_class
) {
624 bt_field_class_put_ref(src_field_class
);
631 bt_field_class
*create_field_class_copy(struct trace_ir_metadata_maps
*md_maps
,
632 const bt_field_class
*in_field_class
)
634 return create_field_class_copy_internal(md_maps
, in_field_class
);
638 int copy_field_class_content(struct trace_ir_metadata_maps
*md_maps
,
639 const bt_field_class
*in_field_class
,
640 bt_field_class
*out_field_class
)
642 return copy_field_class_content_internal(md_maps
, in_field_class
,