X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=formats%2Fctf%2Fir%2Ftrace.c;h=9c314fe11801573aa6cfed39356e1d9a9bd8d006;hb=9643611120f9bf174684cb0a06ec9ed418e3341e;hp=59f696ebc9eab6280801de9e15c65292e00fd7cf;hpb=29d9d76c476cbf3fdf6fa709bfbdc24309974f06;p=babeltrace.git diff --git a/formats/ctf/ir/trace.c b/formats/ctf/ir/trace.c index 59f696eb..9c314fe1 100644 --- a/formats/ctf/ir/trace.c +++ b/formats/ctf/ir/trace.c @@ -27,23 +27,39 @@ */ #include -#include +#include #include #include +#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 +struct listener_wrapper { + bt_ctf_listener_cb listener; + void *data; +}; + static -void environment_variable_destroy(struct environment_variable *var); -static -void bt_ctf_trace_destroy(struct bt_ctf_ref *ref); +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 const unsigned int field_type_aliases_alignments[] = { @@ -73,46 +89,85 @@ struct bt_ctf_trace *bt_ctf_trace_create(void) } bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE); - bt_ctf_ref_init(&trace->ref_count); - trace->environment = g_ptr_array_new_with_free_func( - (GDestroyNotify)environment_variable_destroy); + bt_object_init(trace, bt_ctf_trace_destroy); trace->clocks = g_ptr_array_new_with_free_func( - (GDestroyNotify)bt_ctf_clock_put); + (GDestroyNotify) bt_put); trace->streams = g_ptr_array_new_with_free_func( - (GDestroyNotify)bt_ctf_stream_put); + (GDestroyNotify) bt_object_release); trace->stream_classes = g_ptr_array_new_with_free_func( - (GDestroyNotify)bt_ctf_stream_class_put); - if (!trace->environment || !trace->clocks || - !trace->stream_classes || !trace->streams) { - goto error_destroy; + (GDestroyNotify) bt_object_release); + if (!trace->clocks || !trace->stream_classes || !trace->streams) { + goto error; } /* Generate a trace UUID */ uuid_generate(trace->uuid); if (init_trace_packet_header(trace)) { - goto error_destroy; + goto error; + } + + /* Create the environment array object */ + trace->environment = bt_ctf_attributes_create(); + if (!trace->environment) { + goto error; + } + + trace->listeners = g_ptr_array_new_with_free_func( + (GDestroyNotify) g_free); + if (!trace->listeners) { + goto error; } return trace; -error_destroy: - bt_ctf_trace_destroy(&trace->ref_count); - trace = NULL; error: + BT_PUT(trace); return trace; } -void bt_ctf_trace_destroy(struct bt_ctf_ref *ref) +const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace) { - struct bt_ctf_trace *trace; + const char *name = NULL; - if (!ref) { - return; + if (!trace || !trace->name) { + goto end; + } + + name = trace->name->str; +end: + return name; +} + +int bt_ctf_trace_set_name(struct bt_ctf_trace *trace, const char *name) +{ + int ret = 0; + + if (!trace || !name || trace->frozen) { + ret = -1; + goto end; + } + + trace->name = trace->name ? g_string_assign(trace->name, name) : + g_string_new(name); + if (!trace->name) { + ret = -1; + goto end; } +end: + return ret; +} + +void bt_ctf_trace_destroy(struct bt_object *obj) +{ + struct bt_ctf_trace *trace; - trace = container_of(ref, struct bt_ctf_trace, ref_count); + trace = container_of(obj, struct bt_ctf_trace, base); if (trace->environment) { - g_ptr_array_free(trace->environment, TRUE); + bt_ctf_attributes_destroy(trace->environment); + } + + if (trace->name) { + g_string_free(trace->name, TRUE); } if (trace->clocks) { @@ -127,152 +182,237 @@ void bt_ctf_trace_destroy(struct bt_ctf_ref *ref) g_ptr_array_free(trace->stream_classes, TRUE); } - bt_ctf_field_type_put(trace->packet_header_type); + if (trace->listeners) { + g_ptr_array_free(trace->listeners, TRUE); + } + + bt_put(trace->packet_header_type); g_free(trace); } -struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace, - struct bt_ctf_stream_class *stream_class) +int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace, + const char *name, struct bt_value *value) { - int ret; - int stream_class_found = 0; - size_t i; - struct bt_ctf_stream *stream = NULL; - - if (!trace || !stream_class) { - goto error; - } + int ret = 0; - ret = bt_ctf_stream_class_set_byte_order(stream_class, - trace->byte_order == LITTLE_ENDIAN ? - BT_CTF_BYTE_ORDER_LITTLE_ENDIAN : BT_CTF_BYTE_ORDER_BIG_ENDIAN); - if (ret) { - goto error; + if (!trace || !name || !value || + bt_ctf_validate_identifier(name) || + !(bt_value_is_integer(value) || bt_value_is_string(value))) { + ret = -1; + goto end; } - stream = bt_ctf_stream_create(stream_class, trace); - if (!stream) { - goto error; + if (strchr(name, ' ')) { + ret = -1; + goto end; } - for (i = 0; i < trace->stream_classes->len; i++) { - if (trace->stream_classes->pdata[i] == stream_class) { - stream_class_found = 1; + if (trace->frozen) { + /* + * New environment fields may be added to a frozen trace, + * but existing fields may not be changed. + * + * The object passed is frozen like all other attributes. + */ + struct bt_value *attribute = + bt_ctf_attributes_get_field_value_by_name( + trace->environment, name); + + if (attribute) { + BT_PUT(attribute); + ret = -1; + goto end; } + + bt_value_freeze(value); } - if (!stream_class_found) { - int64_t stream_id = bt_ctf_stream_class_get_id(stream_class); + ret = bt_ctf_attributes_set_field_value(trace->environment, name, + value); - if (stream_id < 0) { - /* Try to assign a new stream id */ - if (bt_ctf_stream_class_set_id(stream->stream_class, - trace->next_stream_id++)) { - goto error; - } - } +end: + return ret; +} - 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 */ - goto error; - } +int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace, + const char *name, const char *value) +{ + int ret = 0; + struct bt_value *env_value_string_obj = NULL; + + if (!trace || !name || !value) { + 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; } - bt_ctf_stream_class_get(stream->stream_class); - g_ptr_array_add(trace->stream_classes, stream->stream_class); } - bt_ctf_stream_get(stream); - g_ptr_array_add(trace->streams, stream); - bt_ctf_stream_class_freeze(stream_class); - trace->frozen = 1; - return stream; + env_value_string_obj = bt_value_string_create_init(value); -error: - bt_ctf_stream_put(stream); - return NULL; + if (!env_value_string_obj) { + ret = -1; + goto end; + } + + if (trace->frozen) { + bt_value_freeze(env_value_string_obj); + } + ret = bt_ctf_trace_set_environment_field(trace, name, + env_value_string_obj); + +end: + BT_PUT(env_value_string_obj); + return ret; } -int bt_ctf_trace_add_environment_field(struct bt_ctf_trace *trace, - const char *name, - const char *value) +int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace, + const char *name, int64_t value) { - struct environment_variable *var = NULL; - char *escaped_value = NULL; int ret = 0; + struct bt_value *env_value_integer_obj = NULL; - if (!trace || !name || !value || bt_ctf_validate_identifier(name)) { + if (!trace || !name) { ret = -1; - goto error; + goto end; } - if (strchr(name, ' ')) { - ret = -1; - goto error; + 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; + } } - var = g_new0(struct environment_variable, 1); - if (!var) { + env_value_integer_obj = bt_value_integer_create_init(value); + if (!env_value_integer_obj) { ret = -1; - goto error; + goto end; } - escaped_value = g_strescape(value, NULL); - if (!escaped_value) { - ret = -1; - goto error; + 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); + return ret; +} + +int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace) +{ + int ret = 0; - var->name = g_string_new(name); - var->value = g_string_new(escaped_value); - g_free(escaped_value); - if (!var->name || !var->value) { + if (!trace) { ret = -1; - goto error; + goto end; } - g_ptr_array_add(trace->environment, var); + ret = bt_ctf_attributes_get_count(trace->environment); + +end: return ret; +} -error: - if (var && var->name) { - g_string_free(var->name, TRUE); +const char * +bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace, + int index) +{ + const char *ret = NULL; + + if (!trace) { + goto end; + } + + ret = bt_ctf_attributes_get_field_name(trace->environment, index); + +end: + return ret; +} + +struct bt_value *bt_ctf_trace_get_environment_field_value( + struct bt_ctf_trace *trace, int index) +{ + struct bt_value *ret = NULL; + + if (!trace) { + goto end; } - if (var && var->value) { - g_string_free(var->value, TRUE); + ret = bt_ctf_attributes_get_field_value(trace->environment, index); + +end: + return ret; +} + +struct bt_value *bt_ctf_trace_get_environment_field_value_by_name( + struct bt_ctf_trace *trace, const char *name) +{ + struct bt_value *ret = NULL; + + if (!trace || !name) { + goto end; } - g_free(var); + ret = bt_ctf_attributes_get_field_value_by_name(trace->environment, + name); + +end: return ret; } -int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace, - struct bt_ctf_clock *clock) +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, .found = 0 }; + struct search_query query = { .value = clock_class, .found = 0 }; - if (!trace || !clock) { + if (!trace || !bt_ctf_clock_class_is_valid(clock_class)) { ret = -1; goto end; } - /* Check for duplicate clocks */ + /* Check for duplicate clock classes */ g_ptr_array_foreach(trace->clocks, value_exists, &query); if (query.found) { ret = -1; goto end; } - bt_ctf_clock_get(clock); - g_ptr_array_add(trace->clocks, clock); + bt_get(clock_class); + g_ptr_array_add(trace->clocks, clock_class); + + if (trace->frozen) { + bt_ctf_clock_class_freeze(clock_class); + } end: return ret; } -int bt_ctf_trace_get_clock_count(struct bt_ctf_trace *trace) +int bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace) { int ret = -1; @@ -285,19 +425,382 @@ end: return ret; } -struct bt_ctf_clock *bt_ctf_trace_get_clock(struct bt_ctf_trace *trace, - int index) +struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class( + struct bt_ctf_trace *trace, int index) { - struct bt_ctf_clock *clock = NULL; + struct bt_ctf_clock_class *clock_class = NULL; if (!trace || index < 0 || index >= trace->clocks->len) { goto end; } - clock = g_ptr_array_index(trace->clocks, index); - bt_ctf_clock_get(clock); + clock_class = g_ptr_array_index(trace->clocks, index); + bt_get(clock_class); +end: + return clock_class; +} + +int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, + struct bt_ctf_stream_class *stream_class) +{ + int ret, i; + int64_t stream_id; + struct bt_ctf_validation_output trace_sc_validation_output = { 0 }; + struct bt_ctf_validation_output *ec_validation_outputs = NULL; + const enum bt_ctf_validation_flag trace_sc_validation_flags = + BT_CTF_VALIDATION_FLAG_TRACE | + BT_CTF_VALIDATION_FLAG_STREAM; + const enum bt_ctf_validation_flag ec_validation_flags = + BT_CTF_VALIDATION_FLAG_EVENT; + struct bt_ctf_field_type *packet_header_type = NULL; + 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; + struct bt_ctf_trace *current_parent_trace = NULL; + + if (!trace || !stream_class) { + ret = -1; + goto end; + } + + current_parent_trace = bt_ctf_stream_class_get_trace(stream_class); + if (current_parent_trace) { + /* Stream class is already associated to a trace, abort. */ + ret = -1; + goto end; + } + + event_class_count = + 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; + + if (trace->is_created_by_writer) { + /* + * Make sure this clock was also added to the + * trace (potentially through its CTF writer + * owner). + */ + size_t i; + + for (i = 0; i < trace->clocks->len; i++) { + if (trace->clocks->pdata[i] == + stream_clock_class) { + /* Found! */ + break; + } + } + + if (i == trace->clocks->len) { + /* Not found */ + ret = -1; + goto end; + } + } else { + /* + * This trace was NOT created by a CTF writer, + * thus do not allow the stream class to add to + * have a clock at all. Those are two + * independent APIs (non-writer and writer + * APIs), and isolating them simplifies things. + */ + ret = -1; + goto end; + } + } + + /* + * We're about to freeze both the trace and the stream class. + * Also, each event class contained in this stream class are + * already frozen. + * + * This trace, this stream class, and all its event classes + * should be valid at this point. + * + * Validate trace and stream class first, then each event + * class of this stream class can be validated individually. + */ + packet_header_type = + bt_ctf_trace_get_packet_header_type(trace); + packet_context_type = + bt_ctf_stream_class_get_packet_context_type(stream_class); + event_header_type = + bt_ctf_stream_class_get_event_header_type(stream_class); + stream_event_ctx_type = + bt_ctf_stream_class_get_event_context_type(stream_class); + 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, + stream_class->valid, 1, &trace_sc_validation_output, + trace_sc_validation_flags); + BT_PUT(packet_header_type); + BT_PUT(packet_context_type); + BT_PUT(event_header_type); + BT_PUT(stream_event_ctx_type); + + if (ret) { + /* + * This means something went wrong during the validation + * process, not that the objects are invalid. + */ + goto end; + } + + if ((trace_sc_validation_output.valid_flags & + trace_sc_validation_flags) != + trace_sc_validation_flags) { + /* Invalid trace/stream class */ + ret = -1; + goto end; + } + + if (event_class_count > 0) { + ec_validation_outputs = g_new0(struct bt_ctf_validation_output, + event_class_count); + if (!ec_validation_outputs) { + ret = -1; + goto end; + } + } + + /* 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); + struct bt_ctf_field_type *event_context_type = NULL; + struct bt_ctf_field_type *event_payload_type = NULL; + + event_context_type = + bt_ctf_event_class_get_context_type(event_class); + event_payload_type = + bt_ctf_event_class_get_payload_type(event_class); + + /* + * It is important to use the field types returned by + * the previous trace and stream class validation here + * because copies could have been made. + */ + ret = bt_ctf_validate_class_types(trace->environment, + trace_sc_validation_output.packet_header_type, + trace_sc_validation_output.packet_context_type, + trace_sc_validation_output.event_header_type, + trace_sc_validation_output.stream_event_ctx_type, + event_context_type, event_payload_type, + 1, 1, event_class->valid, &ec_validation_outputs[i], + ec_validation_flags); + BT_PUT(event_context_type); + BT_PUT(event_payload_type); + BT_PUT(event_class); + + if (ret) { + goto end; + } + + if ((ec_validation_outputs[i].valid_flags & + ec_validation_flags) != ec_validation_flags) { + /* Invalid event class */ + ret = -1; + goto end; + } + } + + stream_id = bt_ctf_stream_class_get_id(stream_class); + if (stream_id < 0) { + stream_id = trace->next_stream_id++; + + /* 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 */ + ret = -1; + goto end; + } + } + + if (bt_ctf_stream_class_set_id_no_check(stream_class, + stream_id)) { + /* TODO Should retry with a different stream id */ + ret = -1; + goto end; + } + } + + bt_object_set_parent(stream_class, trace); + g_ptr_array_add(trace->stream_classes, stream_class); + + /* + * At this point we know that the function will be successful. + * Therefore we can replace the trace and stream class field + * types with what's in their validation output structure and + * mark them as valid. We can also replace the field types of + * all the event classes of the stream class and mark them as + * valid. + */ + bt_ctf_validation_replace_types(trace, stream_class, NULL, + &trace_sc_validation_output, trace_sc_validation_flags); + trace->valid = 1; + stream_class->valid = 1; + + /* + * Put what was not moved in bt_ctf_validation_replace_types(). + */ + bt_ctf_validation_output_put_types(&trace_sc_validation_output); + + 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_validation_replace_types(NULL, NULL, event_class, + &ec_validation_outputs[i], ec_validation_flags); + event_class->valid = 1; + BT_PUT(event_class); + + /* + * Put what was not moved in + * bt_ctf_validation_replace_types(). + */ + bt_ctf_validation_output_put_types(&ec_validation_outputs[i]); + } + + /* + * All field type byte orders set as "native" byte ordering can now be + * safely set to trace's own endianness, including the stream class'. + */ + bt_ctf_field_type_set_native_byte_order(trace->packet_header_type, + trace->byte_order); + bt_ctf_stream_class_set_byte_order(stream_class, trace->byte_order); + + /* + * Freeze the trace and the stream class. + */ + bt_ctf_stream_class_freeze(stream_class); + bt_ctf_trace_freeze(trace); + + /* Notifiy listeners of the trace's schema modification. */ + bt_ctf_stream_class_visit(stream_class, + bt_ctf_trace_object_modification, trace); +end: + if (ret) { + bt_object_set_parent(stream_class, NULL); + + if (ec_validation_outputs) { + for (i = 0; i < event_class_count; i++) { + bt_ctf_validation_output_put_types( + &ec_validation_outputs[i]); + } + } + } + + g_free(ec_validation_outputs); + bt_ctf_validation_output_put_types(&trace_sc_validation_output); + bt_put(current_parent_trace); + assert(!packet_header_type); + assert(!packet_context_type); + assert(!event_header_type); + assert(!stream_event_ctx_type); + + return ret; +} + +int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace) +{ + int ret; + + if (!trace) { + ret = -1; + goto end; + } + + ret = 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 *stream_class = NULL; + + if (!trace || index < 0 || index >= trace->stream_classes->len) { + goto end; + } + + stream_class = g_ptr_array_index(trace->stream_classes, index); + bt_get(stream_class); +end: + return stream_class; +} + +struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id( + struct bt_ctf_trace *trace, uint32_t id) +{ + int i; + struct bt_ctf_stream_class *stream_class = NULL; + + if (!trace) { + goto end; + } + + for (i = 0; i < trace->stream_classes->len; i++) { + struct bt_ctf_stream_class *stream_class_candidate; + + stream_class_candidate = + g_ptr_array_index(trace->stream_classes, i); + + if (bt_ctf_stream_class_get_id(stream_class_candidate) == + (int64_t) id) { + stream_class = stream_class_candidate; + bt_get(stream_class); + goto end; + } + } + +end: + return stream_class; +} + +struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name( + struct bt_ctf_trace *trace, const char *name) +{ + size_t i; + struct bt_ctf_clock_class *clock_class = NULL; + + if (!trace || !name) { + goto end; + } + + for (i = 0; i < trace->clocks->len; i++) { + struct bt_ctf_clock_class *cur_clk = + g_ptr_array_index(trace->clocks, i); + const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk); + + if (!cur_clk_name) { + goto end; + } + + if (!strcmp(cur_clk_name, name)) { + clock_class = cur_clk; + bt_get(clock_class); + goto end; + } + } + end: - return clock; + return clock_class; } BT_HIDDEN @@ -356,25 +859,85 @@ end: return ret; } -static -void append_env_field_metadata(struct environment_variable *var, - struct metadata_context *context) -{ - g_string_append_printf(context->string, "\t%s = \"%s\";\n", - var->name->str, var->value->str); -} - static void append_env_metadata(struct bt_ctf_trace *trace, struct metadata_context *context) { - if (trace->environment->len == 0) { + int i; + int env_size; + + env_size = bt_ctf_attributes_get_count(trace->environment); + + if (env_size <= 0) { return; } g_string_append(context->string, "env {\n"); - g_ptr_array_foreach(trace->environment, - (GFunc)append_env_field_metadata, context); + + for (i = 0; i < env_size; i++) { + struct bt_value *env_field_value_obj = NULL; + const char *entry_name; + + entry_name = bt_ctf_attributes_get_field_name( + trace->environment, i); + env_field_value_obj = bt_ctf_attributes_get_field_value( + trace->environment, i); + + if (!entry_name || !env_field_value_obj) { + goto loop_next; + } + + switch (bt_value_get_type(env_field_value_obj)) { + case BT_VALUE_TYPE_INTEGER: + { + int ret; + int64_t int_value; + + ret = bt_value_integer_get(env_field_value_obj, + &int_value); + + if (ret) { + goto loop_next; + } + + g_string_append_printf(context->string, + "\t%s = %" PRId64 ";\n", entry_name, + int_value); + break; + } + case BT_VALUE_TYPE_STRING: + { + int ret; + const char *str_value; + char *escaped_str = NULL; + + ret = bt_value_string_get(env_field_value_obj, + &str_value); + + if (ret) { + goto loop_next; + } + + escaped_str = g_strescape(str_value, NULL); + + if (!escaped_str) { + goto loop_next; + } + + g_string_append_printf(context->string, + "\t%s = \"%s\";\n", entry_name, escaped_str); + free(escaped_str); + break; + } + + default: + goto loop_next; + } + +loop_next: + BT_PUT(env_field_value_obj); + } + g_string_append(context->string, "};\n\n"); } @@ -402,7 +965,7 @@ char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) } append_env_metadata(trace, context); g_ptr_array_foreach(trace->clocks, - (GFunc)bt_ctf_clock_serialize, context); + (GFunc)bt_ctf_clock_class_serialize, context); for (i = 0; i < trace->stream_classes->len; i++) { err = bt_ctf_stream_class_serialize( @@ -421,6 +984,28 @@ end: return metadata; } +enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace) +{ + enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; + + if (!trace) { + goto end; + } + + switch (trace->byte_order) { + case BIG_ENDIAN: + ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN; + break; + case LITTLE_ENDIAN: + ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN; + break; + default: + break; + } +end: + return ret; +} + int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace, enum bt_ctf_byte_order byte_order) { @@ -434,7 +1019,16 @@ int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace, switch (byte_order) { case BT_CTF_BYTE_ORDER_NATIVE: - internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ? + /* + * This doesn't make sense since the CTF specification defines + * the "native" byte order as "the byte order described in the + * trace description". However, this behavior had been + * implemented as part of v1.2 and is kept to maintain + * compatibility. + * + * This may be changed on a major version bump only. + */ + internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN; break; case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: @@ -450,9 +1044,6 @@ int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace, } trace->byte_order = internal_byte_order; - if (trace->packet_header_type) { - init_trace_packet_header(trace); - } end: return ret; } @@ -466,7 +1057,7 @@ struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type( goto end; } - bt_ctf_field_type_get(trace->packet_header_type); + bt_get(trace->packet_header_type); field_type = trace->packet_header_type; end: return field_type; @@ -477,60 +1068,168 @@ int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace, { int ret = 0; - if (!trace || !packet_header_type || trace->frozen) { + if (!trace || trace->frozen) { ret = -1; goto end; } - /* packet_header_type must be a structure */ - if (bt_ctf_field_type_get_type_id(packet_header_type) != - CTF_TYPE_STRUCT) { + /* 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) { ret = -1; goto end; } - bt_ctf_field_type_get(packet_header_type); - bt_ctf_field_type_put(trace->packet_header_type); - trace->packet_header_type = packet_header_type; + bt_put(trace->packet_header_type); + trace->packet_header_type = bt_get(packet_header_type); end: return ret; } -void bt_ctf_trace_get(struct bt_ctf_trace *trace) +static +int get_stream_class_count(void *element) { - if (!trace) { - return; + return bt_ctf_trace_get_stream_class_count( + (struct bt_ctf_trace *) element); +} + +static +void *get_stream_class(void *element, int i) +{ + return bt_ctf_trace_get_stream_class( + (struct bt_ctf_trace *) element, i); +} + +static +int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data) +{ + return bt_ctf_stream_class_visit(object, visitor, data); +} + +int bt_ctf_trace_visit(struct bt_ctf_trace *trace, + bt_ctf_visitor visitor, void *data) +{ + int ret; + struct bt_ctf_object obj = + { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE }; + + if (!trace || !visitor) { + ret = -1; + goto end; } - bt_ctf_ref_get(&trace->ref_count); + ret = visitor_helper(&obj, get_stream_class_count, + get_stream_class, visit_stream_class, visitor, data); +end: + return ret; +} + +static +int invoke_listener(struct bt_ctf_object *object, void *data) +{ + struct listener_wrapper *listener_wrapper = data; + + listener_wrapper->listener(object, listener_wrapper->data); + return 0; } -void bt_ctf_trace_put(struct bt_ctf_trace *trace) +int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace, + bt_ctf_listener_cb listener, void *listener_data) { - if (!trace) { - return; + int ret = 0; + struct listener_wrapper *listener_wrapper = + g_new0(struct listener_wrapper, 1); + + if (!trace || !listener || !listener_wrapper) { + ret = -1; + goto error; } - bt_ctf_ref_put(&trace->ref_count, bt_ctf_trace_destroy); + listener_wrapper->listener = listener; + listener_wrapper->data = listener_data; + + /* Visit the current schema. */ + ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper); + if (ret) { + goto error; + } + + /* + * Add listener to the array of callbacks which will be invoked on + * schema changes. + */ + g_ptr_array_add(trace->listeners, listener_wrapper); + return ret; +error: + g_free(listener_wrapper); + return ret; +} + +BT_HIDDEN +int bt_ctf_trace_object_modification(struct bt_ctf_object *object, + void *trace_ptr) +{ + size_t i; + struct bt_ctf_trace *trace = trace_ptr; + + assert(trace); + assert(object); + + if (trace->listeners->len == 0) { + goto end; + } + + for (i = 0; i < trace->listeners->len; i++) { + struct listener_wrapper *listener = + g_ptr_array_index(trace->listeners, i); + + listener->listener(object, listener->data); + } +end: + return 0; } BT_HIDDEN struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) { + int ret; unsigned int alignment, size; - struct bt_ctf_field_type *field_type; + struct bt_ctf_field_type *field_type = NULL; if (alias >= NR_FIELD_TYPE_ALIAS) { - return NULL; + goto end; } alignment = field_type_aliases_alignments[alias]; size = field_type_aliases_sizes[alias]; field_type = bt_ctf_field_type_integer_create(size); - bt_ctf_field_type_set_alignment(field_type, alignment); + ret = bt_ctf_field_type_set_alignment(field_type, alignment); + if (ret) { + BT_PUT(field_type); + } +end: return field_type; } +static +void bt_ctf_trace_freeze(struct bt_ctf_trace *trace) +{ + int i; + + bt_ctf_field_type_freeze(trace->packet_header_type); + bt_ctf_attributes_freeze(trace->environment); + + for (i = 0; i < trace->clocks->len; i++) { + struct bt_ctf_clock_class *clock_class = + g_ptr_array_index(trace->clocks, i); + + bt_ctf_clock_class_freeze(clock_class); + } + + trace->frozen = 1; +} + static int init_trace_packet_header(struct bt_ctf_trace *trace) { @@ -550,14 +1249,6 @@ int init_trace_packet_header(struct bt_ctf_trace *trace) goto end; } - ret = bt_ctf_field_type_set_byte_order(_uint32_t, - (trace->byte_order == LITTLE_ENDIAN ? - BT_CTF_BYTE_ORDER_LITTLE_ENDIAN : - BT_CTF_BYTE_ORDER_BIG_ENDIAN)); - if (ret) { - goto end; - } - ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, _uint32_t, "magic"); if (ret) { @@ -576,25 +1267,17 @@ int init_trace_packet_header(struct bt_ctf_trace *trace) goto end; } - bt_ctf_field_type_put(trace->packet_header_type); - trace->packet_header_type = trace_packet_header_type; -end: - bt_ctf_field_type_put(uuid_array_type); - bt_ctf_field_type_put(_uint32_t); - bt_ctf_field_type_put(_uint8_t); - bt_ctf_field_put(magic); - bt_ctf_field_put(uuid_array); + ret = bt_ctf_trace_set_packet_header_type(trace, + trace_packet_header_type); if (ret) { - bt_ctf_field_type_put(trace_packet_header_type); + goto end; } - +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; } - -static -void environment_variable_destroy(struct environment_variable *var) -{ - g_string_free(var->name, TRUE); - g_string_free(var->value, TRUE); - g_free(var); -}