Make API CTF-agnostic
[babeltrace.git] / plugins / ctf / common / metadata / ctf-meta-translate.c
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
29 struct bt_field_type *ctf_field_type_to_ir(struct ctf_field_type *ft,
30 struct ctf_trace_class *tc,
31 struct ctf_stream_class *sc,
32 struct ctf_event_class *ec);
33
34 static inline
35 void ctf_field_type_int_set_props(struct ctf_field_type_int *ft,
36 struct bt_field_type *ir_ft)
37 {
38 int ret;
39
40 ret = bt_field_type_integer_set_field_value_range(ir_ft, ft->base.size);
41 BT_ASSERT(ret == 0);
42 ret = bt_field_type_integer_set_preferred_display_base(ir_ft,
43 ft->disp_base);
44 BT_ASSERT(ret == 0);
45 }
46
47 static inline
48 struct bt_field_type *ctf_field_type_int_to_ir(struct ctf_field_type_int *ft)
49 {
50 struct bt_field_type *ir_ft;
51
52 if (ft->is_signed) {
53 ir_ft = bt_field_type_signed_integer_create();
54 } else {
55 ir_ft = bt_field_type_unsigned_integer_create();
56 }
57
58 BT_ASSERT(ir_ft);
59 ctf_field_type_int_set_props(ft, ir_ft);
60 return ir_ft;
61 }
62
63 static inline
64 struct bt_field_type *ctf_field_type_enum_to_ir(struct ctf_field_type_enum *ft)
65 {
66 int ret;
67 struct bt_field_type *ir_ft;
68 uint64_t i;
69
70 if (ft->base.is_signed) {
71 ir_ft = bt_field_type_signed_enumeration_create();
72 } else {
73 ir_ft = bt_field_type_unsigned_enumeration_create();
74 }
75
76 BT_ASSERT(ir_ft);
77 ctf_field_type_int_set_props((void *) ft, ir_ft);
78
79 for (i = 0; i < ft->mappings->len; i++) {
80 struct ctf_field_type_enum_mapping *mapping =
81 ctf_field_type_enum_borrow_mapping_by_index(ft, i);
82
83 if (ft->base.is_signed) {
84 ret = bt_field_type_signed_enumeration_map_range(
85 ir_ft, mapping->label->str,
86 mapping->range.lower.i, mapping->range.upper.i);
87 } else {
88 ret = bt_field_type_unsigned_enumeration_map_range(
89 ir_ft, mapping->label->str,
90 mapping->range.lower.u, mapping->range.upper.u);
91 }
92
93 BT_ASSERT(ret == 0);
94 }
95
96 return ir_ft;
97 }
98
99 static inline
100 struct bt_field_type *ctf_field_type_float_to_ir(
101 struct ctf_field_type_float *ft)
102 {
103 struct bt_field_type *ir_ft;
104 int ret;
105
106 ir_ft = bt_field_type_real_create();
107 BT_ASSERT(ir_ft);
108
109 if (ft->base.size == 32) {
110 ret = bt_field_type_real_set_is_single_precision(ir_ft,
111 BT_TRUE);
112 BT_ASSERT(ret == 0);
113 }
114
115 return ir_ft;
116 }
117
118 static inline
119 struct bt_field_type *ctf_field_type_string_to_ir(
120 struct ctf_field_type_string *ft)
121 {
122 struct bt_field_type *ir_ft = bt_field_type_string_create();
123
124 BT_ASSERT(ir_ft);
125 return ir_ft;
126 }
127
128 static inline
129 struct bt_field_type *ctf_field_type_struct_to_ir(
130 struct ctf_field_type_struct *ft,
131 struct ctf_trace_class *tc,
132 struct ctf_stream_class *sc,
133 struct ctf_event_class *ec)
134 {
135 int ret;
136 struct bt_field_type *ir_ft = bt_field_type_structure_create();
137 uint64_t i;
138
139 BT_ASSERT(ir_ft);
140
141 for (i = 0; i < ft->members->len; i++) {
142 struct ctf_named_field_type *named_ft =
143 ctf_field_type_struct_borrow_member_by_index(ft, i);
144 struct bt_field_type *member_ir_ft;
145
146 if (!named_ft->ft->in_ir) {
147 continue;
148 }
149
150 member_ir_ft = ctf_field_type_to_ir(named_ft->ft, tc, sc, ec);
151 BT_ASSERT(member_ir_ft);
152 ret = bt_field_type_structure_append_member(ir_ft,
153 named_ft->name->str, member_ir_ft);
154 BT_ASSERT(ret == 0);
155 bt_put(member_ir_ft);
156 }
157
158 return ir_ft;
159 }
160
161 static inline
162 struct bt_field_type *borrow_ir_ft_from_field_path(
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 {
168 struct bt_field_type *ir_ft = NULL;
169 struct ctf_field_type *ft = ctf_field_path_borrow_field_type(
170 field_path, tc, sc, ec);
171
172 BT_ASSERT(ft);
173
174 if (ft->in_ir) {
175 ir_ft = ft->ir_ft;
176 }
177
178 return ir_ft;
179 }
180
181 static inline
182 struct bt_field_type *ctf_field_type_variant_to_ir(
183 struct ctf_field_type_variant *ft,
184 struct ctf_trace_class *tc,
185 struct ctf_stream_class *sc,
186 struct ctf_event_class *ec)
187 {
188 int ret;
189 struct bt_field_type *ir_ft = bt_field_type_variant_create();
190 uint64_t i;
191
192 BT_ASSERT(ir_ft);
193 ret = bt_field_type_variant_set_selector_field_type(ir_ft,
194 borrow_ir_ft_from_field_path(&ft->tag_path, tc, sc, ec));
195 BT_ASSERT(ret == 0);
196
197 for (i = 0; i < ft->options->len; i++) {
198 struct ctf_named_field_type *named_ft =
199 ctf_field_type_variant_borrow_option_by_index(ft, i);
200 struct bt_field_type *option_ir_ft;
201
202 BT_ASSERT(named_ft->ft->in_ir);
203 option_ir_ft = ctf_field_type_to_ir(named_ft->ft, tc, sc, ec);
204 BT_ASSERT(option_ir_ft);
205 ret = bt_field_type_variant_append_option(ir_ft,
206 named_ft->name->str, option_ir_ft);
207 BT_ASSERT(ret == 0);
208 bt_put(option_ir_ft);
209 }
210
211 return ir_ft;
212 }
213
214 static inline
215 struct bt_field_type *ctf_field_type_array_to_ir(
216 struct ctf_field_type_array *ft,
217 struct ctf_trace_class *tc,
218 struct ctf_stream_class *sc,
219 struct ctf_event_class *ec)
220 {
221 struct bt_field_type *ir_ft;
222 struct bt_field_type *elem_ir_ft;
223
224 if (ft->base.is_text) {
225 ir_ft = bt_field_type_string_create();
226 BT_ASSERT(ir_ft);
227 goto end;
228 }
229
230 elem_ir_ft = ctf_field_type_to_ir(ft->base.elem_ft, tc, sc, ec);
231 BT_ASSERT(elem_ir_ft);
232 ir_ft = bt_field_type_static_array_create(elem_ir_ft, ft->length);
233 BT_ASSERT(ir_ft);
234 bt_put(elem_ir_ft);
235
236 end:
237 return ir_ft;
238 }
239
240 static inline
241 struct bt_field_type *ctf_field_type_sequence_to_ir(
242 struct ctf_field_type_sequence *ft,
243 struct ctf_trace_class *tc,
244 struct ctf_stream_class *sc,
245 struct ctf_event_class *ec)
246 {
247 int ret;
248 struct bt_field_type *ir_ft;
249 struct bt_field_type *elem_ir_ft;
250
251 if (ft->base.is_text) {
252 ir_ft = bt_field_type_string_create();
253 BT_ASSERT(ir_ft);
254 goto end;
255 }
256
257 elem_ir_ft = ctf_field_type_to_ir(ft->base.elem_ft, tc, sc, ec);
258 BT_ASSERT(elem_ir_ft);
259 ir_ft = bt_field_type_dynamic_array_create(elem_ir_ft);
260 BT_ASSERT(ir_ft);
261 bt_put(elem_ir_ft);
262 BT_ASSERT(ir_ft);
263 ret = bt_field_type_dynamic_array_set_length_field_type(ir_ft,
264 borrow_ir_ft_from_field_path(&ft->length_path, tc, sc, ec));
265 BT_ASSERT(ret == 0);
266
267 end:
268 return ir_ft;
269 }
270
271 static inline
272 struct bt_field_type *ctf_field_type_to_ir(struct ctf_field_type *ft,
273 struct ctf_trace_class *tc,
274 struct ctf_stream_class *sc,
275 struct ctf_event_class *ec)
276 {
277 struct bt_field_type *ir_ft = NULL;
278
279 BT_ASSERT(ft);
280 BT_ASSERT(ft->in_ir);
281
282 switch (ft->id) {
283 case CTF_FIELD_TYPE_ID_INT:
284 ir_ft = ctf_field_type_int_to_ir((void *) ft);
285 break;
286 case CTF_FIELD_TYPE_ID_ENUM:
287 ir_ft = ctf_field_type_enum_to_ir((void *) ft);
288 break;
289 case CTF_FIELD_TYPE_ID_FLOAT:
290 ir_ft = ctf_field_type_float_to_ir((void *) ft);
291 break;
292 case CTF_FIELD_TYPE_ID_STRING:
293 ir_ft = ctf_field_type_string_to_ir((void *) ft);
294 break;
295 case CTF_FIELD_TYPE_ID_STRUCT:
296 ir_ft = ctf_field_type_struct_to_ir((void *) ft, tc, sc, ec);
297 break;
298 case CTF_FIELD_TYPE_ID_ARRAY:
299 ir_ft = ctf_field_type_array_to_ir((void *) ft, tc, sc, ec);
300 break;
301 case CTF_FIELD_TYPE_ID_SEQUENCE:
302 ir_ft = ctf_field_type_sequence_to_ir((void *) ft, tc, sc, ec);
303 break;
304 case CTF_FIELD_TYPE_ID_VARIANT:
305 ir_ft = ctf_field_type_variant_to_ir((void *) ft, tc, sc, ec);
306 break;
307 default:
308 abort();
309 }
310
311 ft->ir_ft = ir_ft;
312 return ir_ft;
313 }
314
315 static inline
316 bool ctf_field_type_struct_has_immediate_member_in_ir(
317 struct ctf_field_type_struct *ft)
318 {
319 uint64_t i;
320 bool has_immediate_member_in_ir = false;
321
322 for (i = 0; i < ft->members->len; i++) {
323 struct ctf_named_field_type *named_ft =
324 ctf_field_type_struct_borrow_member_by_index(ft, i);
325
326 if (named_ft->ft->in_ir) {
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
337 struct bt_field_type *scope_ctf_field_type_to_ir(struct ctf_field_type *ft,
338 struct ctf_trace_class *tc,
339 struct ctf_stream_class *sc,
340 struct ctf_event_class *ec)
341 {
342 struct bt_field_type *ir_ft = NULL;
343
344 if (!ft) {
345 goto end;
346 }
347
348 BT_ASSERT(ft->id == CTF_FIELD_TYPE_ID_STRUCT);
349
350 if (!ctf_field_type_struct_has_immediate_member_in_ir((void *) ft)) {
351 /*
352 * Nothing for IR in this scope: typical for packet
353 * header, packet context, and event header.
354 */
355 goto end;
356 }
357
358 ir_ft = ctf_field_type_to_ir(ft, tc, sc, ec);
359
360 end:
361 return ir_ft;
362 }
363
364 static inline
365 struct ctf_field_type_int *borrow_named_int_field_type(
366 struct ctf_field_type_struct *struct_ft, const char *name)
367 {
368 struct ctf_named_field_type *named_ft = NULL;
369 struct ctf_field_type_int *int_ft = NULL;
370
371 if (!struct_ft) {
372 goto end;
373 }
374
375 named_ft = ctf_field_type_struct_borrow_member_by_name(struct_ft, name);
376 if (!named_ft) {
377 goto end;
378 }
379
380 if (named_ft->ft->id != CTF_FIELD_TYPE_ID_INT &&
381 named_ft->ft->id != CTF_FIELD_TYPE_ID_ENUM) {
382 goto end;
383 }
384
385 int_ft = (void *) named_ft->ft;
386
387 end:
388 return int_ft;
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);
408 bt_put(ir_ec);
409
410 if (ec->spec_context_ft) {
411 struct bt_field_type *ir_ft = scope_ctf_field_type_to_ir(
412 ec->spec_context_ft, tc, sc, ec);
413
414 if (ir_ft) {
415 ret = bt_event_class_set_specific_context_field_type(
416 ir_ec, ir_ft);
417 BT_ASSERT(ret == 0);
418 bt_put(ir_ft);
419 }
420 }
421
422 if (ec->payload_ft) {
423 struct bt_field_type *ir_ft = scope_ctf_field_type_to_ir(
424 ec->payload_ft, tc, sc, ec);
425
426 if (ir_ft) {
427 ret = bt_event_class_set_payload_field_type(ir_ec,
428 ir_ft);
429 BT_ASSERT(ret == 0);
430 bt_put(ir_ft);
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;
463 struct ctf_field_type_int *int_ft;
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);
473 bt_put(ir_sc);
474
475 if (sc->packet_context_ft) {
476 struct bt_field_type *ir_ft = scope_ctf_field_type_to_ir(
477 sc->packet_context_ft, tc, sc, NULL);
478
479 if (ir_ft) {
480 ret = bt_stream_class_set_packet_context_field_type(
481 ir_sc, ir_ft);
482 BT_ASSERT(ret == 0);
483 bt_put(ir_ft);
484 }
485 }
486
487 if (sc->event_header_ft) {
488 struct bt_field_type *ir_ft = scope_ctf_field_type_to_ir(
489 sc->event_header_ft, tc, sc, NULL);
490
491 if (ir_ft) {
492 ret = bt_stream_class_set_event_header_field_type(ir_sc,
493 ir_ft);
494 BT_ASSERT(ret == 0);
495 bt_put(ir_ft);
496 }
497 }
498
499 if (sc->event_common_context_ft) {
500 struct bt_field_type *ir_ft = scope_ctf_field_type_to_ir(
501 sc->event_common_context_ft, tc, sc, NULL);
502
503 if (ir_ft) {
504 ret = bt_stream_class_set_event_common_context_field_type(
505 ir_sc, ir_ft);
506 BT_ASSERT(ret == 0);
507 bt_put(ir_ft);
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
523 int_ft = borrow_named_int_field_type((void *) sc->packet_context_ft,
524 "events_discarded");
525 if (int_ft) {
526 if (int_ft->meaning == CTF_FIELD_TYPE_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT) {
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
533 int_ft = borrow_named_int_field_type((void *) sc->packet_context_ft,
534 "packet_seq_num");
535 if (int_ft) {
536 if (int_ft->meaning == CTF_FIELD_TYPE_MEANING_PACKET_COUNTER_SNAPSHOT) {
537 ret = bt_stream_class_set_packets_have_packet_counter_snapshot(
538 ir_sc, BT_TRUE);
539 BT_ASSERT(ret == 0);
540 }
541 }
542
543 int_ft = borrow_named_int_field_type((void *) sc->packet_context_ft,
544 "timestamp_begin");
545 if (int_ft) {
546 if (int_ft->meaning == CTF_FIELD_TYPE_MEANING_PACKET_BEGINNING_TIME) {
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
553 int_ft = borrow_named_int_field_type((void *) sc->packet_context_ft,
554 "timestamp_end");
555 if (int_ft) {
556 if (int_ft->meaning == CTF_FIELD_TYPE_MEANING_PACKET_END_TIME) {
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
581 if (tc->packet_header_ft) {
582 struct bt_field_type *ir_ft = scope_ctf_field_type_to_ir(
583 tc->packet_header_ft, tc, NULL, NULL);
584
585 if (ir_ft) {
586 ret = bt_trace_set_packet_header_field_type(ir_trace,
587 ir_ft);
588 BT_ASSERT(ret == 0);
589 bt_put(ir_ft);
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 }
This page took 0.042962 seconds and 4 git commands to generate.