ir: rename event-fields/event-types -> fields/field-types
[babeltrace.git] / formats / ctf / ir / event.c
index 226251f7bbd84adc7793806f50c382964827e099..8ddf8034617ec76dde8503e4f2ae93709930ef98 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-writer/event.h>
-#include <babeltrace/ctf-writer/event-types.h>
-#include <babeltrace/ctf-writer/event-fields.h>
-#include <babeltrace/ctf-ir/event-fields-internal.h>
-#include <babeltrace/ctf-ir/event-types-internal.h>
+#include <babeltrace/ctf-ir/fields-internal.h>
+#include <babeltrace/ctf-ir/field-types-internal.h>
 #include <babeltrace/ctf-ir/event-internal.h>
 #include <babeltrace/ctf-ir/stream-class.h>
 #include <babeltrace/ctf-ir/stream-class-internal.h>
 #include <babeltrace/ctf-ir/trace-internal.h>
+#include <babeltrace/ctf-ir/validation-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/ref.h>
 #include <babeltrace/ctf-ir/attributes-internal.h>
@@ -167,13 +165,16 @@ int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class,
 {
        int ret = 0;
        struct bt_value *obj = NULL;
+       struct bt_ctf_stream_class *stream_class = NULL;
+
 
        if (!event_class) {
                ret = -1;
                goto end;
        }
 
-       if (event_class->stream_class) {
+       stream_class = bt_ctf_event_class_get_stream_class(event_class);
+       if (stream_class) {
                /*
                 * We don't allow changing the id if the event class has already
                 * been added to a stream class.
@@ -195,6 +196,7 @@ int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class,
 
 end:
        BT_PUT(obj);
+       BT_PUT(stream_class);
        return ret;
 }
 
@@ -320,16 +322,7 @@ end:
 struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class(
                struct bt_ctf_event_class *event_class)
 {
-       struct bt_ctf_stream_class *stream_class = NULL;
-
-       if (!event_class) {
-               goto end;
-       }
-
-       stream_class = event_class->stream_class;
-       bt_get(stream_class);
-end:
-       return stream_class;
+       return (struct bt_ctf_stream_class *) bt_object_get_parent(event_class);
 }
 
 struct bt_ctf_field_type *bt_ctf_event_class_get_payload_type(
@@ -353,7 +346,8 @@ int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class *event_class,
        int ret = 0;
 
        if (!event_class || !payload ||
-               bt_ctf_field_type_get_type_id(payload) != CTF_TYPE_STRUCT) {
+               bt_ctf_field_type_get_type_id(payload) !=
+               BT_CTF_TYPE_ID_STRUCT) {
                ret = -1;
                goto end;
        }
@@ -378,7 +372,7 @@ int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class,
        }
 
        if (bt_ctf_field_type_get_type_id(event_class->fields) !=
-               CTF_TYPE_STRUCT) {
+               BT_CTF_TYPE_ID_STRUCT) {
                ret = -1;
                goto end;
        }
@@ -400,7 +394,7 @@ int bt_ctf_event_class_get_field_count(
        }
 
        if (bt_ctf_field_type_get_type_id(event_class->fields) !=
-               CTF_TYPE_STRUCT) {
+               BT_CTF_TYPE_ID_STRUCT) {
                ret = -1;
                goto end;
        }
@@ -422,7 +416,7 @@ int bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class,
        }
 
        if (bt_ctf_field_type_get_type_id(event_class->fields) !=
-               CTF_TYPE_STRUCT) {
+               BT_CTF_TYPE_ID_STRUCT) {
                ret = -1;
                goto end;
        }
@@ -444,7 +438,7 @@ struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name(
        }
 
        if (bt_ctf_field_type_get_type_id(event_class->fields) !=
-               CTF_TYPE_STRUCT) {
+               BT_CTF_TYPE_ID_STRUCT) {
                goto end;
        }
 
@@ -489,7 +483,7 @@ int bt_ctf_event_class_set_context_type(
                goto end;
        }
 
-       if (bt_ctf_field_type_get_type_id(context) != CTF_TYPE_STRUCT) {
+       if (bt_ctf_field_type_get_type_id(context) != BT_CTF_TYPE_ID_STRUCT) {
                ret = -1;
                goto end;
        }
@@ -529,6 +523,10 @@ int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
        ret = bt_ctf_attributes_set_field_value(event_class->attributes,
                "stream_id", obj);
 
+       if (event_class->frozen) {
+               bt_ctf_attributes_freeze(event_class->attributes);
+       }
+
 end:
        BT_PUT(obj);
        return ret;
@@ -536,58 +534,184 @@ end:
 
 struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
 {
+       int ret;
+       enum bt_ctf_validation_flag validation_flags =
+               BT_CTF_VALIDATION_FLAG_STREAM |
+               BT_CTF_VALIDATION_FLAG_EVENT;
        struct bt_ctf_event *event = NULL;
+       struct bt_ctf_trace *trace = NULL;
+       struct bt_ctf_stream_class *stream_class = NULL;
+       struct bt_ctf_field_type *packet_header_type = NULL;
+       struct bt_ctf_field_type *packet_context_type = NULL;
+       struct bt_ctf_field_type *event_header_type = NULL;
+       struct bt_ctf_field_type *stream_event_ctx_type = NULL;
+       struct bt_ctf_field_type *event_context_type = NULL;
+       struct bt_ctf_field_type *event_payload_type = NULL;
+       struct bt_ctf_field *event_header = NULL;
+       struct bt_ctf_field *event_context = NULL;
+       struct bt_ctf_field *event_payload = NULL;
+       struct bt_value *environment = NULL;
+       struct bt_ctf_validation_output validation_output = { 0 };
+       int trace_valid = 0;
 
        if (!event_class) {
-               goto end;
+               goto error;
        }
 
+       stream_class = bt_ctf_event_class_get_stream_class(event_class);
+
        /*
-        * The event class does not keep ownership of the stream class to
-        * which it as been added. Therefore, it can't assume it has been
-        * set. However, we disallow the creation of an event if its
-        * associated stream class has been reclaimed.
+        * We disallow the creation of an event if its event class has not been
+        * associated to a stream class.
         */
-       if (!event_class->stream_class) {
-               goto end;
+       if (!stream_class) {
+               goto error;
+       }
+
+       /* A stream class should always have an existing event header type */
+       assert(stream_class->event_header_type);
+
+       /* The event class was frozen when added to its stream class */
+       assert(event_class->frozen);
+
+       /* Validate the trace (if any), the stream class, and the event class */
+       trace = bt_ctf_stream_class_get_trace(stream_class);
+       if (trace) {
+               packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
+               trace_valid = trace->valid;
+               assert(trace_valid);
+               environment = trace->environment;
+       }
+
+       packet_context_type = bt_ctf_stream_class_get_packet_context_type(
+               stream_class);
+       event_header_type = bt_ctf_stream_class_get_event_header_type(
+               stream_class);
+       stream_event_ctx_type = bt_ctf_stream_class_get_event_context_type(
+               stream_class);
+       event_context_type = bt_ctf_event_class_get_context_type(event_class);
+       event_payload_type = bt_ctf_event_class_get_payload_type(event_class);
+       ret = bt_ctf_validate_class_types(environment, packet_header_type,
+               packet_context_type, event_header_type, stream_event_ctx_type,
+               event_context_type, event_payload_type, trace_valid,
+               stream_class->valid, event_class->valid,
+               &validation_output, validation_flags);
+       BT_PUT(packet_header_type);
+       BT_PUT(packet_context_type);
+       BT_PUT(event_header_type);
+       BT_PUT(stream_event_ctx_type);
+       BT_PUT(event_context_type);
+       BT_PUT(event_payload_type);
+
+       if (ret) {
+               /*
+                * This means something went wrong during the validation
+                * process, not that the objects are invalid.
+                */
+               goto error;
+       }
+
+       if ((validation_output.valid_flags & validation_flags) !=
+                       validation_flags) {
+               /* Invalid trace/stream class/event class */
+               goto error;
        }
-       assert(event_class->stream_class->event_header_type);
+
+       /*
+        * At this point we know the trace (if associated to the stream
+        * class), the stream class, and the event class, with their
+        * current types, are valid. We may proceed with creating
+        * the event.
+        */
        event = g_new0(struct bt_ctf_event, 1);
        if (!event) {
-               goto end;
+               goto error;
        }
 
        bt_object_init(event, bt_ctf_event_destroy);
-       bt_get(event_class);
-       bt_ctf_event_class_freeze(event_class);
-       event->event_class = event_class;
 
-       event->event_header = bt_ctf_field_create(
-               event_class->stream_class->event_header_type);
-       if (!event->event_header) {
+       /*
+        * event does not share a common ancestor with the event class; it has
+        * to guarantee its existence by holding a reference. This reference
+        * shall be released once the event is associated to a stream since,
+        * from that point, the event and its class will share the same
+        * lifetime.
+        */
+       event->event_class = bt_get(event_class);
+       event_header =
+               bt_ctf_field_create(validation_output.event_header_type);
+
+       if (!event_header) {
                goto error;
        }
-       if (event_class->context) {
-               event->context_payload = bt_ctf_field_create(
-                       event_class->context);
-               if (!event->context_payload) {
+
+       if (validation_output.event_context_type) {
+               event_context = bt_ctf_field_create(
+                       validation_output.event_context_type);
+               if (!event_context) {
                        goto error;
                }
        }
-       event->fields_payload = bt_ctf_field_create(event_class->fields);
-       if (!event->fields_payload) {
-               goto error;
+
+       if (validation_output.event_payload_type) {
+               event_payload = bt_ctf_field_create(
+                       validation_output.event_payload_type);
+               if (!event_payload) {
+                       goto error;
+               }
        }
 
+       /*
+        * At this point all the fields are created, potentially from
+        * validated copies of field types, so that the field types and
+        * fields can be replaced in the trace, stream class,
+        * event class, and created event.
+        */
+       bt_ctf_validation_replace_types(trace, stream_class,
+               event_class, &validation_output, validation_flags);
+       BT_MOVE(event->event_header, event_header);
+       BT_MOVE(event->context_payload, event_context);
+       BT_MOVE(event->fields_payload, event_payload);
+
+       /*
+        * Put what was not moved in bt_ctf_validation_replace_types().
+        */
+       bt_ctf_validation_output_put_types(&validation_output);
+
        /*
         * Freeze the stream class since the event header must not be changed
         * anymore.
         */
-       bt_ctf_stream_class_freeze(event_class->stream_class);
-end:
+       bt_ctf_stream_class_freeze(stream_class);
+
+       /*
+        * Mark stream class, and event class as valid since
+        * they're all frozen now.
+        */
+       stream_class->valid = 1;
+       event_class->valid = 1;
+
+       /* Put stuff we borrowed from the event class */
+       BT_PUT(stream_class);
+       BT_PUT(trace);
+
        return event;
+
 error:
+       bt_ctf_validation_output_put_types(&validation_output);
        BT_PUT(event);
+       BT_PUT(stream_class);
+       BT_PUT(trace);
+       BT_PUT(event_header);
+       BT_PUT(event_context);
+       BT_PUT(event_payload);
+       assert(!packet_header_type);
+       assert(!packet_context_type);
+       assert(!event_header_type);
+       assert(!stream_event_ctx_type);
+       assert(!event_context_type);
+       assert(!event_payload_type);
+
        return event;
 }
 
@@ -607,16 +731,7 @@ end:
 
 struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event)
 {
-       struct bt_ctf_stream *stream = NULL;
-
-       if (!event) {
-               goto end;
-       }
-
-       stream = event->stream;
-       bt_get(stream);
-end:
-       return stream;
+       return (struct bt_ctf_stream *) bt_object_get_parent(event);
 }
 
 struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event)
@@ -670,7 +785,9 @@ int bt_ctf_event_set_payload(struct bt_ctf_event *event,
                struct bt_ctf_field_type *payload_type;
 
                payload_type = bt_ctf_field_get_type(payload);
-               if (payload_type == event->event_class->fields) {
+
+               if (bt_ctf_field_type_compare(payload_type,
+                               event->event_class->fields) == 0) {
                        bt_put(event->fields_payload);
                        bt_get(payload);
                        event->fields_payload = payload;
@@ -715,7 +832,8 @@ int bt_ctf_event_set_payload_field(struct bt_ctf_event *event,
                goto end;
        }
 
-       if (bt_ctf_field_type_get_type_id(payload_type) != CTF_TYPE_STRUCT) {
+       if (bt_ctf_field_type_get_type_id(payload_type) !=
+                       BT_CTF_TYPE_ID_STRUCT) {
                ret = -1;
                goto end;
        }
@@ -784,24 +902,22 @@ int bt_ctf_event_set_header(struct bt_ctf_event *event,
 {
        int ret = 0;
        struct bt_ctf_field_type *field_type = NULL;
+       struct bt_ctf_stream_class *stream_class = NULL;
 
        if (!event || !header) {
                ret = -1;
                goto end;
        }
 
-       /* Could be NULL since an event class doesn't own a stream class */
-       if (!event->event_class->stream_class) {
-               ret = -1;
-               goto end;
-       }
-
+       stream_class = (struct bt_ctf_stream_class *) bt_object_get_parent(
+               event->event_class);
        /*
         * Ensure the provided header's type matches the one registered to the
         * stream class.
         */
        field_type = bt_ctf_field_get_type(header);
-       if (field_type != event->event_class->stream_class->event_header_type) {
+       if (bt_ctf_field_type_compare(field_type,
+                       stream_class->event_header_type)) {
                ret = -1;
                goto end;
        }
@@ -810,6 +926,7 @@ int bt_ctf_event_set_header(struct bt_ctf_event *event,
        bt_put(event->event_header);
        event->event_header = header;
 end:
+       bt_put(stream_class);
        bt_put(field_type);
        return ret;
 }
@@ -841,7 +958,8 @@ int bt_ctf_event_set_event_context(struct bt_ctf_event *event,
        }
 
        field_type = bt_ctf_field_get_type(context);
-       if (field_type != event->event_class->context) {
+       if (bt_ctf_field_type_compare(field_type,
+                       event->event_class->context)) {
                ret = -1;
                goto end;
        }
@@ -869,10 +987,6 @@ void bt_ctf_event_class_destroy(struct bt_object *obj)
 {
        struct bt_ctf_event_class *event_class;
 
-       /*
-        * Don't call put() on the stream class. See comment in
-        * bt_ctf_event_class_set_stream_class for explanation.
-        */
        event_class = container_of(obj, struct bt_ctf_event_class, base);
        bt_ctf_attributes_destroy(event_class->attributes);
        bt_put(event_class->context);
@@ -886,7 +1000,14 @@ void bt_ctf_event_destroy(struct bt_object *obj)
        struct bt_ctf_event *event;
 
        event = container_of(obj, struct bt_ctf_event, base);
-       bt_put(event->event_class);
+       if (!event->base.parent) {
+               /*
+                * Event was keeping a reference to its class since it shared no
+                * common ancestor with it to guarantee they would both have the
+                * same lifetime.
+                */
+               bt_put(event->event_class);
+       }
        bt_put(event->event_header);
        bt_put(event->context_payload);
        bt_put(event->fields_payload);
@@ -912,7 +1033,8 @@ int set_integer_field_value(struct bt_ctf_field* field, uint64_t value)
        field_type = bt_ctf_field_get_type(field);
        assert(field_type);
 
-       if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) {
+       if (bt_ctf_field_type_get_type_id(field_type) !=
+                       BT_CTF_TYPE_ID_INTEGER) {
                /* Not an integer and the value is unset, error. */
                ret = -1;
                goto end;
@@ -946,36 +1068,6 @@ void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class)
        bt_ctf_attributes_freeze(event_class->attributes);
 }
 
-BT_HIDDEN
-int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class *event_class,
-               struct bt_ctf_stream_class *stream_class)
-{
-       int ret = 0;
-
-       if (!event_class) {
-               ret = -1;
-               goto end;
-       }
-
-       /* Allow a NULL stream_class to unset the current stream_class */
-       if (stream_class && event_class->stream_class) {
-               ret = -1;
-               goto end;
-       }
-
-       event_class->stream_class = stream_class;
-       /*
-        * We don't get() the stream_class since doing so would introduce
-        * a circular ownership between event classes and stream classes.
-        *
-        * A stream class will always unset itself from its events before
-        * being destroyed. This ensures that a user won't get a pointer
-        * to a stale stream class instance from an event class.
-        */
-end:
-       return ret;
-}
-
 BT_HIDDEN
 int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
                struct metadata_context *context)
@@ -1085,6 +1177,9 @@ void bt_ctf_event_class_set_native_byte_order(
                return;
        }
 
+       assert(byte_order == 0 || byte_order == LITTLE_ENDIAN ||
+               byte_order == BIG_ENDIAN);
+
        bt_ctf_field_type_set_native_byte_order(event_class->context,
                byte_order);
        bt_ctf_field_type_set_native_byte_order(event_class->fields,
@@ -1173,11 +1268,11 @@ int bt_ctf_event_populate_event_header(struct bt_ctf_event *event)
                        timestamp_field_type);
                bt_put(timestamp_field_type);
                if (mapped_clock) {
-                       uint64_t timestamp = bt_ctf_clock_get_time(
-                               mapped_clock);
+                       int64_t timestamp;
 
+                       ret = bt_ctf_clock_get_time(mapped_clock, &timestamp);
                        bt_put(mapped_clock);
-                       if (timestamp == (uint64_t) -1ULL) {
+                       if (ret) {
                                goto end;
                        }
 
@@ -1194,28 +1289,6 @@ end:
        return ret;
 }
 
-BT_HIDDEN
-int bt_ctf_event_set_stream(struct bt_ctf_event *event,
-               struct bt_ctf_stream *stream)
-{
-       int ret = 0;
-
-       if (!event) {
-               ret = -1;
-               goto end;
-       }
-
-       if (event->stream && stream) {
-               /* Already attached to a stream */
-               ret = -1;
-               goto end;
-       }
-
-       event->stream = stream;
-end:
-       return ret;
-}
-
 struct bt_ctf_event *bt_ctf_event_copy(struct bt_ctf_event *event)
 {
        struct bt_ctf_event *copy = NULL;
@@ -1230,9 +1303,7 @@ struct bt_ctf_event *bt_ctf_event_copy(struct bt_ctf_event *event)
        }
 
        bt_object_init(copy, bt_ctf_event_destroy);
-       copy->event_class = event->event_class;
-       bt_get(copy->event_class);
-       copy->stream = event->stream;
+       copy->event_class = bt_get(event->event_class);
 
        if (event->event_header) {
                copy->event_header = bt_ctf_field_copy(event->event_header);
This page took 0.029683 seconds and 4 git commands to generate.