From: Philippe Proulx Date: Tue, 2 May 2017 16:18:38 +0000 (-0400) Subject: ir: add trace UUID getter and setter X-Git-Tag: v2.0.0-pre1~331 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=4a32fda021d515d6d197840fa1444ea4a41ec186 ir: add trace UUID getter and setter 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 Signed-off-by: Jérémie Galarneau --- diff --git a/include/babeltrace/ctf-ir/trace-internal.h b/include/babeltrace/ctf-ir/trace-internal.h index c3121cbd..619104d3 100644 --- a/include/babeltrace/ctf-ir/trace-internal.h +++ b/include/babeltrace/ctf-ir/trace-internal.h @@ -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 */ diff --git a/include/babeltrace/ctf-ir/trace.h b/include/babeltrace/ctf-ir/trace.h index 4286df93..df5d34b7 100644 --- a/include/babeltrace/ctf-ir/trace.h +++ b/include/babeltrace/ctf-ir/trace.h @@ -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. diff --git a/lib/ctf-ir/trace.c b/lib/ctf-ir/trace.c index fd464b01..8737bfd3 100644 --- a/lib/ctf-ir/trace.c +++ b/lib/ctf-ir/trace.c @@ -46,6 +46,7 @@ #include #include #include +#include #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)); diff --git a/lib/ctf-writer/writer.c b/lib/ctf-writer/writer.c index e9d74731..9fd2f7dd 100644 --- a/lib/ctf-writer/writer.c +++ b/lib/ctf-writer/writer.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -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); diff --git a/tests/lib/test_ctf_writer.c b/tests/lib/test_ctf_writer.c index 9a790bbc..f6224f5c 100644 --- a/tests/lib/test_ctf_writer.c +++ b/tests/lib/test_ctf_writer.c @@ -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");