ctf.fs: bt_ctf_notif_iter_create(): assert() that all medops exist
[babeltrace.git] / formats / ctf / ir / trace.c
index de8055b8da39b4b7e4d032a8a9d55cc8edbd5fae..32ceca636f813e44aae98da61df8a41de5e2c5c7 100644 (file)
  */
 
 #include <babeltrace/ctf-ir/trace-internal.h>
-#include <babeltrace/ctf-ir/clock-internal.h>
+#include <babeltrace/ctf-ir/clock-class-internal.h>
 #include <babeltrace/ctf-ir/stream-internal.h>
 #include <babeltrace/ctf-ir/stream-class-internal.h>
 #include <babeltrace/ctf-ir/event-internal.h>
+#include <babeltrace/ctf-ir/event-class.h>
+#include <babeltrace/ctf-ir/event-class-internal.h>
 #include <babeltrace/ctf-writer/functor-internal.h>
+#include <babeltrace/ctf-writer/clock-internal.h>
 #include <babeltrace/ctf-ir/field-types-internal.h>
 #include <babeltrace/ctf-ir/attributes-internal.h>
 #include <babeltrace/ctf-ir/validation-internal.h>
+#include <babeltrace/ctf-ir/visitor-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
+#include <babeltrace/graph/notification-schema.h>
 #include <babeltrace/compiler.h>
 #include <babeltrace/values.h>
 #include <babeltrace/ref.h>
 #define DEFAULT_IDENTIFIER_SIZE 128
 #define DEFAULT_METADATA_STRING_SIZE 4096
 
+struct listener_wrapper {
+       bt_ctf_listener_cb listener;
+       void *data;
+};
+
 static
 void bt_ctf_trace_destroy(struct bt_object *obj);
 static
@@ -102,6 +112,12 @@ struct bt_ctf_trace *bt_ctf_trace_create(void)
                goto error;
        }
 
+       trace->listeners = g_ptr_array_new_with_free_func(
+                       (GDestroyNotify) g_free);
+       if (!trace->listeners) {
+               goto error;
+       }
+
        return trace;
 
 error:
@@ -109,6 +125,38 @@ error:
        return trace;
 }
 
+const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace)
+{
+       const char *name = NULL;
+
+       if (!trace || !trace->name) {
+               goto end;
+       }
+
+       name = trace->name->str;
+end:
+       return name;
+}
+
+int bt_ctf_trace_set_name(struct bt_ctf_trace *trace, const char *name)
+{
+       int ret = 0;
+
+       if (!trace || !name || trace->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       trace->name = trace->name ? g_string_assign(trace->name, name) :
+                       g_string_new(name);
+       if (!trace->name) {
+               ret = -1;
+               goto end;
+       }
+end:
+       return ret;
+}
+
 void bt_ctf_trace_destroy(struct bt_object *obj)
 {
        struct bt_ctf_trace *trace;
@@ -118,6 +166,10 @@ void bt_ctf_trace_destroy(struct bt_object *obj)
                bt_ctf_attributes_destroy(trace->environment);
        }
 
+       if (trace->name) {
+               g_string_free(trace->name, TRUE);
+       }
+
        if (trace->clocks) {
                g_ptr_array_free(trace->clocks, TRUE);
        }
@@ -130,45 +182,12 @@ void bt_ctf_trace_destroy(struct bt_object *obj)
                g_ptr_array_free(trace->stream_classes, TRUE);
        }
 
-       bt_put(trace->packet_header_type);
-       g_free(trace);
-}
-
-struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace,
-               struct bt_ctf_stream_class *stream_class)
-{
-       int ret;
-       int stream_class_found = 0;
-       size_t i;
-       struct bt_ctf_stream *stream = NULL;
-
-       if (!trace || !stream_class) {
-               goto error;
-       }
-
-       for (i = 0; i < trace->stream_classes->len; i++) {
-               if (trace->stream_classes->pdata[i] == stream_class) {
-                       stream_class_found = 1;
-               }
-       }
-
-       if (!stream_class_found) {
-               ret = bt_ctf_trace_add_stream_class(trace, stream_class);
-               if (ret) {
-                       goto error;
-               }
-       }
-
-       stream = bt_ctf_stream_create(stream_class, trace);
-       if (!stream) {
-               goto error;
+       if (trace->listeners) {
+               g_ptr_array_free(trace->listeners, TRUE);
        }
 
-       g_ptr_array_add(trace->streams, stream);
-       return stream;
-error:
-        BT_PUT(stream);
-       return stream;
+       bt_put(trace->packet_header_type);
+       g_free(trace);
 }
 
 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
@@ -365,31 +384,35 @@ end:
        return ret;
 }
 
-int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace,
-               struct bt_ctf_clock *clock)
+int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace,
+               struct bt_ctf_clock_class *clock_class)
 {
        int ret = 0;
-       struct search_query query = { .value = clock, .found = 0 };
+       struct search_query query = { .value = clock_class, .found = 0 };
 
-       if (!trace || !clock) {
+       if (!trace || !bt_ctf_clock_class_is_valid(clock_class)) {
                ret = -1;
                goto end;
        }
 
-       /* Check for duplicate clocks */
+       /* Check for duplicate clock classes */
        g_ptr_array_foreach(trace->clocks, value_exists, &query);
        if (query.found) {
                ret = -1;
                goto end;
        }
 
-       bt_get(clock);
-       g_ptr_array_add(trace->clocks, clock);
+       bt_get(clock_class);
+       g_ptr_array_add(trace->clocks, clock_class);
+
+       if (trace->frozen) {
+               bt_ctf_clock_class_freeze(clock_class);
+       }
 end:
        return ret;
 }
 
-int bt_ctf_trace_get_clock_count(struct bt_ctf_trace *trace)
+int bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
 {
        int ret = -1;
 
@@ -402,19 +425,19 @@ end:
        return ret;
 }
 
-struct bt_ctf_clock *bt_ctf_trace_get_clock(struct bt_ctf_trace *trace,
-               int index)
+struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class(
+               struct bt_ctf_trace *trace, int index)
 {
-       struct bt_ctf_clock *clock = NULL;
+       struct bt_ctf_clock_class *clock_class = NULL;
 
        if (!trace || index < 0 || index >= trace->clocks->len) {
                goto end;
        }
 
-       clock = g_ptr_array_index(trace->clocks, index);
-       bt_get(clock);
+       clock_class = g_ptr_array_index(trace->clocks, index);
+       bt_get(clock_class);
 end:
-       return clock;
+       return clock_class;
 }
 
 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
@@ -434,12 +457,20 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
        struct bt_ctf_field_type *event_header_type = NULL;
        struct bt_ctf_field_type *stream_event_ctx_type = NULL;
        int event_class_count;
+       struct bt_ctf_trace *current_parent_trace = NULL;
 
        if (!trace || !stream_class) {
                ret = -1;
                goto end;
        }
 
+       current_parent_trace = bt_ctf_stream_class_get_trace(stream_class);
+       if (current_parent_trace) {
+               /* Stream class is already associated to a trace, abort. */
+               ret = -1;
+               goto end;
+       }
+
        event_class_count =
                bt_ctf_stream_class_get_event_class_count(stream_class);
        assert(event_class_count >= 0);
@@ -453,6 +484,44 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
                }
        }
 
+       if (stream_class->clock) {
+               struct bt_ctf_clock_class *stream_clock_class =
+                       stream_class->clock->clock_class;
+
+               if (trace->is_created_by_writer) {
+                       /*
+                        * Make sure this clock was also added to the
+                        * trace (potentially through its CTF writer
+                        * owner).
+                        */
+                       size_t i;
+
+                       for (i = 0; i < trace->clocks->len; i++) {
+                               if (trace->clocks->pdata[i] ==
+                                               stream_clock_class) {
+                                       /* Found! */
+                                       break;
+                               }
+                       }
+
+                       if (i == trace->clocks->len) {
+                               /* Not found */
+                               ret = -1;
+                               goto end;
+                       }
+               } else {
+                       /*
+                        * This trace was NOT created by a CTF writer,
+                        * thus do not allow the stream class to add to
+                        * have a clock at all. Those are two
+                        * independent APIs (non-writer and writer
+                        * APIs), and isolating them simplifies things.
+                        */
+                       ret = -1;
+                       goto end;
+               }
+       }
+
        /*
         * We're about to freeze both the trace and the stream class.
         * Also, each event class contained in this stream class are
@@ -508,7 +577,7 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
        }
 
        /* Validate each event class individually */
-       for (i = 0; i < event_class_count; ++i) {
+       for (i = 0; i < event_class_count; i++) {
                struct bt_ctf_event_class *event_class =
                        bt_ctf_stream_class_get_event_class(stream_class, i);
                struct bt_ctf_field_type *event_context_type = NULL;
@@ -591,7 +660,7 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
         */
        bt_ctf_validation_output_put_types(&trace_sc_validation_output);
 
-       for (i = 0; i < event_class_count; ++i) {
+       for (i = 0; i < event_class_count; i++) {
                struct bt_ctf_event_class *event_class =
                        bt_ctf_stream_class_get_event_class(stream_class, i);
 
@@ -621,12 +690,15 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
        bt_ctf_stream_class_freeze(stream_class);
        bt_ctf_trace_freeze(trace);
 
+       /* Notifiy listeners of the trace's schema modification. */
+       bt_ctf_stream_class_visit(stream_class,
+                       bt_ctf_trace_object_modification, trace);
 end:
        if (ret) {
                bt_object_set_parent(stream_class, NULL);
 
                if (ec_validation_outputs) {
-                       for (i = 0; i < event_class_count; ++i) {
+                       for (i = 0; i < event_class_count; i++) {
                                bt_ctf_validation_output_put_types(
                                        &ec_validation_outputs[i]);
                        }
@@ -635,6 +707,7 @@ end:
 
        g_free(ec_validation_outputs);
        bt_ctf_validation_output_put_types(&trace_sc_validation_output);
+       bt_put(current_parent_trace);
        assert(!packet_header_type);
        assert(!packet_context_type);
        assert(!event_header_type);
@@ -682,7 +755,7 @@ struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
                goto end;
        }
 
-       for (i = 0; i < trace->stream_classes->len; ++i) {
+       for (i = 0; i < trace->stream_classes->len; i++) {
                struct bt_ctf_stream_class *stream_class_candidate;
 
                stream_class_candidate =
@@ -700,34 +773,34 @@ end:
        return stream_class;
 }
 
-struct bt_ctf_clock *bt_ctf_trace_get_clock_by_name(
+struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
                struct bt_ctf_trace *trace, const char *name)
 {
        size_t i;
-       struct bt_ctf_clock *clock = NULL;
+       struct bt_ctf_clock_class *clock_class = NULL;
 
        if (!trace || !name) {
                goto end;
        }
 
-       for (i = 0; i < trace->clocks->len; ++i) {
-               struct bt_ctf_clock *cur_clk =
+       for (i = 0; i < trace->clocks->len; i++) {
+               struct bt_ctf_clock_class *cur_clk =
                        g_ptr_array_index(trace->clocks, i);
-               const char *cur_clk_name = bt_ctf_clock_get_name(cur_clk);
+               const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk);
 
                if (!cur_clk_name) {
                        goto end;
                }
 
                if (!strcmp(cur_clk_name, name)) {
-                       clock = cur_clk;
-                       bt_get(clock);
+                       clock_class = cur_clk;
+                       bt_get(clock_class);
                        goto end;
                }
        }
 
 end:
-       return clock;
+       return clock_class;
 }
 
 BT_HIDDEN
@@ -801,7 +874,7 @@ void append_env_metadata(struct bt_ctf_trace *trace,
 
        g_string_append(context->string, "env {\n");
 
-       for (i = 0; i < env_size; ++i) {
+       for (i = 0; i < env_size; i++) {
                struct bt_value *env_field_value_obj = NULL;
                const char *entry_name;
 
@@ -892,7 +965,7 @@ char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
        }
        append_env_metadata(trace, context);
        g_ptr_array_foreach(trace->clocks,
-               (GFunc)bt_ctf_clock_serialize, context);
+               (GFunc)bt_ctf_clock_class_serialize, context);
 
        for (i = 0; i < trace->stream_classes->len; i++) {
                err = bt_ctf_stream_class_serialize(
@@ -995,39 +1068,132 @@ int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
 {
        int ret = 0;
 
-       if (!trace || !packet_header_type || trace->frozen) {
+       if (!trace || trace->frozen) {
                ret = -1;
                goto end;
        }
 
-       /* packet_header_type must be a structure */
-       if (bt_ctf_field_type_get_type_id(packet_header_type) !=
-               BT_CTF_TYPE_ID_STRUCT) {
+       /* packet_header_type must be a structure. */
+       if (packet_header_type &&
+                       bt_ctf_field_type_get_type_id(packet_header_type) !=
+                               BT_CTF_TYPE_ID_STRUCT) {
                ret = -1;
                goto end;
        }
 
-       bt_get(packet_header_type);
        bt_put(trace->packet_header_type);
-       trace->packet_header_type = packet_header_type;
+       trace->packet_header_type = bt_get(packet_header_type);
 end:
        return ret;
 }
 
-void bt_ctf_trace_get(struct bt_ctf_trace *trace)
+static
+int get_stream_class_count(void *element)
+{
+       return bt_ctf_trace_get_stream_class_count(
+                       (struct bt_ctf_trace *) element);
+}
+
+static
+void *get_stream_class(void *element, int i)
+{
+       return bt_ctf_trace_get_stream_class(
+                       (struct bt_ctf_trace *) element, i);
+}
+
+static
+int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data)
+{
+       return bt_ctf_stream_class_visit(object, visitor, data);
+}
+
+int bt_ctf_trace_visit(struct bt_ctf_trace *trace,
+               bt_ctf_visitor visitor, void *data)
+{
+       int ret;
+       struct bt_ctf_object obj =
+                       { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE };
+
+       if (!trace || !visitor) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = visitor_helper(&obj, get_stream_class_count,
+                       get_stream_class, visit_stream_class, visitor, data);
+end:
+       return ret;
+}
+
+static
+int invoke_listener(struct bt_ctf_object *object, void *data)
+{
+       struct listener_wrapper *listener_wrapper = data;
+
+       listener_wrapper->listener(object, listener_wrapper->data);
+       return 0;
+}
+
+int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace,
+               bt_ctf_listener_cb listener, void *listener_data)
 {
-       bt_get(trace);
+       int ret = 0;
+       struct listener_wrapper *listener_wrapper =
+                       g_new0(struct listener_wrapper, 1);
+
+       if (!trace || !listener || !listener_wrapper) {
+               ret = -1;
+               goto error;
+       }
+
+       listener_wrapper->listener = listener;
+       listener_wrapper->data = listener_data;
+
+       /* Visit the current schema. */
+       ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper);
+       if (ret) {
+               goto error;
+       }
+
+       /*
+        * Add listener to the array of callbacks which will be invoked on
+        * schema changes.
+        */
+       g_ptr_array_add(trace->listeners, listener_wrapper);
+       return ret;
+error:
+       g_free(listener_wrapper);
+       return ret;
 }
 
-void bt_ctf_trace_put(struct bt_ctf_trace *trace)
+BT_HIDDEN
+int bt_ctf_trace_object_modification(struct bt_ctf_object *object,
+               void *trace_ptr)
 {
-       bt_put(trace);
+       size_t i;
+       struct bt_ctf_trace *trace = trace_ptr;
 
+       assert(trace);
+       assert(object);
+
+       if (trace->listeners->len == 0) {
+               goto end;
+       }
+
+       for (i = 0; i < trace->listeners->len; i++) {
+               struct listener_wrapper *listener =
+                               g_ptr_array_index(trace->listeners, i);
+
+               listener->listener(object, listener->data);
+       }
+end:
+       return 0;
 }
 
 BT_HIDDEN
 struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
 {
+       int ret;
        unsigned int alignment, size;
        struct bt_ctf_field_type *field_type = NULL;
 
@@ -1038,7 +1204,10 @@ struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
        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);
+       ret = bt_ctf_field_type_set_alignment(field_type, alignment);
+       if (ret) {
+               BT_PUT(field_type);
+       }
 end:
        return field_type;
 }
@@ -1046,8 +1215,18 @@ end:
 static
 void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
 {
+       int i;
+
        bt_ctf_field_type_freeze(trace->packet_header_type);
        bt_ctf_attributes_freeze(trace->environment);
+
+       for (i = 0; i < trace->clocks->len; i++) {
+               struct bt_ctf_clock_class *clock_class =
+                       g_ptr_array_index(trace->clocks, i);
+
+               bt_ctf_clock_class_freeze(clock_class);
+       }
+
        trace->frozen = 1;
 }
 
This page took 0.03149 seconds and 4 git commands to generate.