From 29664b2a3a15c7233d916887d2f58fc42e18521e Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Mon, 13 Apr 2015 18:14:59 -0400 Subject: [PATCH] Fix: ir: make sure "stream_id" attr is always right MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Make sure the "stream_id" attribute of all the event classes of a given stream class is updated at the following places: * user sets the stream class ID manually: calling bt_ctf_stream_class_set_id() * stream class ID is automatically set: in bt_ctf_trace_add_stream_class() * an event class is added to an existing stream class: in bt_ctf_stream_class_add_event_class() Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- formats/ctf/ir/event.c | 40 ++++++++------- formats/ctf/ir/stream-class.c | 51 +++++++++++++++++-- formats/ctf/ir/trace.c | 3 +- include/babeltrace/ctf-ir/event-internal.h | 4 ++ .../babeltrace/ctf-ir/stream-class-internal.h | 4 ++ 5 files changed, 79 insertions(+), 23 deletions(-) diff --git a/formats/ctf/ir/event.c b/formats/ctf/ir/event.c index 2d15c834..db55b26b 100644 --- a/formats/ctf/ir/event.c +++ b/formats/ctf/ir/event.c @@ -526,10 +526,31 @@ void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class) bt_ctf_ref_put(&event_class->ref_count, bt_ctf_event_class_destroy); } +BT_HIDDEN +int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class, + uint32_t stream_id) +{ + int ret = 0; + struct bt_object *obj; + + obj = bt_object_integer_create_init(stream_id); + + if (!obj) { + ret = -1; + goto end; + } + + ret = bt_ctf_attributes_set_field_value(event_class->attributes, + "stream_id", obj); + +end: + BT_OBJECT_PUT(obj); + + return ret; +} + struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class) { - int ret; - struct bt_object *obj = NULL; struct bt_ctf_event *event = NULL; if (!event_class) { @@ -546,21 +567,6 @@ struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class) goto end; } assert(event_class->stream_class->event_header_type); - - /* set "stream_id" attribute now that we know its value */ - obj = bt_object_integer_create_init(event_class->stream_class->id); - if (!obj) { - goto end; - } - - ret = bt_ctf_attributes_set_field_value(event_class->attributes, - "stream_id", obj); - BT_OBJECT_PUT(obj); - - if (ret) { - goto end; - } - event = g_new0(struct bt_ctf_event, 1); if (!event) { goto end; diff --git a/formats/ctf/ir/stream-class.c b/formats/ctf/ir/stream-class.c index 572d7424..aba76cd1 100644 --- a/formats/ctf/ir/stream-class.c +++ b/formats/ctf/ir/stream-class.c @@ -219,13 +219,36 @@ int _bt_ctf_stream_class_set_id( return 0; } -int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, - uint32_t id) +struct event_class_set_stream_id_data { + uint32_t stream_id; + int ret; +}; + +static +void event_class_set_stream_id(gpointer event_class, gpointer data) +{ + struct event_class_set_stream_id_data *typed_data = data; + + typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class, + typed_data->stream_id); +} + +BT_HIDDEN +int bt_ctf_stream_class_set_id_no_check( + struct bt_ctf_stream_class *stream_class, uint32_t id) { int ret = 0; + struct event_class_set_stream_id_data data = + { .stream_id = id, .ret = 0 }; - if (!stream_class || stream_class->frozen) { - ret = -1; + /* + * Make sure all event classes have their "stream_id" attribute + * set to this value. + */ + g_ptr_array_foreach(stream_class->event_classes, + event_class_set_stream_id, &data); + ret = data.ret; + if (ret) { goto end; } @@ -237,6 +260,21 @@ end: return ret; } +int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, + uint32_t id) +{ + int ret = 0; + + if (!stream_class || stream_class->frozen) { + ret = -1; + goto end; + } + + ret = bt_ctf_stream_class_set_id_no_check(stream_class, id); +end: + return ret; +} + static void event_class_exists(gpointer element, gpointer query) { @@ -317,6 +355,11 @@ int bt_ctf_stream_class_add_event_class( goto end; } + ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id); + if (ret) { + goto end; + } + bt_ctf_event_class_get(event_class); g_ptr_array_add(stream_class->event_classes, event_class); bt_ctf_event_class_freeze(event_class); diff --git a/formats/ctf/ir/trace.c b/formats/ctf/ir/trace.c index 94a332d1..5abfcc79 100644 --- a/formats/ctf/ir/trace.c +++ b/formats/ctf/ir/trace.c @@ -462,8 +462,7 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, } } - if (_bt_ctf_stream_class_set_id(stream_class, - stream_id)) { + 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; diff --git a/include/babeltrace/ctf-ir/event-internal.h b/include/babeltrace/ctf-ir/event-internal.h index 2a172a94..5201ee53 100644 --- a/include/babeltrace/ctf-ir/event-internal.h +++ b/include/babeltrace/ctf-ir/event-internal.h @@ -81,6 +81,10 @@ void bt_ctf_event_class_set_native_byte_order( struct bt_ctf_event_class *event_class, int byte_order); +BT_HIDDEN +int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class, + uint32_t stream_id); + BT_HIDDEN int bt_ctf_event_validate(struct bt_ctf_event *event); diff --git a/include/babeltrace/ctf-ir/stream-class-internal.h b/include/babeltrace/ctf-ir/stream-class-internal.h index 3b368d1d..c88be91c 100644 --- a/include/babeltrace/ctf-ir/stream-class-internal.h +++ b/include/babeltrace/ctf-ir/stream-class-internal.h @@ -70,6 +70,10 @@ BT_HIDDEN int _bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, uint32_t id); +BT_HIDDEN +int bt_ctf_stream_class_set_id_no_check( + struct bt_ctf_stream_class *stream_class, uint32_t id); + BT_HIDDEN int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class *stream_class, struct bt_ctf_trace *trace); -- 2.34.1