Validate CTF semantics in selected CTF IR functions
[babeltrace.git] / lib / ctf-ir / trace.c
index c16686a086b4ff9728c51b50f5561d6e5064fb0b..47f136d5ddb7073a4dcc735a84d25a84ad44362c 100644 (file)
@@ -26,6 +26,9 @@
  * SOFTWARE.
  */
 
+#define BT_LOG_TAG "TRACE"
+#include <babeltrace/lib-logging-internal.h>
+
 #include <babeltrace/ctf-ir/trace-internal.h>
 #include <babeltrace/ctf-ir/clock-class-internal.h>
 #include <babeltrace/ctf-ir/stream-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/compiler-internal.h>
 #include <babeltrace/values.h>
 #include <babeltrace/ref.h>
-#include <babeltrace/endian.h>
+#include <babeltrace/types.h>
+#include <babeltrace/endian-internal.h>
 #include <inttypes.h>
+#include <stdint.h>
+#include <string.h>
 
 #define DEFAULT_IDENTIFIER_SIZE 128
 #define DEFAULT_METADATA_STRING_SIZE 4096
@@ -58,8 +63,6 @@ struct listener_wrapper {
 static
 void bt_ctf_trace_destroy(struct bt_object *obj);
 static
-int init_trace_packet_header(struct bt_ctf_trace *trace);
-static
 void bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
 
 static
@@ -83,13 +86,14 @@ const unsigned int field_type_aliases_sizes[] = {
 struct bt_ctf_trace *bt_ctf_trace_create(void)
 {
        struct bt_ctf_trace *trace = NULL;
+       struct bt_ctf_field_type *packet_header_type = NULL;
 
        trace = g_new0(struct bt_ctf_trace, 1);
        if (!trace) {
                goto error;
        }
 
-       bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE);
+       trace->native_byte_order = BT_CTF_BYTE_ORDER_NATIVE;
        bt_object_init(trace, bt_ctf_trace_destroy);
        trace->clocks = g_ptr_array_new_with_free_func(
                (GDestroyNotify) bt_put);
@@ -101,12 +105,13 @@ struct bt_ctf_trace *bt_ctf_trace_create(void)
                goto error;
        }
 
-       /* Generate a trace UUID */
-       uuid_generate(trace->uuid);
-       if (init_trace_packet_header(trace)) {
+       packet_header_type = bt_ctf_field_type_structure_create();
+       if (!packet_header_type) {
                goto error;
        }
 
+       BT_MOVE(trace->packet_header_type, packet_header_type);
+
        /* Create the environment array object */
        trace->environment = bt_ctf_attributes_create();
        if (!trace->environment) {
@@ -123,6 +128,7 @@ struct bt_ctf_trace *bt_ctf_trace_create(void)
 
 error:
        BT_PUT(trace);
+       bt_put(packet_header_type);
        return trace;
 }
 
@@ -158,6 +164,27 @@ end:
        return ret;
 }
 
+const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace *trace)
+{
+       return trace && trace->uuid_set ? trace->uuid : NULL;
+}
+
+int bt_ctf_trace_set_uuid(struct bt_ctf_trace *trace, const unsigned char *uuid)
+{
+       int ret = 0;
+
+       if (!trace || !uuid || trace->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       memcpy(trace->uuid, uuid, sizeof(uuid_t));
+       trace->uuid_set = BT_TRUE;
+
+end:
+       return ret;
+}
+
 void bt_ctf_trace_destroy(struct bt_object *obj)
 {
        struct bt_ctf_trace *trace;
@@ -323,12 +350,12 @@ end:
        return ret;
 }
 
-int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
+int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
 {
-       int ret = 0;
+       int64_t ret = 0;
 
        if (!trace) {
-               ret = -1;
+               ret = (int64_t) -1;
                goto end;
        }
 
@@ -339,8 +366,8 @@ end:
 }
 
 const char *
-bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace,
-               int index)
+bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace *trace,
+               uint64_t index)
 {
        const char *ret = NULL;
 
@@ -354,8 +381,8 @@ end:
        return ret;
 }
 
-struct bt_value *bt_ctf_trace_get_environment_field_value(
-               struct bt_ctf_trace *trace, int index)
+struct bt_value *bt_ctf_trace_get_environment_field_value_by_index(
+               struct bt_ctf_trace *trace, uint64_t index)
 {
        struct bt_value *ret = NULL;
 
@@ -389,16 +416,15 @@ 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_class, .found = 0 };
 
-       if (!trace || !bt_ctf_clock_class_is_valid(clock_class)) {
+       if (!trace || trace->is_static ||
+                       !bt_ctf_clock_class_is_valid(clock_class)) {
                ret = -1;
                goto end;
        }
 
        /* Check for duplicate clock classes */
-       g_ptr_array_foreach(trace->clocks, value_exists, &query);
-       if (query.found) {
+       if (bt_ctf_trace_has_clock_class(trace, clock_class)) {
                ret = -1;
                goto end;
        }
@@ -413,9 +439,9 @@ end:
        return ret;
 }
 
-int bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
+int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
 {
-       int ret = -1;
+       int64_t ret = (int64_t) -1;
 
        if (!trace) {
                goto end;
@@ -426,12 +452,12 @@ end:
        return ret;
 }
 
-struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class(
-               struct bt_ctf_trace *trace, int index)
+struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index(
+               struct bt_ctf_trace *trace, uint64_t index)
 {
        struct bt_ctf_clock_class *clock_class = NULL;
 
-       if (!trace || index < 0 || index >= trace->clocks->len) {
+       if (!trace || index >= trace->clocks->len) {
                goto end;
        }
 
@@ -441,10 +467,457 @@ end:
        return clock_class;
 }
 
+static
+bool packet_header_field_type_is_valid(struct bt_ctf_trace *trace,
+               struct bt_ctf_field_type *packet_header_type)
+{
+       int ret;
+       bool is_valid = true;
+       struct bt_ctf_field_type *field_type = NULL;
+
+       if (!packet_header_type) {
+               /*
+                * No packet header field type: trace must have only
+                * one stream. At this point the stream class being
+                * added is not part of the trace yet, so we validate
+                * that the trace contains no stream classes yet.
+                */
+               if (trace->stream_classes->len >= 1) {
+                       BT_LOGW_STR("Invalid packet header field type: "
+                               "packet header field type does not exist but there's more than one stream class in the trace.");
+                       goto invalid;
+               }
+
+               /* No packet header field type: valid at this point */
+               goto end;
+       }
+
+       /* Packet header field type, if it exists, must be a structure */
+       if (!bt_ctf_field_type_is_structure(packet_header_type)) {
+               BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
+                       "ft-addr=%p, ft-id=%s",
+                       packet_header_type,
+                       bt_ctf_field_type_id_string(packet_header_type->id));
+               goto invalid;
+       }
+
+       /*
+        * If there's a `magic` field, it must be a 32-bit unsigned
+        * integer field type. Also it must be the first field of the
+        * packet header field type.
+        */
+       field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               packet_header_type, "magic");
+       if (field_type) {
+               const char *field_name;
+
+               if (!bt_ctf_field_type_is_integer(field_type)) {
+                       BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
+                               "magic-ft-addr=%p, magic-ft-id=%s",
+                               field_type,
+                               bt_ctf_field_type_id_string(field_type->id));
+                       goto invalid;
+               }
+
+               if (bt_ctf_field_type_integer_is_signed(field_type)) {
+                       BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
+                               "magic-ft-addr=%p", field_type);
+                       goto invalid;
+               }
+
+               if (bt_ctf_field_type_integer_get_size(field_type) != 32) {
+                       BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
+                               "magic-ft-addr=%p, magic-ft-size=%u",
+                               field_type,
+                               bt_ctf_field_type_integer_get_size(field_type));
+                       goto invalid;
+               }
+
+               ret = bt_ctf_field_type_structure_get_field_by_index(
+                       packet_header_type, &field_name, NULL, 0);
+               assert(ret == 0);
+
+               if (strcmp(field_name, "magic") != 0) {
+                       BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
+                               "magic-ft-addr=%p, first-field-name=\"%s\"",
+                               field_type, field_name);
+                       goto invalid;
+               }
+
+               BT_PUT(field_type);
+       }
+
+       /*
+        * If there's a `uuid` field, it must be an array field type of
+        * length 16 with an 8-bit unsigned integer element field type.
+        */
+       field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               packet_header_type, "uuid");
+       if (field_type) {
+               struct bt_ctf_field_type *elem_ft;
+
+               if (!bt_ctf_field_type_is_array(field_type)) {
+                       BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
+                               "uuid-ft-addr=%p, uuid-ft-id=%s",
+                               field_type,
+                               bt_ctf_field_type_id_string(field_type->id));
+                       goto invalid;
+               }
+
+               if (bt_ctf_field_type_array_get_length(field_type) != 16) {
+                       BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
+                               "uuid-ft-addr=%p, uuid-ft-length=%" PRId64,
+                               field_type,
+                               bt_ctf_field_type_array_get_length(field_type));
+                       goto invalid;
+               }
+
+               elem_ft = bt_ctf_field_type_array_get_element_type(field_type);
+               assert(elem_ft);
+
+               if (!bt_ctf_field_type_is_integer(elem_ft)) {
+                       BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
+                               "elem-ft-addr=%p, elem-ft-id=%s",
+                               elem_ft,
+                               bt_ctf_field_type_id_string(elem_ft->id));
+                       bt_put(elem_ft);
+                       goto invalid;
+               }
+
+               if (bt_ctf_field_type_integer_is_signed(elem_ft)) {
+                       BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
+                               "elem-ft-addr=%p", elem_ft);
+                       bt_put(elem_ft);
+                       goto invalid;
+               }
+
+               if (bt_ctf_field_type_integer_get_size(elem_ft) != 8) {
+                       BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
+                               "elem-ft-addr=%p, elem-ft-size=%u",
+                               elem_ft,
+                               bt_ctf_field_type_integer_get_size(elem_ft));
+                       bt_put(elem_ft);
+                       goto invalid;
+               }
+
+               bt_put(elem_ft);
+               BT_PUT(field_type);
+       }
+
+       /*
+        * The `stream_id` field must exist if there's more than one
+        * stream classes in the trace.
+        */
+       field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               packet_header_type, "stream_id");
+
+       if (!field_type && trace->stream_classes->len >= 1) {
+               BT_LOGW_STR("Invalid packet header field type: "
+                       "`stream_id` field does not exist but there's more than one stream class in the trace.");
+               goto invalid;
+       }
+
+       /*
+        * If there's a `stream_id` field, it must be an unsigned
+        * integer field type.
+        */
+       if (field_type) {
+               if (!bt_ctf_field_type_is_integer(field_type)) {
+                       BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
+                               "stream-id-ft-addr=%p, stream-id-ft-id=%s",
+                               field_type,
+                               bt_ctf_field_type_id_string(field_type->id));
+                       goto invalid;
+               }
+
+               if (bt_ctf_field_type_integer_is_signed(field_type)) {
+                       BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
+                               "stream-id-ft-addr=%p", field_type);
+                       goto invalid;
+               }
+
+               BT_PUT(field_type);
+       }
+
+       goto end;
+
+invalid:
+       is_valid = false;
+
+end:
+       bt_put(field_type);
+       return is_valid;
+}
+
+static
+bool packet_context_field_type_is_valid(struct bt_ctf_trace *trace,
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_field_type *packet_context_type)
+{
+       bool is_valid = true;
+       struct bt_ctf_field_type *field_type = NULL;
+
+       if (!packet_context_type) {
+               /* No packet context field type: valid at this point */
+               goto end;
+       }
+
+       /* Packet context field type, if it exists, must be a structure */
+       if (!bt_ctf_field_type_is_structure(packet_context_type)) {
+               BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
+                       "ft-addr=%p, ft-id=%s",
+                       packet_context_type,
+                       bt_ctf_field_type_id_string(packet_context_type->id));
+               goto invalid;
+       }
+
+       /*
+        * If there's a `packet_size` field, it must be an unsigned
+        * integer field type.
+        */
+       field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               packet_context_type, "packet_size");
+       if (field_type) {
+               if (!bt_ctf_field_type_is_integer(field_type)) {
+                       BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
+                               "packet-size-ft-addr=%p, packet-size-ft-id=%s",
+                               field_type,
+                               bt_ctf_field_type_id_string(field_type->id));
+                       goto invalid;
+               }
+
+               if (bt_ctf_field_type_integer_is_signed(field_type)) {
+                       BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
+                               "packet-size-ft-addr=%p", field_type);
+                       goto invalid;
+               }
+
+               BT_PUT(field_type);
+       }
+
+       /*
+        * If there's a `content_size` field, it must be an unsigned
+        * integer field type.
+        */
+       field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               packet_context_type, "content_size");
+       if (field_type) {
+               if (!bt_ctf_field_type_is_integer(field_type)) {
+                       BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
+                               "content-size-ft-addr=%p, content-size-ft-id=%s",
+                               field_type,
+                               bt_ctf_field_type_id_string(field_type->id));
+                       goto invalid;
+               }
+
+               if (bt_ctf_field_type_integer_is_signed(field_type)) {
+                       BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
+                               "content-size-ft-addr=%p", field_type);
+                       goto invalid;
+               }
+
+               BT_PUT(field_type);
+       }
+
+       /*
+        * If there's a `events_discarded` field, it must be an unsigned
+        * integer field type.
+        */
+       field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               packet_context_type, "events_discarded");
+       if (field_type) {
+               if (!bt_ctf_field_type_is_integer(field_type)) {
+                       BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
+                               "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
+                               field_type,
+                               bt_ctf_field_type_id_string(field_type->id));
+                       goto invalid;
+               }
+
+               if (bt_ctf_field_type_integer_is_signed(field_type)) {
+                       BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
+                               "events-discarded-ft-addr=%p", field_type);
+                       goto invalid;
+               }
+
+               BT_PUT(field_type);
+       }
+
+       /*
+        * If there's a `timestamp_begin` field, it must be an unsigned
+        * integer field type. Also, if the trace is not a CTF writer's
+        * trace, then we cannot automatically set the mapped clock
+        * class of this field, so it must have a mapped clock class.
+        */
+       field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               packet_context_type, "timestamp_begin");
+       if (field_type) {
+               if (!bt_ctf_field_type_is_integer(field_type)) {
+                       BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
+                               "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
+                               field_type,
+                               bt_ctf_field_type_id_string(field_type->id));
+                       goto invalid;
+               }
+
+               if (bt_ctf_field_type_integer_is_signed(field_type)) {
+                       BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
+                               "timestamp-begin-ft-addr=%p", field_type);
+                       goto invalid;
+               }
+
+               if (!trace->is_created_by_writer) {
+                       struct bt_ctf_clock_class *clock_class =
+                               bt_ctf_field_type_integer_get_mapped_clock_class(
+                                       field_type);
+
+                       bt_put(clock_class);
+                       if (!clock_class) {
+                               BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
+                                       "timestamp-begin-ft-addr=%p", field_type);
+                               goto invalid;
+                       }
+               }
+
+               BT_PUT(field_type);
+       }
+
+       /*
+        * If there's a `timestamp_end` field, it must be an unsigned
+        * integer field type. Also, if the trace is not a CTF writer's
+        * trace, then we cannot automatically set the mapped clock
+        * class of this field, so it must have a mapped clock class.
+        */
+       field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               packet_context_type, "timestamp_end");
+       if (field_type) {
+               if (!bt_ctf_field_type_is_integer(field_type)) {
+                       BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
+                               "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
+                               field_type,
+                               bt_ctf_field_type_id_string(field_type->id));
+                       goto invalid;
+               }
+
+               if (bt_ctf_field_type_integer_is_signed(field_type)) {
+                       BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
+                               "timestamp-end-ft-addr=%p", field_type);
+                       goto invalid;
+               }
+
+               if (!trace->is_created_by_writer) {
+                       struct bt_ctf_clock_class *clock_class =
+                               bt_ctf_field_type_integer_get_mapped_clock_class(
+                                       field_type);
+
+                       bt_put(clock_class);
+                       if (!clock_class) {
+                               BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
+                                       "timestamp-end-ft-addr=%p", field_type);
+                               goto invalid;
+                       }
+               }
+
+               BT_PUT(field_type);
+       }
+
+       goto end;
+
+invalid:
+       is_valid = false;
+
+end:
+       bt_put(field_type);
+       return is_valid;
+}
+
+static
+bool event_header_field_type_is_valid(struct bt_ctf_trace *trace,
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_field_type *event_header_type)
+{
+       bool is_valid = true;
+       struct bt_ctf_field_type *field_type = NULL;
+
+       /*
+        * We do not validate that the `timestamp` field exists here
+        * because CTF does not require this exact name to be mapped to
+        * a clock class.
+        */
+
+       if (!event_header_type) {
+               /*
+                * No event header field type: stream class must have
+                * only one event class.
+                */
+               if (bt_ctf_stream_class_get_event_class_count(stream_class) > 1) {
+                       BT_LOGW_STR("Invalid event header field type: "
+                               "event header field type does not exist but there's more than one event class in the stream class.");
+                       goto invalid;
+               }
+
+               /* No event header field type: valid at this point */
+               goto end;
+       }
+
+       /* Event header field type, if it exists, must be a structure */
+       if (!bt_ctf_field_type_is_structure(event_header_type)) {
+               BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
+                       "ft-addr=%p, ft-id=%s",
+                       event_header_type,
+                       bt_ctf_field_type_id_string(event_header_type->id));
+               goto invalid;
+       }
+
+       /*
+        * If there's an `id` field, it must be an unsigned integer
+        * field type or an enumeration field type with an unsigned
+        * integer container field type.
+        */
+       field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               event_header_type, "id");
+       if (field_type) {
+               struct bt_ctf_field_type *int_ft;
+
+               if (bt_ctf_field_type_is_integer(field_type)) {
+                       int_ft = bt_get(field_type);
+               } else if (bt_ctf_field_type_is_enumeration(field_type)) {
+                       int_ft = bt_ctf_field_type_enumeration_get_container_type(
+                               field_type);
+               } else {
+                       BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
+                               "id-ft-addr=%p, id-ft-id=%s",
+                               field_type,
+                               bt_ctf_field_type_id_string(field_type->id));
+                       goto invalid;
+               }
+
+               assert(int_ft);
+               if (bt_ctf_field_type_integer_is_signed(int_ft)) {
+                       BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
+                               "id-ft-addr=%p", int_ft);
+                       goto invalid;
+               }
+
+               bt_put(int_ft);
+               BT_PUT(field_type);
+       }
+
+       goto end;
+
+invalid:
+       is_valid = false;
+
+end:
+       bt_put(field_type);
+       return is_valid;
+}
+
 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
                struct bt_ctf_stream_class *stream_class)
 {
-       int ret, i;
+       int ret;
+       int64_t i;
        int64_t stream_id;
        struct bt_ctf_validation_output trace_sc_validation_output = { 0 };
        struct bt_ctf_validation_output *ec_validation_outputs = NULL;
@@ -457,10 +930,19 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
        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;
-       int event_class_count;
+       int64_t event_class_count;
        struct bt_ctf_trace *current_parent_trace = NULL;
 
-       if (!trace || !stream_class) {
+       if (!trace || !stream_class || trace->is_static) {
+               ret = -1;
+               goto end;
+       }
+
+       /*
+        * At the end of this function we freeze the trace, so its
+        * native byte order must NOT be BT_CTF_BYTE_ORDER_NATIVE.
+        */
+       if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
                ret = -1;
                goto end;
        }
@@ -580,7 +1062,8 @@ 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++) {
                struct bt_ctf_event_class *event_class =
-                       bt_ctf_stream_class_get_event_class(stream_class, i);
+                       bt_ctf_stream_class_get_event_class_by_index(
+                               stream_class, i);
                struct bt_ctf_field_type *event_context_type = NULL;
                struct bt_ctf_field_type *event_payload_type = NULL;
 
@@ -621,6 +1104,10 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
        stream_id = bt_ctf_stream_class_get_id(stream_class);
        if (stream_id < 0) {
                stream_id = trace->next_stream_id++;
+               if (stream_id < 0) {
+                       ret = -1;
+                       goto end;
+               }
 
                /* Try to assign a new stream id */
                for (i = 0; i < trace->stream_classes->len; i++) {
@@ -640,6 +1127,49 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
                }
        }
 
+       /*
+        * At this point all the field types in the validation output
+        * are valid. Validate the semantics of some scopes according to
+        * the CTF specification.
+        */
+       if (!packet_header_field_type_is_valid(trace,
+                       trace_sc_validation_output.packet_header_type)) {
+               ret = -1;
+               goto end;
+       }
+
+       if (!packet_context_field_type_is_valid(trace,
+                       stream_class,
+                       trace_sc_validation_output.packet_context_type)) {
+               ret = -1;
+               goto end;
+       }
+
+       if (!event_header_field_type_is_valid(trace,
+                       stream_class,
+                       trace_sc_validation_output.event_header_type)) {
+               ret = -1;
+               goto end;
+       }
+
+       /*
+        * Now is the time to automatically map specific field types of
+        * the stream class's packet context and event header field
+        * types to the stream class's clock's class if they are not
+        * mapped to a clock class yet. We do it here because we know
+        * that after this point, everything is frozen so it won't be
+        * possible for the user to modify the stream class's clock, or
+        * to map those field types to other clock classes.
+        */
+       if (trace->is_created_by_writer) {
+               if (bt_ctf_stream_class_map_clock_class(stream_class,
+                               trace_sc_validation_output.packet_context_type,
+                               trace_sc_validation_output.event_header_type)) {
+                       ret = -1;
+                       goto end;
+               }
+       }
+
        bt_object_set_parent(stream_class, trace);
        g_ptr_array_add(trace->stream_classes, stream_class);
 
@@ -663,7 +1193,8 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
 
        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);
+                       bt_ctf_stream_class_get_event_class_by_index(
+                               stream_class, i);
 
                bt_ctf_validation_replace_types(NULL, NULL, event_class,
                        &ec_validation_outputs[i], ec_validation_flags);
@@ -709,26 +1240,57 @@ end:
        return ret;
 }
 
-int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
+int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace)
 {
-       int ret;
+       int64_t ret;
 
        if (!trace) {
-               ret = -1;
+               ret = (int64_t) -1;
                goto end;
        }
 
-       ret = trace->stream_classes->len;
+       ret = (int64_t) trace->streams->len;
+
+end:
+       return ret;
+}
+
+struct bt_ctf_stream *bt_ctf_trace_get_stream_by_index(
+               struct bt_ctf_trace *trace,
+               uint64_t index)
+{
+       struct bt_ctf_stream *stream = NULL;
+
+       if (!trace || index >= trace->streams->len) {
+               goto end;
+       }
+
+       stream = bt_get(g_ptr_array_index(trace->streams, index));
+
+end:
+       return stream;
+}
+
+int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
+{
+       int64_t ret;
+
+       if (!trace) {
+               ret = (int64_t) -1;
+               goto end;
+       }
+
+       ret = (int64_t) trace->stream_classes->len;
 end:
        return ret;
 }
 
-struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class(
-               struct bt_ctf_trace *trace, int index)
+struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_index(
+               struct bt_ctf_trace *trace, uint64_t index)
 {
        struct bt_ctf_stream_class *stream_class = NULL;
 
-       if (!trace || index < 0 || index >= trace->stream_classes->len) {
+       if (!trace || index >= trace->stream_classes->len) {
                goto end;
        }
 
@@ -739,12 +1301,13 @@ end:
 }
 
 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
-               struct bt_ctf_trace *trace, uint32_t id)
+               struct bt_ctf_trace *trace, uint64_t id_param)
 {
        int i;
        struct bt_ctf_stream_class *stream_class = NULL;
+       int64_t id = (int64_t) id_param;
 
-       if (!trace) {
+       if (!trace || id < 0) {
                goto end;
        }
 
@@ -796,6 +1359,19 @@ end:
        return clock_class;
 }
 
+BT_HIDDEN
+bt_bool bt_ctf_trace_has_clock_class(struct bt_ctf_trace *trace,
+               struct bt_ctf_clock_class *clock_class)
+{
+       struct search_query query = { .value = clock_class, .found = 0 };
+
+       assert(trace);
+       assert(clock_class);
+
+       g_ptr_array_foreach(trace->clocks, value_exists, &query);
+       return query.found;
+}
+
 BT_HIDDEN
 const char *get_byte_order_string(enum bt_ctf_byte_order byte_order)
 {
@@ -812,7 +1388,7 @@ const char *get_byte_order_string(enum bt_ctf_byte_order byte_order)
                string = "native";
                break;
        default:
-               assert(false);
+               assert(BT_FALSE);
        }
 
        return string;
@@ -823,33 +1399,43 @@ int append_trace_metadata(struct bt_ctf_trace *trace,
                struct metadata_context *context)
 {
        unsigned char *uuid = trace->uuid;
-       int ret;
+       int ret = 0;
 
-       g_string_append(context->string, "trace {\n");
+       if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
+               ret = -1;
+               goto end;
+       }
 
+       g_string_append(context->string, "trace {\n");
        g_string_append(context->string, "\tmajor = 1;\n");
        g_string_append(context->string, "\tminor = 8;\n");
        assert(trace->native_byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ||
                trace->native_byte_order == BT_CTF_BYTE_ORDER_BIG_ENDIAN ||
                trace->native_byte_order == BT_CTF_BYTE_ORDER_NETWORK);
-       g_string_append_printf(context->string,
-               "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
-               uuid[0], uuid[1], uuid[2], uuid[3],
-               uuid[4], uuid[5], uuid[6], uuid[7],
-               uuid[8], uuid[9], uuid[10], uuid[11],
-               uuid[12], uuid[13], uuid[14], uuid[15]);
+
+       if (trace->uuid_set) {
+               g_string_append_printf(context->string,
+                       "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
+                       uuid[0], uuid[1], uuid[2], uuid[3],
+                       uuid[4], uuid[5], uuid[6], uuid[7],
+                       uuid[8], uuid[9], uuid[10], uuid[11],
+                       uuid[12], uuid[13], uuid[14], uuid[15]);
+       }
+
        g_string_append_printf(context->string, "\tbyte_order = %s;\n",
                get_byte_order_string(trace->native_byte_order));
 
-       g_string_append(context->string, "\tpacket.header := ");
-       context->current_indentation_level++;
-       g_string_assign(context->field_name, "");
-       ret = bt_ctf_field_type_serialize(trace->packet_header_type,
-               context);
-       if (ret) {
-               goto end;
+       if (trace->packet_header_type) {
+               g_string_append(context->string, "\tpacket.header := ");
+               context->current_indentation_level++;
+               g_string_assign(context->field_name, "");
+               ret = bt_ctf_field_type_serialize(trace->packet_header_type,
+                       context);
+               if (ret) {
+                       goto end;
+               }
+               context->current_indentation_level--;
        }
-       context->current_indentation_level--;
 
        g_string_append(context->string, ";\n};\n\n");
 end:
@@ -860,8 +1446,8 @@ static
 void append_env_metadata(struct bt_ctf_trace *trace,
                struct metadata_context *context)
 {
-       int i;
-       int env_size;
+       int64_t i;
+       int64_t env_size;
 
        env_size = bt_ctf_attributes_get_count(trace->environment);
 
@@ -981,7 +1567,8 @@ end:
        return metadata;
 }
 
-enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace)
+enum bt_ctf_byte_order bt_ctf_trace_get_native_byte_order(
+               struct bt_ctf_trace *trace)
 {
        enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
 
@@ -995,7 +1582,7 @@ end:
        return ret;
 }
 
-int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace,
+int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace,
                enum bt_ctf_byte_order byte_order)
 {
        int ret = 0;
@@ -1046,7 +1633,7 @@ int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
        /* 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) {
+                               BT_CTF_FIELD_TYPE_ID_STRUCT) {
                ret = -1;
                goto end;
        }
@@ -1058,7 +1645,7 @@ end:
 }
 
 static
-int get_stream_class_count(void *element)
+int64_t get_stream_class_count(void *element)
 {
        return bt_ctf_trace_get_stream_class_count(
                        (struct bt_ctf_trace *) element);
@@ -1067,7 +1654,7 @@ int get_stream_class_count(void *element)
 static
 void *get_stream_class(void *element, int i)
 {
-       return bt_ctf_trace_get_stream_class(
+       return bt_ctf_trace_get_stream_class_by_index(
                        (struct bt_ctf_trace *) element, i);
 }
 
@@ -1200,54 +1787,32 @@ void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
        trace->frozen = 1;
 }
 
-static
-int init_trace_packet_header(struct bt_ctf_trace *trace)
+bt_bool bt_ctf_trace_is_static(struct bt_ctf_trace *trace)
 {
-       int ret = 0;
-       struct bt_ctf_field *magic = NULL, *uuid_array = NULL;
-       struct bt_ctf_field_type *_uint32_t =
-               get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
-       struct bt_ctf_field_type *_uint8_t =
-               get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
-       struct bt_ctf_field_type *trace_packet_header_type =
-               bt_ctf_field_type_structure_create();
-       struct bt_ctf_field_type *uuid_array_type =
-               bt_ctf_field_type_array_create(_uint8_t, 16);
-
-       if (!trace_packet_header_type || !uuid_array_type) {
-               ret = -1;
-               goto end;
-       }
+       bt_bool is_static = BT_FALSE;
 
-       ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
-               _uint32_t, "magic");
-       if (ret) {
+       if (!trace) {
                goto end;
        }
 
-       ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
-               uuid_array_type, "uuid");
-       if (ret) {
-               goto end;
-       }
+       is_static = trace->is_static;
 
-       ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
-               _uint32_t, "stream_id");
-       if (ret) {
-               goto end;
-       }
+end:
+       return is_static;
+}
 
-       ret = bt_ctf_trace_set_packet_header_type(trace,
-               trace_packet_header_type);
-       if (ret) {
+int bt_ctf_trace_set_is_static(struct bt_ctf_trace *trace)
+{
+       int ret = 0;
+
+       if (!trace) {
+               ret = -1;
                goto end;
        }
+
+       trace->is_static = BT_TRUE;
+       bt_ctf_trace_freeze(trace);
+
 end:
-       bt_put(uuid_array_type);
-       bt_put(_uint32_t);
-       bt_put(_uint8_t);
-       bt_put(magic);
-       bt_put(uuid_array);
-       bt_put(trace_packet_header_type);
        return ret;
 }
This page took 0.035581 seconds and 4 git commands to generate.