Fix: Stream classes' id field is always serialized
[babeltrace.git] / lib / ctf-ir / stream-class.c
index a9f1c12a01cb88d184a771766b62e99b24093d4d..db666c4371252a1e530ea5b290ab8afbf2a39927 100644 (file)
@@ -97,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.");
@@ -124,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 create 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 create 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 create 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);
@@ -1009,6 +985,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\", "
@@ -1023,8 +1001,38 @@ 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", stream_class->id);
+       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 := ");
@@ -1083,6 +1091,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;
 }
This page took 0.025546 seconds and 4 git commands to generate.