2 * Copyright 2018 - Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
15 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-META-TRANSLATE"
18 #include <babeltrace/babeltrace.h>
19 #include <babeltrace/babeltrace-internal.h>
20 #include <babeltrace/assert-internal.h>
26 #include "ctf-meta-visitors.h"
29 bt_field_class
*ctf_field_class_to_ir(struct ctf_field_class
*fc
,
30 struct ctf_trace_class
*tc
,
31 struct ctf_stream_class
*sc
,
32 struct ctf_event_class
*ec
);
35 void ctf_field_class_int_set_props(struct ctf_field_class_int
*fc
,
36 bt_field_class
*ir_fc
)
38 bt_field_class_integer_set_field_value_range(ir_fc
,
40 bt_field_class_integer_set_preferred_display_base(ir_fc
,
45 bt_field_class
*ctf_field_class_int_to_ir(
46 struct ctf_field_class_int
*fc
)
48 bt_field_class
*ir_fc
;
51 ir_fc
= bt_field_class_signed_integer_create();
53 ir_fc
= bt_field_class_unsigned_integer_create();
57 ctf_field_class_int_set_props(fc
, ir_fc
);
62 bt_field_class
*ctf_field_class_enum_to_ir(
63 struct ctf_field_class_enum
*fc
)
66 bt_field_class
*ir_fc
;
69 if (fc
->base
.is_signed
) {
70 ir_fc
= bt_field_class_signed_enumeration_create();
72 ir_fc
= bt_field_class_unsigned_enumeration_create();
76 ctf_field_class_int_set_props((void *) fc
, ir_fc
);
78 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
79 struct ctf_field_class_enum_mapping
*mapping
=
80 ctf_field_class_enum_borrow_mapping_by_index(fc
, i
);
82 if (fc
->base
.is_signed
) {
83 ret
= bt_field_class_signed_enumeration_map_range(
84 ir_fc
, mapping
->label
->str
,
85 mapping
->range
.lower
.i
, mapping
->range
.upper
.i
);
87 ret
= bt_field_class_unsigned_enumeration_map_range(
88 ir_fc
, mapping
->label
->str
,
89 mapping
->range
.lower
.u
, mapping
->range
.upper
.u
);
99 bt_field_class
*ctf_field_class_float_to_ir(
100 struct ctf_field_class_float
*fc
)
102 bt_field_class
*ir_fc
;
104 ir_fc
= bt_field_class_real_create();
107 if (fc
->base
.size
== 32) {
108 bt_field_class_real_set_is_single_precision(ir_fc
,
116 bt_field_class
*ctf_field_class_string_to_ir(
117 struct ctf_field_class_string
*fc
)
119 bt_field_class
*ir_fc
=
120 bt_field_class_string_create();
127 bt_field_class
*ctf_field_class_struct_to_ir(
128 struct ctf_field_class_struct
*fc
,
129 struct ctf_trace_class
*tc
,
130 struct ctf_stream_class
*sc
,
131 struct ctf_event_class
*ec
)
134 bt_field_class
*ir_fc
=
135 bt_field_class_structure_create();
140 for (i
= 0; i
< fc
->members
->len
; i
++) {
141 struct ctf_named_field_class
*named_fc
=
142 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
143 bt_field_class
*member_ir_fc
;
145 if (!named_fc
->fc
->in_ir
) {
149 member_ir_fc
= ctf_field_class_to_ir(named_fc
->fc
, tc
, sc
, ec
);
150 BT_ASSERT(member_ir_fc
);
151 ret
= bt_field_class_structure_append_member(
152 ir_fc
, named_fc
->name
->str
, member_ir_fc
);
154 bt_field_class_put_ref(member_ir_fc
);
161 bt_field_class
*borrow_ir_ft_from_field_path(
162 struct ctf_field_path
*field_path
,
163 struct ctf_trace_class
*tc
,
164 struct ctf_stream_class
*sc
,
165 struct ctf_event_class
*ec
)
167 bt_field_class
*ir_fc
= NULL
;
168 struct ctf_field_class
*fc
= ctf_field_path_borrow_field_class(
169 field_path
, tc
, sc
, ec
);
181 bt_field_class
*ctf_field_class_variant_to_ir(
182 struct ctf_field_class_variant
*fc
,
183 struct ctf_trace_class
*tc
,
184 struct ctf_stream_class
*sc
,
185 struct ctf_event_class
*ec
)
188 bt_field_class
*ir_fc
=
189 bt_field_class_variant_create();
193 ret
= bt_field_class_variant_set_selector_field_class(
194 ir_fc
, borrow_ir_ft_from_field_path(&fc
->tag_path
, tc
, sc
, ec
));
197 for (i
= 0; i
< fc
->options
->len
; i
++) {
198 struct ctf_named_field_class
*named_fc
=
199 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
200 bt_field_class
*option_ir_fc
;
202 BT_ASSERT(named_fc
->fc
->in_ir
);
203 option_ir_fc
= ctf_field_class_to_ir(named_fc
->fc
, tc
, sc
, ec
);
204 BT_ASSERT(option_ir_fc
);
205 ret
= bt_field_class_variant_append_option(
206 ir_fc
, named_fc
->name
->str
, option_ir_fc
);
208 bt_field_class_put_ref(option_ir_fc
);
215 bt_field_class
*ctf_field_class_array_to_ir(
216 struct ctf_field_class_array
*fc
,
217 struct ctf_trace_class
*tc
,
218 struct ctf_stream_class
*sc
,
219 struct ctf_event_class
*ec
)
221 bt_field_class
*ir_fc
;
222 bt_field_class
*elem_ir_fc
;
224 if (fc
->base
.is_text
) {
225 ir_fc
= bt_field_class_string_create();
230 elem_ir_fc
= ctf_field_class_to_ir(fc
->base
.elem_fc
, tc
, sc
, ec
);
231 BT_ASSERT(elem_ir_fc
);
232 ir_fc
= bt_field_class_static_array_create(elem_ir_fc
,
235 bt_field_class_put_ref(elem_ir_fc
);
242 bt_field_class
*ctf_field_class_sequence_to_ir(
243 struct ctf_field_class_sequence
*fc
,
244 struct ctf_trace_class
*tc
,
245 struct ctf_stream_class
*sc
,
246 struct ctf_event_class
*ec
)
249 bt_field_class
*ir_fc
;
250 bt_field_class
*elem_ir_fc
;
252 if (fc
->base
.is_text
) {
253 ir_fc
= bt_field_class_string_create();
258 elem_ir_fc
= ctf_field_class_to_ir(fc
->base
.elem_fc
, tc
, sc
, ec
);
259 BT_ASSERT(elem_ir_fc
);
260 ir_fc
= bt_field_class_dynamic_array_create(elem_ir_fc
);
262 bt_field_class_put_ref(elem_ir_fc
);
264 ret
= bt_field_class_dynamic_array_set_length_field_class(
266 borrow_ir_ft_from_field_path(&fc
->length_path
, tc
, sc
, ec
));
274 bt_field_class
*ctf_field_class_to_ir(struct ctf_field_class
*fc
,
275 struct ctf_trace_class
*tc
,
276 struct ctf_stream_class
*sc
,
277 struct ctf_event_class
*ec
)
279 bt_field_class
*ir_fc
= NULL
;
282 BT_ASSERT(fc
->in_ir
);
285 case CTF_FIELD_CLASS_TYPE_INT
:
286 ir_fc
= ctf_field_class_int_to_ir((void *) fc
);
288 case CTF_FIELD_CLASS_TYPE_ENUM
:
289 ir_fc
= ctf_field_class_enum_to_ir((void *) fc
);
291 case CTF_FIELD_CLASS_TYPE_FLOAT
:
292 ir_fc
= ctf_field_class_float_to_ir((void *) fc
);
294 case CTF_FIELD_CLASS_TYPE_STRING
:
295 ir_fc
= ctf_field_class_string_to_ir((void *) fc
);
297 case CTF_FIELD_CLASS_TYPE_STRUCT
:
298 ir_fc
= ctf_field_class_struct_to_ir((void *) fc
, tc
, sc
, ec
);
300 case CTF_FIELD_CLASS_TYPE_ARRAY
:
301 ir_fc
= ctf_field_class_array_to_ir((void *) fc
, tc
, sc
, ec
);
303 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
304 ir_fc
= ctf_field_class_sequence_to_ir((void *) fc
, tc
, sc
, ec
);
306 case CTF_FIELD_CLASS_TYPE_VARIANT
:
307 ir_fc
= ctf_field_class_variant_to_ir((void *) fc
, tc
, sc
, ec
);
318 bool ctf_field_class_struct_has_immediate_member_in_ir(
319 struct ctf_field_class_struct
*fc
)
322 bool has_immediate_member_in_ir
= false;
324 for (i
= 0; i
< fc
->members
->len
; i
++) {
325 struct ctf_named_field_class
*named_fc
=
326 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
328 if (named_fc
->fc
->in_ir
) {
329 has_immediate_member_in_ir
= true;
335 return has_immediate_member_in_ir
;
339 bt_field_class
*scope_ctf_field_class_to_ir(struct ctf_field_class
*fc
,
340 struct ctf_trace_class
*tc
,
341 struct ctf_stream_class
*sc
,
342 struct ctf_event_class
*ec
)
344 bt_field_class
*ir_fc
= NULL
;
350 BT_ASSERT(fc
->type
== CTF_FIELD_CLASS_TYPE_STRUCT
);
352 if (!ctf_field_class_struct_has_immediate_member_in_ir((void *) fc
)) {
354 * Nothing for IR in this scope: typical for packet
355 * header, packet context, and event header.
360 ir_fc
= ctf_field_class_to_ir(fc
, tc
, sc
, ec
);
367 struct ctf_field_class_int
*borrow_named_int_field_class(
368 struct ctf_field_class_struct
*struct_fc
, const char *name
)
370 struct ctf_named_field_class
*named_fc
= NULL
;
371 struct ctf_field_class_int
*int_fc
= NULL
;
377 named_fc
= ctf_field_class_struct_borrow_member_by_name(struct_fc
, name
);
382 if (named_fc
->fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&&
383 named_fc
->fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
387 int_fc
= (void *) named_fc
->fc
;
394 bt_event_class
*ctf_event_class_to_ir(struct ctf_event_class
*ec
,
395 bt_stream_class
*ir_sc
, struct ctf_trace_class
*tc
,
396 struct ctf_stream_class
*sc
)
399 bt_event_class
*ir_ec
= NULL
;
401 if (ec
->is_translated
) {
402 ir_ec
= bt_stream_class_borrow_event_class_by_id(
408 ir_ec
= bt_event_class_create_with_id(ir_sc
, ec
->id
);
410 bt_event_class_put_ref(ir_ec
);
412 if (ec
->spec_context_fc
) {
413 bt_field_class
*ir_fc
= scope_ctf_field_class_to_ir(
414 ec
->spec_context_fc
, tc
, sc
, ec
);
417 ret
= bt_event_class_set_specific_context_field_class(
420 bt_field_class_put_ref(ir_fc
);
424 if (ec
->payload_fc
) {
425 bt_field_class
*ir_fc
= scope_ctf_field_class_to_ir(
426 ec
->payload_fc
, tc
, sc
, ec
);
429 ret
= bt_event_class_set_payload_field_class(ir_ec
,
432 bt_field_class_put_ref(ir_fc
);
436 if (ec
->name
->len
> 0) {
437 ret
= bt_event_class_set_name(ir_ec
, ec
->name
->str
);
441 if (ec
->emf_uri
->len
> 0) {
442 ret
= bt_event_class_set_emf_uri(ir_ec
, ec
->emf_uri
->str
);
446 if (ec
->log_level
!= -1) {
447 bt_event_class_set_log_level(ir_ec
, ec
->log_level
);
450 ec
->is_translated
= true;
459 bt_stream_class
*ctf_stream_class_to_ir(struct ctf_stream_class
*sc
,
460 bt_trace_class
*ir_tc
, struct ctf_trace_class
*tc
)
463 bt_stream_class
*ir_sc
= NULL
;
464 struct ctf_field_class_int
*int_fc
;
466 if (sc
->is_translated
) {
467 ir_sc
= bt_trace_class_borrow_stream_class_by_id(ir_tc
, sc
->id
);
472 ir_sc
= bt_stream_class_create_with_id(ir_tc
, sc
->id
);
474 bt_stream_class_put_ref(ir_sc
);
476 if (sc
->packet_context_fc
) {
477 bt_field_class
*ir_fc
= scope_ctf_field_class_to_ir(
478 sc
->packet_context_fc
, tc
, sc
, NULL
);
481 ret
= bt_stream_class_set_packet_context_field_class(
484 bt_field_class_put_ref(ir_fc
);
488 if (sc
->event_header_fc
) {
489 bt_field_class
*ir_fc
= scope_ctf_field_class_to_ir(
490 sc
->event_header_fc
, tc
, sc
, NULL
);
493 ret
= bt_stream_class_set_event_header_field_class(
496 bt_field_class_put_ref(ir_fc
);
500 if (sc
->event_common_context_fc
) {
501 bt_field_class
*ir_fc
= scope_ctf_field_class_to_ir(
502 sc
->event_common_context_fc
, tc
, sc
, NULL
);
505 ret
= bt_stream_class_set_event_common_context_field_class(
508 bt_field_class_put_ref(ir_fc
);
512 bt_stream_class_set_assigns_automatic_event_class_id(ir_sc
,
514 bt_stream_class_set_assigns_automatic_stream_id(ir_sc
, BT_FALSE
);
516 if (sc
->default_clock_class
) {
517 BT_ASSERT(sc
->default_clock_class
->ir_cc
);
518 ret
= bt_stream_class_set_default_clock_class(ir_sc
,
519 sc
->default_clock_class
->ir_cc
);
523 int_fc
= borrow_named_int_field_class((void *) sc
->packet_context_fc
,
526 if (int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT
) {
527 bt_stream_class_set_packets_have_discarded_event_counter_snapshot(
532 int_fc
= borrow_named_int_field_class((void *) sc
->packet_context_fc
,
535 if (int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT
) {
536 bt_stream_class_set_packets_have_packet_counter_snapshot(
541 int_fc
= borrow_named_int_field_class((void *) sc
->packet_context_fc
,
544 if (int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME
) {
545 bt_stream_class_set_packets_have_default_beginning_clock_snapshot(
550 int_fc
= borrow_named_int_field_class((void *) sc
->packet_context_fc
,
553 if (int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_PACKET_END_TIME
) {
554 bt_stream_class_set_packets_have_default_end_clock_snapshot(
559 sc
->is_translated
= true;
567 void ctf_clock_class_to_ir(bt_clock_class
*ir_cc
, struct ctf_clock_class
*cc
)
571 if (strlen(cc
->name
->str
) > 0) {
572 ret
= bt_clock_class_set_name(ir_cc
, cc
->name
->str
);
576 if (strlen(cc
->description
->str
) > 0) {
577 ret
= bt_clock_class_set_description(ir_cc
, cc
->description
->str
);
581 bt_clock_class_set_frequency(ir_cc
, cc
->frequency
);
582 bt_clock_class_set_precision(ir_cc
, cc
->precision
);
583 bt_clock_class_set_offset(ir_cc
, cc
->offset_seconds
, cc
->offset_cycles
);
586 bt_clock_class_set_uuid(ir_cc
, cc
->uuid
);
589 bt_clock_class_set_is_absolute(ir_cc
, cc
->is_absolute
);
593 int ctf_trace_class_to_ir(bt_trace_class
*ir_tc
, struct ctf_trace_class
*tc
)
598 if (tc
->is_translated
) {
602 if (tc
->packet_header_fc
) {
603 bt_field_class
*ir_fc
= scope_ctf_field_class_to_ir(
604 tc
->packet_header_fc
, tc
, NULL
, NULL
);
607 ret
= bt_trace_class_set_packet_header_field_class(
610 bt_field_class_put_ref(ir_fc
);
614 if (tc
->is_uuid_set
) {
615 bt_trace_class_set_uuid(ir_tc
, tc
->uuid
);
618 for (i
= 0; i
< tc
->env_entries
->len
; i
++) {
619 struct ctf_trace_class_env_entry
*env_entry
=
620 ctf_trace_class_borrow_env_entry_by_index(tc
, i
);
622 switch (env_entry
->type
) {
623 case CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
:
624 ret
= bt_trace_class_set_environment_entry_integer(
625 ir_tc
, env_entry
->name
->str
,
628 case CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
:
629 ret
= bt_trace_class_set_environment_entry_string(
630 ir_tc
, env_entry
->name
->str
,
631 env_entry
->value
.str
->str
);
642 for (i
= 0; i
< tc
->clock_classes
->len
; i
++) {
643 struct ctf_clock_class
*cc
= tc
->clock_classes
->pdata
[i
];
645 cc
->ir_cc
= bt_clock_class_create(ir_tc
);
646 ctf_clock_class_to_ir(cc
->ir_cc
, cc
);
649 bt_trace_class_set_assigns_automatic_stream_class_id(ir_tc
,
651 tc
->is_translated
= true;
659 int ctf_trace_class_translate(bt_trace_class
*ir_tc
,
660 struct ctf_trace_class
*tc
)
665 ret
= ctf_trace_class_to_ir(ir_tc
, tc
);
670 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
672 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
673 bt_stream_class
*ir_sc
;
675 ir_sc
= ctf_stream_class_to_ir(sc
, ir_tc
, tc
);
681 for (j
= 0; j
< sc
->event_classes
->len
; j
++) {
682 struct ctf_event_class
*ec
= sc
->event_classes
->pdata
[j
];
683 bt_event_class
*ir_ec
;
685 ir_ec
= ctf_event_class_to_ir(ec
, ir_sc
, tc
, sc
);