Commit | Line | Data |
---|---|---|
7b33a0e0 PP |
1 | /* |
2 | * Copyright 2018 - Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
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: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | */ | |
14 | ||
15 | #define BT_LOG_TAG "PLUGIN-CTF-METADATA-META-TRANSLATE" | |
16 | #include "logging.h" | |
17 | ||
18 | #include <babeltrace/babeltrace.h> | |
19 | #include <babeltrace/babeltrace-internal.h> | |
20 | #include <babeltrace/assert-internal.h> | |
21 | #include <glib.h> | |
22 | #include <stdint.h> | |
23 | #include <string.h> | |
24 | #include <inttypes.h> | |
25 | ||
26 | #include "ctf-meta-visitors.h" | |
27 | ||
28 | static inline | |
939190b3 | 29 | struct bt_field_class *ctf_field_class_to_ir(struct ctf_field_class *fc, |
7b33a0e0 PP |
30 | struct ctf_trace_class *tc, |
31 | struct ctf_stream_class *sc, | |
32 | struct ctf_event_class *ec); | |
33 | ||
34 | static inline | |
939190b3 PP |
35 | void ctf_field_class_int_set_props(struct ctf_field_class_int *fc, |
36 | struct bt_field_class *ir_fc) | |
7b33a0e0 PP |
37 | { |
38 | int ret; | |
39 | ||
939190b3 | 40 | ret = bt_field_class_integer_set_field_value_range(ir_fc, fc->base.size); |
7b33a0e0 | 41 | BT_ASSERT(ret == 0); |
939190b3 PP |
42 | ret = bt_field_class_integer_set_preferred_display_base(ir_fc, |
43 | fc->disp_base); | |
7b33a0e0 PP |
44 | BT_ASSERT(ret == 0); |
45 | } | |
46 | ||
47 | static inline | |
939190b3 | 48 | struct bt_field_class *ctf_field_class_int_to_ir(struct ctf_field_class_int *fc) |
7b33a0e0 | 49 | { |
939190b3 | 50 | struct bt_field_class *ir_fc; |
7b33a0e0 | 51 | |
939190b3 PP |
52 | if (fc->is_signed) { |
53 | ir_fc = bt_field_class_signed_integer_create(); | |
7b33a0e0 | 54 | } else { |
939190b3 | 55 | ir_fc = bt_field_class_unsigned_integer_create(); |
7b33a0e0 PP |
56 | } |
57 | ||
939190b3 PP |
58 | BT_ASSERT(ir_fc); |
59 | ctf_field_class_int_set_props(fc, ir_fc); | |
60 | return ir_fc; | |
7b33a0e0 PP |
61 | } |
62 | ||
63 | static inline | |
939190b3 | 64 | struct bt_field_class *ctf_field_class_enum_to_ir(struct ctf_field_class_enum *fc) |
7b33a0e0 PP |
65 | { |
66 | int ret; | |
939190b3 | 67 | struct bt_field_class *ir_fc; |
7b33a0e0 PP |
68 | uint64_t i; |
69 | ||
939190b3 PP |
70 | if (fc->base.is_signed) { |
71 | ir_fc = bt_field_class_signed_enumeration_create(); | |
7b33a0e0 | 72 | } else { |
939190b3 | 73 | ir_fc = bt_field_class_unsigned_enumeration_create(); |
7b33a0e0 PP |
74 | } |
75 | ||
939190b3 PP |
76 | BT_ASSERT(ir_fc); |
77 | ctf_field_class_int_set_props((void *) fc, ir_fc); | |
7b33a0e0 | 78 | |
939190b3 PP |
79 | for (i = 0; i < fc->mappings->len; i++) { |
80 | struct ctf_field_class_enum_mapping *mapping = | |
81 | ctf_field_class_enum_borrow_mapping_by_index(fc, i); | |
7b33a0e0 | 82 | |
939190b3 PP |
83 | if (fc->base.is_signed) { |
84 | ret = bt_field_class_signed_enumeration_map_range( | |
85 | ir_fc, mapping->label->str, | |
7b33a0e0 PP |
86 | mapping->range.lower.i, mapping->range.upper.i); |
87 | } else { | |
939190b3 PP |
88 | ret = bt_field_class_unsigned_enumeration_map_range( |
89 | ir_fc, mapping->label->str, | |
7b33a0e0 PP |
90 | mapping->range.lower.u, mapping->range.upper.u); |
91 | } | |
92 | ||
93 | BT_ASSERT(ret == 0); | |
94 | } | |
95 | ||
939190b3 | 96 | return ir_fc; |
7b33a0e0 PP |
97 | } |
98 | ||
99 | static inline | |
939190b3 PP |
100 | struct bt_field_class *ctf_field_class_float_to_ir( |
101 | struct ctf_field_class_float *fc) | |
7b33a0e0 | 102 | { |
939190b3 | 103 | struct bt_field_class *ir_fc; |
7b33a0e0 PP |
104 | int ret; |
105 | ||
939190b3 PP |
106 | ir_fc = bt_field_class_real_create(); |
107 | BT_ASSERT(ir_fc); | |
7b33a0e0 | 108 | |
939190b3 PP |
109 | if (fc->base.size == 32) { |
110 | ret = bt_field_class_real_set_is_single_precision(ir_fc, | |
7b33a0e0 PP |
111 | BT_TRUE); |
112 | BT_ASSERT(ret == 0); | |
113 | } | |
114 | ||
939190b3 | 115 | return ir_fc; |
7b33a0e0 PP |
116 | } |
117 | ||
118 | static inline | |
939190b3 PP |
119 | struct bt_field_class *ctf_field_class_string_to_ir( |
120 | struct ctf_field_class_string *fc) | |
7b33a0e0 | 121 | { |
939190b3 | 122 | struct bt_field_class *ir_fc = bt_field_class_string_create(); |
7b33a0e0 | 123 | |
939190b3 PP |
124 | BT_ASSERT(ir_fc); |
125 | return ir_fc; | |
7b33a0e0 PP |
126 | } |
127 | ||
128 | static inline | |
939190b3 PP |
129 | struct bt_field_class *ctf_field_class_struct_to_ir( |
130 | struct ctf_field_class_struct *fc, | |
7b33a0e0 PP |
131 | struct ctf_trace_class *tc, |
132 | struct ctf_stream_class *sc, | |
133 | struct ctf_event_class *ec) | |
134 | { | |
135 | int ret; | |
939190b3 | 136 | struct bt_field_class *ir_fc = bt_field_class_structure_create(); |
7b33a0e0 PP |
137 | uint64_t i; |
138 | ||
939190b3 | 139 | BT_ASSERT(ir_fc); |
7b33a0e0 | 140 | |
939190b3 PP |
141 | for (i = 0; i < fc->members->len; i++) { |
142 | struct ctf_named_field_class *named_fc = | |
143 | ctf_field_class_struct_borrow_member_by_index(fc, i); | |
144 | struct bt_field_class *member_ir_fc; | |
7b33a0e0 | 145 | |
939190b3 | 146 | if (!named_fc->fc->in_ir) { |
7b33a0e0 PP |
147 | continue; |
148 | } | |
149 | ||
939190b3 PP |
150 | member_ir_fc = ctf_field_class_to_ir(named_fc->fc, tc, sc, ec); |
151 | BT_ASSERT(member_ir_fc); | |
152 | ret = bt_field_class_structure_append_member(ir_fc, | |
153 | named_fc->name->str, member_ir_fc); | |
7b33a0e0 | 154 | BT_ASSERT(ret == 0); |
8138bfe1 | 155 | bt_object_put_ref(member_ir_fc); |
7b33a0e0 PP |
156 | } |
157 | ||
939190b3 | 158 | return ir_fc; |
7b33a0e0 PP |
159 | } |
160 | ||
161 | static inline | |
939190b3 | 162 | struct bt_field_class *borrow_ir_ft_from_field_path( |
7b33a0e0 PP |
163 | struct ctf_field_path *field_path, |
164 | struct ctf_trace_class *tc, | |
165 | struct ctf_stream_class *sc, | |
166 | struct ctf_event_class *ec) | |
167 | { | |
939190b3 PP |
168 | struct bt_field_class *ir_fc = NULL; |
169 | struct ctf_field_class *fc = ctf_field_path_borrow_field_class( | |
7b33a0e0 PP |
170 | field_path, tc, sc, ec); |
171 | ||
939190b3 | 172 | BT_ASSERT(fc); |
7b33a0e0 | 173 | |
939190b3 PP |
174 | if (fc->in_ir) { |
175 | ir_fc = fc->ir_fc; | |
7b33a0e0 PP |
176 | } |
177 | ||
939190b3 | 178 | return ir_fc; |
7b33a0e0 PP |
179 | } |
180 | ||
181 | static inline | |
939190b3 PP |
182 | struct bt_field_class *ctf_field_class_variant_to_ir( |
183 | struct ctf_field_class_variant *fc, | |
7b33a0e0 PP |
184 | struct ctf_trace_class *tc, |
185 | struct ctf_stream_class *sc, | |
186 | struct ctf_event_class *ec) | |
187 | { | |
188 | int ret; | |
939190b3 | 189 | struct bt_field_class *ir_fc = bt_field_class_variant_create(); |
7b33a0e0 PP |
190 | uint64_t i; |
191 | ||
939190b3 PP |
192 | BT_ASSERT(ir_fc); |
193 | ret = bt_field_class_variant_set_selector_field_class(ir_fc, | |
194 | borrow_ir_ft_from_field_path(&fc->tag_path, tc, sc, ec)); | |
7b33a0e0 PP |
195 | BT_ASSERT(ret == 0); |
196 | ||
939190b3 PP |
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 | struct bt_field_class *option_ir_fc; | |
7b33a0e0 | 201 | |
939190b3 PP |
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(ir_fc, | |
206 | named_fc->name->str, option_ir_fc); | |
7b33a0e0 | 207 | BT_ASSERT(ret == 0); |
8138bfe1 | 208 | bt_object_put_ref(option_ir_fc); |
7b33a0e0 PP |
209 | } |
210 | ||
939190b3 | 211 | return ir_fc; |
7b33a0e0 PP |
212 | } |
213 | ||
214 | static inline | |
939190b3 PP |
215 | struct bt_field_class *ctf_field_class_array_to_ir( |
216 | struct ctf_field_class_array *fc, | |
7b33a0e0 PP |
217 | struct ctf_trace_class *tc, |
218 | struct ctf_stream_class *sc, | |
219 | struct ctf_event_class *ec) | |
220 | { | |
939190b3 PP |
221 | struct bt_field_class *ir_fc; |
222 | struct bt_field_class *elem_ir_fc; | |
7b33a0e0 | 223 | |
939190b3 PP |
224 | if (fc->base.is_text) { |
225 | ir_fc = bt_field_class_string_create(); | |
226 | BT_ASSERT(ir_fc); | |
7b33a0e0 PP |
227 | goto end; |
228 | } | |
229 | ||
939190b3 PP |
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, fc->length); | |
233 | BT_ASSERT(ir_fc); | |
8138bfe1 | 234 | bt_object_put_ref(elem_ir_fc); |
7b33a0e0 PP |
235 | |
236 | end: | |
939190b3 | 237 | return ir_fc; |
7b33a0e0 PP |
238 | } |
239 | ||
240 | static inline | |
939190b3 PP |
241 | struct bt_field_class *ctf_field_class_sequence_to_ir( |
242 | struct ctf_field_class_sequence *fc, | |
7b33a0e0 PP |
243 | struct ctf_trace_class *tc, |
244 | struct ctf_stream_class *sc, | |
245 | struct ctf_event_class *ec) | |
246 | { | |
247 | int ret; | |
939190b3 PP |
248 | struct bt_field_class *ir_fc; |
249 | struct bt_field_class *elem_ir_fc; | |
7b33a0e0 | 250 | |
939190b3 PP |
251 | if (fc->base.is_text) { |
252 | ir_fc = bt_field_class_string_create(); | |
253 | BT_ASSERT(ir_fc); | |
7b33a0e0 PP |
254 | goto end; |
255 | } | |
256 | ||
939190b3 PP |
257 | elem_ir_fc = ctf_field_class_to_ir(fc->base.elem_fc, tc, sc, ec); |
258 | BT_ASSERT(elem_ir_fc); | |
259 | ir_fc = bt_field_class_dynamic_array_create(elem_ir_fc); | |
260 | BT_ASSERT(ir_fc); | |
8138bfe1 | 261 | bt_object_put_ref(elem_ir_fc); |
939190b3 PP |
262 | BT_ASSERT(ir_fc); |
263 | ret = bt_field_class_dynamic_array_set_length_field_class(ir_fc, | |
264 | borrow_ir_ft_from_field_path(&fc->length_path, tc, sc, ec)); | |
7b33a0e0 PP |
265 | BT_ASSERT(ret == 0); |
266 | ||
267 | end: | |
939190b3 | 268 | return ir_fc; |
7b33a0e0 PP |
269 | } |
270 | ||
271 | static inline | |
939190b3 | 272 | struct bt_field_class *ctf_field_class_to_ir(struct ctf_field_class *fc, |
7b33a0e0 PP |
273 | struct ctf_trace_class *tc, |
274 | struct ctf_stream_class *sc, | |
275 | struct ctf_event_class *ec) | |
276 | { | |
939190b3 | 277 | struct bt_field_class *ir_fc = NULL; |
7b33a0e0 | 278 | |
939190b3 PP |
279 | BT_ASSERT(fc); |
280 | BT_ASSERT(fc->in_ir); | |
7b33a0e0 | 281 | |
af0c18e3 PP |
282 | switch (fc->type) { |
283 | case CTF_FIELD_CLASS_TYPE_INT: | |
939190b3 | 284 | ir_fc = ctf_field_class_int_to_ir((void *) fc); |
7b33a0e0 | 285 | break; |
af0c18e3 | 286 | case CTF_FIELD_CLASS_TYPE_ENUM: |
939190b3 | 287 | ir_fc = ctf_field_class_enum_to_ir((void *) fc); |
7b33a0e0 | 288 | break; |
af0c18e3 | 289 | case CTF_FIELD_CLASS_TYPE_FLOAT: |
939190b3 | 290 | ir_fc = ctf_field_class_float_to_ir((void *) fc); |
7b33a0e0 | 291 | break; |
af0c18e3 | 292 | case CTF_FIELD_CLASS_TYPE_STRING: |
939190b3 | 293 | ir_fc = ctf_field_class_string_to_ir((void *) fc); |
7b33a0e0 | 294 | break; |
af0c18e3 | 295 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
939190b3 | 296 | ir_fc = ctf_field_class_struct_to_ir((void *) fc, tc, sc, ec); |
7b33a0e0 | 297 | break; |
af0c18e3 | 298 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
939190b3 | 299 | ir_fc = ctf_field_class_array_to_ir((void *) fc, tc, sc, ec); |
7b33a0e0 | 300 | break; |
af0c18e3 | 301 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
939190b3 | 302 | ir_fc = ctf_field_class_sequence_to_ir((void *) fc, tc, sc, ec); |
7b33a0e0 | 303 | break; |
af0c18e3 | 304 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
939190b3 | 305 | ir_fc = ctf_field_class_variant_to_ir((void *) fc, tc, sc, ec); |
7b33a0e0 PP |
306 | break; |
307 | default: | |
308 | abort(); | |
309 | } | |
310 | ||
939190b3 PP |
311 | fc->ir_fc = ir_fc; |
312 | return ir_fc; | |
7b33a0e0 PP |
313 | } |
314 | ||
315 | static inline | |
939190b3 PP |
316 | bool ctf_field_class_struct_has_immediate_member_in_ir( |
317 | struct ctf_field_class_struct *fc) | |
7b33a0e0 PP |
318 | { |
319 | uint64_t i; | |
320 | bool has_immediate_member_in_ir = false; | |
321 | ||
939190b3 PP |
322 | for (i = 0; i < fc->members->len; i++) { |
323 | struct ctf_named_field_class *named_fc = | |
324 | ctf_field_class_struct_borrow_member_by_index(fc, i); | |
7b33a0e0 | 325 | |
939190b3 | 326 | if (named_fc->fc->in_ir) { |
7b33a0e0 PP |
327 | has_immediate_member_in_ir = true; |
328 | goto end; | |
329 | } | |
330 | } | |
331 | ||
332 | end: | |
333 | return has_immediate_member_in_ir; | |
334 | } | |
335 | ||
336 | static inline | |
939190b3 | 337 | struct bt_field_class *scope_ctf_field_class_to_ir(struct ctf_field_class *fc, |
7b33a0e0 PP |
338 | struct ctf_trace_class *tc, |
339 | struct ctf_stream_class *sc, | |
340 | struct ctf_event_class *ec) | |
341 | { | |
939190b3 | 342 | struct bt_field_class *ir_fc = NULL; |
7b33a0e0 | 343 | |
939190b3 | 344 | if (!fc) { |
7b33a0e0 PP |
345 | goto end; |
346 | } | |
347 | ||
af0c18e3 | 348 | BT_ASSERT(fc->type == CTF_FIELD_CLASS_TYPE_STRUCT); |
7b33a0e0 | 349 | |
939190b3 | 350 | if (!ctf_field_class_struct_has_immediate_member_in_ir((void *) fc)) { |
7b33a0e0 PP |
351 | /* |
352 | * Nothing for IR in this scope: typical for packet | |
353 | * header, packet context, and event header. | |
354 | */ | |
355 | goto end; | |
356 | } | |
357 | ||
939190b3 | 358 | ir_fc = ctf_field_class_to_ir(fc, tc, sc, ec); |
7b33a0e0 PP |
359 | |
360 | end: | |
939190b3 | 361 | return ir_fc; |
7b33a0e0 PP |
362 | } |
363 | ||
364 | static inline | |
939190b3 PP |
365 | struct ctf_field_class_int *borrow_named_int_field_class( |
366 | struct ctf_field_class_struct *struct_fc, const char *name) | |
7b33a0e0 | 367 | { |
939190b3 PP |
368 | struct ctf_named_field_class *named_fc = NULL; |
369 | struct ctf_field_class_int *int_fc = NULL; | |
7b33a0e0 | 370 | |
939190b3 | 371 | if (!struct_fc) { |
7b33a0e0 PP |
372 | goto end; |
373 | } | |
374 | ||
939190b3 PP |
375 | named_fc = ctf_field_class_struct_borrow_member_by_name(struct_fc, name); |
376 | if (!named_fc) { | |
7b33a0e0 PP |
377 | goto end; |
378 | } | |
379 | ||
af0c18e3 PP |
380 | if (named_fc->fc->type != CTF_FIELD_CLASS_TYPE_INT && |
381 | named_fc->fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
7b33a0e0 PP |
382 | goto end; |
383 | } | |
384 | ||
939190b3 | 385 | int_fc = (void *) named_fc->fc; |
7b33a0e0 PP |
386 | |
387 | end: | |
939190b3 | 388 | return int_fc; |
7b33a0e0 PP |
389 | } |
390 | ||
391 | static inline | |
392 | struct bt_event_class *ctf_event_class_to_ir(struct ctf_event_class *ec, | |
393 | struct bt_stream_class *ir_sc, struct ctf_trace_class *tc, | |
394 | struct ctf_stream_class *sc) | |
395 | { | |
396 | int ret; | |
397 | struct bt_event_class *ir_ec = NULL; | |
398 | ||
399 | if (ec->is_translated) { | |
400 | ir_ec = bt_stream_class_borrow_event_class_by_id( | |
401 | ir_sc, ec->id); | |
402 | BT_ASSERT(ir_ec); | |
403 | goto end; | |
404 | } | |
405 | ||
406 | ir_ec = bt_event_class_create_with_id(ir_sc, ec->id); | |
407 | BT_ASSERT(ir_ec); | |
8138bfe1 | 408 | bt_object_put_ref(ir_ec); |
7b33a0e0 | 409 | |
939190b3 PP |
410 | if (ec->spec_context_fc) { |
411 | struct bt_field_class *ir_fc = scope_ctf_field_class_to_ir( | |
412 | ec->spec_context_fc, tc, sc, ec); | |
7b33a0e0 | 413 | |
939190b3 PP |
414 | if (ir_fc) { |
415 | ret = bt_event_class_set_specific_context_field_class( | |
416 | ir_ec, ir_fc); | |
7b33a0e0 | 417 | BT_ASSERT(ret == 0); |
8138bfe1 | 418 | bt_object_put_ref(ir_fc); |
7b33a0e0 PP |
419 | } |
420 | } | |
421 | ||
939190b3 PP |
422 | if (ec->payload_fc) { |
423 | struct bt_field_class *ir_fc = scope_ctf_field_class_to_ir( | |
424 | ec->payload_fc, tc, sc, ec); | |
7b33a0e0 | 425 | |
939190b3 PP |
426 | if (ir_fc) { |
427 | ret = bt_event_class_set_payload_field_class(ir_ec, | |
428 | ir_fc); | |
7b33a0e0 | 429 | BT_ASSERT(ret == 0); |
8138bfe1 | 430 | bt_object_put_ref(ir_fc); |
7b33a0e0 PP |
431 | } |
432 | } | |
433 | ||
434 | if (ec->name->len > 0) { | |
435 | ret = bt_event_class_set_name(ir_ec, ec->name->str); | |
436 | BT_ASSERT(ret == 0); | |
437 | } | |
438 | ||
439 | if (ec->emf_uri->len > 0) { | |
440 | ret = bt_event_class_set_emf_uri(ir_ec, ec->emf_uri->str); | |
441 | BT_ASSERT(ret == 0); | |
442 | } | |
443 | ||
444 | if (ec->log_level != -1) { | |
445 | ret = bt_event_class_set_log_level(ir_ec, ec->log_level); | |
446 | BT_ASSERT(ret == 0); | |
447 | } | |
448 | ||
449 | ec->is_translated = true; | |
450 | ec->ir_ec = ir_ec; | |
451 | ||
452 | end: | |
453 | return ir_ec; | |
454 | } | |
455 | ||
456 | ||
457 | static inline | |
458 | struct bt_stream_class *ctf_stream_class_to_ir(struct ctf_stream_class *sc, | |
459 | struct bt_trace *ir_trace, struct ctf_trace_class *tc) | |
460 | { | |
461 | int ret; | |
462 | struct bt_stream_class *ir_sc = NULL; | |
939190b3 | 463 | struct ctf_field_class_int *int_fc; |
7b33a0e0 PP |
464 | |
465 | if (sc->is_translated) { | |
466 | ir_sc = bt_trace_borrow_stream_class_by_id(ir_trace, sc->id); | |
467 | BT_ASSERT(ir_sc); | |
468 | goto end; | |
469 | } | |
470 | ||
471 | ir_sc = bt_stream_class_create_with_id(ir_trace, sc->id); | |
472 | BT_ASSERT(ir_sc); | |
8138bfe1 | 473 | bt_object_put_ref(ir_sc); |
7b33a0e0 | 474 | |
939190b3 PP |
475 | if (sc->packet_context_fc) { |
476 | struct bt_field_class *ir_fc = scope_ctf_field_class_to_ir( | |
477 | sc->packet_context_fc, tc, sc, NULL); | |
7b33a0e0 | 478 | |
939190b3 PP |
479 | if (ir_fc) { |
480 | ret = bt_stream_class_set_packet_context_field_class( | |
481 | ir_sc, ir_fc); | |
7b33a0e0 | 482 | BT_ASSERT(ret == 0); |
8138bfe1 | 483 | bt_object_put_ref(ir_fc); |
7b33a0e0 PP |
484 | } |
485 | } | |
486 | ||
939190b3 PP |
487 | if (sc->event_header_fc) { |
488 | struct bt_field_class *ir_fc = scope_ctf_field_class_to_ir( | |
489 | sc->event_header_fc, tc, sc, NULL); | |
7b33a0e0 | 490 | |
939190b3 PP |
491 | if (ir_fc) { |
492 | ret = bt_stream_class_set_event_header_field_class(ir_sc, | |
493 | ir_fc); | |
7b33a0e0 | 494 | BT_ASSERT(ret == 0); |
8138bfe1 | 495 | bt_object_put_ref(ir_fc); |
7b33a0e0 PP |
496 | } |
497 | } | |
498 | ||
939190b3 PP |
499 | if (sc->event_common_context_fc) { |
500 | struct bt_field_class *ir_fc = scope_ctf_field_class_to_ir( | |
501 | sc->event_common_context_fc, tc, sc, NULL); | |
7b33a0e0 | 502 | |
939190b3 PP |
503 | if (ir_fc) { |
504 | ret = bt_stream_class_set_event_common_context_field_class( | |
505 | ir_sc, ir_fc); | |
7b33a0e0 | 506 | BT_ASSERT(ret == 0); |
8138bfe1 | 507 | bt_object_put_ref(ir_fc); |
7b33a0e0 PP |
508 | } |
509 | } | |
510 | ||
511 | ret = bt_stream_class_set_assigns_automatic_event_class_id(ir_sc, | |
512 | BT_FALSE); | |
513 | BT_ASSERT(ret == 0); | |
514 | ret = bt_stream_class_set_assigns_automatic_stream_id(ir_sc, BT_FALSE); | |
515 | BT_ASSERT(ret == 0); | |
516 | ||
517 | if (sc->default_clock_class) { | |
518 | ret = bt_stream_class_set_default_clock_class(ir_sc, | |
519 | sc->default_clock_class); | |
520 | BT_ASSERT(ret == 0); | |
521 | } | |
522 | ||
939190b3 | 523 | int_fc = borrow_named_int_field_class((void *) sc->packet_context_fc, |
7b33a0e0 | 524 | "events_discarded"); |
939190b3 PP |
525 | if (int_fc) { |
526 | if (int_fc->meaning == CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT) { | |
7b33a0e0 PP |
527 | ret = bt_stream_class_set_packets_have_discarded_event_counter_snapshot( |
528 | ir_sc, BT_TRUE); | |
529 | BT_ASSERT(ret == 0); | |
530 | } | |
531 | } | |
532 | ||
939190b3 | 533 | int_fc = borrow_named_int_field_class((void *) sc->packet_context_fc, |
7b33a0e0 | 534 | "packet_seq_num"); |
939190b3 PP |
535 | if (int_fc) { |
536 | if (int_fc->meaning == CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT) { | |
7b33a0e0 PP |
537 | ret = bt_stream_class_set_packets_have_packet_counter_snapshot( |
538 | ir_sc, BT_TRUE); | |
539 | BT_ASSERT(ret == 0); | |
540 | } | |
541 | } | |
542 | ||
939190b3 | 543 | int_fc = borrow_named_int_field_class((void *) sc->packet_context_fc, |
7b33a0e0 | 544 | "timestamp_begin"); |
939190b3 PP |
545 | if (int_fc) { |
546 | if (int_fc->meaning == CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME) { | |
7b33a0e0 PP |
547 | ret = bt_stream_class_set_packets_have_default_beginning_clock_value( |
548 | ir_sc, BT_TRUE); | |
549 | BT_ASSERT(ret == 0); | |
550 | } | |
551 | } | |
552 | ||
939190b3 | 553 | int_fc = borrow_named_int_field_class((void *) sc->packet_context_fc, |
7b33a0e0 | 554 | "timestamp_end"); |
939190b3 PP |
555 | if (int_fc) { |
556 | if (int_fc->meaning == CTF_FIELD_CLASS_MEANING_PACKET_END_TIME) { | |
7b33a0e0 PP |
557 | ret = bt_stream_class_set_packets_have_default_end_clock_value( |
558 | ir_sc, BT_TRUE); | |
559 | BT_ASSERT(ret == 0); | |
560 | } | |
561 | } | |
562 | ||
563 | sc->is_translated = true; | |
564 | sc->ir_sc = ir_sc; | |
565 | ||
566 | end: | |
567 | return ir_sc; | |
568 | } | |
569 | ||
570 | static inline | |
571 | int ctf_trace_class_to_ir(struct bt_trace *ir_trace, | |
572 | struct ctf_trace_class *tc) | |
573 | { | |
574 | int ret = 0; | |
575 | uint64_t i; | |
576 | ||
577 | if (tc->is_translated) { | |
578 | goto end; | |
579 | } | |
580 | ||
939190b3 PP |
581 | if (tc->packet_header_fc) { |
582 | struct bt_field_class *ir_fc = scope_ctf_field_class_to_ir( | |
583 | tc->packet_header_fc, tc, NULL, NULL); | |
7b33a0e0 | 584 | |
939190b3 PP |
585 | if (ir_fc) { |
586 | ret = bt_trace_set_packet_header_field_class(ir_trace, | |
587 | ir_fc); | |
7b33a0e0 | 588 | BT_ASSERT(ret == 0); |
8138bfe1 | 589 | bt_object_put_ref(ir_fc); |
7b33a0e0 PP |
590 | } |
591 | } | |
592 | ||
593 | if (tc->name->len > 0) { | |
594 | ret = bt_trace_set_name(ir_trace, tc->name->str); | |
595 | if (ret) { | |
596 | goto end; | |
597 | } | |
598 | } | |
599 | ||
600 | if (tc->is_uuid_set) { | |
601 | ret = bt_trace_set_uuid(ir_trace, tc->uuid); | |
602 | if (ret) { | |
603 | goto end; | |
604 | } | |
605 | } | |
606 | ||
607 | for (i = 0; i < tc->env_entries->len; i++) { | |
608 | struct ctf_trace_class_env_entry *env_entry = | |
609 | ctf_trace_class_borrow_env_entry_by_index(tc, i); | |
610 | ||
611 | switch (env_entry->type) { | |
612 | case CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT: | |
613 | ret = bt_trace_set_environment_entry_integer( | |
614 | ir_trace, env_entry->name->str, | |
615 | env_entry->value.i); | |
616 | break; | |
617 | case CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR: | |
618 | ret = bt_trace_set_environment_entry_string( | |
619 | ir_trace, env_entry->name->str, | |
620 | env_entry->value.str->str); | |
621 | break; | |
622 | default: | |
623 | abort(); | |
624 | } | |
625 | ||
626 | if (ret) { | |
627 | goto end; | |
628 | } | |
629 | } | |
630 | ||
631 | ret = bt_trace_set_assigns_automatic_stream_class_id(ir_trace, | |
632 | BT_FALSE); | |
633 | if (ret) { | |
634 | goto end; | |
635 | } | |
636 | ||
637 | tc->is_translated = true; | |
638 | tc->ir_tc = ir_trace; | |
639 | ||
640 | end: | |
641 | return ret; | |
642 | } | |
643 | ||
644 | BT_HIDDEN | |
645 | int ctf_trace_class_translate(struct bt_trace *ir_trace, | |
646 | struct ctf_trace_class *tc) | |
647 | { | |
648 | int ret = 0; | |
649 | uint64_t i; | |
650 | ||
651 | ret = ctf_trace_class_to_ir(ir_trace, tc); | |
652 | if (ret) { | |
653 | goto end; | |
654 | } | |
655 | ||
656 | for (i = 0; i < tc->stream_classes->len; i++) { | |
657 | uint64_t j; | |
658 | struct ctf_stream_class *sc = tc->stream_classes->pdata[i]; | |
659 | struct bt_stream_class *ir_sc; | |
660 | ||
661 | ir_sc = ctf_stream_class_to_ir(sc, ir_trace, tc); | |
662 | if (!ir_sc) { | |
663 | ret = -1; | |
664 | goto end; | |
665 | } | |
666 | ||
667 | for (j = 0; j < sc->event_classes->len; j++) { | |
668 | struct ctf_event_class *ec = sc->event_classes->pdata[j]; | |
669 | struct bt_event_class *ir_ec; | |
670 | ||
671 | ir_ec = ctf_event_class_to_ir(ec, ir_sc, tc, sc); | |
672 | if (!ir_ec) { | |
673 | ret = -1; | |
674 | goto end; | |
675 | } | |
676 | } | |
677 | } | |
678 | ||
679 | end: | |
680 | return ret; | |
681 | } |