Fix: Create stream after adding its stream class to a trace
[babeltrace.git] / formats / ctf / ir / trace.c
index 5abfcc79c7ef197db758c781e6c4180a28b28776..d911dfd69946029e8078814a4a94ac30f2ca1af1 100644 (file)
@@ -33,6 +33,7 @@
 #include <babeltrace/ctf-writer/functor-internal.h>
 #include <babeltrace/ctf-ir/event-types-internal.h>
 #include <babeltrace/ctf-ir/attributes-internal.h>
+#include <babeltrace/ctf-ir/visitor-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/compiler.h>
 #include <babeltrace/objects.h>
@@ -45,7 +46,7 @@ void bt_ctf_trace_destroy(struct bt_ctf_ref *ref);
 static
 int init_trace_packet_header(struct bt_ctf_trace *trace);
 static
-void bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
+int bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
 
 static
 const unsigned int field_type_aliases_alignments[] = {
@@ -155,11 +156,6 @@ struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace,
                goto error;
        }
 
-       stream = bt_ctf_stream_create(stream_class, trace);
-       if (!stream) {
-               goto error;
-       }
-
        for (i = 0; i < trace->stream_classes->len; i++) {
                if (trace->stream_classes->pdata[i] == stream_class) {
                        stream_class_found = 1;
@@ -173,6 +169,11 @@ struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace,
                }
        }
 
+       stream = bt_ctf_stream_create(stream_class, trace);
+       if (!stream) {
+               goto error;
+       }
+
        bt_ctf_stream_get(stream);
        g_ptr_array_add(trace->streams, stream);
 
@@ -443,11 +444,17 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
 
        for (i = 0; i < trace->stream_classes->len; i++) {
                if (trace->stream_classes->pdata[i] == stream_class) {
+                       /* Stream already registered to the trace */
                        ret = -1;
                        goto end;
                }
        }
 
+       ret = bt_ctf_stream_class_resolve_types(stream_class, trace);
+       if (ret) {
+               goto end;
+       }
+
        stream_id = bt_ctf_stream_class_get_id(stream_class);
        if (stream_id < 0) {
                stream_id = trace->next_stream_id++;
@@ -462,7 +469,8 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
                        }
                }
 
-               if (bt_ctf_stream_class_set_id_no_check(stream_class, stream_id)) {
+               if (bt_ctf_stream_class_set_id_no_check(stream_class,
+                       stream_id)) {
                        /* TODO Should retry with a different stream id */
                        ret = -1;
                        goto end;
@@ -495,7 +503,10 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
        }
 
        bt_ctf_stream_class_freeze(stream_class);
-       bt_ctf_trace_freeze(trace);
+       if (!trace->frozen) {
+               ret = bt_ctf_trace_freeze(trace);
+               goto end;
+       }
 end:
        if (ret) {
                (void) bt_ctf_stream_class_set_trace(stream_class, NULL);
@@ -868,24 +879,34 @@ BT_HIDDEN
 struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
 {
        unsigned int alignment, size;
-       struct bt_ctf_field_type *field_type;
+       struct bt_ctf_field_type *field_type = NULL;
 
        if (alias >= NR_FIELD_TYPE_ALIAS) {
-               return NULL;
+               goto end;
        }
 
        alignment = field_type_aliases_alignments[alias];
        size = field_type_aliases_sizes[alias];
        field_type = bt_ctf_field_type_integer_create(size);
        bt_ctf_field_type_set_alignment(field_type, alignment);
+end:
        return field_type;
 }
 
 static
-void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
+int bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
 {
+       int ret = 0;
+
+       ret = bt_ctf_trace_resolve_types(trace);
+       if (ret) {
+               goto end;
+       }
+
        bt_ctf_attributes_freeze(trace->environment);
        trace->frozen = 1;
+end:
+       return ret;
 }
 
 static
This page took 0.024894 seconds and 4 git commands to generate.