ctf: remove strict metadata mode, update automatic CC mapping behaviour
[babeltrace.git] / plugins / ctf / common / metadata / visitor-generate-ir.c
index 56069e5cd6d6d26c9a236ec1325598c21c0d9598..b18b747b989f7f6789f06598c74f971df83de6e1 100644 (file)
@@ -3766,63 +3766,149 @@ end:
 }
 
 static
-int auto_map_fields_to_trace_clock_class(struct ctx *ctx,
-               struct bt_ctf_field_type *packet_context_field_type,
-               const char **field_names)
+int auto_map_field_to_trace_clock_class(struct ctx *ctx,
+               struct bt_ctf_field_type *ft)
 {
-       _BT_CTF_FIELD_TYPE_INIT(ft);
-       struct bt_ctf_clock_class *clock_class = NULL;
+       struct bt_ctf_clock_class *clock_class_to_map_to = NULL;
        struct bt_ctf_clock_class *mapped_clock_class = NULL;
        int ret = 0;
-       const char **field_name;
+       int64_t clock_class_count;
+
+       if (!ft || !bt_ctf_field_type_is_integer(ft)) {
+               goto end;
+       }
+
+       mapped_clock_class =
+               bt_ctf_field_type_integer_get_mapped_clock_class(ft);
+       if (mapped_clock_class) {
+               goto end;
+       }
+
+       clock_class_count = bt_ctf_trace_get_clock_class_count(ctx->trace);
+       assert(clock_class_count >= 0);
+
+       switch (clock_class_count) {
+       case 0:
+               /*
+                * No clock class exists in the trace at this
+                * point. Create an implicit one at 1 GHz,
+                * named `default`, and use this clock class.
+                */
+               clock_class_to_map_to = bt_ctf_clock_class_create("default");
+               if (!clock_class_to_map_to) {
+                       BT_LOGE_STR("Cannot create a clock class.");
+                       ret = -1;
+                       goto end;
+               }
+
+               ret = bt_ctf_clock_class_set_frequency(clock_class_to_map_to,
+                       1000000000);
+               assert(ret == 0);
 
-       if (ctx->decoder_config.strict) {
+               ret = bt_ctf_trace_add_clock_class(ctx->trace,
+                       clock_class_to_map_to);
+               if (ret) {
+                       BT_LOGE_STR("Cannot add clock class to trace.");
+                       goto end;
+               }
+               break;
+       case 1:
+               /*
+                * Only one clock class exists in the trace at
+                * this point: use this one.
+                */
+               clock_class_to_map_to =
+                       bt_ctf_trace_get_clock_class_by_index(ctx->trace, 0);
+               assert(clock_class_to_map_to);
+               break;
+       default:
+               /*
+                * Timestamp field not mapped to a clock class
+                * and there's more than one clock class in the
+                * trace: this is an error.
+                */
+               BT_LOGE_STR("Timestamp field found with no mapped clock class, "
+                       "but there's more than one clock class in the trace at this point.");
+               ret = -1;
                goto end;
        }
 
-       if (!packet_context_field_type) {
+       assert(clock_class_to_map_to);
+       ret = bt_ctf_field_type_integer_set_mapped_clock_class(ft,
+               clock_class_to_map_to);
+       if (ret) {
+               BT_LOGE("Cannot map field type's field to trace's clock class: "
+                       "clock-class-name=\"%s\", ret=%d",
+                       bt_ctf_clock_class_get_name(clock_class_to_map_to),
+                       ret);
                goto end;
        }
 
-       if (!bt_ctf_field_type_is_structure(packet_context_field_type)) {
+end:
+       bt_put(clock_class_to_map_to);
+       bt_put(mapped_clock_class);
+       return ret;
+}
+
+static
+int auto_map_fields_to_trace_clock_class(struct ctx *ctx,
+               struct bt_ctf_field_type *root_ft, const char *field_name)
+{
+       int ret = 0;
+       int64_t i, count;
+
+       if (!root_ft) {
                goto end;
        }
 
-       if (bt_ctf_trace_get_clock_class_count(ctx->trace) != 1) {
+       if (!bt_ctf_field_type_is_structure(root_ft) &&
+                       !bt_ctf_field_type_is_variant(root_ft)) {
                goto end;
        }
 
-       clock_class = bt_ctf_trace_get_clock_class_by_index(ctx->trace, 0);
-       assert(clock_class);
+       if (bt_ctf_field_type_is_structure(root_ft)) {
+               count = bt_ctf_field_type_structure_get_field_count(root_ft);
+       } else {
+               count = bt_ctf_field_type_variant_get_field_count(root_ft);
+       }
 
-       for (field_name = field_names; *field_name; field_name++) {
-               ft = bt_ctf_field_type_structure_get_field_type_by_name(
-                       packet_context_field_type, *field_name);
+       assert(count >= 0);
 
-               if (ft && bt_ctf_field_type_is_integer(ft)) {
-                       mapped_clock_class =
-                               bt_ctf_field_type_integer_get_mapped_clock_class(ft);
+       for (i = 0; i < count; i++) {
+               _BT_CTF_FIELD_TYPE_INIT(ft);
+               const char *name;
 
-                       if (!mapped_clock_class) {
-                               ret = bt_ctf_field_type_integer_set_mapped_clock_class(
-                                       ft, clock_class);
-                               if (ret) {
-                                       BT_LOGE("Cannot map field type's field to trace's clock class: "
-                                               "field-name=\"%s\", ret=%d",
-                                               *field_name, ret);
-                                       goto end;
-                               }
+               if (bt_ctf_field_type_is_structure(root_ft)) {
+                       ret = bt_ctf_field_type_structure_get_field_by_index(
+                               root_ft, &name, &ft, i);
+               } else if (bt_ctf_field_type_is_variant(root_ft)) {
+                       ret = bt_ctf_field_type_variant_get_field_by_index(
+                               root_ft, &name, &ft, i);
+               }
+
+               assert(ret == 0);
+
+               if (strcmp(name, field_name) == 0) {
+                       ret = auto_map_field_to_trace_clock_class(ctx, ft);
+                       if (ret) {
+                               BT_LOGE("Cannot automatically map field to trace's clock class: "
+                                       "field-name=\"%s\"", field_name);
+                               bt_put(ft);
+                               goto end;
                        }
                }
 
-               BT_PUT(mapped_clock_class);
-               BT_PUT(ft);
+               ret = auto_map_fields_to_trace_clock_class(ctx, ft, field_name);
+               bt_put(ft);
+               if (ret) {
+                       BT_LOGE("Cannot automatically map structure or variant field type's fields to trace's clock class: "
+                               "field-name=\"%s\", root-field-name=\"%s\"",
+                               field_name, name);
+                       goto end;
+               }
        }
 
 end:
-       bt_put(mapped_clock_class);
-       bt_put(clock_class);
-       bt_put(ft);
        return ret;
 }
 
@@ -3903,12 +3989,6 @@ int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node,
 
                        _SET(set, _STREAM_ID_SET);
                } else if (!strcmp(left, "event.header")) {
-                       const char *field_names[] = {
-                               "timestamp",
-                               "ts",
-                               NULL,
-                       };
-
                        if (_IS_SET(set, _STREAM_EVENT_HEADER_SET)) {
                                _BT_LOGE_NODE(node,
                                        "Duplicate `event.header` entry in stream class.");
@@ -3929,10 +4009,10 @@ int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node,
 
                        assert(decl);
                        ret = auto_map_fields_to_trace_clock_class(ctx,
-                               decl, field_names);
+                               decl, "timestamp");
                        if (ret) {
                                _BT_LOGE_NODE(node,
-                                       "Cannot automatically map specific event header field type fields to trace's clock class.");
+                                       "Cannot automatically map specific event header field type fields named `timestamp` to trace's clock class.");
                                goto error;
                        }
 
@@ -3978,12 +4058,6 @@ int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node,
 
                        _SET(set, _STREAM_EVENT_CONTEXT_SET);
                } else if (!strcmp(left, "packet.context")) {
-                       const char *field_names[] = {
-                               "timestamp_begin",
-                               "timestamp_end",
-                               NULL,
-                       };
-
                        if (_IS_SET(set, _STREAM_PACKET_CONTEXT_SET)) {
                                _BT_LOGE_NODE(node,
                                        "Duplicate `packet.context` entry in stream class.");
@@ -4004,10 +4078,18 @@ int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node,
 
                        assert(decl);
                        ret = auto_map_fields_to_trace_clock_class(ctx,
-                               decl, field_names);
+                               decl, "timestamp_begin");
+                       if (ret) {
+                               _BT_LOGE_NODE(node,
+                                       "Cannot automatically map specific packet context field type fields named `timestamp_begin` to trace's clock class.");
+                               goto error;
+                       }
+
+                       ret = auto_map_fields_to_trace_clock_class(ctx,
+                               decl, "timestamp_end");
                        if (ret) {
                                _BT_LOGE_NODE(node,
-                                       "Cannot automatically map specific packet context field type fields to trace's clock class.");
+                                       "Cannot automatically map specific packet context field type fields named `timestamp_end` to trace's clock class.");
                                goto error;
                        }
 
@@ -4443,14 +4525,12 @@ int visit_env(struct ctx *ctx, struct ctf_node *node)
                                goto error;
                        }
 
-                       if (!ctx->decoder_config.strict) {
-                               if (strcmp(left, "tracer_name") == 0) {
-                                       if (strncmp(right, "lttng", 5) == 0) {
-                                               BT_LOGI("Non-strict mode: detected LTTng trace from `%s` environment value: "
-                                                       "tracer-name=\"%s\"",
-                                                       left, right);
-                                               ctx->is_lttng = 1;
-                                       }
+                       if (strcmp(left, "tracer_name") == 0) {
+                               if (strncmp(right, "lttng", 5) == 0) {
+                                       BT_LOGI("Detected LTTng trace from `%s` environment value: "
+                                               "tracer-name=\"%s\"",
+                                               left, right);
+                                       ctx->is_lttng = 1;
                                }
                        }
 
This page took 0.027807 seconds and 4 git commands to generate.