Fix: ir: make duplicate event classes check smarter
[babeltrace.git] / formats / ctf / ir / stream-class.c
index 7957a4961733c1f8a56f2863b77cffb4646dc2c8..9ef5ca790d7bbf8c8fec5ed95620b2ec5f422cef 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * stream.c
+ * stream-class.c
  *
- * Babeltrace CTF Writer
+ * Babeltrace CTF IR - Stream Class
  *
  * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
 #include <babeltrace/ctf-writer/stream.h>
 #include <babeltrace/ctf-ir/stream-class-internal.h>
 #include <babeltrace/ctf-writer/functor-internal.h>
+#include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/compiler.h>
 #include <babeltrace/align.h>
 
 static
 void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref);
 static
-int init_event_header(struct bt_ctf_stream_class *stream_class,
-               enum bt_ctf_byte_order byte_order);
+int init_event_header(struct bt_ctf_stream_class *stream_class);
 static
-int init_packet_context(struct bt_ctf_stream_class *stream_class,
-               enum bt_ctf_byte_order byte_order);
+int init_packet_context(struct bt_ctf_stream_class *stream_class);
 
 struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
 {
+       int ret;
        struct bt_ctf_stream_class *stream_class = NULL;
 
-       if (!name || !strlen(name)) {
+       if (name && bt_ctf_validate_identifier(name)) {
                goto error;
        }
 
@@ -67,6 +67,16 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
                goto error_destroy;
        }
 
+       ret = init_event_header(stream_class);
+       if (ret) {
+               goto error_destroy;
+       }
+
+       ret = init_packet_context(stream_class);
+       if (ret) {
+               goto error_destroy;
+       }
+
        bt_ctf_ref_init(&stream_class->ref_count);
        return stream_class;
 
@@ -77,6 +87,35 @@ error:
        return stream_class;
 }
 
+const char *bt_ctf_stream_class_get_name(
+               struct bt_ctf_stream_class *stream_class)
+{
+       const char *name = NULL;
+
+       if (!stream_class) {
+               goto end;
+       }
+
+       name = stream_class->name->str;
+end:
+       return name;
+}
+
+int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class,
+               const char *name)
+{
+       int ret = 0;
+
+       if (!stream_class || stream_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       g_string_assign(stream_class->name, name);
+end:
+       return ret;
+}
+
 struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
                struct bt_ctf_stream_class *stream_class)
 {
@@ -96,12 +135,37 @@ int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
                struct bt_ctf_clock *clock)
 {
        int ret = 0;
+       struct bt_ctf_field_type *timestamp_field = NULL;
 
        if (!stream_class || !clock || stream_class->frozen) {
                ret = -1;
                goto end;
        }
 
+       /*
+        * Look for a "timestamp" field in the stream class' event header type
+        * and map the stream's clock to that field if no current mapping is
+        * currently set.
+        */
+       timestamp_field = bt_ctf_field_type_structure_get_field_type_by_name(
+               stream_class->event_header_type, "timestamp");
+       if (timestamp_field) {
+               struct bt_ctf_clock *mapped_clock;
+
+               mapped_clock = bt_ctf_field_type_integer_get_mapped_clock(
+                       timestamp_field);
+               if (mapped_clock) {
+                       bt_ctf_clock_put(mapped_clock);
+                       goto end;
+               }
+
+               ret = bt_ctf_field_type_integer_set_mapped_clock(
+                       timestamp_field, clock);
+               if (ret) {
+                       goto end;
+               }
+       }
+
        if (stream_class->clock) {
                bt_ctf_clock_put(stream_class->clock);
        }
@@ -109,6 +173,9 @@ int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
        stream_class->clock = clock;
        bt_ctf_clock_get(clock);
 end:
+       if (timestamp_field) {
+               bt_ctf_field_type_put(timestamp_field);
+       }
        return ret;
 }
 
@@ -126,22 +193,77 @@ end:
        return ret;
 }
 
+BT_HIDDEN
+int _bt_ctf_stream_class_set_id(
+               struct bt_ctf_stream_class *stream_class, uint32_t id)
+{
+       stream_class->id = id;
+       stream_class->id_set = 1;
+       return 0;
+}
+
 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
                uint32_t id)
 {
        int ret = 0;
 
-       if (!stream_class) {
+       if (!stream_class || stream_class->frozen) {
                ret = -1;
                goto end;
        }
 
-       stream_class->id = id;
-       stream_class->id_set = 1;
+       ret = _bt_ctf_stream_class_set_id(stream_class, id);
+       if (ret) {
+               goto end;
+       }
 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;
+}
+
 int bt_ctf_stream_class_add_event_class(
                struct bt_ctf_stream_class *stream_class,
                struct bt_ctf_event_class *event_class)
@@ -156,7 +278,8 @@ 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;
@@ -179,6 +302,231 @@ int bt_ctf_stream_class_add_event_class(
 
        bt_ctf_event_class_get(event_class);
        g_ptr_array_add(stream_class->event_classes, event_class);
+       bt_ctf_event_class_freeze(event_class);
+
+       if (stream_class->byte_order) {
+               /*
+                * Only set native byte order if it has been initialized
+                * when the stream class was added to a trace.
+                *
+                * If not set here, this will be set when the stream
+                * classe will be added to a trace.
+                */
+               bt_ctf_event_class_set_native_byte_order(event_class,
+                       stream_class->byte_order);
+       }
+end:
+       return ret;
+}
+
+int bt_ctf_stream_class_get_event_class_count(
+               struct bt_ctf_stream_class *stream_class)
+{
+       int ret;
+
+       if (!stream_class) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = (int) stream_class->event_classes->len;
+end:
+       return ret;
+}
+
+struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
+               struct bt_ctf_stream_class *stream_class, int index)
+{
+       struct bt_ctf_event_class *event_class = NULL;
+
+       if (!stream_class || index < 0 ||
+               index >= stream_class->event_classes->len) {
+               goto end;
+       }
+
+       event_class = g_ptr_array_index(stream_class->event_classes, index);
+       bt_ctf_event_class_get(event_class);
+end:
+       return event_class;
+}
+
+struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
+               struct bt_ctf_stream_class *stream_class, const char *name)
+{
+       size_t i;
+       struct bt_ctf_event_class *event_class = NULL;
+
+       if (!stream_class || !name) {
+               goto end;
+       }
+
+       for (i = 0; i < stream_class->event_classes->len; i++) {
+               struct bt_ctf_event_class *cur_event_class =
+                       g_ptr_array_index(stream_class->event_classes, i);
+               const char *cur_event_class_name =
+                       bt_ctf_event_class_get_name(cur_event_class);
+
+               if (!strcmp(name, cur_event_class_name)) {
+                       event_class = cur_event_class;
+                       bt_ctf_event_class_get(event_class);
+                       goto end;
+               }
+       }
+end:
+       return event_class;
+}
+
+struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id(
+               struct bt_ctf_stream_class *stream_class, uint32_t id)
+{
+       size_t i;
+       struct bt_ctf_event_class *event_class = NULL;
+
+       if (!stream_class) {
+               goto end;
+       }
+
+       for (i = 0; i < stream_class->event_classes->len; i++) {
+               struct bt_ctf_event_class *current_event_class =
+                       g_ptr_array_index(stream_class->event_classes, i);
+
+               if (bt_ctf_event_class_get_id(current_event_class) == id) {
+                       event_class = current_event_class;
+                       bt_ctf_event_class_get(event_class);
+                       goto end;
+               }
+       }
+end:
+       return event_class;
+}
+
+struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
+               struct bt_ctf_stream_class *stream_class)
+{
+       struct bt_ctf_field_type *ret = NULL;
+
+       if (!stream_class) {
+               goto end;
+       }
+
+       assert(stream_class->packet_context_type);
+       bt_ctf_field_type_get(stream_class->packet_context_type);
+       ret = stream_class->packet_context_type;
+end:
+       return ret;
+}
+
+int bt_ctf_stream_class_set_packet_context_type(
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_field_type *packet_context_type)
+{
+       int ret = 0;
+
+       if (!stream_class || !packet_context_type || stream_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       assert(stream_class->packet_context_type);
+       if (stream_class->packet_context_type == packet_context_type) {
+               goto end;
+       }
+       if (bt_ctf_field_type_get_type_id(packet_context_type) !=
+               CTF_TYPE_STRUCT) {
+               /* A packet context must be a structure */
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_field_type_put(stream_class->packet_context_type);
+       bt_ctf_field_type_get(packet_context_type);
+       stream_class->packet_context_type = packet_context_type;
+end:
+       return ret;
+}
+
+struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type(
+               struct bt_ctf_stream_class *stream_class)
+{
+       struct bt_ctf_field_type *ret = NULL;
+
+       if (!stream_class || !stream_class->event_header_type) {
+               goto end;
+       }
+
+       assert(stream_class->event_header_type);
+       bt_ctf_field_type_get(stream_class->event_header_type);
+       ret = stream_class->event_header_type;
+end:
+       return ret;
+}
+
+int bt_ctf_stream_class_set_event_header_type(
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_field_type *event_header_type)
+{
+       int ret = 0;
+
+       if (!stream_class || !event_header_type || stream_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       assert(stream_class->event_header_type);
+       if (stream_class->event_header_type == event_header_type) {
+               goto end;
+       }
+       if (bt_ctf_field_type_get_type_id(event_header_type) !=
+               CTF_TYPE_STRUCT) {
+               /* An event header must be a structure */
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_field_type_put(stream_class->event_header_type);
+       bt_ctf_field_type_get(event_header_type);
+       stream_class->event_header_type = event_header_type;
+end:
+       return ret;
+}
+
+struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
+               struct bt_ctf_stream_class *stream_class)
+{
+       struct bt_ctf_field_type *ret = NULL;
+
+       if (!stream_class || !stream_class->event_context_type) {
+               goto end;
+       }
+
+       assert(stream_class->event_context_type);
+       bt_ctf_field_type_get(stream_class->event_context_type);
+       ret = stream_class->event_context_type;
+end:
+       return ret;
+}
+
+int bt_ctf_stream_class_set_event_context_type(
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_field_type *event_context_type)
+{
+       int ret = 0;
+
+       if (!stream_class || !event_context_type || stream_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       if (bt_ctf_field_type_get_type_id(event_context_type) !=
+               CTF_TYPE_STRUCT) {
+               /* A packet context must be a structure */
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_field_type_put(stream_class->event_context_type);
+       bt_ctf_field_type_get(event_context_type);
+       stream_class->event_context_type = event_context_type;
 end:
        return ret;
 }
@@ -209,26 +557,57 @@ void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
        }
 
        stream_class->frozen = 1;
+       bt_ctf_field_type_freeze(stream_class->event_header_type);
+       bt_ctf_field_type_freeze(stream_class->packet_context_type);
+       bt_ctf_field_type_freeze(stream_class->event_context_type);
        bt_ctf_clock_freeze(stream_class->clock);
-       g_ptr_array_foreach(stream_class->event_classes,
-               (GFunc)bt_ctf_event_class_freeze, NULL);
 }
 
 BT_HIDDEN
 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
        enum bt_ctf_byte_order byte_order)
 {
-       int ret = 0;
+       int i, ret = 0;
+       int internal_byte_order;
 
-       ret = init_packet_context(stream_class, byte_order);
-       if (ret) {
+       /* Note that "NATIVE" means the trace's endianness, not the host's. */
+       if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN ||
+               byte_order > BT_CTF_BYTE_ORDER_NETWORK) {
+               ret = -1;
                goto end;
        }
 
-       ret = init_event_header(stream_class, byte_order);
-       if (ret) {
+       switch (byte_order) {
+       case BT_CTF_BYTE_ORDER_NETWORK:
+       case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
+               internal_byte_order = BIG_ENDIAN;
+               break;
+       case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
+               internal_byte_order = LITTLE_ENDIAN;
+               break;
+       default:
+               ret = -1;
                goto end;
        }
+
+       stream_class->byte_order = internal_byte_order;
+
+       /* Set native byte order to little or big endian */
+       bt_ctf_field_type_set_native_byte_order(
+               stream_class->event_header_type, stream_class->byte_order);
+       bt_ctf_field_type_set_native_byte_order(
+               stream_class->packet_context_type, stream_class->byte_order);
+       bt_ctf_field_type_set_native_byte_order(
+               stream_class->event_context_type, stream_class->byte_order);
+
+       /* Set all events' native byte order */
+       for (i = 0; i < stream_class->event_classes->len; i++) {
+               bt_ctf_event_class_set_native_byte_order(
+                       g_ptr_array_index(stream_class->event_classes, i),
+                       stream_class->byte_order);
+               bt_ctf_event_class_freeze(
+                       g_ptr_array_index(stream_class->event_classes, i));
+       }
 end:
        return ret;
 }
@@ -277,10 +656,6 @@ int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
                struct bt_ctf_event_class *event_class =
                        stream_class->event_classes->pdata[i];
 
-               if (ret) {
-                       goto end;
-               }
-
                ret = bt_ctf_event_class_serialize(event_class, context);
                if (ret) {
                        goto end;
@@ -323,17 +698,15 @@ void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
        }
 
        bt_ctf_field_type_put(stream_class->event_header_type);
-       bt_ctf_field_put(stream_class->event_header);
        bt_ctf_field_type_put(stream_class->packet_context_type);
-       bt_ctf_field_put(stream_class->packet_context);
-       bt_ctf_field_type_put(stream_class->event_context_type);
-       bt_ctf_field_put(stream_class->event_context);
+       if (stream_class->event_context_type) {
+               bt_ctf_field_type_put(stream_class->event_context_type);
+       }
        g_free(stream_class);
 }
 
 static
-int init_event_header(struct bt_ctf_stream_class *stream_class,
-               enum bt_ctf_byte_order byte_order)
+int init_event_header(struct bt_ctf_stream_class *stream_class)
 {
        int ret = 0;
        struct bt_ctf_field_type *event_header_type =
@@ -348,16 +721,6 @@ int init_event_header(struct bt_ctf_stream_class *stream_class,
                goto end;
        }
 
-       ret = bt_ctf_field_type_set_byte_order(_uint32_t, byte_order);
-       if (ret) {
-               goto end;
-       }
-
-       ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
-       if (ret) {
-               goto end;
-       }
-
        ret = bt_ctf_field_type_structure_add_field(event_header_type,
                _uint32_t, "id");
        if (ret) {
@@ -370,12 +733,10 @@ int init_event_header(struct bt_ctf_stream_class *stream_class,
                goto end;
        }
 
-       stream_class->event_header_type = event_header_type;
-       stream_class->event_header = bt_ctf_field_create(
-               stream_class->event_header_type);
-       if (!stream_class->event_header) {
-               ret = -1;
+       if (stream_class->event_header_type) {
+               bt_ctf_field_type_put(stream_class->event_header_type);
        }
+       stream_class->event_header_type = event_header_type;
 end:
        if (ret) {
                bt_ctf_field_type_put(event_header_type);
@@ -387,8 +748,7 @@ end:
 }
 
 static
-int init_packet_context(struct bt_ctf_stream_class *stream_class,
-               enum bt_ctf_byte_order byte_order)
+int init_packet_context(struct bt_ctf_stream_class *stream_class)
 {
        int ret = 0;
        struct bt_ctf_field_type *packet_context_type =
@@ -405,11 +765,6 @@ int init_packet_context(struct bt_ctf_stream_class *stream_class,
         * We create a stream packet context as proposed in the CTF
         * specification.
         */
-       ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
-       if (ret) {
-               goto end;
-       }
-
        ret = bt_ctf_field_type_structure_add_field(packet_context_type,
                _uint64_t, "timestamp_begin");
        if (ret) {
@@ -440,11 +795,10 @@ int init_packet_context(struct bt_ctf_stream_class *stream_class,
                goto end;
        }
 
-       stream_class->packet_context_type = packet_context_type;
-       stream_class->packet_context = bt_ctf_field_create(packet_context_type);
-       if (!stream_class->packet_context) {
-               ret = -1;
+       if (stream_class->packet_context_type) {
+               bt_ctf_field_type_put(stream_class->packet_context_type);
        }
+       stream_class->packet_context_type = packet_context_type;
 end:
        if (ret) {
                bt_ctf_field_type_put(packet_context_type);
This page took 0.03093 seconds and 4 git commands to generate.