From 0c1fafedfae2efbae9a914ef676cb39879099921 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Mon, 19 Feb 2018 17:32:52 -0500 Subject: [PATCH] Fix: call bt_stream_class_map_clock_class() in bt_event_create() Issue ===== It is possible, for backward compatibility, to create an event before it is appended to a CTF writer stream. This means that, when bt_ctf_writer_create_stream() is called, bt_event_create() is already called. Because bt_ctf_writer_create_stream() calls bt_trace_add_stream_class(), which calls bt_stream_class_map_clock_class() to map selected fields to the stream class's clock's class, this is skipped in this use case and selected fields are never mapped. The problematic sequence is: 1. Create a stream class. 2. Set this stream class's CTF writer clock. 3. Create an event with bt_event_create(). 4. Create a CTF writer stream with bt_ctf_writer_create_stream(). 5. Append the event (3.) to the CTF writer stream (4.) with bt_stream_append_event(). In 5., the event header's `timestamp` field is not automatically populated from the current value of the stream class's clock because its field type was not automatically mapped to a clock class. Solution ======== After validating the field types in bt_event_create(), call bt_stream_class_map_clock_class() on the validated packet context and event header field types. bt_stream_class_map_clock_class() only performs automatic mapping when the stream class has a registered CTF writer clock, which eventually guarantees that this is a CTF writer stream class because it is forbidden to call bt_trace_add_stream_class() with such a stream class when the trace was not created by a CTF writer. In other words, this additional bt_stream_class_map_clock_class() in bt_event_create() does NOT affect non-CTF writer objects. Known drawbacks =============== None. Signed-off-by: Philippe Proulx --- lib/ctf-ir/event.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/ctf-ir/event.c b/lib/ctf-ir/event.c index fd456374..64385644 100644 --- a/lib/ctf-ir/event.c +++ b/lib/ctf-ir/event.c @@ -185,6 +185,19 @@ struct bt_event *bt_event_create(struct bt_event_class *event_class) goto error; } + /* + * Safe to automatically map selected fields to the stream's + * clock's class here because the stream class is about to be + * frozen. + */ + if (bt_stream_class_map_clock_class(stream_class, + validation_output.packet_context_type, + validation_output.event_header_type)) { + BT_LOGW_STR("Cannot automatically map selected stream class's " + "field types to stream class's clock's class."); + goto error; + } + /* * At this point we know the trace (if associated to the stream * class), the stream class, and the event class, with their -- 2.34.1