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_self_component_source
*self_comp
;
30 bt_trace_class
*ir_tc
;
31 bt_stream_class
*ir_sc
;
32 struct ctf_trace_class
*tc
;
33 struct ctf_stream_class
*sc
;
34 struct ctf_event_class
*ec
;
39 bt_field_class
*ctf_field_class_to_ir(struct ctx
*ctx
,
40 struct ctf_field_class
*fc
);
43 void ctf_field_class_int_set_props(struct ctf_field_class_int
*fc
,
44 bt_field_class
*ir_fc
)
46 bt_field_class_integer_set_field_value_range(ir_fc
,
48 bt_field_class_integer_set_preferred_display_base(ir_fc
,
53 bt_field_class
*ctf_field_class_int_to_ir(struct ctx
*ctx
,
54 struct ctf_field_class_int
*fc
)
56 bt_field_class
*ir_fc
;
59 ir_fc
= bt_field_class_signed_integer_create(ctx
->ir_tc
);
61 ir_fc
= bt_field_class_unsigned_integer_create(ctx
->ir_tc
);
65 ctf_field_class_int_set_props(fc
, ir_fc
);
70 bt_field_class
*ctf_field_class_enum_to_ir(struct ctx
*ctx
,
71 struct ctf_field_class_enum
*fc
)
74 bt_field_class
*ir_fc
;
77 if (fc
->base
.is_signed
) {
78 ir_fc
= bt_field_class_signed_enumeration_create(ctx
->ir_tc
);
80 ir_fc
= bt_field_class_unsigned_enumeration_create(ctx
->ir_tc
);
84 ctf_field_class_int_set_props((void *) fc
, ir_fc
);
86 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
87 struct ctf_field_class_enum_mapping
*mapping
=
88 ctf_field_class_enum_borrow_mapping_by_index(fc
, i
);
90 if (fc
->base
.is_signed
) {
91 ret
= bt_field_class_signed_enumeration_map_range(
92 ir_fc
, mapping
->label
->str
,
93 mapping
->range
.lower
.i
, mapping
->range
.upper
.i
);
95 ret
= bt_field_class_unsigned_enumeration_map_range(
96 ir_fc
, mapping
->label
->str
,
97 mapping
->range
.lower
.u
, mapping
->range
.upper
.u
);
107 bt_field_class
*ctf_field_class_float_to_ir(struct ctx
*ctx
,
108 struct ctf_field_class_float
*fc
)
110 bt_field_class
*ir_fc
;
112 ir_fc
= bt_field_class_real_create(ctx
->ir_tc
);
115 if (fc
->base
.size
== 32) {
116 bt_field_class_real_set_is_single_precision(ir_fc
,
124 bt_field_class
*ctf_field_class_string_to_ir(struct ctx
*ctx
,
125 struct ctf_field_class_string
*fc
)
127 bt_field_class
*ir_fc
= bt_field_class_string_create(ctx
->ir_tc
);
134 void translate_struct_field_class_members(struct ctx
*ctx
,
135 struct ctf_field_class_struct
*fc
, bt_field_class
*ir_fc
,
136 bool with_header_prefix
,
137 struct ctf_field_class_struct
*context_fc
)
142 for (i
= 0; i
< fc
->members
->len
; i
++) {
143 struct ctf_named_field_class
*named_fc
=
144 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
145 bt_field_class
*member_ir_fc
;
146 const char *name
= named_fc
->name
->str
;
148 if (!named_fc
->fc
->in_ir
) {
152 member_ir_fc
= ctf_field_class_to_ir(ctx
, named_fc
->fc
);
153 BT_ASSERT(member_ir_fc
);
154 ret
= bt_field_class_structure_append_member(ir_fc
, name
,
157 bt_field_class_put_ref(member_ir_fc
);
162 bt_field_class
*ctf_field_class_struct_to_ir(struct ctx
*ctx
,
163 struct ctf_field_class_struct
*fc
)
165 bt_field_class
*ir_fc
= bt_field_class_structure_create(ctx
->ir_tc
);
168 translate_struct_field_class_members(ctx
, fc
, ir_fc
, false, NULL
);
173 bt_field_class
*borrow_ir_fc_from_field_path(struct ctx
*ctx
,
174 struct ctf_field_path
*field_path
)
176 bt_field_class
*ir_fc
= NULL
;
177 struct ctf_field_class
*fc
= ctf_field_path_borrow_field_class(
178 field_path
, ctx
->tc
, ctx
->sc
, ctx
->ec
);
190 bt_field_class
*ctf_field_class_variant_to_ir(struct ctx
*ctx
,
191 struct ctf_field_class_variant
*fc
)
194 bt_field_class
*ir_fc
= bt_field_class_variant_create(ctx
->ir_tc
);
199 if (fc
->tag_path
.root
!= CTF_SCOPE_PACKET_HEADER
&&
200 fc
->tag_path
.root
!= CTF_SCOPE_EVENT_HEADER
) {
201 ret
= bt_field_class_variant_set_selector_field_class(
202 ir_fc
, borrow_ir_fc_from_field_path(ctx
,
207 for (i
= 0; i
< fc
->options
->len
; i
++) {
208 struct ctf_named_field_class
*named_fc
=
209 ctf_field_class_variant_borrow_option_by_index(fc
, i
);
210 bt_field_class
*option_ir_fc
;
212 BT_ASSERT(named_fc
->fc
->in_ir
);
213 option_ir_fc
= ctf_field_class_to_ir(ctx
, named_fc
->fc
);
214 BT_ASSERT(option_ir_fc
);
215 ret
= bt_field_class_variant_append_option(
216 ir_fc
, named_fc
->name
->str
, option_ir_fc
);
218 bt_field_class_put_ref(option_ir_fc
);
225 bt_field_class
*ctf_field_class_array_to_ir(struct ctx
*ctx
,
226 struct ctf_field_class_array
*fc
)
228 bt_field_class
*ir_fc
;
229 bt_field_class
*elem_ir_fc
;
231 if (fc
->base
.is_text
) {
232 ir_fc
= bt_field_class_string_create(ctx
->ir_tc
);
237 elem_ir_fc
= ctf_field_class_to_ir(ctx
, fc
->base
.elem_fc
);
238 BT_ASSERT(elem_ir_fc
);
239 ir_fc
= bt_field_class_static_array_create(ctx
->ir_tc
, elem_ir_fc
,
242 bt_field_class_put_ref(elem_ir_fc
);
249 bt_field_class
*ctf_field_class_sequence_to_ir(struct ctx
*ctx
,
250 struct ctf_field_class_sequence
*fc
)
253 bt_field_class
*ir_fc
;
254 bt_field_class
*elem_ir_fc
;
256 if (fc
->base
.is_text
) {
257 ir_fc
= bt_field_class_string_create(ctx
->ir_tc
);
262 elem_ir_fc
= ctf_field_class_to_ir(ctx
, fc
->base
.elem_fc
);
263 BT_ASSERT(elem_ir_fc
);
264 ir_fc
= bt_field_class_dynamic_array_create(ctx
->ir_tc
, elem_ir_fc
);
266 bt_field_class_put_ref(elem_ir_fc
);
269 if (fc
->length_path
.root
!= CTF_SCOPE_PACKET_HEADER
&&
270 fc
->length_path
.root
!= CTF_SCOPE_EVENT_HEADER
) {
271 ret
= bt_field_class_dynamic_array_set_length_field_class(
272 ir_fc
, borrow_ir_fc_from_field_path(ctx
, &fc
->length_path
));
281 bt_field_class
*ctf_field_class_to_ir(struct ctx
*ctx
,
282 struct ctf_field_class
*fc
)
284 bt_field_class
*ir_fc
= NULL
;
287 BT_ASSERT(fc
->in_ir
);
290 case CTF_FIELD_CLASS_TYPE_INT
:
291 ir_fc
= ctf_field_class_int_to_ir(ctx
, (void *) fc
);
293 case CTF_FIELD_CLASS_TYPE_ENUM
:
294 ir_fc
= ctf_field_class_enum_to_ir(ctx
, (void *) fc
);
296 case CTF_FIELD_CLASS_TYPE_FLOAT
:
297 ir_fc
= ctf_field_class_float_to_ir(ctx
, (void *) fc
);
299 case CTF_FIELD_CLASS_TYPE_STRING
:
300 ir_fc
= ctf_field_class_string_to_ir(ctx
, (void *) fc
);
302 case CTF_FIELD_CLASS_TYPE_STRUCT
:
303 ir_fc
= ctf_field_class_struct_to_ir(ctx
, (void *) fc
);
305 case CTF_FIELD_CLASS_TYPE_ARRAY
:
306 ir_fc
= ctf_field_class_array_to_ir(ctx
, (void *) fc
);
308 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
309 ir_fc
= ctf_field_class_sequence_to_ir(ctx
, (void *) fc
);
311 case CTF_FIELD_CLASS_TYPE_VARIANT
:
312 ir_fc
= ctf_field_class_variant_to_ir(ctx
, (void *) fc
);
323 bool ctf_field_class_struct_has_immediate_member_in_ir(
324 struct ctf_field_class_struct
*fc
)
327 bool has_immediate_member_in_ir
= false;
330 * If the structure field class has no members at all, then it
331 * was an empty structure in the beginning, so leave it existing
334 if (fc
->members
->len
== 0) {
335 has_immediate_member_in_ir
= true;
339 for (i
= 0; i
< fc
->members
->len
; i
++) {
340 struct ctf_named_field_class
*named_fc
=
341 ctf_field_class_struct_borrow_member_by_index(fc
, i
);
343 if (named_fc
->fc
->in_ir
) {
344 has_immediate_member_in_ir
= true;
350 return has_immediate_member_in_ir
;
354 bt_field_class
*scope_ctf_field_class_to_ir(struct ctx
*ctx
)
356 bt_field_class
*ir_fc
= NULL
;
357 struct ctf_field_class
*fc
= NULL
;
359 switch (ctx
->scope
) {
360 case CTF_SCOPE_PACKET_CONTEXT
:
361 fc
= ctx
->sc
->packet_context_fc
;
363 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
364 fc
= ctx
->sc
->event_common_context_fc
;
366 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
367 fc
= ctx
->ec
->spec_context_fc
;
369 case CTF_SCOPE_EVENT_PAYLOAD
:
370 fc
= ctx
->ec
->payload_fc
;
376 if (fc
&& ctf_field_class_struct_has_immediate_member_in_ir(
378 ir_fc
= ctf_field_class_to_ir(ctx
, fc
);
385 void ctf_event_class_to_ir(struct ctx
*ctx
)
388 bt_event_class
*ir_ec
= NULL
;
389 bt_field_class
*ir_fc
;
393 if (ctx
->ec
->is_translated
) {
394 ir_ec
= bt_stream_class_borrow_event_class_by_id(
395 ctx
->ir_sc
, ctx
->ec
->id
);
400 ir_ec
= bt_event_class_create_with_id(ctx
->ir_sc
, ctx
->ec
->id
);
402 bt_event_class_put_ref(ir_ec
);
403 ctx
->scope
= CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
;
404 ir_fc
= scope_ctf_field_class_to_ir(ctx
);
406 ret
= bt_event_class_set_specific_context_field_class(
409 bt_field_class_put_ref(ir_fc
);
412 ctx
->scope
= CTF_SCOPE_EVENT_PAYLOAD
;
413 ir_fc
= scope_ctf_field_class_to_ir(ctx
);
415 ret
= bt_event_class_set_payload_field_class(ir_ec
,
418 bt_field_class_put_ref(ir_fc
);
421 if (ctx
->ec
->name
->len
> 0) {
422 ret
= bt_event_class_set_name(ir_ec
, ctx
->ec
->name
->str
);
426 if (ctx
->ec
->emf_uri
->len
> 0) {
427 ret
= bt_event_class_set_emf_uri(ir_ec
, ctx
->ec
->emf_uri
->str
);
431 if (ctx
->ec
->log_level
!= -1) {
432 bt_event_class_set_log_level(ir_ec
, ctx
->ec
->log_level
);
435 ctx
->ec
->is_translated
= true;
436 ctx
->ec
->ir_ec
= ir_ec
;
444 void ctf_stream_class_to_ir(struct ctx
*ctx
)
447 bt_field_class
*ir_fc
;
451 if (ctx
->sc
->is_translated
) {
452 ctx
->ir_sc
= bt_trace_class_borrow_stream_class_by_id(
453 ctx
->ir_tc
, ctx
->sc
->id
);
454 BT_ASSERT(ctx
->ir_sc
);
458 ctx
->ir_sc
= bt_stream_class_create_with_id(ctx
->ir_tc
, ctx
->sc
->id
);
459 BT_ASSERT(ctx
->ir_sc
);
460 bt_stream_class_put_ref(ctx
->ir_sc
);
461 ctx
->scope
= CTF_SCOPE_PACKET_CONTEXT
;
462 ir_fc
= scope_ctf_field_class_to_ir(ctx
);
464 ret
= bt_stream_class_set_packet_context_field_class(
467 bt_field_class_put_ref(ir_fc
);
470 ctx
->scope
= CTF_SCOPE_EVENT_COMMON_CONTEXT
;
471 ir_fc
= scope_ctf_field_class_to_ir(ctx
);
473 ret
= bt_stream_class_set_event_common_context_field_class(
476 bt_field_class_put_ref(ir_fc
);
479 bt_stream_class_set_assigns_automatic_event_class_id(ctx
->ir_sc
,
481 bt_stream_class_set_assigns_automatic_stream_id(ctx
->ir_sc
, BT_FALSE
);
483 if (ctx
->sc
->default_clock_class
) {
484 BT_ASSERT(ctx
->sc
->default_clock_class
->ir_cc
);
485 ret
= bt_stream_class_set_default_clock_class(ctx
->ir_sc
,
486 ctx
->sc
->default_clock_class
->ir_cc
);
490 bt_stream_class_set_packets_have_beginning_default_clock_snapshot(
491 ctx
->ir_sc
, ctx
->sc
->packets_have_ts_begin
);
492 bt_stream_class_set_packets_have_end_default_clock_snapshot(
493 ctx
->ir_sc
, ctx
->sc
->packets_have_ts_end
);
494 bt_stream_class_set_supports_discarded_events(ctx
->ir_sc
,
495 ctx
->sc
->has_discarded_events
,
496 ctx
->sc
->discarded_events_have_default_cs
);
497 bt_stream_class_set_supports_discarded_packets(ctx
->ir_sc
,
498 ctx
->sc
->has_discarded_packets
,
499 ctx
->sc
->discarded_packets_have_default_cs
);
500 ctx
->sc
->is_translated
= true;
501 ctx
->sc
->ir_sc
= ctx
->ir_sc
;
508 void ctf_clock_class_to_ir(bt_clock_class
*ir_cc
, struct ctf_clock_class
*cc
)
512 if (strlen(cc
->name
->str
) > 0) {
513 ret
= bt_clock_class_set_name(ir_cc
, cc
->name
->str
);
517 if (strlen(cc
->description
->str
) > 0) {
518 ret
= bt_clock_class_set_description(ir_cc
, cc
->description
->str
);
522 bt_clock_class_set_frequency(ir_cc
, cc
->frequency
);
523 bt_clock_class_set_precision(ir_cc
, cc
->precision
);
524 bt_clock_class_set_offset(ir_cc
, cc
->offset_seconds
, cc
->offset_cycles
);
527 bt_clock_class_set_uuid(ir_cc
, cc
->uuid
);
530 bt_clock_class_set_origin_is_unix_epoch(ir_cc
, cc
->is_absolute
);
534 int ctf_trace_class_to_ir(struct ctx
*ctx
)
540 BT_ASSERT(ctx
->ir_tc
);
542 if (ctx
->tc
->is_translated
) {
546 if (ctx
->tc
->is_uuid_set
) {
547 bt_trace_class_set_uuid(ctx
->ir_tc
, ctx
->tc
->uuid
);
550 for (i
= 0; i
< ctx
->tc
->env_entries
->len
; i
++) {
551 struct ctf_trace_class_env_entry
*env_entry
=
552 ctf_trace_class_borrow_env_entry_by_index(ctx
->tc
, i
);
554 switch (env_entry
->type
) {
555 case CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
:
556 ret
= bt_trace_class_set_environment_entry_integer(
557 ctx
->ir_tc
, env_entry
->name
->str
,
560 case CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
:
561 ret
= bt_trace_class_set_environment_entry_string(
562 ctx
->ir_tc
, env_entry
->name
->str
,
563 env_entry
->value
.str
->str
);
574 for (i
= 0; i
< ctx
->tc
->clock_classes
->len
; i
++) {
575 struct ctf_clock_class
*cc
= ctx
->tc
->clock_classes
->pdata
[i
];
577 cc
->ir_cc
= bt_clock_class_create(
578 bt_self_component_source_as_self_component(
580 ctf_clock_class_to_ir(cc
->ir_cc
, cc
);
583 bt_trace_class_set_assigns_automatic_stream_class_id(ctx
->ir_tc
,
585 ctx
->tc
->is_translated
= true;
586 ctx
->tc
->ir_tc
= ctx
->ir_tc
;
593 int ctf_trace_class_translate(bt_self_component_source
*self_comp
,
594 bt_trace_class
*ir_tc
, struct ctf_trace_class
*tc
)
598 struct ctx ctx
= { 0 };
600 ctx
.self_comp
= self_comp
;
603 ret
= ctf_trace_class_to_ir(&ctx
);
608 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
610 ctx
.sc
= tc
->stream_classes
->pdata
[i
];
612 ctf_stream_class_to_ir(&ctx
);
614 for (j
= 0; j
< ctx
.sc
->event_classes
->len
; j
++) {
615 ctx
.ec
= ctx
.sc
->event_classes
->pdata
[j
];
617 ctf_event_class_to_ir(&ctx
);