ir: validate sequence length fields and variant tags of event classes
[babeltrace.git] / formats / ctf / ir / stream-class.c
index 0f823ea7aab6e209d9cc4811152ba4739a54ca37..72b07b84c14cb26b98d983e908f4e88c18635dd3 100644 (file)
@@ -34,6 +34,7 @@
 #include <babeltrace/ctf-ir/event-fields-internal.h>
 #include <babeltrace/ctf-writer/stream.h>
 #include <babeltrace/ctf-ir/stream-class-internal.h>
+#include <babeltrace/ctf-ir/visitor-internal.h>
 #include <babeltrace/ctf-writer/functor-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/compiler.h>
@@ -87,6 +88,23 @@ error:
        return stream_class;
 }
 
+struct bt_ctf_trace *bt_ctf_stream_class_get_trace(
+               struct bt_ctf_stream_class *stream_class)
+{
+       struct bt_ctf_trace *trace = NULL;
+
+       if (!stream_class) {
+               goto end;
+       }
+
+       trace = stream_class->trace;
+       if (trace) {
+               bt_ctf_trace_get(trace);
+       }
+end:
+       return trace;
+}
+
 const char *bt_ctf_stream_class_get_name(
                struct bt_ctf_stream_class *stream_class)
 {
@@ -202,6 +220,47 @@ int _bt_ctf_stream_class_set_id(
        return 0;
 }
 
+struct event_class_set_stream_id_data {
+       uint32_t stream_id;
+       int ret;
+};
+
+static
+void event_class_set_stream_id(gpointer event_class, gpointer data)
+{
+       struct event_class_set_stream_id_data *typed_data = data;
+
+       typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class,
+               typed_data->stream_id);
+}
+
+BT_HIDDEN
+int bt_ctf_stream_class_set_id_no_check(
+               struct bt_ctf_stream_class *stream_class, uint32_t id)
+{
+       int ret = 0;
+       struct event_class_set_stream_id_data data =
+               { .stream_id = id, .ret = 0 };
+
+       /*
+        * Make sure all event classes have their "stream_id" attribute
+        * set to this value.
+        */
+       g_ptr_array_foreach(stream_class->event_classes,
+               event_class_set_stream_id, &data);
+       ret = data.ret;
+       if (ret) {
+               goto end;
+       }
+
+       ret = _bt_ctf_stream_class_set_id(stream_class, id);
+       if (ret) {
+               goto end;
+       }
+end:
+       return ret;
+}
+
 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
                uint32_t id)
 {
@@ -212,12 +271,53 @@ int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
                goto end;
        }
 
-       ret = _bt_ctf_stream_class_set_id(stream_class, id);
-       if (ret) {
+       ret = bt_ctf_stream_class_set_id_no_check(stream_class, id);
+end:
+       return ret;
+}
+
+static
+void event_class_exists(gpointer element, gpointer query)
+{
+       struct bt_ctf_event_class *event_class_a = element;
+       struct search_query *search_query = query;
+       struct bt_ctf_event_class *event_class_b = search_query->value;
+       int64_t id_a, id_b;
+
+       if (search_query->value == element) {
+               search_query->found = 1;
+               goto end;
+       }
+
+       /*
+        * Two event classes cannot share the same name in a given
+        * stream class.
+        */
+       if (!strcmp(bt_ctf_event_class_get_name(event_class_a),
+                       bt_ctf_event_class_get_name(event_class_b))) {
+               search_query->found = 1;
+               goto end;
+       }
+
+       /*
+        * Two event classes cannot share the same ID in a given
+        * stream class.
+        */
+       id_a = bt_ctf_event_class_get_id(event_class_a);
+       id_b = bt_ctf_event_class_get_id(event_class_b);
+
+       if (id_a < 0 || id_b < 0) {
+               /* at least one ID is not set: will be automatically set later */
                goto end;
        }
+
+       if (id_a == id_b) {
+               search_query->found = 1;
+               goto end;
+       }
+
 end:
-       return ret;
+       return;
 }
 
 int bt_ctf_stream_class_add_event_class(
@@ -234,12 +334,27 @@ int bt_ctf_stream_class_add_event_class(
 
        /* Check for duplicate event classes */
        struct search_query query = { .value = event_class, .found = 0 };
-       g_ptr_array_foreach(stream_class->event_classes, value_exists, &query);
+       g_ptr_array_foreach(stream_class->event_classes, event_class_exists,
+               &query);
        if (query.found) {
                ret = -1;
                goto end;
        }
 
+       /*
+        * Resolve the event's sequence length and variant tags if the
+        * stream is already associated with a trace. Otherwise, this
+        * validation will be performed once the stream is registered
+        * to a trace.
+        */
+       if (stream_class->trace) {
+               ret = bt_ctf_event_class_resolve_types(event_class,
+                       stream_class->trace, stream_class);
+               if (ret) {
+                       goto end;
+               }
+       }
+
        /* Only set an event id if none was explicitly set before */
        event_id = bt_ctf_event_class_get_id(event_class);
        if (event_id < 0) {
@@ -255,6 +370,11 @@ int bt_ctf_stream_class_add_event_class(
                goto end;
        }
 
+       ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id);
+       if (ret) {
+               goto end;
+       }
+
        bt_ctf_event_class_get(event_class);
        g_ptr_array_add(stream_class->event_classes, event_class);
        bt_ctf_event_class_freeze(event_class);
@@ -621,6 +741,28 @@ end:
        return ret;
 }
 
+BT_HIDDEN
+int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_trace *trace)
+{
+       int ret = 0;
+
+       if (!stream_class) {
+               ret = -1;
+               goto end;
+       }
+
+       if (stream_class->trace && trace) {
+               /* Already attached to a trace */
+               ret = -1;
+               goto end;
+       }
+
+       stream_class->trace = trace;
+end:
+       return ret;
+}
+
 static
 void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
 {
This page took 0.025515 seconds and 4 git commands to generate.