2829a0578cd65fcef4c7d0fd574a4289656b6aaf
[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_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);
33
34 static inline
35 void ctf_field_class_int_set_props(struct ctf_field_class_int *fc,
36 struct bt_field_class *ir_fc)
37 {
38 int ret;
39
40 ret = bt_field_class_integer_set_field_value_range(ir_fc, fc->base.size);
41 BT_ASSERT(ret == 0);
42 ret = bt_field_class_integer_set_preferred_display_base(ir_fc,
43 fc->disp_base);
44 BT_ASSERT(ret == 0);
45 }
46
47 static inline
48 struct bt_field_class *ctf_field_class_int_to_ir(struct ctf_field_class_int *fc)
49 {
50 struct bt_field_class *ir_fc;
51
52 if (fc->is_signed) {
53 ir_fc = bt_field_class_signed_integer_create();
54 } else {
55 ir_fc = bt_field_class_unsigned_integer_create();
56 }
57
58 BT_ASSERT(ir_fc);
59 ctf_field_class_int_set_props(fc, ir_fc);
60 return ir_fc;
61 }
62
63 static inline
64 struct bt_field_class *ctf_field_class_enum_to_ir(struct ctf_field_class_enum *fc)
65 {
66 int ret;
67 struct bt_field_class *ir_fc;
68 uint64_t i;
69
70 if (fc->base.is_signed) {
71 ir_fc = bt_field_class_signed_enumeration_create();
72 } else {
73 ir_fc = bt_field_class_unsigned_enumeration_create();
74 }
75
76 BT_ASSERT(ir_fc);
77 ctf_field_class_int_set_props((void *) fc, ir_fc);
78
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);
82
83 if (fc->base.is_signed) {
84 ret = bt_field_class_signed_enumeration_map_range(
85 ir_fc, mapping->label->str,
86 mapping->range.lower.i, mapping->range.upper.i);
87 } else {
88 ret = bt_field_class_unsigned_enumeration_map_range(
89 ir_fc, mapping->label->str,
90 mapping->range.lower.u, mapping->range.upper.u);
91 }
92
93 BT_ASSERT(ret == 0);
94 }
95
96 return ir_fc;
97 }
98
99 static inline
100 struct bt_field_class *ctf_field_class_float_to_ir(
101 struct ctf_field_class_float *fc)
102 {
103 struct bt_field_class *ir_fc;
104 int ret;
105
106 ir_fc = bt_field_class_real_create();
107 BT_ASSERT(ir_fc);
108
109 if (fc->base.size == 32) {
110 ret = bt_field_class_real_set_is_single_precision(ir_fc,
111 BT_TRUE);
112 BT_ASSERT(ret == 0);
113 }
114
115 return ir_fc;
116 }
117
118 static inline
119 struct bt_field_class *ctf_field_class_string_to_ir(
120 struct ctf_field_class_string *fc)
121 {
122 struct bt_field_class *ir_fc = bt_field_class_string_create();
123
124 BT_ASSERT(ir_fc);
125 return ir_fc;
126 }
127
128 static inline
129 struct bt_field_class *ctf_field_class_struct_to_ir(
130 struct ctf_field_class_struct *fc,
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_class *ir_fc = bt_field_class_structure_create();
137 uint64_t i;
138
139 BT_ASSERT(ir_fc);
140
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;
145
146 if (!named_fc->fc->in_ir) {
147 continue;
148 }
149
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);
154 BT_ASSERT(ret == 0);
155 bt_put(member_ir_fc);
156 }
157
158 return ir_fc;
159 }
160
161 static inline
162 struct bt_field_class *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_class *ir_fc = NULL;
169 struct ctf_field_class *fc = ctf_field_path_borrow_field_class(
170 field_path, tc, sc, ec);
171
172 BT_ASSERT(fc);
173
174 if (fc->in_ir) {
175 ir_fc = fc->ir_fc;
176 }
177
178 return ir_fc;
179 }
180
181 static inline
182 struct bt_field_class *ctf_field_class_variant_to_ir(
183 struct ctf_field_class_variant *fc,
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_class *ir_fc = bt_field_class_variant_create();
190 uint64_t i;
191
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));
195 BT_ASSERT(ret == 0);
196
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;
201
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);
207 BT_ASSERT(ret == 0);
208 bt_put(option_ir_fc);
209 }
210
211 return ir_fc;
212 }
213
214 static inline
215 struct 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)
220 {
221 struct bt_field_class *ir_fc;
222 struct bt_field_class *elem_ir_fc;
223
224 if (fc->base.is_text) {
225 ir_fc = bt_field_class_string_create();
226 BT_ASSERT(ir_fc);
227 goto end;
228 }
229
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);
234 bt_put(elem_ir_fc);
235
236 end:
237 return ir_fc;
238 }
239
240 static inline
241 struct bt_field_class *ctf_field_class_sequence_to_ir(
242 struct ctf_field_class_sequence *fc,
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_class *ir_fc;
249 struct bt_field_class *elem_ir_fc;
250
251 if (fc->base.is_text) {
252 ir_fc = bt_field_class_string_create();
253 BT_ASSERT(ir_fc);
254 goto end;
255 }
256
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);
261 bt_put(elem_ir_fc);
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));
265 BT_ASSERT(ret == 0);
266
267 end:
268 return ir_fc;
269 }
270
271 static inline
272 struct bt_field_class *ctf_field_class_to_ir(struct ctf_field_class *fc,
273 struct ctf_trace_class *tc,
274 struct ctf_stream_class *sc,
275 struct ctf_event_class *ec)
276 {
277 struct bt_field_class *ir_fc = NULL;
278
279 BT_ASSERT(fc);
280 BT_ASSERT(fc->in_ir);
281
282 switch (fc->id) {
283 case CTF_FIELD_CLASS_ID_INT:
284 ir_fc = ctf_field_class_int_to_ir((void *) fc);
285 break;
286 case CTF_FIELD_CLASS_ID_ENUM:
287 ir_fc = ctf_field_class_enum_to_ir((void *) fc);
288 break;
289 case CTF_FIELD_CLASS_ID_FLOAT:
290 ir_fc = ctf_field_class_float_to_ir((void *) fc);
291 break;
292 case CTF_FIELD_CLASS_ID_STRING:
293 ir_fc = ctf_field_class_string_to_ir((void *) fc);
294 break;
295 case CTF_FIELD_CLASS_ID_STRUCT:
296 ir_fc = ctf_field_class_struct_to_ir((void *) fc, tc, sc, ec);
297 break;
298 case CTF_FIELD_CLASS_ID_ARRAY:
299 ir_fc = ctf_field_class_array_to_ir((void *) fc, tc, sc, ec);
300 break;
301 case CTF_FIELD_CLASS_ID_SEQUENCE:
302 ir_fc = ctf_field_class_sequence_to_ir((void *) fc, tc, sc, ec);
303 break;
304 case CTF_FIELD_CLASS_ID_VARIANT:
305 ir_fc = ctf_field_class_variant_to_ir((void *) fc, tc, sc, ec);
306 break;
307 default:
308 abort();
309 }
310
311 fc->ir_fc = ir_fc;
312 return ir_fc;
313 }
314
315 static inline
316 bool ctf_field_class_struct_has_immediate_member_in_ir(
317 struct ctf_field_class_struct *fc)
318 {
319 uint64_t i;
320 bool has_immediate_member_in_ir = false;
321
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);
325
326 if (named_fc->fc->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_class *scope_ctf_field_class_to_ir(struct ctf_field_class *fc,
338 struct ctf_trace_class *tc,
339 struct ctf_stream_class *sc,
340 struct ctf_event_class *ec)
341 {
342 struct bt_field_class *ir_fc = NULL;
343
344 if (!fc) {
345 goto end;
346 }
347
348 BT_ASSERT(fc->id == CTF_FIELD_CLASS_ID_STRUCT);
349
350 if (!ctf_field_class_struct_has_immediate_member_in_ir((void *) fc)) {
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_fc = ctf_field_class_to_ir(fc, tc, sc, ec);
359
360 end:
361 return ir_fc;
362 }
363
364 static inline
365 struct ctf_field_class_int *borrow_named_int_field_class(
366 struct ctf_field_class_struct *struct_fc, const char *name)
367 {
368 struct ctf_named_field_class *named_fc = NULL;
369 struct ctf_field_class_int *int_fc = NULL;
370
371 if (!struct_fc) {
372 goto end;
373 }
374
375 named_fc = ctf_field_class_struct_borrow_member_by_name(struct_fc, name);
376 if (!named_fc) {
377 goto end;
378 }
379
380 if (named_fc->fc->id != CTF_FIELD_CLASS_ID_INT &&
381 named_fc->fc->id != CTF_FIELD_CLASS_ID_ENUM) {
382 goto end;
383 }
384
385 int_fc = (void *) named_fc->fc;
386
387 end:
388 return int_fc;
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_fc) {
411 struct bt_field_class *ir_fc = scope_ctf_field_class_to_ir(
412 ec->spec_context_fc, tc, sc, ec);
413
414 if (ir_fc) {
415 ret = bt_event_class_set_specific_context_field_class(
416 ir_ec, ir_fc);
417 BT_ASSERT(ret == 0);
418 bt_put(ir_fc);
419 }
420 }
421
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);
425
426 if (ir_fc) {
427 ret = bt_event_class_set_payload_field_class(ir_ec,
428 ir_fc);
429 BT_ASSERT(ret == 0);
430 bt_put(ir_fc);
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_class_int *int_fc;
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_fc) {
476 struct bt_field_class *ir_fc = scope_ctf_field_class_to_ir(
477 sc->packet_context_fc, tc, sc, NULL);
478
479 if (ir_fc) {
480 ret = bt_stream_class_set_packet_context_field_class(
481 ir_sc, ir_fc);
482 BT_ASSERT(ret == 0);
483 bt_put(ir_fc);
484 }
485 }
486
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);
490
491 if (ir_fc) {
492 ret = bt_stream_class_set_event_header_field_class(ir_sc,
493 ir_fc);
494 BT_ASSERT(ret == 0);
495 bt_put(ir_fc);
496 }
497 }
498
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);
502
503 if (ir_fc) {
504 ret = bt_stream_class_set_event_common_context_field_class(
505 ir_sc, ir_fc);
506 BT_ASSERT(ret == 0);
507 bt_put(ir_fc);
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_fc = borrow_named_int_field_class((void *) sc->packet_context_fc,
524 "events_discarded");
525 if (int_fc) {
526 if (int_fc->meaning == CTF_FIELD_CLASS_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_fc = borrow_named_int_field_class((void *) sc->packet_context_fc,
534 "packet_seq_num");
535 if (int_fc) {
536 if (int_fc->meaning == CTF_FIELD_CLASS_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_fc = borrow_named_int_field_class((void *) sc->packet_context_fc,
544 "timestamp_begin");
545 if (int_fc) {
546 if (int_fc->meaning == CTF_FIELD_CLASS_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_fc = borrow_named_int_field_class((void *) sc->packet_context_fc,
554 "timestamp_end");
555 if (int_fc) {
556 if (int_fc->meaning == CTF_FIELD_CLASS_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_fc) {
582 struct bt_field_class *ir_fc = scope_ctf_field_class_to_ir(
583 tc->packet_header_fc, tc, NULL, NULL);
584
585 if (ir_fc) {
586 ret = bt_trace_set_packet_header_field_class(ir_trace,
587 ir_fc);
588 BT_ASSERT(ret == 0);
589 bt_put(ir_fc);
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.063232 seconds and 3 git commands to generate.