Commit | Line | Data |
---|---|---|
44c440bc | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
44c440bc | 3 | * |
0235b0db | 4 | * Copyright 2018 Philippe Proulx <pproulx@efficios.com> |
44c440bc PP |
5 | */ |
6 | ||
3fadfbc0 | 7 | #include <babeltrace2/babeltrace.h> |
91d81473 | 8 | #include "common/macros.h" |
578e048b | 9 | #include "common/assert.h" |
44c440bc | 10 | #include <glib.h> |
c4f23e30 | 11 | #include <stdbool.h> |
44c440bc PP |
12 | #include <stdint.h> |
13 | #include <string.h> | |
14 | #include <inttypes.h> | |
15 | ||
16 | #include "ctf-meta-visitors.h" | |
17 | ||
83ebb7f1 | 18 | struct ctx { |
f7b785ac | 19 | bt_self_component *self_comp; |
83ebb7f1 PP |
20 | bt_trace_class *ir_tc; |
21 | bt_stream_class *ir_sc; | |
22 | struct ctf_trace_class *tc; | |
23 | struct ctf_stream_class *sc; | |
24 | struct ctf_event_class *ec; | |
25 | enum ctf_scope scope; | |
26 | }; | |
27 | ||
44c440bc | 28 | static inline |
83ebb7f1 PP |
29 | bt_field_class *ctf_field_class_to_ir(struct ctx *ctx, |
30 | struct ctf_field_class *fc); | |
44c440bc PP |
31 | |
32 | static inline | |
5cd6d0e5 | 33 | void ctf_field_class_int_set_props(struct ctf_field_class_int *fc, |
b19ff26f | 34 | bt_field_class *ir_fc) |
44c440bc | 35 | { |
40f4ba76 | 36 | bt_field_class_integer_set_field_value_range(ir_fc, |
e5be10ef | 37 | fc->base.size); |
40f4ba76 | 38 | bt_field_class_integer_set_preferred_display_base(ir_fc, |
5cd6d0e5 | 39 | fc->disp_base); |
44c440bc PP |
40 | } |
41 | ||
42 | static inline | |
83ebb7f1 | 43 | bt_field_class *ctf_field_class_int_to_ir(struct ctx *ctx, |
e5be10ef | 44 | struct ctf_field_class_int *fc) |
44c440bc | 45 | { |
b19ff26f | 46 | bt_field_class *ir_fc; |
44c440bc | 47 | |
5cd6d0e5 | 48 | if (fc->is_signed) { |
9c08c816 | 49 | ir_fc = bt_field_class_integer_signed_create(ctx->ir_tc); |
44c440bc | 50 | } else { |
9c08c816 | 51 | ir_fc = bt_field_class_integer_unsigned_create(ctx->ir_tc); |
44c440bc PP |
52 | } |
53 | ||
5cd6d0e5 PP |
54 | BT_ASSERT(ir_fc); |
55 | ctf_field_class_int_set_props(fc, ir_fc); | |
56 | return ir_fc; | |
44c440bc PP |
57 | } |
58 | ||
59 | static inline | |
83ebb7f1 | 60 | bt_field_class *ctf_field_class_enum_to_ir(struct ctx *ctx, |
e5be10ef | 61 | struct ctf_field_class_enum *fc) |
44c440bc PP |
62 | { |
63 | int ret; | |
b19ff26f | 64 | bt_field_class *ir_fc; |
44c440bc PP |
65 | uint64_t i; |
66 | ||
5cd6d0e5 | 67 | if (fc->base.is_signed) { |
9c08c816 | 68 | ir_fc = bt_field_class_enumeration_signed_create(ctx->ir_tc); |
44c440bc | 69 | } else { |
9c08c816 | 70 | ir_fc = bt_field_class_enumeration_unsigned_create(ctx->ir_tc); |
44c440bc PP |
71 | } |
72 | ||
5cd6d0e5 PP |
73 | BT_ASSERT(ir_fc); |
74 | ctf_field_class_int_set_props((void *) fc, ir_fc); | |
44c440bc | 75 | |
5cd6d0e5 PP |
76 | for (i = 0; i < fc->mappings->len; i++) { |
77 | struct ctf_field_class_enum_mapping *mapping = | |
78 | ctf_field_class_enum_borrow_mapping_by_index(fc, i); | |
45c51519 PP |
79 | void *range_set; |
80 | uint64_t range_i; | |
44c440bc | 81 | |
5cd6d0e5 | 82 | if (fc->base.is_signed) { |
45c51519 | 83 | range_set = bt_integer_range_set_signed_create(); |
44c440bc | 84 | } else { |
45c51519 PP |
85 | range_set = bt_integer_range_set_unsigned_create(); |
86 | } | |
87 | ||
88 | BT_ASSERT(range_set); | |
89 | ||
90 | for (range_i = 0; range_i < mapping->ranges->len; range_i++) { | |
91 | struct ctf_range *range = | |
92 | ctf_field_class_enum_mapping_borrow_range_by_index( | |
93 | mapping, range_i); | |
94 | ||
95 | if (fc->base.is_signed) { | |
96 | ret = bt_integer_range_set_signed_add_range( | |
97 | range_set, range->lower.i, | |
98 | range->upper.i); | |
99 | } else { | |
100 | ret = bt_integer_range_set_unsigned_add_range( | |
101 | range_set, range->lower.u, | |
102 | range->upper.u); | |
103 | } | |
104 | ||
105 | BT_ASSERT(ret == 0); | |
106 | } | |
107 | ||
108 | if (fc->base.is_signed) { | |
9c08c816 | 109 | ret = bt_field_class_enumeration_signed_add_mapping( |
45c51519 | 110 | ir_fc, mapping->label->str, range_set); |
eb171ee9 | 111 | BT_INTEGER_RANGE_SET_SIGNED_PUT_REF_AND_RESET(range_set); |
45c51519 | 112 | } else { |
9c08c816 | 113 | ret = bt_field_class_enumeration_unsigned_add_mapping( |
45c51519 | 114 | ir_fc, mapping->label->str, range_set); |
eb171ee9 | 115 | BT_INTEGER_RANGE_SET_UNSIGNED_PUT_REF_AND_RESET(range_set); |
44c440bc PP |
116 | } |
117 | ||
118 | BT_ASSERT(ret == 0); | |
119 | } | |
120 | ||
5cd6d0e5 | 121 | return ir_fc; |
44c440bc PP |
122 | } |
123 | ||
124 | static inline | |
83ebb7f1 | 125 | bt_field_class *ctf_field_class_float_to_ir(struct ctx *ctx, |
5cd6d0e5 | 126 | struct ctf_field_class_float *fc) |
44c440bc | 127 | { |
b19ff26f | 128 | bt_field_class *ir_fc; |
44c440bc | 129 | |
5cd6d0e5 | 130 | if (fc->base.size == 32) { |
fe4df857 FD |
131 | ir_fc = bt_field_class_real_single_precision_create(ctx->ir_tc); |
132 | } else { | |
133 | ir_fc = bt_field_class_real_double_precision_create(ctx->ir_tc); | |
44c440bc | 134 | } |
fe4df857 | 135 | BT_ASSERT(ir_fc); |
44c440bc | 136 | |
5cd6d0e5 | 137 | return ir_fc; |
44c440bc PP |
138 | } |
139 | ||
140 | static inline | |
83ebb7f1 | 141 | bt_field_class *ctf_field_class_string_to_ir(struct ctx *ctx, |
5cd6d0e5 | 142 | struct ctf_field_class_string *fc) |
44c440bc | 143 | { |
83ebb7f1 | 144 | bt_field_class *ir_fc = bt_field_class_string_create(ctx->ir_tc); |
44c440bc | 145 | |
5cd6d0e5 PP |
146 | BT_ASSERT(ir_fc); |
147 | return ir_fc; | |
44c440bc PP |
148 | } |
149 | ||
150 | static inline | |
83ebb7f1 PP |
151 | void translate_struct_field_class_members(struct ctx *ctx, |
152 | struct ctf_field_class_struct *fc, bt_field_class *ir_fc, | |
153 | bool with_header_prefix, | |
154 | struct ctf_field_class_struct *context_fc) | |
44c440bc | 155 | { |
44c440bc | 156 | uint64_t i; |
83ebb7f1 | 157 | int ret; |
44c440bc | 158 | |
5cd6d0e5 PP |
159 | for (i = 0; i < fc->members->len; i++) { |
160 | struct ctf_named_field_class *named_fc = | |
161 | ctf_field_class_struct_borrow_member_by_index(fc, i); | |
b19ff26f | 162 | bt_field_class *member_ir_fc; |
83ebb7f1 | 163 | const char *name = named_fc->name->str; |
44c440bc | 164 | |
5cd6d0e5 | 165 | if (!named_fc->fc->in_ir) { |
44c440bc PP |
166 | continue; |
167 | } | |
168 | ||
83ebb7f1 | 169 | member_ir_fc = ctf_field_class_to_ir(ctx, named_fc->fc); |
5cd6d0e5 | 170 | BT_ASSERT(member_ir_fc); |
83ebb7f1 PP |
171 | ret = bt_field_class_structure_append_member(ir_fc, name, |
172 | member_ir_fc); | |
44c440bc | 173 | BT_ASSERT(ret == 0); |
c5b9b441 | 174 | bt_field_class_put_ref(member_ir_fc); |
44c440bc | 175 | } |
83ebb7f1 | 176 | } |
44c440bc | 177 | |
83ebb7f1 PP |
178 | static inline |
179 | bt_field_class *ctf_field_class_struct_to_ir(struct ctx *ctx, | |
180 | struct ctf_field_class_struct *fc) | |
181 | { | |
182 | bt_field_class *ir_fc = bt_field_class_structure_create(ctx->ir_tc); | |
183 | ||
184 | BT_ASSERT(ir_fc); | |
185 | translate_struct_field_class_members(ctx, fc, ir_fc, false, NULL); | |
5cd6d0e5 | 186 | return ir_fc; |
44c440bc PP |
187 | } |
188 | ||
189 | static inline | |
83ebb7f1 PP |
190 | bt_field_class *borrow_ir_fc_from_field_path(struct ctx *ctx, |
191 | struct ctf_field_path *field_path) | |
44c440bc | 192 | { |
b19ff26f | 193 | bt_field_class *ir_fc = NULL; |
5cd6d0e5 | 194 | struct ctf_field_class *fc = ctf_field_path_borrow_field_class( |
83ebb7f1 | 195 | field_path, ctx->tc, ctx->sc, ctx->ec); |
44c440bc | 196 | |
5cd6d0e5 | 197 | BT_ASSERT(fc); |
44c440bc | 198 | |
5cd6d0e5 PP |
199 | if (fc->in_ir) { |
200 | ir_fc = fc->ir_fc; | |
44c440bc PP |
201 | } |
202 | ||
5cd6d0e5 | 203 | return ir_fc; |
44c440bc PP |
204 | } |
205 | ||
45c51519 PP |
206 | static inline |
207 | const void *find_ir_enum_field_class_mapping_by_label(const bt_field_class *fc, | |
208 | const char *label, bool is_signed) | |
209 | { | |
210 | const void *mapping = NULL; | |
211 | uint64_t i; | |
212 | ||
213 | for (i = 0; i < bt_field_class_enumeration_get_mapping_count(fc); i++) { | |
214 | const bt_field_class_enumeration_mapping *this_mapping; | |
215 | const void *spec_this_mapping; | |
216 | ||
217 | if (is_signed) { | |
218 | spec_this_mapping = | |
9c08c816 | 219 | bt_field_class_enumeration_signed_borrow_mapping_by_index_const( |
45c51519 PP |
220 | fc, i); |
221 | this_mapping = | |
9c08c816 | 222 | bt_field_class_enumeration_signed_mapping_as_mapping_const( |
45c51519 PP |
223 | spec_this_mapping); |
224 | } else { | |
225 | spec_this_mapping = | |
9c08c816 | 226 | bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const( |
45c51519 PP |
227 | fc, i); |
228 | this_mapping = | |
9c08c816 | 229 | bt_field_class_enumeration_unsigned_mapping_as_mapping_const( |
45c51519 PP |
230 | spec_this_mapping); |
231 | } | |
232 | ||
233 | BT_ASSERT(this_mapping); | |
234 | BT_ASSERT(spec_this_mapping); | |
235 | ||
236 | if (strcmp(bt_field_class_enumeration_mapping_get_label( | |
237 | this_mapping), label) == 0) { | |
238 | mapping = spec_this_mapping; | |
239 | goto end; | |
240 | } | |
241 | } | |
242 | ||
243 | end: | |
244 | return mapping; | |
245 | } | |
246 | ||
44c440bc | 247 | static inline |
83ebb7f1 PP |
248 | bt_field_class *ctf_field_class_variant_to_ir(struct ctx *ctx, |
249 | struct ctf_field_class_variant *fc) | |
44c440bc PP |
250 | { |
251 | int ret; | |
45c51519 | 252 | bt_field_class *ir_fc; |
44c440bc | 253 | uint64_t i; |
45c51519 | 254 | bt_field_class *ir_tag_fc = NULL; |
83ebb7f1 PP |
255 | |
256 | if (fc->tag_path.root != CTF_SCOPE_PACKET_HEADER && | |
257 | fc->tag_path.root != CTF_SCOPE_EVENT_HEADER) { | |
45c51519 PP |
258 | ir_tag_fc = borrow_ir_fc_from_field_path(ctx, &fc->tag_path); |
259 | BT_ASSERT(ir_tag_fc); | |
83ebb7f1 | 260 | } |
44c440bc | 261 | |
45c51519 PP |
262 | ir_fc = bt_field_class_variant_create(ctx->ir_tc, ir_tag_fc); |
263 | BT_ASSERT(ir_fc); | |
264 | ||
5cd6d0e5 PP |
265 | for (i = 0; i < fc->options->len; i++) { |
266 | struct ctf_named_field_class *named_fc = | |
267 | ctf_field_class_variant_borrow_option_by_index(fc, i); | |
b19ff26f | 268 | bt_field_class *option_ir_fc; |
44c440bc | 269 | |
5cd6d0e5 | 270 | BT_ASSERT(named_fc->fc->in_ir); |
83ebb7f1 | 271 | option_ir_fc = ctf_field_class_to_ir(ctx, named_fc->fc); |
5cd6d0e5 | 272 | BT_ASSERT(option_ir_fc); |
45c51519 PP |
273 | |
274 | if (ir_tag_fc) { | |
275 | /* | |
276 | * At this point the trace IR selector | |
277 | * (enumeration) field class already exists if | |
278 | * the variant is tagged (`ir_tag_fc`). This one | |
279 | * already contains range sets for its mappings, | |
280 | * so we just reuse the same, finding them by | |
281 | * matching a variant field class's option's | |
282 | * _original_ name (with a leading underscore, | |
283 | * possibly) with a selector field class's | |
284 | * mapping name. | |
285 | */ | |
286 | if (fc->tag_fc->base.is_signed) { | |
9c08c816 | 287 | const bt_field_class_enumeration_signed_mapping *mapping = |
45c51519 PP |
288 | find_ir_enum_field_class_mapping_by_label( |
289 | ir_tag_fc, | |
290 | named_fc->orig_name->str, true); | |
291 | const bt_integer_range_set_signed *range_set; | |
292 | ||
293 | BT_ASSERT(mapping); | |
294 | range_set = | |
9c08c816 | 295 | bt_field_class_enumeration_signed_mapping_borrow_ranges_const( |
45c51519 PP |
296 | mapping); |
297 | BT_ASSERT(range_set); | |
de821fe5 | 298 | ret = bt_field_class_variant_with_selector_field_integer_signed_append_option( |
45c51519 PP |
299 | ir_fc, named_fc->name->str, |
300 | option_ir_fc, range_set); | |
301 | } else { | |
9c08c816 | 302 | const bt_field_class_enumeration_unsigned_mapping *mapping = |
45c51519 PP |
303 | find_ir_enum_field_class_mapping_by_label( |
304 | ir_tag_fc, | |
305 | named_fc->orig_name->str, | |
306 | false); | |
307 | const bt_integer_range_set_unsigned *range_set; | |
308 | ||
309 | BT_ASSERT(mapping); | |
310 | range_set = | |
9c08c816 | 311 | bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const( |
45c51519 PP |
312 | mapping); |
313 | BT_ASSERT(range_set); | |
de821fe5 | 314 | ret = bt_field_class_variant_with_selector_field_integer_unsigned_append_option( |
45c51519 PP |
315 | ir_fc, named_fc->name->str, |
316 | option_ir_fc, range_set); | |
317 | } | |
318 | } else { | |
319 | ret = bt_field_class_variant_without_selector_append_option( | |
320 | ir_fc, named_fc->name->str, option_ir_fc); | |
321 | } | |
322 | ||
44c440bc | 323 | BT_ASSERT(ret == 0); |
c5b9b441 | 324 | bt_field_class_put_ref(option_ir_fc); |
44c440bc PP |
325 | } |
326 | ||
5cd6d0e5 | 327 | return ir_fc; |
44c440bc PP |
328 | } |
329 | ||
330 | static inline | |
83ebb7f1 PP |
331 | bt_field_class *ctf_field_class_array_to_ir(struct ctx *ctx, |
332 | struct ctf_field_class_array *fc) | |
44c440bc | 333 | { |
b19ff26f PP |
334 | bt_field_class *ir_fc; |
335 | bt_field_class *elem_ir_fc; | |
44c440bc | 336 | |
5cd6d0e5 | 337 | if (fc->base.is_text) { |
83ebb7f1 | 338 | ir_fc = bt_field_class_string_create(ctx->ir_tc); |
5cd6d0e5 | 339 | BT_ASSERT(ir_fc); |
44c440bc PP |
340 | goto end; |
341 | } | |
342 | ||
83ebb7f1 | 343 | elem_ir_fc = ctf_field_class_to_ir(ctx, fc->base.elem_fc); |
5cd6d0e5 | 344 | BT_ASSERT(elem_ir_fc); |
9c08c816 | 345 | ir_fc = bt_field_class_array_static_create(ctx->ir_tc, elem_ir_fc, |
e5be10ef | 346 | fc->length); |
5cd6d0e5 | 347 | BT_ASSERT(ir_fc); |
c5b9b441 | 348 | bt_field_class_put_ref(elem_ir_fc); |
44c440bc PP |
349 | |
350 | end: | |
5cd6d0e5 | 351 | return ir_fc; |
44c440bc PP |
352 | } |
353 | ||
354 | static inline | |
83ebb7f1 PP |
355 | bt_field_class *ctf_field_class_sequence_to_ir(struct ctx *ctx, |
356 | struct ctf_field_class_sequence *fc) | |
44c440bc | 357 | { |
b19ff26f PP |
358 | bt_field_class *ir_fc; |
359 | bt_field_class *elem_ir_fc; | |
1367bc7c | 360 | bt_field_class *length_fc = NULL; |
44c440bc | 361 | |
5cd6d0e5 | 362 | if (fc->base.is_text) { |
83ebb7f1 | 363 | ir_fc = bt_field_class_string_create(ctx->ir_tc); |
5cd6d0e5 | 364 | BT_ASSERT(ir_fc); |
44c440bc PP |
365 | goto end; |
366 | } | |
367 | ||
83ebb7f1 | 368 | elem_ir_fc = ctf_field_class_to_ir(ctx, fc->base.elem_fc); |
5cd6d0e5 | 369 | BT_ASSERT(elem_ir_fc); |
83ebb7f1 PP |
370 | |
371 | if (fc->length_path.root != CTF_SCOPE_PACKET_HEADER && | |
372 | fc->length_path.root != CTF_SCOPE_EVENT_HEADER) { | |
1367bc7c PP |
373 | length_fc = borrow_ir_fc_from_field_path(ctx, &fc->length_path); |
374 | BT_ASSERT(length_fc); | |
83ebb7f1 | 375 | } |
44c440bc | 376 | |
9c08c816 | 377 | ir_fc = bt_field_class_array_dynamic_create(ctx->ir_tc, elem_ir_fc, |
1367bc7c PP |
378 | length_fc); |
379 | BT_ASSERT(ir_fc); | |
380 | bt_field_class_put_ref(elem_ir_fc); | |
381 | BT_ASSERT(ir_fc); | |
382 | ||
44c440bc | 383 | end: |
5cd6d0e5 | 384 | return ir_fc; |
44c440bc PP |
385 | } |
386 | ||
387 | static inline | |
83ebb7f1 PP |
388 | bt_field_class *ctf_field_class_to_ir(struct ctx *ctx, |
389 | struct ctf_field_class *fc) | |
44c440bc | 390 | { |
b19ff26f | 391 | bt_field_class *ir_fc = NULL; |
44c440bc | 392 | |
5cd6d0e5 PP |
393 | BT_ASSERT(fc); |
394 | BT_ASSERT(fc->in_ir); | |
44c440bc | 395 | |
864cad70 PP |
396 | switch (fc->type) { |
397 | case CTF_FIELD_CLASS_TYPE_INT: | |
83ebb7f1 | 398 | ir_fc = ctf_field_class_int_to_ir(ctx, (void *) fc); |
44c440bc | 399 | break; |
864cad70 | 400 | case CTF_FIELD_CLASS_TYPE_ENUM: |
83ebb7f1 | 401 | ir_fc = ctf_field_class_enum_to_ir(ctx, (void *) fc); |
44c440bc | 402 | break; |
864cad70 | 403 | case CTF_FIELD_CLASS_TYPE_FLOAT: |
83ebb7f1 | 404 | ir_fc = ctf_field_class_float_to_ir(ctx, (void *) fc); |
44c440bc | 405 | break; |
864cad70 | 406 | case CTF_FIELD_CLASS_TYPE_STRING: |
83ebb7f1 | 407 | ir_fc = ctf_field_class_string_to_ir(ctx, (void *) fc); |
44c440bc | 408 | break; |
864cad70 | 409 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
83ebb7f1 | 410 | ir_fc = ctf_field_class_struct_to_ir(ctx, (void *) fc); |
44c440bc | 411 | break; |
864cad70 | 412 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
83ebb7f1 | 413 | ir_fc = ctf_field_class_array_to_ir(ctx, (void *) fc); |
44c440bc | 414 | break; |
864cad70 | 415 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
83ebb7f1 | 416 | ir_fc = ctf_field_class_sequence_to_ir(ctx, (void *) fc); |
44c440bc | 417 | break; |
864cad70 | 418 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
83ebb7f1 | 419 | ir_fc = ctf_field_class_variant_to_ir(ctx, (void *) fc); |
44c440bc PP |
420 | break; |
421 | default: | |
498e7994 | 422 | bt_common_abort(); |
44c440bc PP |
423 | } |
424 | ||
5cd6d0e5 PP |
425 | fc->ir_fc = ir_fc; |
426 | return ir_fc; | |
44c440bc PP |
427 | } |
428 | ||
429 | static inline | |
5cd6d0e5 PP |
430 | bool ctf_field_class_struct_has_immediate_member_in_ir( |
431 | struct ctf_field_class_struct *fc) | |
44c440bc PP |
432 | { |
433 | uint64_t i; | |
434 | bool has_immediate_member_in_ir = false; | |
435 | ||
b2c863b0 PP |
436 | /* |
437 | * If the structure field class has no members at all, then it | |
438 | * was an empty structure in the beginning, so leave it existing | |
439 | * and empty. | |
440 | */ | |
441 | if (fc->members->len == 0) { | |
442 | has_immediate_member_in_ir = true; | |
443 | goto end; | |
444 | } | |
445 | ||
5cd6d0e5 PP |
446 | for (i = 0; i < fc->members->len; i++) { |
447 | struct ctf_named_field_class *named_fc = | |
448 | ctf_field_class_struct_borrow_member_by_index(fc, i); | |
44c440bc | 449 | |
5cd6d0e5 | 450 | if (named_fc->fc->in_ir) { |
44c440bc PP |
451 | has_immediate_member_in_ir = true; |
452 | goto end; | |
453 | } | |
454 | } | |
455 | ||
456 | end: | |
457 | return has_immediate_member_in_ir; | |
458 | } | |
459 | ||
460 | static inline | |
83ebb7f1 | 461 | bt_field_class *scope_ctf_field_class_to_ir(struct ctx *ctx) |
44c440bc | 462 | { |
b19ff26f | 463 | bt_field_class *ir_fc = NULL; |
83ebb7f1 | 464 | struct ctf_field_class *fc = NULL; |
44c440bc | 465 | |
83ebb7f1 PP |
466 | switch (ctx->scope) { |
467 | case CTF_SCOPE_PACKET_CONTEXT: | |
468 | fc = ctx->sc->packet_context_fc; | |
469 | break; | |
470 | case CTF_SCOPE_EVENT_COMMON_CONTEXT: | |
471 | fc = ctx->sc->event_common_context_fc; | |
472 | break; | |
473 | case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT: | |
474 | fc = ctx->ec->spec_context_fc; | |
475 | break; | |
476 | case CTF_SCOPE_EVENT_PAYLOAD: | |
477 | fc = ctx->ec->payload_fc; | |
478 | break; | |
479 | default: | |
498e7994 | 480 | bt_common_abort(); |
44c440bc PP |
481 | } |
482 | ||
83ebb7f1 PP |
483 | if (fc && ctf_field_class_struct_has_immediate_member_in_ir( |
484 | (void *) fc)) { | |
485 | ir_fc = ctf_field_class_to_ir(ctx, fc); | |
44c440bc PP |
486 | } |
487 | ||
5cd6d0e5 | 488 | return ir_fc; |
44c440bc PP |
489 | } |
490 | ||
44c440bc | 491 | static inline |
83ebb7f1 | 492 | void ctf_event_class_to_ir(struct ctx *ctx) |
44c440bc PP |
493 | { |
494 | int ret; | |
b19ff26f | 495 | bt_event_class *ir_ec = NULL; |
83ebb7f1 | 496 | bt_field_class *ir_fc; |
44c440bc | 497 | |
83ebb7f1 PP |
498 | BT_ASSERT(ctx->ec); |
499 | ||
500 | if (ctx->ec->is_translated) { | |
40f4ba76 | 501 | ir_ec = bt_stream_class_borrow_event_class_by_id( |
83ebb7f1 | 502 | ctx->ir_sc, ctx->ec->id); |
44c440bc PP |
503 | BT_ASSERT(ir_ec); |
504 | goto end; | |
505 | } | |
506 | ||
83ebb7f1 | 507 | ir_ec = bt_event_class_create_with_id(ctx->ir_sc, ctx->ec->id); |
44c440bc | 508 | BT_ASSERT(ir_ec); |
c5b9b441 | 509 | bt_event_class_put_ref(ir_ec); |
83ebb7f1 PP |
510 | ctx->scope = CTF_SCOPE_EVENT_SPECIFIC_CONTEXT; |
511 | ir_fc = scope_ctf_field_class_to_ir(ctx); | |
512 | if (ir_fc) { | |
513 | ret = bt_event_class_set_specific_context_field_class( | |
514 | ir_ec, ir_fc); | |
515 | BT_ASSERT(ret == 0); | |
516 | bt_field_class_put_ref(ir_fc); | |
44c440bc PP |
517 | } |
518 | ||
83ebb7f1 PP |
519 | ctx->scope = CTF_SCOPE_EVENT_PAYLOAD; |
520 | ir_fc = scope_ctf_field_class_to_ir(ctx); | |
521 | if (ir_fc) { | |
522 | ret = bt_event_class_set_payload_field_class(ir_ec, | |
523 | ir_fc); | |
524 | BT_ASSERT(ret == 0); | |
525 | bt_field_class_put_ref(ir_fc); | |
44c440bc PP |
526 | } |
527 | ||
83ebb7f1 PP |
528 | if (ctx->ec->name->len > 0) { |
529 | ret = bt_event_class_set_name(ir_ec, ctx->ec->name->str); | |
44c440bc PP |
530 | BT_ASSERT(ret == 0); |
531 | } | |
532 | ||
83ebb7f1 PP |
533 | if (ctx->ec->emf_uri->len > 0) { |
534 | ret = bt_event_class_set_emf_uri(ir_ec, ctx->ec->emf_uri->str); | |
44c440bc PP |
535 | BT_ASSERT(ret == 0); |
536 | } | |
537 | ||
6da709aa | 538 | if (ctx->ec->is_log_level_set) { |
83ebb7f1 | 539 | bt_event_class_set_log_level(ir_ec, ctx->ec->log_level); |
44c440bc PP |
540 | } |
541 | ||
83ebb7f1 PP |
542 | ctx->ec->is_translated = true; |
543 | ctx->ec->ir_ec = ir_ec; | |
44c440bc PP |
544 | |
545 | end: | |
83ebb7f1 | 546 | return; |
44c440bc PP |
547 | } |
548 | ||
549 | ||
550 | static inline | |
83ebb7f1 | 551 | void ctf_stream_class_to_ir(struct ctx *ctx) |
44c440bc PP |
552 | { |
553 | int ret; | |
83ebb7f1 | 554 | bt_field_class *ir_fc; |
44c440bc | 555 | |
83ebb7f1 | 556 | BT_ASSERT(ctx->sc); |
44c440bc | 557 | |
83ebb7f1 PP |
558 | if (ctx->sc->is_translated) { |
559 | ctx->ir_sc = bt_trace_class_borrow_stream_class_by_id( | |
560 | ctx->ir_tc, ctx->sc->id); | |
561 | BT_ASSERT(ctx->ir_sc); | |
562 | goto end; | |
44c440bc PP |
563 | } |
564 | ||
83ebb7f1 PP |
565 | ctx->ir_sc = bt_stream_class_create_with_id(ctx->ir_tc, ctx->sc->id); |
566 | BT_ASSERT(ctx->ir_sc); | |
567 | bt_stream_class_put_ref(ctx->ir_sc); | |
26fc5aed PP |
568 | |
569 | if (ctx->sc->default_clock_class) { | |
570 | BT_ASSERT(ctx->sc->default_clock_class->ir_cc); | |
571 | ret = bt_stream_class_set_default_clock_class(ctx->ir_sc, | |
572 | ctx->sc->default_clock_class->ir_cc); | |
573 | BT_ASSERT(ret == 0); | |
574 | } | |
575 | ||
576 | bt_stream_class_set_supports_packets(ctx->ir_sc, BT_TRUE, | |
577 | ctx->sc->packets_have_ts_begin, ctx->sc->packets_have_ts_end); | |
578 | bt_stream_class_set_supports_discarded_events(ctx->ir_sc, | |
579 | ctx->sc->has_discarded_events, | |
580 | ctx->sc->discarded_events_have_default_cs); | |
581 | bt_stream_class_set_supports_discarded_packets(ctx->ir_sc, | |
582 | ctx->sc->has_discarded_packets, | |
583 | ctx->sc->discarded_packets_have_default_cs); | |
83ebb7f1 PP |
584 | ctx->scope = CTF_SCOPE_PACKET_CONTEXT; |
585 | ir_fc = scope_ctf_field_class_to_ir(ctx); | |
586 | if (ir_fc) { | |
587 | ret = bt_stream_class_set_packet_context_field_class( | |
588 | ctx->ir_sc, ir_fc); | |
589 | BT_ASSERT(ret == 0); | |
590 | bt_field_class_put_ref(ir_fc); | |
44c440bc PP |
591 | } |
592 | ||
83ebb7f1 PP |
593 | ctx->scope = CTF_SCOPE_EVENT_COMMON_CONTEXT; |
594 | ir_fc = scope_ctf_field_class_to_ir(ctx); | |
595 | if (ir_fc) { | |
596 | ret = bt_stream_class_set_event_common_context_field_class( | |
597 | ctx->ir_sc, ir_fc); | |
598 | BT_ASSERT(ret == 0); | |
599 | bt_field_class_put_ref(ir_fc); | |
44c440bc PP |
600 | } |
601 | ||
83ebb7f1 | 602 | bt_stream_class_set_assigns_automatic_event_class_id(ctx->ir_sc, |
44c440bc | 603 | BT_FALSE); |
83ebb7f1 | 604 | bt_stream_class_set_assigns_automatic_stream_id(ctx->ir_sc, BT_FALSE); |
44c440bc | 605 | |
83ebb7f1 PP |
606 | ctx->sc->is_translated = true; |
607 | ctx->sc->ir_sc = ctx->ir_sc; | |
44c440bc PP |
608 | |
609 | end: | |
83ebb7f1 | 610 | return; |
44c440bc PP |
611 | } |
612 | ||
613 | static inline | |
0f2d58c9 PP |
614 | void ctf_clock_class_to_ir(bt_clock_class *ir_cc, struct ctf_clock_class *cc) |
615 | { | |
616 | int ret; | |
617 | ||
618 | if (strlen(cc->name->str) > 0) { | |
619 | ret = bt_clock_class_set_name(ir_cc, cc->name->str); | |
620 | BT_ASSERT(ret == 0); | |
621 | } | |
622 | ||
623 | if (strlen(cc->description->str) > 0) { | |
624 | ret = bt_clock_class_set_description(ir_cc, cc->description->str); | |
625 | BT_ASSERT(ret == 0); | |
626 | } | |
627 | ||
628 | bt_clock_class_set_frequency(ir_cc, cc->frequency); | |
629 | bt_clock_class_set_precision(ir_cc, cc->precision); | |
630 | bt_clock_class_set_offset(ir_cc, cc->offset_seconds, cc->offset_cycles); | |
631 | ||
632 | if (cc->has_uuid) { | |
633 | bt_clock_class_set_uuid(ir_cc, cc->uuid); | |
634 | } | |
635 | ||
5552377a | 636 | bt_clock_class_set_origin_is_unix_epoch(ir_cc, cc->is_absolute); |
0f2d58c9 PP |
637 | } |
638 | ||
639 | static inline | |
83ebb7f1 | 640 | int ctf_trace_class_to_ir(struct ctx *ctx) |
44c440bc PP |
641 | { |
642 | int ret = 0; | |
643 | uint64_t i; | |
644 | ||
83ebb7f1 PP |
645 | BT_ASSERT(ctx->tc); |
646 | BT_ASSERT(ctx->ir_tc); | |
44c440bc | 647 | |
83ebb7f1 PP |
648 | if (ctx->tc->is_translated) { |
649 | goto end; | |
44c440bc PP |
650 | } |
651 | ||
83ebb7f1 PP |
652 | for (i = 0; i < ctx->tc->clock_classes->len; i++) { |
653 | struct ctf_clock_class *cc = ctx->tc->clock_classes->pdata[i]; | |
0f2d58c9 | 654 | |
f7b785ac | 655 | cc->ir_cc = bt_clock_class_create(ctx->self_comp); |
0f2d58c9 PP |
656 | ctf_clock_class_to_ir(cc->ir_cc, cc); |
657 | } | |
658 | ||
83ebb7f1 | 659 | bt_trace_class_set_assigns_automatic_stream_class_id(ctx->ir_tc, |
44c440bc | 660 | BT_FALSE); |
83ebb7f1 PP |
661 | ctx->tc->is_translated = true; |
662 | ctx->tc->ir_tc = ctx->ir_tc; | |
44c440bc PP |
663 | |
664 | end: | |
665 | return ret; | |
666 | } | |
667 | ||
668 | BT_HIDDEN | |
f7b785ac | 669 | int ctf_trace_class_translate(bt_self_component *self_comp, |
7fcdb0a9 | 670 | bt_trace_class *ir_tc, struct ctf_trace_class *tc) |
44c440bc PP |
671 | { |
672 | int ret = 0; | |
673 | uint64_t i; | |
83ebb7f1 | 674 | struct ctx ctx = { 0 }; |
44c440bc | 675 | |
7fcdb0a9 | 676 | ctx.self_comp = self_comp; |
83ebb7f1 PP |
677 | ctx.tc = tc; |
678 | ctx.ir_tc = ir_tc; | |
679 | ret = ctf_trace_class_to_ir(&ctx); | |
44c440bc PP |
680 | if (ret) { |
681 | goto end; | |
682 | } | |
683 | ||
684 | for (i = 0; i < tc->stream_classes->len; i++) { | |
685 | uint64_t j; | |
83ebb7f1 | 686 | ctx.sc = tc->stream_classes->pdata[i]; |
44c440bc | 687 | |
83ebb7f1 | 688 | ctf_stream_class_to_ir(&ctx); |
44c440bc | 689 | |
83ebb7f1 PP |
690 | for (j = 0; j < ctx.sc->event_classes->len; j++) { |
691 | ctx.ec = ctx.sc->event_classes->pdata[j]; | |
44c440bc | 692 | |
83ebb7f1 PP |
693 | ctf_event_class_to_ir(&ctx); |
694 | ctx.ec = NULL; | |
44c440bc | 695 | } |
83ebb7f1 PP |
696 | |
697 | ctx.sc = NULL; | |
44c440bc PP |
698 | } |
699 | ||
700 | end: | |
701 | return ret; | |
702 | } |