ir: add trace UUID getter and setter
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 2 May 2017 16:18:38 +0000 (12:18 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sun, 28 May 2017 16:57:41 +0000 (12:57 -0400)
The trace object's creation does not generate a UUID anymore: a new
trace starts with an unset UUID. When you create a CTF writer object, it
creates a trace object, generates a UUID, and set its trace's UUID. This
ensures backward compatibility with pre-2.0 CTF writer while allowing a
new trace object to have no UUID.

Tests are updated accordingly.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
include/babeltrace/ctf-ir/trace-internal.h
include/babeltrace/ctf-ir/trace.h
lib/ctf-ir/trace.c
lib/ctf-writer/writer.c
tests/lib/test_ctf_writer.c

index c3121cbd8ba54cbbc85c177a06b4c04ff74ed403..619104d339278d60894cb9ac83e921edb2d844de 100644 (file)
@@ -52,6 +52,7 @@ struct bt_ctf_trace {
        GString *name;
        int frozen;
        uuid_t uuid;
+       bool uuid_set;
        enum bt_ctf_byte_order native_byte_order;
        struct bt_value *environment;
        GPtrArray *clocks; /* Array of pointers to bt_ctf_clock_class */
index 4286df93c24a43dc68a590bb977df2730eb15a45..df5d34b705c101627736258616bcef8cc6fde561 100644 (file)
@@ -66,6 +66,7 @@ A trace class has the following properties:
   \link ctfirfieldtypes field types\endlink eventually part of the trace
   class with a byte order set to #BT_CTF_BYTE_ORDER_NATIVE have this
   "real" byte order.
+- A \b UUID.
 - An \b environment, which is a custom key-value mapping. Keys are
   strings and values can be strings or integers.
 
@@ -280,6 +281,45 @@ extern enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(
 extern int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace_class,
                enum bt_ctf_byte_order native_byte_order);
 
+/**
+@brief Returns the UUID of the CTF IR trace class \p trace_class.
+
+On success, the return value is an array of 16 bytes.
+
+@param[in] trace_class Trace class of which to get the UUID.
+@returns               UUID of trace class \p trace_class, or
+                       \c NULL if \p trace_class has no UUID or on error.
+
+@prenotnull{trace_class}
+@postrefcountsame{trace_class}
+
+@sa bt_ctf_trace_set_uuid(): Sets the UUID of a given trace class.
+*/
+extern const unsigned char *bt_ctf_trace_get_uuid(
+               struct bt_ctf_trace *trace_class);
+
+/**
+@brief  Sets the UUID of the CTF IR trace class \p trace_class to
+       \p uuid.
+
+\p uuid \em must be an array of 16 bytes.
+
+@param[in] trace_class         Trace class of which to set the UUID.
+@param[in] uuid                        UUID of the \p trace_class (copied on
+                               success).
+@returns                       0 on success, or a negative value on error.
+
+@prenotnull{trace_class}
+@prenotnull{uuid}
+@prehot{trace_class}
+@pre \p uuid is an array of 16 bytes.
+@postrefcountsame{trace_class}
+
+@sa bt_ctf_trace_get_uuid(): Returns the UUID of a given trace class.
+*/
+extern int bt_ctf_trace_set_uuid(struct bt_ctf_trace *trace_class,
+               const unsigned char *uuid);
+
 /**
 @brief Returns the number of entries contained in the environment of
        the CTF IR trace class \p trace_class.
index fd464b015e197d7769d4da8a2fb41560eb584077..8737bfd392e793105fcb59375141814ad69d9140 100644 (file)
@@ -46,6 +46,7 @@
 #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;
@@ -885,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));
 
index e9d747317825f7c0ec69fa0282c5fc90775a6346..9fd2f7ddee52d6ab3281a69d0440191c7d763613 100644 (file)
@@ -37,6 +37,7 @@
 #include <babeltrace/ref.h>
 #include <babeltrace/endian-internal.h>
 #include <babeltrace/compiler-internal.h>
+#include <babeltrace/compat/uuid-internal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
@@ -52,6 +53,7 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
 {
        int ret;
        struct bt_ctf_writer *writer = NULL;
+       unsigned char uuid[16];
 
        if (!path) {
                goto error;
@@ -73,6 +75,13 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
                goto error_destroy;
        }
 
+       /* Generate a UUID for this writer's trace */
+       uuid_generate(uuid);
+       ret = bt_ctf_trace_set_uuid(writer->trace, uuid);
+       if (ret) {
+               goto error_destroy;
+       }
+
        writer->trace->is_created_by_writer = 1;
        bt_object_set_parent(writer->trace, writer);
        bt_put(writer->trace);
index 9a790bbc181b3c42b0a0e2918d91acd6f60f1d49..f6224f5c8f9977e9b02f29f512eb789b5a147625 100644 (file)
@@ -61,7 +61,7 @@
 #define DEFAULT_CLOCK_TIME 0
 #define DEFAULT_CLOCK_VALUE 0
 
-#define NR_TESTS 607
+#define NR_TESTS 614
 
 static int64_t current_time = 42;
 
@@ -2808,6 +2808,36 @@ void test_static_trace(void)
        bt_put(clock_class);
 }
 
+static
+void test_trace_uuid(void)
+{
+       struct bt_ctf_trace *trace;
+       const unsigned char uuid[] = {
+               0x35, 0x92, 0x63, 0xab, 0xb4, 0xbe, 0x40, 0xb4,
+               0xb2, 0x60, 0xd3, 0xf1, 0x3b, 0xb0, 0xd8, 0x59,
+       };
+       const unsigned char *ret_uuid;
+
+       trace = bt_ctf_trace_create();
+       assert(trace);
+       ok(!bt_ctf_trace_get_uuid(NULL),
+               "bt_ctf_trace_get_uuid() handles NULL");
+       ok(!bt_ctf_trace_get_uuid(trace),
+               "bt_ctf_trace_get_uuid() returns NULL initially");
+       ok(bt_ctf_trace_set_uuid(NULL, uuid),
+               "bt_ctf_trace_set_uuid() handles NULL (trace)");
+       ok(bt_ctf_trace_set_uuid(trace, NULL),
+               "bt_ctf_trace_set_uuid() handles NULL (UUID)");
+       ok(bt_ctf_trace_set_uuid(trace, uuid) == 0,
+               "bt_ctf_trace_set_uuid() succeeds with a valid UUID");
+       ret_uuid = bt_ctf_trace_get_uuid(trace);
+       ok(ret_uuid, "bt_ctf_trace_get_uuid() returns a UUID");
+       ok(memcmp(uuid, ret_uuid, 16) == 0,
+               "bt_ctf_trace_get_uuid() returns the expected UUID");
+
+       bt_put(trace);
+}
+
 int main(int argc, char **argv)
 {
        char trace_path[] = "/tmp/ctfwriter_XXXXXX";
@@ -3463,6 +3493,8 @@ int main(int argc, char **argv)
 
        test_static_trace();
 
+       test_trace_uuid();
+
        metadata_string = bt_ctf_writer_get_metadata_string(writer);
        ok(metadata_string, "Get metadata string");
 
This page took 0.029416 seconds and 4 git commands to generate.