Fix: the fields to reset are optional
[babeltrace.git] / lib / ctf-ir / stream-class.c
index 0e3a33bcf909295acdfebf20290195dd7f5e3c45..1ac20cc55ee339157093f4e9f70fab50b4c78d1d 100644 (file)
@@ -49,6 +49,7 @@
 #include <babeltrace/endian-internal.h>
 #include <inttypes.h>
 #include <stdint.h>
+#include <stdbool.h>
 
 static
 void bt_ctf_stream_class_destroy(struct bt_object *obj);
@@ -96,12 +97,6 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create_empty(const char *name)
 
        BT_LOGD("Creating empty stream class object: name=\"%s\"", name);
 
-       if (name && bt_ctf_validate_identifier(name)) {
-               BT_LOGW("Invalid parameter: stream class's name is not a valid CTF identifier: "
-                       "name=\"%s\"", name);
-               goto error;
-       }
-
        stream_class = g_new0(struct bt_ctf_stream_class, 1);
        if (!stream_class) {
                BT_LOGE_STR("Failed to allocate one stream class.");
@@ -123,24 +118,6 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create_empty(const char *name)
                goto error;
        }
 
-       stream_class->packet_context_type = bt_ctf_field_type_structure_create();
-       if (!stream_class->packet_context_type) {
-               BT_LOGE_STR("Cannot stream class's initial packet context field type.");
-               goto error;
-       }
-
-       stream_class->event_header_type = bt_ctf_field_type_structure_create();
-       if (!stream_class->event_header_type) {
-               BT_LOGE_STR("Cannot stream class's initial event header field type.");
-               goto error;
-       }
-
-       stream_class->event_context_type = bt_ctf_field_type_structure_create();
-       if (!stream_class->event_context_type) {
-               BT_LOGE_STR("Cannot stream class's initial event context field type.");
-               goto error;
-       }
-
        bt_object_init(stream_class, bt_ctf_stream_class_destroy);
        BT_LOGD("Created empty stream class object: addr=%p, name=\"%s\"",
                stream_class, name);
@@ -249,30 +226,6 @@ int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
                goto end;
        }
 
-       /*
-        * Look for a "timestamp" integer field type in the stream
-        * class's event header field type and map the stream class's
-        * clock's class to that field type if there's no current
-        * mapping.
-        */
-       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_class *mapped_clock_class =
-                       bt_ctf_field_type_integer_get_mapped_clock_class(
-                               timestamp_field);
-
-               if (!mapped_clock_class) {
-                       ret = bt_ctf_field_type_integer_set_mapped_clock_class(
-                               timestamp_field, clock->clock_class);
-                       if (ret) {
-                               goto end;
-                       }
-               }
-
-               BT_PUT(mapped_clock_class);
-       }
-
        /* Replace the current clock of this stream class. */
        bt_put(stream_class->clock);
        stream_class->clock = bt_get(clock);
@@ -330,41 +283,12 @@ struct event_class_set_stream_class_id_data {
        int ret;
 };
 
-static
-void event_class_set_stream_id(gpointer event_class, gpointer data)
-{
-       struct event_class_set_stream_class_id_data *typed_data = data;
-
-       typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class,
-               typed_data->stream_class_id);
-}
-
 BT_HIDDEN
 int bt_ctf_stream_class_set_id_no_check(
                struct bt_ctf_stream_class *stream_class, int64_t id)
 {
-       int ret = 0;
-       struct event_class_set_stream_class_id_data data =
-               { .stream_class_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) {
-               BT_LOGE("Cannot set the IDs of all stream class's event classes: "
-                       "addr=%p, name=\"%s\", id=%" PRId64,
-                       stream_class, bt_ctf_stream_class_get_name(stream_class),
-                       bt_ctf_stream_class_get_id(stream_class));
-               goto end;
-       }
-
        _bt_ctf_stream_class_set_id(stream_class, id);
-end:
-       return ret;
+       return 0;
 }
 
 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
@@ -436,7 +360,7 @@ void event_class_exists(gpointer element, gpointer query)
        }
 
        if (id_a == id_b) {
-               BT_LOGD("Event class with this ID already exists in the stream class: "
+               BT_LOGW("Event class with this ID already exists in the stream class: "
                        "id=%" PRId64 ", name=\"%s\"",
                        id_a, bt_ctf_event_class_get_name(event_class_a));
                search_query->found = 1;
@@ -465,6 +389,14 @@ int bt_ctf_stream_class_add_event_class(
        const enum bt_ctf_validation_flag validation_flags =
                BT_CTF_VALIDATION_FLAG_EVENT;
 
+       if (!stream_class || !event_class) {
+               BT_LOGW("Invalid parameter: stream class or event class is NULL: "
+                       "stream-class-addr=%p, event-class-addr=%p",
+                       stream_class, event_class);
+               ret = -1;
+               goto end;
+       }
+
        BT_LOGD("Adding event class to stream class: "
                "stream-class-addr=%p, stream-class-name=\"%s\", "
                "stream-class-id=%" PRId64 ", event-class-addr=%p, "
@@ -475,14 +407,6 @@ int bt_ctf_stream_class_add_event_class(
                bt_ctf_event_class_get_name(event_class),
                bt_ctf_event_class_get_id(event_class));
 
-       if (!stream_class || !event_class) {
-               BT_LOGW("Invalid parameter: stream class or event class is NULL: "
-                       "stream-class-addr=%p, event-class-addr=%p",
-                       stream_class, event_class);
-               ret = -1;
-               goto end;
-       }
-
        trace = bt_ctf_stream_class_get_trace(stream_class);
        if (trace && trace->is_static) {
                ret = -1;
@@ -573,7 +497,7 @@ int bt_ctf_stream_class_add_event_class(
                if ((validation_output.valid_flags & validation_flags) !=
                                validation_flags) {
                        /* Invalid event class */
-                       BT_LOGE("Invalid trace, stream class, or event class: "
+                       BT_LOGW("Invalid trace, stream class, or event class: "
                                "valid-flags=0x%x",
                                validation_output.valid_flags);
                        ret = -1;
@@ -598,13 +522,6 @@ int bt_ctf_stream_class_add_event_class(
                *event_id = stream_class->next_event_id;
        }
 
-       ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id);
-       if (ret) {
-               BT_LOGE("Cannot set event class's stream class ID attribute: ret=%d",
-                       ret);
-               goto end;
-       }
-
        bt_object_set_parent(event_class, stream_class);
 
        if (trace) {
@@ -1032,6 +949,8 @@ int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
 {
        int ret = 0;
        size_t i;
+       struct bt_ctf_trace *trace;
+       struct bt_ctf_field_type *packet_header_type = NULL;
 
        BT_LOGD("Serializing stream class's metadata: "
                "stream-class-addr=%p, stream-class-name=\"%s\", "
@@ -1046,47 +965,87 @@ int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
                goto end;
        }
 
-       g_string_append_printf(context->string,
-               "stream {\n\tid = %" PRId64 ";\n\tevent.header := ",
-               stream_class->id);
-       ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
-               context);
-       if (ret) {
-               BT_LOGE("Cannot serialize stream class's event header field type's metadata: "
-                       "ret=%d", ret);
-               goto end;
+       g_string_append(context->string, "stream {\n");
+
+       /*
+        * The reference to the trace is only borrowed since the
+        * serialization of the stream class might have been triggered
+        * by the trace's destruction. In such a case, the trace's
+        * reference count would, unexepectedly, go through the sequence
+        * 1 -> 0 -> 1 -> 0 -> ..., provoking an endless loop of destruction
+        * and serialization.
+        */
+       trace = bt_ctf_stream_class_borrow_trace(stream_class);
+       assert(trace);
+       packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
+       trace = NULL;
+       if (packet_header_type) {
+               struct bt_ctf_field_type *stream_id_type;
+
+               stream_id_type =
+                       bt_ctf_field_type_structure_get_field_type_by_name(
+                               packet_header_type, "stream_id");
+               if (stream_id_type) {
+                       /*
+                        * Only set the stream's id if the trace's packet header
+                        * contains a stream_id field. This field is only
+                        * needed if the trace contains only one stream
+                        * class.
+                        */
+                       g_string_append_printf(context->string,
+                               "\tid = %" PRId64 ";\n", stream_class->id);
+               }
+               bt_put(stream_id_type);
        }
+       if (stream_class->event_header_type) {
+               BT_LOGD_STR("Serializing stream class's event header field type's metadata.");
+               g_string_append(context->string, "\tevent.header := ");
+               ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
+                       context);
+               if (ret) {
+                       BT_LOGW("Cannot serialize stream class's event header field type's metadata: "
+                               "ret=%d", ret);
+                       goto end;
+               }
+               g_string_append(context->string, ";");
+       }
+
 
        if (stream_class->packet_context_type) {
-               g_string_append(context->string, ";\n\n\tpacket.context := ");
+               BT_LOGD_STR("Serializing stream class's packet context field type's metadata.");
+               g_string_append(context->string, "\n\n\tpacket.context := ");
                ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
                        context);
                if (ret) {
-                       BT_LOGE("Cannot serialize stream class's packet context field type's metadata: "
+                       BT_LOGW("Cannot serialize stream class's packet context field type's metadata: "
                                "ret=%d", ret);
                        goto end;
                }
+               g_string_append(context->string, ";");
        }
 
        if (stream_class->event_context_type) {
-               g_string_append(context->string, ";\n\n\tevent.context := ");
+               BT_LOGD_STR("Serializing stream class's event context field type's metadata.");
+               g_string_append(context->string, "\n\n\tevent.context := ");
                ret = bt_ctf_field_type_serialize(
                        stream_class->event_context_type, context);
                if (ret) {
-                       BT_LOGE("Cannot serialize stream class's event context field type's metadata: "
+                       BT_LOGW("Cannot serialize stream class's event context field type's metadata: "
                                "ret=%d", ret);
                        goto end;
                }
+               g_string_append(context->string, ";");
        }
 
-       g_string_append(context->string, ";\n};\n\n");
+       g_string_append(context->string, "\n};\n\n");
+
        for (i = 0; i < stream_class->event_classes->len; i++) {
                struct bt_ctf_event_class *event_class =
                        stream_class->event_classes->pdata[i];
 
                ret = bt_ctf_event_class_serialize(event_class, context);
                if (ret) {
-                       BT_LOGE("Cannot serialize event class's metadata: "
+                       BT_LOGW("Cannot serialize event class's metadata: "
                                "event-class-addr=%p, event-class-name=\"%s\", "
                                "event-class-id=%" PRId64,
                                event_class,
@@ -1096,6 +1055,7 @@ int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
                }
        }
 end:
+       bt_put(packet_header_type);
        context->current_indentation_level = 0;
        return ret;
 }
@@ -1115,6 +1075,7 @@ void bt_ctf_stream_class_destroy(struct bt_object *obj)
                g_hash_table_destroy(stream_class->event_classes_ht);
        }
        if (stream_class->event_classes) {
+               BT_LOGD_STR("Destroying event classes.");
                g_ptr_array_free(stream_class->event_classes, TRUE);
        }
 
@@ -1122,8 +1083,11 @@ void bt_ctf_stream_class_destroy(struct bt_object *obj)
                g_string_free(stream_class->name, TRUE);
        }
 
+       BT_LOGD_STR("Putting event header field type.");
        bt_put(stream_class->event_header_type);
+       BT_LOGD_STR("Putting packet context field type.");
        bt_put(stream_class->packet_context_type);
+       BT_LOGD_STR("Putting event context field type.");
        bt_put(stream_class->event_context_type);
        g_free(stream_class);
 }
@@ -1178,6 +1142,7 @@ int init_packet_context(struct bt_ctf_stream_class *stream_class)
                bt_ctf_field_type_structure_create();
        struct bt_ctf_field_type *_uint64_t =
                get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
+       struct bt_ctf_field_type *ts_begin_end_uint64_t;
 
        if (!packet_context_type) {
                BT_LOGE_STR("Cannot create empty structure field type.");
@@ -1185,19 +1150,26 @@ int init_packet_context(struct bt_ctf_stream_class *stream_class)
                goto end;
        }
 
+       ts_begin_end_uint64_t = bt_ctf_field_type_copy(_uint64_t);
+       if (!ts_begin_end_uint64_t) {
+               BT_LOGE_STR("Cannot copy integer field type for `timestamp_begin` and `timestamp_end` fields.");
+               ret = -1;
+               goto end;
+       }
+
        /*
         * We create a stream packet context as proposed in the CTF
         * specification.
         */
        ret = bt_ctf_field_type_structure_add_field(packet_context_type,
-               _uint64_t, "timestamp_begin");
+               ts_begin_end_uint64_t, "timestamp_begin");
        if (ret) {
                BT_LOGE_STR("Cannot add `timestamp_begin` field to event header field type.");
                goto end;
        }
 
        ret = bt_ctf_field_type_structure_add_field(packet_context_type,
-               _uint64_t, "timestamp_end");
+               ts_begin_end_uint64_t, "timestamp_end");
        if (ret) {
                BT_LOGE_STR("Cannot add `timestamp_end` field to event header field type.");
                goto end;
@@ -1232,5 +1204,103 @@ end:
        }
 
        bt_put(_uint64_t);
+       bt_put(ts_begin_end_uint64_t);
+       return ret;
+}
+
+static
+int try_map_clock_class(struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_field_type *ft)
+{
+       struct bt_ctf_clock_class *mapped_clock_class = NULL;
+       int ret = 0;
+
+       if (!ft) {
+               /* Field does not exist: not an error */
+               goto end;
+       }
+
+       assert(bt_ctf_field_type_is_integer(ft));
+       mapped_clock_class =
+               bt_ctf_field_type_integer_get_mapped_clock_class(ft);
+       if (!mapped_clock_class) {
+               if (!stream_class->clock) {
+                       BT_LOGW("Cannot automatically set field's type mapped clock class: stream class's clock is not set: "
+                               "stream-class-addr=%p, stream-class-name=\"%s\", "
+                               "stream-class-id=%" PRId64 ", ft-addr=%p",
+                               stream_class, bt_ctf_stream_class_get_name(stream_class),
+                               bt_ctf_stream_class_get_id(stream_class), ft);
+                       ret = -1;
+                       goto end;
+               }
+
+               ret = bt_ctf_field_type_integer_set_mapped_clock_class_no_check(
+                       ft, stream_class->clock->clock_class);
+               if (ret) {
+                       BT_LOGW("Cannot set field type's mapped clock class: "
+                               "stream-class-addr=%p, stream-class-name=\"%s\", "
+                               "stream-class-id=%" PRId64 ", ft-addr=%p",
+                               stream_class, bt_ctf_stream_class_get_name(stream_class),
+                               bt_ctf_stream_class_get_id(stream_class), ft);
+                       goto end;
+               }
+
+               BT_LOGV("Automatically mapped field type to stream class's clock class: "
+                       "stream-class-addr=%p, stream-class-name=\"%s\", "
+                       "stream-class-id=%" PRId64 ", ft-addr=%p",
+                       stream_class, bt_ctf_stream_class_get_name(stream_class),
+                       bt_ctf_stream_class_get_id(stream_class), ft);
+       }
+
+end:
+       bt_put(mapped_clock_class);
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_stream_class_map_clock_class(
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_field_type *packet_context_type,
+               struct bt_ctf_field_type *event_header_type)
+{
+       struct bt_ctf_field_type *ft = NULL;
+       int ret = 0;
+
+       assert(stream_class);
+
+       if (packet_context_type) {
+               ft = bt_ctf_field_type_structure_get_field_type_by_name(
+                       packet_context_type, "timestamp_begin");
+               if (try_map_clock_class(stream_class, ft)) {
+                       BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_begin` field's mapped clock class.");
+                       ret = -1;
+                       goto end;
+               }
+
+               bt_put(ft);
+               ft = bt_ctf_field_type_structure_get_field_type_by_name(
+                       packet_context_type, "timestamp_end");
+               if (try_map_clock_class(stream_class, ft)) {
+                       BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_end` field's mapped clock class.");
+                       ret = -1;
+                       goto end;
+               }
+
+               BT_PUT(ft);
+       }
+
+       if (event_header_type) {
+               ft = bt_ctf_field_type_structure_get_field_type_by_name(
+                       event_header_type, "timestamp");
+               if (try_map_clock_class(stream_class, ft)) {
+                       BT_LOGE_STR("Cannot automatically set stream class's event header field type's `timestamp` field's mapped clock class.");
+                       ret = -1;
+                       goto end;
+               }
+
+               BT_PUT(ft);
+       }
+
+end:
        return ret;
 }
This page took 0.032441 seconds and 4 git commands to generate.