X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=lib%2Fctf-ir%2Ftrace.c;h=fe0d362ea8d893b3fc819768545207ed426e76dc;hb=3602afb05ca664461109fa2d29d4e53db1c13fe2;hp=90432dbac36273de8e896f428aa5e614f79bb6a7;hpb=1487a16a42ca7be6b85ef210644d9ac398d7c0dd;p=babeltrace.git diff --git a/lib/ctf-ir/trace.c b/lib/ctf-ir/trace.c index 90432dba..fe0d362e 100644 --- a/lib/ctf-ir/trace.c +++ b/lib/ctf-ir/trace.c @@ -26,6 +26,9 @@ * SOFTWARE. */ +#define BT_LOG_TAG "TRACE" +#include + #include #include #include @@ -40,12 +43,16 @@ #include #include #include -#include -#include +#include #include +#include #include -#include +#include +#include #include +#include +#include +#include #define DEFAULT_IDENTIFIER_SIZE 128 #define DEFAULT_METADATA_STRING_SIZE 4096 @@ -55,11 +62,14 @@ struct listener_wrapper { void *data; }; +struct bt_ctf_trace_is_static_listener_elem { + bt_ctf_trace_is_static_listener func; + void *data; +}; + 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,46 +93,72 @@ 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) { + BT_LOGE_STR("Failed to allocate one trace."); goto error; } - bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE); + BT_LOGD_STR("Creating trace object."); + 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); + if (!trace->clocks) { + BT_LOGE_STR("Failed to allocate one GPtrArray."); + goto error; + } + trace->streams = g_ptr_array_new_with_free_func( (GDestroyNotify) bt_object_release); + if (!trace->streams) { + BT_LOGE_STR("Failed to allocate one GPtrArray."); + goto error; + } + trace->stream_classes = g_ptr_array_new_with_free_func( (GDestroyNotify) bt_object_release); - if (!trace->clocks || !trace->stream_classes || !trace->streams) { + if (!trace->stream_classes) { + BT_LOGE_STR("Failed to allocate one GPtrArray."); 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) { + BT_LOGE_STR("Cannot create empty attributes object."); goto error; } trace->listeners = g_ptr_array_new_with_free_func( (GDestroyNotify) g_free); if (!trace->listeners) { + BT_LOGE_STR("Failed to allocate one GPtrArray."); + goto error; + } + + trace->is_static_listeners = g_array_new(FALSE, TRUE, + sizeof(struct bt_ctf_trace_is_static_listener_elem)); + if (!trace->is_static_listeners) { + BT_LOGE_STR("Failed to allocate one GArray."); goto error; } + BT_LOGD("Created trace object: addr=%p", trace); return trace; error: BT_PUT(trace); + bt_put(packet_header_type); return trace; } @@ -130,7 +166,12 @@ const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace) { const char *name = NULL; - if (!trace || !trace->name) { + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + goto end; + } + + if (!trace->name) { goto end; } @@ -143,7 +184,22 @@ int bt_ctf_trace_set_name(struct bt_ctf_trace *trace, const char *name) { int ret = 0; - if (!trace || !name || trace->frozen) { + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + ret = -1; + goto end; + } + + if (!name) { + BT_LOGW_STR("Invalid parameter: name is NULL."); + ret = -1; + goto end; + } + + if (trace->frozen) { + BT_LOGW("Invalid parameter: trace is frozen: " + "addr=%p, name=\"%s\"", + trace, bt_ctf_trace_get_name(trace)); ret = -1; goto end; } @@ -151,9 +207,68 @@ int bt_ctf_trace_set_name(struct bt_ctf_trace *trace, const char *name) trace->name = trace->name ? g_string_assign(trace->name, name) : g_string_new(name); if (!trace->name) { + BT_LOGE_STR("Failed to allocate one GString."); + ret = -1; + goto end; + } + + BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace, name); + +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) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + ret = -1; + goto end; + } + + if (!uuid) { + BT_LOGW_STR("Invalid parameter: UUID is NULL."); + ret = -1; + goto end; + } + + if (trace->frozen) { + BT_LOGW("Invalid parameter: trace is frozen: " + "addr=%p, name=\"%s\"", + trace, bt_ctf_trace_get_name(trace)); ret = -1; goto end; } + + memcpy(trace->uuid, uuid, sizeof(uuid_t)); + trace->uuid_set = BT_TRUE; + BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", " + "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"", + trace, bt_ctf_trace_get_name(trace), + (unsigned int) uuid[0], + (unsigned int) uuid[1], + (unsigned int) uuid[2], + (unsigned int) uuid[3], + (unsigned int) uuid[4], + (unsigned int) uuid[5], + (unsigned int) uuid[6], + (unsigned int) uuid[7], + (unsigned int) uuid[8], + (unsigned int) uuid[9], + (unsigned int) uuid[10], + (unsigned int) uuid[11], + (unsigned int) uuid[12], + (unsigned int) uuid[13], + (unsigned int) uuid[14], + (unsigned int) uuid[15]); + end: return ret; } @@ -163,7 +278,12 @@ void bt_ctf_trace_destroy(struct bt_object *obj) struct bt_ctf_trace *trace; trace = container_of(obj, struct bt_ctf_trace, base); + + BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"", + trace, bt_ctf_trace_get_name(trace)); + if (trace->environment) { + BT_LOGD_STR("Destroying environment attributes."); bt_ctf_attributes_destroy(trace->environment); } @@ -172,14 +292,17 @@ void bt_ctf_trace_destroy(struct bt_object *obj) } if (trace->clocks) { + BT_LOGD_STR("Putting clock classes."); g_ptr_array_free(trace->clocks, TRUE); } if (trace->streams) { + BT_LOGD_STR("Destroying streams."); g_ptr_array_free(trace->streams, TRUE); } if (trace->stream_classes) { + BT_LOGD_STR("Destroying stream classes."); g_ptr_array_free(trace->stream_classes, TRUE); } @@ -187,6 +310,11 @@ void bt_ctf_trace_destroy(struct bt_object *obj) g_ptr_array_free(trace->listeners, TRUE); } + if (trace->is_static_listeners) { + g_array_free(trace->is_static_listeners, TRUE); + } + + BT_LOGD_STR("Putting packet header field type."); bt_put(trace->packet_header_type); g_free(trace); } @@ -196,14 +324,47 @@ int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace, { int ret = 0; - if (!trace || !name || !value || - bt_ctf_validate_identifier(name) || - !(bt_value_is_integer(value) || bt_value_is_string(value))) { + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + ret = -1; + goto end; + } + + if (!name) { + BT_LOGW_STR("Invalid parameter: name is NULL."); ret = -1; goto end; } - if (strchr(name, ' ')) { + if (!value) { + BT_LOGW_STR("Invalid parameter: value is NULL."); + ret = -1; + goto end; + } + + if (bt_ctf_validate_identifier(name)) { + BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: " + "trace-addr=%p, trace-name=\"%s\", " + "env-name=\"%s\"", + trace, bt_ctf_trace_get_name(trace), name); + ret = -1; + goto end; + } + + if (!bt_value_is_integer(value) && !bt_value_is_string(value)) { + BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: " + "trace-addr=%p, trace-name=\"%s\", " + "env-name=\"%s\", env-value-type=%s", + trace, bt_ctf_trace_get_name(trace), name, + bt_value_type_string(bt_value_get_type(value))); + ret = -1; + goto end; + } + + if (trace->is_static) { + BT_LOGW("Invalid parameter: trace is static: " + "addr=%p, name=\"%s\"", + trace, bt_ctf_trace_get_name(trace)); ret = -1; goto end; } @@ -220,6 +381,10 @@ int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace, trace->environment, name); if (attribute) { + BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: " + "trace-addr=%p, trace-name=\"%s\", " + "env-name=\"%s\"", + trace, bt_ctf_trace_get_name(trace), name); BT_PUT(attribute); ret = -1; goto end; @@ -230,6 +395,17 @@ int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace, ret = bt_ctf_attributes_set_field_value(trace->environment, name, value); + if (ret) { + BT_LOGE("Cannot set environment field's value: " + "trace-addr=%p, trace-name=\"%s\", " + "env-name=\"%s\"", + trace, bt_ctf_trace_get_name(trace), name); + } else { + BT_LOGV("Set environment field's value: " + "trace-addr=%p, trace-name=\"%s\", " + "env-name=\"%s\", value-addr=%p", + trace, bt_ctf_trace_get_name(trace), name, value); + } end: return ret; @@ -241,42 +417,25 @@ int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace, int ret = 0; struct bt_value *env_value_string_obj = NULL; - if (!trace || !name || !value) { + if (!value) { + BT_LOGW_STR("Invalid parameter: value is NULL."); ret = -1; goto end; } - if (trace->frozen) { - /* - * New environment fields may be added to a frozen trace, - * but existing fields may not be changed. - */ - struct bt_value *attribute = - bt_ctf_attributes_get_field_value_by_name( - trace->environment, name); - - if (attribute) { - BT_PUT(attribute); - ret = -1; - goto end; - } - } - env_value_string_obj = bt_value_string_create_init(value); - if (!env_value_string_obj) { + BT_LOGE_STR("Cannot create string value object."); ret = -1; goto end; } - if (trace->frozen) { - bt_value_freeze(env_value_string_obj); - } + /* bt_ctf_trace_set_environment_field() logs errors */ ret = bt_ctf_trace_set_environment_field(trace, name, env_value_string_obj); end: - BT_PUT(env_value_string_obj); + bt_put(env_value_string_obj); return ret; } @@ -286,65 +445,47 @@ int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace, int ret = 0; struct bt_value *env_value_integer_obj = NULL; - if (!trace || !name) { - ret = -1; - goto end; - } - - if (trace->frozen) { - /* - * New environment fields may be added to a frozen trace, - * but existing fields may not be changed. - */ - struct bt_value *attribute = - bt_ctf_attributes_get_field_value_by_name( - trace->environment, name); - - if (attribute) { - BT_PUT(attribute); - ret = -1; - goto end; - } - } - env_value_integer_obj = bt_value_integer_create_init(value); if (!env_value_integer_obj) { + BT_LOGE_STR("Cannot create integer value object."); ret = -1; goto end; } + /* bt_ctf_trace_set_environment_field() logs errors */ ret = bt_ctf_trace_set_environment_field(trace, name, env_value_integer_obj); - if (trace->frozen) { - bt_value_freeze(env_value_integer_obj); - } + end: - BT_PUT(env_value_integer_obj); + bt_put(env_value_integer_obj); 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; + BT_LOGW_STR("Invalid parameter: trace is NULL."); + ret = (int64_t) -1; goto end; } ret = bt_ctf_attributes_get_count(trace->environment); + assert(ret >= 0); end: return ret; } 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; if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); goto end; } @@ -354,12 +495,13 @@ 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; if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); goto end; } @@ -374,7 +516,13 @@ struct bt_value *bt_ctf_trace_get_environment_field_value_by_name( { struct bt_value *ret = NULL; - if (!trace || !name) { + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + goto end; + } + + if (!name) { + BT_LOGW_STR("Invalid parameter: name is NULL."); goto end; } @@ -389,62 +537,550 @@ 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)) { - ret = -1; - goto end; + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + ret = -1; + goto end; + } + + if (trace->is_static) { + BT_LOGW("Invalid parameter: trace is static: " + "addr=%p, name=\"%s\"", + trace, bt_ctf_trace_get_name(trace)); + ret = -1; + goto end; + } + + if (!bt_ctf_clock_class_is_valid(clock_class)) { + BT_LOGW("Invalid parameter: clock class is invalid: " + "trace-addr=%p, trace-name=\"%s\", " + "clock-class-addr=%p, clock-class-name=\"%s\"", + trace, bt_ctf_trace_get_name(trace), + clock_class, bt_ctf_clock_class_get_name(clock_class)); + ret = -1; + goto end; + } + + /* Check for duplicate clock classes */ + if (bt_ctf_trace_has_clock_class(trace, clock_class)) { + BT_LOGW("Invalid parameter: clock class already exists in trace: " + "trace-addr=%p, trace-name=\"%s\", " + "clock-class-addr=%p, clock-class-name=\"%s\"", + trace, bt_ctf_trace_get_name(trace), + clock_class, bt_ctf_clock_class_get_name(clock_class)); + ret = -1; + goto end; + } + + bt_get(clock_class); + g_ptr_array_add(trace->clocks, clock_class); + + if (trace->frozen) { + BT_LOGV_STR("Freezing added clock class because trace is frozen."); + bt_ctf_clock_class_freeze(clock_class); + } + + BT_LOGV("Added clock class to trace: " + "trace-addr=%p, trace-name=\"%s\", " + "clock-class-addr=%p, clock-class-name=\"%s\"", + trace, bt_ctf_trace_get_name(trace), + clock_class, bt_ctf_clock_class_get_name(clock_class)); + +end: + return ret; +} + +int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace) +{ + int64_t ret = (int64_t) -1; + + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + goto end; + } + + ret = trace->clocks->len; +end: + return ret; +} + +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) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + goto end; + } + + if (index >= trace->clocks->len) { + BT_LOGW("Invalid parameter: index is out of bounds: " + "addr=%p, name=\"%s\", " + "index=%" PRIu64 ", count=%u", + trace, bt_ctf_trace_get_name(trace), + index, trace->clocks->len); + goto end; + } + + clock_class = g_ptr_array_index(trace->clocks, index); + bt_get(clock_class); +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); } - /* Check for duplicate clock classes */ - g_ptr_array_foreach(trace->clocks, value_exists, &query); - if (query.found) { - ret = -1; - goto end; - } + goto end; - bt_get(clock_class); - g_ptr_array_add(trace->clocks, clock_class); +invalid: + is_valid = false; - if (trace->frozen) { - bt_ctf_clock_class_freeze(clock_class); - } end: - return ret; + bt_put(field_type); + return is_valid; } -int bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace) +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) { - int ret = -1; + bool is_valid = true; + struct bt_ctf_field_type *field_type = NULL; - if (!trace) { + /* + * 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; } - ret = trace->clocks->len; -end: - return ret; -} + /* 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; + } -struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class( - struct bt_ctf_trace *trace, int index) -{ - struct bt_ctf_clock_class *clock_class = NULL; + /* + * 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; + } - if (!trace || index < 0 || index >= trace->clocks->len) { - goto end; + 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); } - clock_class = g_ptr_array_index(trace->clocks, index); - bt_get(clock_class); + goto end; + +invalid: + is_valid = false; + end: - return clock_class; + 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 +1093,42 @@ 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) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + ret = -1; + goto end; + } + + if (!stream_class) { + BT_LOGW_STR("Invalid parameter: stream class is NULL."); + ret = -1; + goto end; + } + + if (trace->is_static) { + BT_LOGW_STR("Invalid parameter: trace is static."); + ret = -1; + goto end; + } + + BT_LOGD("Adding stream class to trace: " + "trace-addr=%p, trace-name=\"%s\", " + "stream-class-addr=%p, stream-class-name=\"%s\", " + "stream-class-id=%" PRId64, + trace, bt_ctf_trace_get_name(trace), + stream_class, bt_ctf_stream_class_get_name(stream_class), + bt_ctf_stream_class_get_id(stream_class)); + + /* + * 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) { + BT_LOGW_STR("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE at this point; " + "set it with bt_ctf_trace_set_native_byte_order()."); ret = -1; goto end; } @@ -468,6 +1136,11 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, current_parent_trace = bt_ctf_stream_class_get_trace(stream_class); if (current_parent_trace) { /* Stream class is already associated to a trace, abort. */ + BT_LOGW("Invalid parameter: stream class is already part of a trace: " + "stream-class-trace-addr=%p, " + "stream-class-trace-name=\"%s\"", + current_parent_trace, + bt_ctf_trace_get_name(current_parent_trace)); ret = -1; goto end; } @@ -476,15 +1149,6 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, bt_ctf_stream_class_get_event_class_count(stream_class); assert(event_class_count >= 0); - /* Check for duplicate stream classes */ - for (i = 0; i < trace->stream_classes->len; i++) { - if (trace->stream_classes->pdata[i] == stream_class) { - /* Stream class already registered to the trace */ - ret = -1; - goto end; - } - } - if (stream_class->clock) { struct bt_ctf_clock_class *stream_clock_class = stream_class->clock->clock_class; @@ -507,6 +1171,10 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, if (i == trace->clocks->len) { /* Not found */ + BT_LOGW("Stream class's clock's class is not part of the trace: " + "clock-class-addr=%p, clock-class-name=\"%s\"", + stream_clock_class, + bt_ctf_clock_class_get_name(stream_clock_class)); ret = -1; goto end; } @@ -518,6 +1186,10 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, * independent APIs (non-writer and writer * APIs), and isolating them simplifies things. */ + BT_LOGW("Cannot add stream class with a clock to a trace which was not created by a CTF writer object: " + "clock-class-addr=%p, clock-class-name=\"%s\"", + stream_clock_class, + bt_ctf_clock_class_get_name(stream_clock_class)); ret = -1; goto end; } @@ -542,6 +1214,8 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, bt_ctf_stream_class_get_event_header_type(stream_class); stream_event_ctx_type = bt_ctf_stream_class_get_event_context_type(stream_class); + + BT_LOGD("Validating trace and stream class field types."); ret = bt_ctf_validate_class_types(trace->environment, packet_header_type, packet_context_type, event_header_type, stream_event_ctx_type, NULL, NULL, trace->valid, @@ -557,6 +1231,8 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, * This means something went wrong during the validation * process, not that the objects are invalid. */ + BT_LOGE("Failed to validate trace and stream class field types: " + "ret=%d", ret); goto end; } @@ -564,6 +1240,9 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, trace_sc_validation_flags) != trace_sc_validation_flags) { /* Invalid trace/stream class */ + BT_LOGW("Invalid trace or stream class field types: " + "valid-flags=0x%x", + trace_sc_validation_output.valid_flags); ret = -1; goto end; } @@ -572,6 +1251,7 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, ec_validation_outputs = g_new0(struct bt_ctf_validation_output, event_class_count); if (!ec_validation_outputs) { + BT_LOGE_STR("Failed to allocate one validation output structure."); ret = -1; goto end; } @@ -580,7 +1260,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; @@ -594,6 +1275,10 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, * the previous trace and stream class validation here * because copies could have been made. */ + BT_LOGD("Validating event class's field types: " + "addr=%p, name=\"%s\", id=%" PRId64, + event_class, bt_ctf_event_class_get_name(event_class), + bt_ctf_event_class_get_id(event_class)); ret = bt_ctf_validate_class_types(trace->environment, trace_sc_validation_output.packet_header_type, trace_sc_validation_output.packet_context_type, @@ -607,12 +1292,17 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, BT_PUT(event_class); if (ret) { + BT_LOGE("Failed to validate event class field types: " + "ret=%d", ret); goto end; } if ((ec_validation_outputs[i].valid_flags & ec_validation_flags) != ec_validation_flags) { /* Invalid event class */ + BT_LOGW("Invalid event class field types: " + "valid-flags=0x%x", + ec_validation_outputs[i].valid_flags); ret = -1; goto end; } @@ -621,20 +1311,76 @@ 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) { + BT_LOGE_STR("No more stream class IDs available."); + ret = -1; + goto end; + } /* Try to assign a new stream id */ for (i = 0; i < trace->stream_classes->len; i++) { if (stream_id == bt_ctf_stream_class_get_id( trace->stream_classes->pdata[i])) { /* Duplicate stream id found */ + BT_LOGW("Duplicate stream class ID: " + "id=%" PRId64, (int64_t) stream_id); ret = -1; goto end; } } if (bt_ctf_stream_class_set_id_no_check(stream_class, - stream_id)) { + stream_id)) { /* TODO Should retry with a different stream id */ + BT_LOGE("Cannot set stream class's ID: " + "id=%" PRId64, (int64_t) stream_id); + ret = -1; + goto end; + } + } + + /* + * 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)) { + BT_LOGW_STR("Invalid trace's packet header field type."); + ret = -1; + goto end; + } + + if (!packet_context_field_type_is_valid(trace, + stream_class, + trace_sc_validation_output.packet_context_type)) { + BT_LOGW_STR("Invalid stream class's packet context field type."); + ret = -1; + goto end; + } + + if (!event_header_field_type_is_valid(trace, + stream_class, + trace_sc_validation_output.event_header_type)) { + BT_LOGW_STR("Invalid steam class's event header field 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)) { + BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class."); ret = -1; goto end; } @@ -663,7 +1409,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); @@ -686,6 +1433,14 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, /* Notifiy listeners of the trace's schema modification. */ bt_ctf_stream_class_visit(stream_class, bt_ctf_trace_object_modification, trace); + BT_LOGD("Added stream class to trace: " + "trace-addr=%p, trace-name=\"%s\", " + "stream-class-addr=%p, stream-class-name=\"%s\", " + "stream-class-id=%" PRId64, + trace, bt_ctf_trace_get_name(trace), + stream_class, bt_ctf_stream_class_get_name(stream_class), + bt_ctf_stream_class_get_id(stream_class)); + end: if (ret) { bt_object_set_parent(stream_class, NULL); @@ -705,30 +1460,82 @@ end: assert(!packet_context_type); assert(!event_header_type); assert(!stream_event_ctx_type); + return ret; +} + +int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace) +{ + int64_t ret; + + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + ret = (int64_t) -1; + goto end; + } + + ret = (int64_t) trace->streams->len; +end: return ret; } -int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace) +struct bt_ctf_stream *bt_ctf_trace_get_stream_by_index( + struct bt_ctf_trace *trace, + uint64_t index) { - int ret; + struct bt_ctf_stream *stream = NULL; if (!trace) { - ret = -1; + BT_LOGW_STR("Invalid parameter: trace is NULL."); + goto end; + } + + if (index >= trace->streams->len) { + BT_LOGW("Invalid parameter: index is out of bounds: " + "addr=%p, name=\"%s\", " + "index=%" PRIu64 ", count=%u", + trace, bt_ctf_trace_get_name(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) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + ret = (int64_t) -1; goto end; } - ret = trace->stream_classes->len; + 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) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + goto end; + } + + if (index >= trace->stream_classes->len) { + BT_LOGW("Invalid parameter: index is out of bounds: " + "addr=%p, name=\"%s\", " + "index=%" PRIu64 ", count=%u", + trace, bt_ctf_trace_get_name(trace), + index, trace->stream_classes->len); goto end; } @@ -739,12 +1546,21 @@ 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) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + goto end; + } + + if (id < 0) { + BT_LOGW("Invalid parameter: invalid stream class's ID: " + "trace-addr=%p, trace-name=\"%s\", id=%" PRIu64, + trace, bt_ctf_trace_get_name(trace), id_param); goto end; } @@ -772,7 +1588,13 @@ struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name( size_t i; struct bt_ctf_clock_class *clock_class = NULL; - if (!trace || !name) { + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + goto end; + } + + if (!name) { + BT_LOGW_STR("Invalid parameter: name is NULL."); goto end; } @@ -796,6 +1618,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 +1647,7 @@ const char *get_byte_order_string(enum bt_ctf_byte_order byte_order) string = "native"; break; default: - assert(false); + abort(); } return string; @@ -823,33 +1658,48 @@ 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) { + BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE at this point; " + "set it with bt_ctf_trace_set_native_byte_order(): " + "addr=%p, name=\"%s\"", + trace, bt_ctf_trace_get_name(trace)); + 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, ""); + BT_LOGD_STR("Serializing trace's packet header field type's metadata."); + 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,11 +1710,10 @@ 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); - if (env_size <= 0) { return; } @@ -880,9 +1729,8 @@ void append_env_metadata(struct bt_ctf_trace *trace, env_field_value_obj = bt_ctf_attributes_get_field_value( trace->environment, i); - if (!entry_name || !env_field_value_obj) { - goto loop_next; - } + assert(entry_name); + assert(env_field_value_obj); switch (bt_value_get_type(env_field_value_obj)) { case BT_VALUE_TYPE_INTEGER: @@ -892,11 +1740,7 @@ void append_env_metadata(struct bt_ctf_trace *trace, ret = bt_value_integer_get(env_field_value_obj, &int_value); - - if (ret) { - goto loop_next; - } - + assert(ret == 0); g_string_append_printf(context->string, "\t%s = %" PRId64 ";\n", entry_name, int_value); @@ -910,14 +1754,11 @@ void append_env_metadata(struct bt_ctf_trace *trace, ret = bt_value_string_get(env_field_value_obj, &str_value); - - if (ret) { - goto loop_next; - } - + assert(ret == 0); escaped_str = g_strescape(str_value, NULL); - if (!escaped_str) { + BT_LOGE("Cannot escape string: string=\"%s\"", + str_value); goto loop_next; } @@ -926,7 +1767,6 @@ void append_env_metadata(struct bt_ctf_trace *trace, free(escaped_str); break; } - default: goto loop_next; } @@ -946,11 +1786,13 @@ char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) size_t i; if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); goto end; } context = g_new0(struct metadata_context, 1); if (!context) { + BT_LOGE_STR("Failed to allocate one metadata context."); goto end; } @@ -958,6 +1800,7 @@ char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); g_string_append(context->string, "/* CTF 1.8 */\n\n"); if (append_trace_metadata(trace, context)) { + /* append_trace_metadata() logs errors */ goto error; } append_env_metadata(trace, context); @@ -965,27 +1808,33 @@ char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) (GFunc)bt_ctf_clock_class_serialize, context); for (i = 0; i < trace->stream_classes->len; i++) { + /* bt_ctf_stream_class_serialize() logs details */ err = bt_ctf_stream_class_serialize( trace->stream_classes->pdata[i], context); if (err) { + /* bt_ctf_stream_class_serialize() logs errors */ goto error; } } metadata = context->string->str; + error: g_string_free(context->string, err ? TRUE : FALSE); g_string_free(context->field_name, TRUE); g_free(context); + 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; if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); goto end; } @@ -995,12 +1844,21 @@ 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; - if (!trace || trace->frozen) { + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + ret = -1; + goto end; + } + + if (trace->frozen) { + BT_LOGW("Invalid parameter: trace is frozen: " + "addr=%p, name=\"%s\"", + trace, bt_ctf_trace_get_name(trace)); ret = -1; goto end; } @@ -1008,11 +1866,19 @@ int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace, if (byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN && byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN && byte_order != BT_CTF_BYTE_ORDER_NETWORK) { + BT_LOGW("Invalid parameter: invalid byte order: " + "addr=%p, name=\"%s\", bo=%s", + trace, bt_ctf_trace_get_name(trace), + bt_ctf_byte_order_string(byte_order)); ret = -1; goto end; } trace->native_byte_order = byte_order; + BT_LOGV("Set trace's native byte order: " + "addr=%p, name=\"%s\", bo=%s", + trace, bt_ctf_trace_get_name(trace), + bt_ctf_byte_order_string(byte_order)); end: return ret; @@ -1024,6 +1890,7 @@ struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type( struct bt_ctf_field_type *field_type = NULL; if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); goto end; } @@ -1038,27 +1905,43 @@ int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace, { int ret = 0; - if (!trace || trace->frozen) { + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + ret = -1; + goto end; + } + + if (trace->frozen) { + BT_LOGW("Invalid parameter: trace is frozen: " + "addr=%p, name=\"%s\"", + trace, bt_ctf_trace_get_name(trace)); ret = -1; goto end; } /* packet_header_type must be a structure. */ if (packet_header_type && - bt_ctf_field_type_get_type_id(packet_header_type) != - BT_CTF_FIELD_TYPE_ID_STRUCT) { + !bt_ctf_field_type_is_structure(packet_header_type)) { + BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: " + "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s", + trace, bt_ctf_trace_get_name(trace), + packet_header_type, + bt_ctf_field_type_id_string(packet_header_type->id)); ret = -1; goto end; } bt_put(trace->packet_header_type); trace->packet_header_type = bt_get(packet_header_type); + BT_LOGV("Set trace's packet header field type: " + "addr=%p, name=\"%s\", packet-context-ft-addr=%p", + trace, bt_ctf_trace_get_name(trace), packet_header_type); end: return ret; } 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 +1950,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); } @@ -1084,11 +1967,20 @@ int bt_ctf_trace_visit(struct bt_ctf_trace *trace, struct bt_ctf_object obj = { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE }; - if (!trace || !visitor) { + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + ret = -1; + goto end; + } + + if (!visitor) { + BT_LOGW_STR("Invalid parameter: visitor is NULL."); ret = -1; goto end; } + BT_LOGV("Visiting trace: addr=%p, name=\"%s\"", + trace, bt_ctf_trace_get_name(trace)); ret = visitor_helper(&obj, get_stream_class_count, get_stream_class, visit_stream_class, visitor, data); end: @@ -1104,6 +1996,7 @@ int invoke_listener(struct bt_ctf_object *object, void *data) return 0; } +// TODO: add logging to this function once we use it internally. int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace, bt_ctf_listener_cb listener, void *listener_data) { @@ -1187,9 +2080,21 @@ void bt_ctf_trace_freeze(struct bt_ctf_trace *trace) { int i; + if (trace->frozen) { + return; + } + + BT_LOGD("Freezing trace: addr=%p, name=\"%s\"", + trace, bt_ctf_trace_get_name(trace)); + BT_LOGD_STR("Freezing packet header field type."); bt_ctf_field_type_freeze(trace->packet_header_type); + BT_LOGD_STR("Freezing environment attributes."); bt_ctf_attributes_freeze(trace->environment); + if (trace->clocks->len > 0) { + BT_LOGD_STR("Freezing clock classes."); + } + for (i = 0; i < trace->clocks->len; i++) { struct bt_ctf_clock_class *clock_class = g_ptr_array_index(trace->clocks, i); @@ -1200,54 +2105,154 @@ 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) +{ + bt_bool is_static = BT_FALSE; + + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + 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; - 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) { + size_t i; + + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); ret = -1; goto end; } - ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, - _uint32_t, "magic"); - if (ret) { + trace->is_static = BT_TRUE; + bt_ctf_trace_freeze(trace); + BT_LOGV("Set trace static: addr=%p, name=\"%s\"", + trace, bt_ctf_trace_get_name(trace)); + + /* Call all the "trace is static" listeners */ + for (i = 0; i < trace->is_static_listeners->len; i++) { + struct bt_ctf_trace_is_static_listener_elem elem = + g_array_index(trace->is_static_listeners, + struct bt_ctf_trace_is_static_listener_elem, i); + + if (elem.func) { + elem.func(trace, elem.data); + } + } + +end: + return ret; +} + +int bt_ctf_trace_add_is_static_listener(struct bt_ctf_trace *trace, + bt_ctf_trace_is_static_listener listener, void *data) +{ + int i; + struct bt_ctf_trace_is_static_listener_elem new_elem = { + .func = listener, + .data = data, + }; + + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + i = -1; goto end; } - ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, - uuid_array_type, "uuid"); - if (ret) { + if (!listener) { + BT_LOGW_STR("Invalid parameter: listener is NULL."); + i = -1; goto end; } - ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, - _uint32_t, "stream_id"); - if (ret) { + if (trace->is_static) { + BT_LOGW("Invalid parameter: trace is already static: " + "addr=%p, name=\"%s\"", + trace, bt_ctf_trace_get_name(trace)); + i = -1; goto end; } - ret = bt_ctf_trace_set_packet_header_type(trace, - trace_packet_header_type); - if (ret) { + /* Find the next available spot */ + for (i = 0; i < trace->is_static_listeners->len; i++) { + struct bt_ctf_trace_is_static_listener_elem elem = + g_array_index(trace->is_static_listeners, + struct bt_ctf_trace_is_static_listener_elem, i); + + if (!elem.func) { + break; + } + } + + if (i == trace->is_static_listeners->len) { + g_array_append_val(trace->is_static_listeners, new_elem); + } else { + g_array_insert_val(trace->is_static_listeners, i, new_elem); + } + + BT_LOGV("Added \"trace is static\" listener: " + "trace-addr=%p, trace-name=\"%s\", func-addr=%p, " + "data-addr=%p, listener-id=%d", + trace, bt_ctf_trace_get_name(trace), listener, data, i); + +end: + return i; +} + +int bt_ctf_trace_remove_is_static_listener( + struct bt_ctf_trace *trace, int listener_id) +{ + int ret = 0; + struct bt_ctf_trace_is_static_listener_elem *elem; + + if (!trace) { + BT_LOGW_STR("Invalid parameter: trace is NULL."); + ret = -1; + goto end; + } + + if (listener_id < 0) { + BT_LOGW("Invalid listener ID: must be zero or positive: " + "listener-id=%d", listener_id); + ret = -1; + goto end; + } + + if (listener_id >= trace->is_static_listeners->len) { + BT_LOGW("Invalid parameter: no listener with this listener ID: " + "addr=%p, name=\"%s\", listener-id=%d", + trace, bt_ctf_trace_get_name(trace), + listener_id); + ret = -1; + goto end; + } + + elem = &g_array_index(trace->is_static_listeners, + struct bt_ctf_trace_is_static_listener_elem, + listener_id); + if (!elem->func) { + BT_LOGW("Invalid parameter: no listener with this listener ID: " + "addr=%p, name=\"%s\", listener-id=%d", + trace, bt_ctf_trace_get_name(trace), + listener_id); + ret = -1; goto end; } + + elem->func = NULL; + elem->data = NULL; + BT_LOGV("Removed \"trace is static\" listener: " + "trace-addr=%p, trace-name=\"%s\", " + "listener-id=%d", trace, bt_ctf_trace_get_name(trace), + listener_id); + 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; }