Move to kernel style SPDX license identifiers
[babeltrace.git] / src / plugins / ctf / fs-sink / translate-ctf-ir-to-tsdl.c
index 4c0653a988024f94d7e49c0a3bebc6b862968dfe..85f6bee7e51b27c02c6a6ef5f3b220a8a94ad345 100644 (file)
@@ -1,25 +1,11 @@
 /*
- * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
+ * SPDX-License-Identifier: MIT
  *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
+ * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
  */
 
+#include "translate-ctf-ir-to-tsdl.h"
+
 #include <babeltrace2/babeltrace.h>
 #include "common/macros.h"
 #include <stdio.h>
@@ -50,23 +36,8 @@ static
 void append_uuid(struct ctx *ctx, bt_uuid uuid)
 {
        g_string_append_printf(ctx->tsdl,
-               "\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
-               (unsigned int) uuid[0],
-               (unsigned int) uuid[1],
-               (unsigned int) uuid[2],
-               (unsigned int) uuid[3],
-               (unsigned int) uuid[4],
-               (unsigned int) uuid[5],
-               (unsigned int) uuid[6],
-               (unsigned int) uuid[7],
-               (unsigned int) uuid[8],
-               (unsigned int) uuid[9],
-               (unsigned int) uuid[10],
-               (unsigned int) uuid[11],
-               (unsigned int) uuid[12],
-               (unsigned int) uuid[13],
-               (unsigned int) uuid[14],
-               (unsigned int) uuid[15]);
+               "\"" BT_UUID_FMT "\"",
+               BT_UUID_FMT_VALUES(uuid));
 }
 
 static
@@ -151,7 +122,7 @@ void append_integer_field_class_from_props(struct ctx *ctx, unsigned int size,
                        g_string_append(ctx->tsdl, "x");
                        break;
                default:
-                       abort();
+                       bt_common_abort();
                }
 
                g_string_append_c(ctx->tsdl, ';');
@@ -196,17 +167,45 @@ void append_end_block_semi_nl_nl(struct ctx *ctx)
        g_string_append_c(ctx->tsdl, '\n');
 }
 
+static
+void append_bool_field_class(struct ctx *ctx,
+               __attribute__((unused)) struct fs_sink_ctf_field_class_bool *fc)
+{
+       /*
+        * CTF 1.8 has no boolean field class type, so this component
+        * translates it to an 8-bit unsigned integer field class.
+        */
+       append_integer_field_class_from_props(ctx, fc->base.size,
+               fc->base.base.alignment, false,
+               BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL,
+               NULL, NULL, false);
+}
+
+static
+void append_bit_array_field_class(struct ctx *ctx,
+               struct fs_sink_ctf_field_class_bit_array *fc)
+{
+       /*
+        * CTF 1.8 has no bit array field class type, so this component
+        * translates it to an unsigned integer field class with an
+        * hexadecimal base.
+        */
+       append_integer_field_class_from_props(ctx, fc->size,
+               fc->base.alignment, false,
+               BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL,
+               NULL, NULL, false);
+}
+
 static
 void append_integer_field_class(struct ctx *ctx,
                struct fs_sink_ctf_field_class_int *fc)
 {
        const bt_field_class *ir_fc = fc->base.base.ir_fc;
        bt_field_class_type type = bt_field_class_get_type(ir_fc);
-       bool is_signed = type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION ||
-               type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER;
+       bool is_signed = bt_field_class_type_is(type,
+               BT_FIELD_CLASS_TYPE_SIGNED_INTEGER);
 
-       if (type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION ||
-                       type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION) {
+       if (bt_field_class_type_is(type, BT_FIELD_CLASS_TYPE_ENUMERATION)) {
                g_string_append(ctx->tsdl, "enum : ");
        }
 
@@ -215,8 +214,7 @@ void append_integer_field_class(struct ctx *ctx,
                bt_field_class_integer_get_preferred_display_base(ir_fc),
                NULL, NULL, false);
 
-       if (type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION ||
-                       type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION) {
+       if (bt_field_class_type_is(type, BT_FIELD_CLASS_TYPE_ENUMERATION)) {
                uint64_t i;
 
                g_string_append(ctx->tsdl, " {\n");
@@ -225,59 +223,55 @@ void append_integer_field_class(struct ctx *ctx,
                for (i = 0; i < bt_field_class_enumeration_get_mapping_count(ir_fc); i++) {
                        const char *label;
                        const bt_field_class_enumeration_mapping *mapping;
-                       const bt_field_class_unsigned_enumeration_mapping *u_mapping;
-                       const bt_field_class_signed_enumeration_mapping *i_mapping;
+                       const bt_field_class_enumeration_unsigned_mapping *u_mapping;
+                       const bt_field_class_enumeration_signed_mapping *s_mapping;
+                       const bt_integer_range_set *ranges;
+                       const bt_integer_range_set_unsigned *u_ranges;
+                       const bt_integer_range_set_signed *s_ranges;
                        uint64_t range_count;
                        uint64_t range_i;
 
                        if (is_signed) {
-                               i_mapping = bt_field_class_signed_enumeration_borrow_mapping_by_index_const(
+                               s_mapping = bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
                                        ir_fc, i);
-                               mapping = bt_field_class_signed_enumeration_mapping_as_mapping_const(
-                                       i_mapping);
+                               mapping = bt_field_class_enumeration_signed_mapping_as_mapping_const(
+                                       s_mapping);
+                               s_ranges = bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
+                                       s_mapping);
+                               ranges = bt_integer_range_set_signed_as_range_set_const(
+                                       s_ranges);
                        } else {
-                               u_mapping = bt_field_class_unsigned_enumeration_borrow_mapping_by_index_const(
+                               u_mapping = bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
                                        ir_fc, i);
-                               mapping = bt_field_class_unsigned_enumeration_mapping_as_mapping_const(
+                               mapping = bt_field_class_enumeration_unsigned_mapping_as_mapping_const(
+                                       u_mapping);
+                               u_ranges = bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
                                        u_mapping);
+                               ranges = bt_integer_range_set_unsigned_as_range_set_const(
+                                       u_ranges);
                        }
 
                        label = bt_field_class_enumeration_mapping_get_label(
                                mapping);
-                       range_count =
-                               bt_field_class_enumeration_mapping_get_range_count(
-                                       mapping);
+                       range_count = bt_integer_range_set_get_range_count(
+                               ranges);
 
                        for (range_i = 0; range_i < range_count; range_i++) {
                                append_indent(ctx);
-
-                               /*
-                                * Systematically prepend `_` to the
-                                * mapping's label as this could be used
-                                * as the tag of a subsequent variant
-                                * field class and variant FC option
-                                * names are systematically protected
-                                * with a leading `_`.
-                                *
-                                * FIXME: This is temporary as the
-                                * library's API should change to
-                                * decouple variant FC option names from
-                                * selector FC labels. The current
-                                * drawback is that an original label
-                                * `HELLO` becomes `_HELLO` in the
-                                * generated metadata, therefore tools
-                                * expecting `HELLO` could fail.
-                                */
-                               g_string_append(ctx->tsdl, "\"_");
+                               g_string_append(ctx->tsdl, "\"");
                                append_quoted_string_content(ctx, label);
                                g_string_append(ctx->tsdl, "\" = ");
 
                                if (is_signed) {
+                                       const bt_integer_range_signed *range;
                                        int64_t lower, upper;
 
-                                       bt_field_class_signed_enumeration_mapping_get_range_by_index(
-                                               i_mapping, range_i,
-                                               &lower, &upper);
+                                       range = bt_integer_range_set_signed_borrow_range_by_index_const(
+                                               s_ranges, range_i);
+                                       lower = bt_integer_range_signed_get_lower(
+                                               range);
+                                       upper = bt_integer_range_signed_get_upper(
+                                               range);
 
                                        if (lower == upper) {
                                                g_string_append_printf(
@@ -289,11 +283,15 @@ void append_integer_field_class(struct ctx *ctx,
                                                        lower, upper);
                                        }
                                } else {
+                                       const bt_integer_range_unsigned *range;
                                        uint64_t lower, upper;
 
-                                       bt_field_class_unsigned_enumeration_mapping_get_range_by_index(
-                                               u_mapping, range_i,
-                                               &lower, &upper);
+                                       range = bt_integer_range_set_unsigned_borrow_range_by_index_const(
+                                               u_ranges, range_i);
+                                       lower = bt_integer_range_unsigned_get_lower(
+                                               range);
+                                       upper = bt_integer_range_unsigned_get_upper(
+                                               range);
 
                                        if (lower == upper) {
                                                g_string_append_printf(
@@ -320,7 +318,8 @@ void append_float_field_class(struct ctx *ctx,
 {
        unsigned int mant_dig, exp_dig;
 
-       if (bt_field_class_real_is_single_precision(fc->base.base.ir_fc)) {
+       if (bt_field_class_get_type(fc->base.base.ir_fc) ==
+                       BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) {
                mant_dig = 24;
                exp_dig = 8;
        } else {
@@ -350,6 +349,8 @@ void append_member(struct ctx *ctx, const char *name,
        GString *lengths = NULL;
        const char *lengths_str = "";
 
+       BT_ASSERT(fc);
+
        while (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY ||
                        fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE) {
                if (!lengths) {
@@ -399,6 +400,11 @@ void append_struct_field_class_members(struct ctx *ctx,
                                struct_fc, i);
                struct fs_sink_ctf_field_class *fc = named_fc->fc;
 
+               /*
+                * For sequence, option, and variant field classes, if
+                * the length/tag field class is generated before, write
+                * it now before the dependent field class.
+                */
                if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE) {
                        struct fs_sink_ctf_field_class_sequence *seq_fc =
                                (void *) fc;
@@ -410,6 +416,42 @@ void append_struct_field_class_members(struct ctx *ctx,
                                        BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL,
                                        NULL, seq_fc->length_ref->str, true);
                        }
+               } else if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION) {
+                       struct fs_sink_ctf_field_class_option *opt_fc =
+                               (void *) fc;
+
+                       /*
+                        * CTF 1.8 does not support the option field
+                        * class type. To write something anyway, this
+                        * component translates this type to a variant
+                        * field class where the options are:
+                        *
+                        * * An empty structure field class.
+                        * * The optional field class itself.
+                        *
+                        * The "tag" is always generated/before in that
+                        * case (an 8-bit unsigned enumeration field
+                        * class).
+                        */
+                       append_indent(ctx);
+                       g_string_append(ctx->tsdl,
+                               "/* The enumeration and variant field classes "
+                               "below were a trace IR option field class. */\n");
+                       append_indent(ctx);
+                       g_string_append(ctx->tsdl, "enum : ");
+                       append_integer_field_class_from_props(ctx,
+                               8, 8, false,
+                               BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL,
+                               NULL, NULL, false);
+                       g_string_append(ctx->tsdl, " {\n");
+                       ctx->indent_level++;
+                       append_indent(ctx);
+                       g_string_append(ctx->tsdl, "none = 0,\n");
+                       append_indent(ctx);
+                       g_string_append(ctx->tsdl, "content = 1,\n");
+                       append_end_block(ctx);
+                       g_string_append_printf(ctx->tsdl, " %s;\n",
+                               opt_fc->tag_ref->str);
                } else if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT) {
                        struct fs_sink_ctf_field_class_variant *var_fc =
                                (void *) fc;
@@ -425,20 +467,28 @@ void append_struct_field_class_members(struct ctx *ctx,
                                ctx->indent_level++;
 
                                for (i = 0; i < var_fc->options->len; i++) {
-                                       struct fs_sink_ctf_named_field_class *named_fc =
+                                       struct fs_sink_ctf_named_field_class *option_named_fc =
                                                fs_sink_ctf_field_class_variant_borrow_option_by_index(
                                                        var_fc, i);
 
                                        append_indent(ctx);
                                        g_string_append_printf(ctx->tsdl,
                                                "\"%s\" = %" PRIu64 ",\n",
-                                               named_fc->name->str, i);
+                                               option_named_fc->name->str, i);
                                }
 
                                append_end_block(ctx);
                                g_string_append_printf(ctx->tsdl, " %s;\n",
                                        var_fc->tag_ref->str);
                        }
+               } else if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL) {
+                       append_indent(ctx);
+                       g_string_append(ctx->tsdl,
+                               "/* The integer field class below was a trace IR boolean field class. */\n");
+               } else if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY) {
+                       append_indent(ctx);
+                       g_string_append(ctx->tsdl,
+                               "/* The integer field class below was a trace IR bit array field class. */\n");
                }
 
                append_indent(ctx);
@@ -458,6 +508,20 @@ void append_struct_field_class(struct ctx *ctx,
                fc->base.alignment);
 }
 
+static
+void append_option_field_class(struct ctx *ctx,
+               struct fs_sink_ctf_field_class_option *opt_fc)
+{
+       g_string_append_printf(ctx->tsdl, "variant <%s> {\n",
+               opt_fc->tag_ref->str);
+       ctx->indent_level++;
+       append_indent(ctx);
+       g_string_append(ctx->tsdl, "struct { } none;\n");
+       append_indent(ctx);
+       append_member(ctx, "content", opt_fc->content_fc);
+       append_end_block(ctx);
+}
+
 static
 void append_variant_field_class(struct ctx *ctx,
                struct fs_sink_ctf_field_class_variant *var_fc)
@@ -484,6 +548,12 @@ static
 void append_field_class(struct ctx *ctx, struct fs_sink_ctf_field_class *fc)
 {
        switch (fc->type) {
+       case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL:
+               append_bool_field_class(ctx, (void *) fc);
+               break;
+       case FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY:
+               append_bit_array_field_class(ctx, (void *) fc);
+               break;
        case FS_SINK_CTF_FIELD_CLASS_TYPE_INT:
                append_integer_field_class(ctx, (void *) fc);
                break;
@@ -496,11 +566,14 @@ void append_field_class(struct ctx *ctx, struct fs_sink_ctf_field_class *fc)
        case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT:
                append_struct_field_class(ctx, (void *) fc);
                break;
+       case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION:
+               append_option_field_class(ctx, (void *) fc);
+               break;
        case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT:
                append_variant_field_class(ctx, (void *) fc);
                break;
        default:
-               abort();
+               bt_common_abort();
        }
 }
 
@@ -594,7 +667,7 @@ void append_event_class(struct ctx *ctx, struct fs_sink_ctf_event_class *ec)
                        level = 14;
                        break;
                default:
-                       abort();
+                       bt_common_abort();
                }
 
                g_string_append_printf(ctx->tsdl, "%u;\n", level);
@@ -792,7 +865,7 @@ void append_stream_class(struct ctx *ctx,
 }
 
 BT_HIDDEN
-void translate_trace_class_ctf_ir_to_tsdl(struct fs_sink_ctf_trace_class *tc,
+void translate_trace_ctf_ir_to_tsdl(struct fs_sink_ctf_trace *trace,
                GString *tsdl)
 {
        struct ctx ctx = {
@@ -817,7 +890,7 @@ void translate_trace_class_ctf_ir_to_tsdl(struct fs_sink_ctf_trace_class *tc,
        g_string_append(tsdl, "minor = 8;\n");
        append_indent(&ctx);
        g_string_append(tsdl, "uuid = ");
-       append_uuid(&ctx, tc->uuid);
+       append_uuid(&ctx, trace->uuid);
        g_string_append(tsdl, ";\n");
        append_indent(&ctx);
        g_string_append(tsdl, "byte_order = ");
@@ -858,8 +931,8 @@ void translate_trace_class_ctf_ir_to_tsdl(struct fs_sink_ctf_trace_class *tc,
        /* End trace class */
        append_end_block_semi_nl_nl(&ctx);
 
-       /* Trace class environment */
-       count = bt_trace_class_get_environment_entry_count(tc->ir_tc);
+       /* Trace environment */
+       count = bt_trace_get_environment_entry_count(trace->ir_trace);
        if (count > 0) {
                append_indent(&ctx);
                g_string_append(tsdl, "env {\n");
@@ -869,15 +942,15 @@ void translate_trace_class_ctf_ir_to_tsdl(struct fs_sink_ctf_trace_class *tc,
                        const char *name;
                        const bt_value *val;
 
-                       bt_trace_class_borrow_environment_entry_by_index_const(
-                               tc->ir_tc, i, &name, &val);
+                       bt_trace_borrow_environment_entry_by_index_const(
+                               trace->ir_trace, i, &name, &val);
                        append_indent(&ctx);
                        g_string_append_printf(tsdl, "%s = ", name);
 
                        switch (bt_value_get_type(val)) {
                        case BT_VALUE_TYPE_SIGNED_INTEGER:
                                g_string_append_printf(tsdl, "%" PRId64,
-                                       bt_value_signed_integer_get(val));
+                                       bt_value_integer_signed_get(val));
                                break;
                        case BT_VALUE_TYPE_STRING:
                                append_quoted_string(&ctx, bt_value_string_get(val));
@@ -885,9 +958,9 @@ void translate_trace_class_ctf_ir_to_tsdl(struct fs_sink_ctf_trace_class *tc,
                        default:
                                /*
                                 * This is checked in
-                                * translate_trace_class_trace_ir_to_ctf_ir().
+                                * translate_trace_trace_ir_to_ctf_ir().
                                 */
-                               abort();
+                               bt_common_abort();
                        }
 
                        g_string_append(tsdl, ";\n");
@@ -898,7 +971,7 @@ void translate_trace_class_ctf_ir_to_tsdl(struct fs_sink_ctf_trace_class *tc,
        }
 
        /* Stream classes and their event classes */
-       for (i = 0; i < tc->stream_classes->len; i++) {
-               append_stream_class(&ctx, tc->stream_classes->pdata[i]);
+       for (i = 0; i < trace->stream_classes->len; i++) {
+               append_stream_class(&ctx, trace->stream_classes->pdata[i]);
        }
 }
This page took 0.031247 seconds and 4 git commands to generate.