ir: add trace UUID getter and setter
[babeltrace.git] / lib / ctf-ir / trace.c
index 5229338106bc4c37b1c489582a9c87a89e2baa0b..8737bfd392e793105fcb59375141814ad69d9140 100644 (file)
 #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/endian-internal.h>
 #include <inttypes.h>
+#include <stdint.h>
+#include <string.h>
 
 #define DEFAULT_IDENTIFIER_SIZE 128
 #define DEFAULT_METADATA_STRING_SIZE 4096
@@ -101,8 +102,6 @@ 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)) {
                goto error;
        }
@@ -158,6 +157,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 = true;
+
+end:
+       return ret;
+}
+
 void bt_ctf_trace_destroy(struct bt_object *obj)
 {
        struct bt_ctf_trace *trace;
@@ -323,12 +343,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 +359,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 +374,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 +409,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 +432,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 +445,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;
        }
 
@@ -444,7 +463,8 @@ end:
 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 +477,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 +609,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 +651,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++) {
@@ -663,7 +697,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 +744,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_class *bt_ctf_trace_get_stream_class(
-               struct bt_ctf_trace *trace, int index)
+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_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 +805,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 +863,19 @@ end:
        return clock_class;
 }
 
+BT_HIDDEN
+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)
 {
@@ -825,19 +905,27 @@ int append_trace_metadata(struct bt_ctf_trace *trace,
        unsigned char *uuid = trace->uuid;
        int ret;
 
-       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));
 
@@ -860,8 +948,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);
 
@@ -1058,7 +1146,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 +1155,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);
 }
 
@@ -1251,3 +1339,33 @@ end:
        bt_put(trace_packet_header_type);
        return ret;
 }
+
+bool bt_ctf_trace_is_static(struct bt_ctf_trace *trace)
+{
+       bool is_static = false;
+
+       if (!trace) {
+               goto end;
+       }
+
+       is_static = trace->is_static;
+
+end:
+       return is_static;
+}
+
+int bt_ctf_trace_set_is_static(struct bt_ctf_trace *trace)
+{
+       int ret = 0;
+
+       if (!trace) {
+               ret = -1;
+               goto end;
+       }
+
+       trace->is_static = true;
+       bt_ctf_trace_freeze(trace);
+
+end:
+       return ret;
+}
This page took 0.029323 seconds and 4 git commands to generate.