Make API CTF-agnostic
[babeltrace.git] / lib / ctf-ir / trace.c
index ba18eab8de35525873fcafe061438570e2718c43..69189c44cf8df64a198ffa3b170e5d5a36cb9064 100644 (file)
  * SOFTWARE.
  */
 
+#define BT_LOG_TAG "TRACE"
+#include <babeltrace/lib-logging-internal.h>
+
+#include <babeltrace/assert-pre-internal.h>
 #include <babeltrace/ctf-ir/trace-internal.h>
 #include <babeltrace/ctf-ir/clock-class-internal.h>
 #include <babeltrace/ctf-ir/stream-internal.h>
 #include <babeltrace/ctf-ir/event-class-internal.h>
 #include <babeltrace/ctf-writer/functor-internal.h>
 #include <babeltrace/ctf-writer/clock-internal.h>
+#include <babeltrace/ctf-ir/field-wrapper-internal.h>
 #include <babeltrace/ctf-ir/field-types-internal.h>
 #include <babeltrace/ctf-ir/attributes-internal.h>
-#include <babeltrace/ctf-ir/validation-internal.h>
-#include <babeltrace/ctf-ir/visitor-internal.h>
-#include <babeltrace/ctf-ir/utils.h>
+#include <babeltrace/ctf-ir/utils-internal.h>
+#include <babeltrace/ctf-ir/resolve-field-path-internal.h>
 #include <babeltrace/compiler-internal.h>
 #include <babeltrace/values.h>
+#include <babeltrace/values-internal.h>
 #include <babeltrace/ref.h>
+#include <babeltrace/types.h>
 #include <babeltrace/endian-internal.h>
+#include <babeltrace/assert-internal.h>
+#include <babeltrace/compat/glib-internal.h>
 #include <inttypes.h>
 #include <stdint.h>
 #include <string.h>
+#include <stdlib.h>
 
-#define DEFAULT_IDENTIFIER_SIZE 128
-#define DEFAULT_METADATA_STRING_SIZE 4096
-
-struct listener_wrapper {
-       bt_ctf_listener_cb listener;
+struct bt_trace_is_static_listener_elem {
+       bt_trace_is_static_listener func;
+       bt_trace_listener_removed removed;
        void *data;
 };
 
-static
-void bt_ctf_trace_destroy(struct bt_object *obj);
-static
-void bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
-
-static
-const unsigned int field_type_aliases_alignments[] = {
-       [FIELD_TYPE_ALIAS_UINT5_T] = 1,
-       [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8,
-       [FIELD_TYPE_ALIAS_UINT27_T] = 1,
-       [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8,
-};
+#define BT_ASSERT_PRE_TRACE_HOT(_trace) \
+       BT_ASSERT_PRE_HOT((_trace), "Trace", ": %!+t", (_trace))
 
 static
-const unsigned int field_type_aliases_sizes[] = {
-       [FIELD_TYPE_ALIAS_UINT5_T] = 5,
-       [FIELD_TYPE_ALIAS_UINT8_T] = 8,
-       [FIELD_TYPE_ALIAS_UINT16_T] = 16,
-       [FIELD_TYPE_ALIAS_UINT27_T] = 27,
-       [FIELD_TYPE_ALIAS_UINT32_T] = 32,
-       [FIELD_TYPE_ALIAS_UINT64_T] = 64,
-};
-
-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) {
-               goto error;
-       }
-
-       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);
-       trace->streams = g_ptr_array_new_with_free_func(
-               (GDestroyNotify) bt_object_release);
-       trace->stream_classes = g_ptr_array_new_with_free_func(
-               (GDestroyNotify) bt_object_release);
-       if (!trace->clocks || !trace->stream_classes || !trace->streams) {
-               goto error;
-       }
-
-       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) {
-               goto error;
-       }
-
-       trace->listeners = g_ptr_array_new_with_free_func(
-                       (GDestroyNotify) g_free);
-       if (!trace->listeners) {
-               goto error;
-       }
-
-       return trace;
-
-error:
-       BT_PUT(trace);
-       bt_put(packet_header_type);
-       return trace;
-}
-
-const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace)
+void destroy_trace(struct bt_object *obj)
 {
-       const char *name = NULL;
-
-       if (!trace || !trace->name) {
-               goto end;
-       }
-
-       name = trace->name->str;
-end:
-       return name;
-}
+       struct bt_trace *trace = (void *) obj;
 
-int bt_ctf_trace_set_name(struct bt_ctf_trace *trace, const char *name)
-{
-       int ret = 0;
+       BT_LIB_LOGD("Destroying trace object: %!+t", trace);
 
-       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;
-}
+       /*
+        * Call remove listeners first so that everything else still
+        * exists in the trace.
+        */
+       if (trace->is_static_listeners) {
+               size_t i;
 
-const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace *trace)
-{
-       return trace && trace->uuid_set ? trace->uuid : NULL;
-}
+               for (i = 0; i < trace->is_static_listeners->len; i++) {
+                       struct bt_trace_is_static_listener_elem elem =
+                               g_array_index(trace->is_static_listeners,
+                                       struct bt_trace_is_static_listener_elem, i);
 
-int bt_ctf_trace_set_uuid(struct bt_ctf_trace *trace, const unsigned char *uuid)
-{
-       int ret = 0;
+                       if (elem.removed) {
+                               elem.removed(trace, elem.data);
+                       }
+               }
 
-       if (!trace || !uuid || trace->frozen) {
-               ret = -1;
-               goto end;
+               g_array_free(trace->is_static_listeners, TRUE);
        }
 
-       memcpy(trace->uuid, uuid, sizeof(uuid_t));
-       trace->uuid_set = true;
-
-end:
-       return ret;
-}
-
-void bt_ctf_trace_destroy(struct bt_object *obj)
-{
-       struct bt_ctf_trace *trace;
+       bt_object_pool_finalize(&trace->packet_header_field_pool);
 
-       trace = container_of(obj, struct bt_ctf_trace, base);
        if (trace->environment) {
-               bt_ctf_attributes_destroy(trace->environment);
+               BT_LOGD_STR("Destroying environment attributes.");
+               bt_attributes_destroy(trace->environment);
        }
 
-       if (trace->name) {
-               g_string_free(trace->name, TRUE);
-       }
-
-       if (trace->clocks) {
-               g_ptr_array_free(trace->clocks, TRUE);
+       if (trace->name.str) {
+               g_string_free(trace->name.str, 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);
        }
 
-       if (trace->listeners) {
-               g_ptr_array_free(trace->listeners, TRUE);
+       if (trace->stream_classes_stream_count) {
+               g_hash_table_destroy(trace->stream_classes_stream_count);
        }
 
-       bt_put(trace->packet_header_type);
+       BT_LOGD_STR("Putting packet header field type.");
+       bt_put(trace->packet_header_ft);
        g_free(trace);
 }
 
-int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
-               const char *name, struct bt_value *value)
+static
+void free_packet_header_field(struct bt_field_wrapper *field_wrapper,
+               struct bt_trace *trace)
 {
-       int ret = 0;
-
-       if (!trace || !name || !value ||
-               bt_ctf_validate_identifier(name) ||
-               !(bt_value_is_integer(value) || bt_value_is_string(value))) {
-               ret = -1;
-               goto end;
-       }
-
-       if (strchr(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.
-                *
-                * 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);
-       }
-
-       ret = bt_ctf_attributes_set_field_value(trace->environment, name,
-               value);
-
-end:
-       return ret;
+       bt_field_wrapper_destroy(field_wrapper);
 }
 
-int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
-               const char *name, const char *value)
+struct bt_trace *bt_trace_create(void)
 {
-       int ret = 0;
-       struct bt_value *env_value_string_obj = NULL;
+       struct bt_trace *trace = NULL;
+       int ret;
 
-       if (!trace || !name || !value) {
-               ret = -1;
-               goto end;
+       BT_LOGD_STR("Creating default trace object.");
+       trace = g_new0(struct bt_trace, 1);
+       if (!trace) {
+               BT_LOGE_STR("Failed to allocate one trace.");
+               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;
-               }
+       bt_object_init_shared_with_parent(&trace->base, destroy_trace);
+       trace->streams = g_ptr_array_new_with_free_func(
+               (GDestroyNotify) bt_object_try_spec_release);
+       if (!trace->streams) {
+               BT_LOGE_STR("Failed to allocate one GPtrArray.");
+               goto error;
        }
 
-       env_value_string_obj = bt_value_string_create_init(value);
-
-       if (!env_value_string_obj) {
-               ret = -1;
-               goto end;
+       trace->stream_classes = g_ptr_array_new_with_free_func(
+               (GDestroyNotify) bt_object_try_spec_release);
+       if (!trace->stream_classes) {
+               BT_LOGE_STR("Failed to allocate one GPtrArray.");
+               goto error;
        }
 
-       if (trace->frozen) {
-               bt_value_freeze(env_value_string_obj);
+       trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash,
+               g_direct_equal);
+       if (!trace->stream_classes_stream_count) {
+               BT_LOGE_STR("Failed to allocate one GHashTable.");
+               goto error;
        }
-       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_set_environment_field_integer(struct bt_ctf_trace *trace,
-               const char *name, int64_t value)
-{
-       int ret = 0;
-       struct bt_value *env_value_integer_obj = NULL;
-
-       if (!trace || !name) {
-               ret = -1;
-               goto end;
+       trace->name.str = g_string_new(NULL);
+       if (!trace->name.str) {
+               BT_LOGE_STR("Failed to allocate one GString.");
+               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;
-               }
+       trace->environment = bt_attributes_create();
+       if (!trace->environment) {
+               BT_LOGE_STR("Cannot create empty attributes object.");
+               goto error;
        }
 
-       env_value_integer_obj = bt_value_integer_create_init(value);
-       if (!env_value_integer_obj) {
-               ret = -1;
-               goto end;
+       trace->is_static_listeners = g_array_new(FALSE, TRUE,
+               sizeof(struct bt_trace_is_static_listener_elem));
+       if (!trace->is_static_listeners) {
+               BT_LOGE_STR("Failed to allocate one GArray.");
+               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);
+       trace->assigns_automatic_stream_class_id = true;
+       ret = bt_object_pool_initialize(&trace->packet_header_field_pool,
+               (bt_object_pool_new_object_func) bt_field_wrapper_new,
+               (bt_object_pool_destroy_object_func) free_packet_header_field,
+               trace);
+       if (ret) {
+               BT_LOGE("Failed to initialize packet header field pool: ret=%d",
+                       ret);
+               goto error;
        }
-end:
-       BT_PUT(env_value_integer_obj);
-       return ret;
-}
 
-int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
-{
-       int64_t ret = 0;
-
-       if (!trace) {
-               ret = (int64_t) -1;
-               goto end;
-       }
+       BT_LIB_LOGD("Created trace object: %!+t", trace);
+       goto end;
 
-       ret = bt_ctf_attributes_get_count(trace->environment);
+error:
+       BT_PUT(trace);
 
 end:
-       return ret;
+       return trace;
 }
 
-const char *
-bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace *trace,
-               uint64_t index)
+const char *bt_trace_get_name(struct bt_trace *trace)
 {
-       const char *ret = NULL;
-
-       if (!trace) {
-               goto end;
-       }
-
-       ret = bt_ctf_attributes_get_field_name(trace->environment, index);
-
-end:
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       return trace->name.value;
 }
 
-struct bt_value *bt_ctf_trace_get_environment_field_value_by_index(
-               struct bt_ctf_trace *trace, uint64_t index)
+int bt_trace_set_name(struct bt_trace *trace, const char *name)
 {
-       struct bt_value *ret = NULL;
-
-       if (!trace) {
-               goto end;
-       }
-
-       ret = bt_ctf_attributes_get_field_value(trace->environment, index);
-
-end:
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       BT_ASSERT_PRE_NON_NULL(name, "Name");
+       BT_ASSERT_PRE_TRACE_HOT(trace);
+       g_string_assign(trace->name.str, name);
+       trace->name.value = trace->name.str->str;
+       BT_LIB_LOGV("Set trace's name: %!+t", trace);
+       return 0;
 }
 
-struct bt_value *bt_ctf_trace_get_environment_field_value_by_name(
-               struct bt_ctf_trace *trace, const char *name)
+bt_uuid bt_trace_get_uuid(struct bt_trace *trace)
 {
-       struct bt_value *ret = NULL;
-
-       if (!trace || !name) {
-               goto end;
-       }
-
-       ret = bt_ctf_attributes_get_field_value_by_name(trace->environment,
-               name);
-
-end:
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       return trace->uuid.value;
 }
 
-int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace,
-               struct bt_ctf_clock_class *clock_class)
+int bt_trace_set_uuid(struct bt_trace *trace, bt_uuid uuid)
 {
-       int ret = 0;
-
-       if (!trace || trace->is_static ||
-                       !bt_ctf_clock_class_is_valid(clock_class)) {
-               ret = -1;
-               goto end;
-       }
-
-       /* Check for duplicate clock classes */
-       if (bt_ctf_trace_has_clock_class(trace, clock_class)) {
-               ret = -1;
-               goto end;
-       }
-
-       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;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
+       BT_ASSERT_PRE_TRACE_HOT(trace);
+       memcpy(trace->uuid.uuid, uuid, BABELTRACE_UUID_LEN);
+       trace->uuid.value = trace->uuid.uuid;
+       BT_LIB_LOGV("Set trace's UUID: %!+t", trace);
+       return 0;
 }
 
-int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
+BT_ASSERT_FUNC
+static
+bool trace_has_environment_entry(struct bt_trace *trace, const char *name)
 {
-       int64_t ret = (int64_t) -1;
+       struct bt_value *attribute;
 
-       if (!trace) {
-               goto end;
-       }
+       BT_ASSERT(trace);
 
-       ret = trace->clocks->len;
-end:
-       return ret;
+       attribute = bt_attributes_borrow_field_value_by_name(
+               trace->environment, name);
+       return attribute != NULL;
 }
 
-struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index(
-               struct bt_ctf_trace *trace, uint64_t index)
+static
+int set_environment_entry(struct bt_trace *trace, const char *name,
+               struct bt_value *value)
 {
-       struct bt_ctf_clock_class *clock_class = NULL;
+       int ret;
 
-       if (!trace || index >= trace->clocks->len) {
-               goto end;
+       BT_ASSERT(trace);
+       BT_ASSERT(name);
+       BT_ASSERT(value);
+       BT_ASSERT_PRE(!trace->frozen ||
+               !trace_has_environment_entry(trace, name),
+               "Trace is frozen: cannot replace environment entry: "
+               "%![trace-]+t, entry-name=\"%s\"", trace, name);
+       ret = bt_attributes_set_field_value(trace->environment, name,
+               value);
+       bt_value_freeze(value);
+       if (ret) {
+               BT_LIB_LOGE("Cannot set trace's environment entry: "
+                       "%![trace-]+t, entry-name=\"%s\"", trace, name);
+       } else {
+               BT_LIB_LOGV("Set trace's environment entry: "
+                       "%![trace-]+t, entry-name=\"%s\"", trace, name);
        }
 
-       clock_class = g_ptr_array_index(trace->clocks, index);
-       bt_get(clock_class);
-end:
-       return clock_class;
+       return ret;
 }
 
-int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
-               struct bt_ctf_stream_class *stream_class)
+int bt_trace_set_environment_entry_string(struct bt_trace *trace,
+               const char *name, const char *value)
 {
        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;
-       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;
-       int64_t event_class_count;
-       struct bt_ctf_trace *current_parent_trace = NULL;
-
-       if (!trace || !stream_class || trace->is_static) {
-               ret = -1;
-               goto end;
-       }
-
-       /*
-        * 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) {
-               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. */
+       struct bt_value *value_obj;
+
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       BT_ASSERT_PRE_NON_NULL(name, "Name");
+       BT_ASSERT_PRE_NON_NULL(value, "Value");
+       value_obj = bt_value_string_create_init(value);
+       if (!value_obj) {
+               BT_LOGE_STR("Cannot create a string value object.");
                ret = -1;
                goto end;
        }
 
-       event_class_count =
-               bt_ctf_stream_class_get_event_class_count(stream_class);
-       assert(event_class_count >= 0);
+       /* set_environment_entry() logs errors */
+       ret = set_environment_entry(trace, name, value_obj);
 
-       /* 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_by_index(
-                               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++;
-               if (stream_id < 0) {
-                       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 */
-                               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_by_index(
-                               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]);
-       }
-
-       /*
-        * 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);
-
+       bt_put(value_obj);
        return ret;
 }
 
-int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace)
+int bt_trace_set_environment_entry_integer(
+               struct bt_trace *trace, const char *name, int64_t value)
 {
-       int64_t ret;
+       int ret;
+       struct bt_value *value_obj;
 
-       if (!trace) {
-               ret = (int64_t) -1;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       BT_ASSERT_PRE_NON_NULL(name, "Name");
+       value_obj = bt_value_integer_create_init(value);
+       if (!value_obj) {
+               BT_LOGE_STR("Cannot create an integer value object.");
+               ret = -1;
                goto end;
        }
 
-       ret = (int64_t) trace->streams->len;
+       /* set_environment_entry() logs errors */
+       ret = set_environment_entry(trace, name, value_obj);
 
 end:
+       bt_put(value_obj);
        return ret;
 }
 
-struct bt_ctf_stream *bt_ctf_trace_get_stream_by_index(
-               struct bt_ctf_trace *trace,
-               uint64_t index)
+uint64_t bt_trace_get_environment_entry_count(struct bt_trace *trace)
 {
-       struct bt_ctf_stream *stream = NULL;
-
-       if (!trace || index >= trace->streams->len) {
-               goto end;
-       }
-
-       stream = bt_get(g_ptr_array_index(trace->streams, index));
+       int64_t ret;
 
-end:
-       return stream;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       ret = bt_attributes_get_count(trace->environment);
+       BT_ASSERT(ret >= 0);
+       return (uint64_t) ret;
 }
 
-int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
+void bt_trace_borrow_environment_entry_by_index(
+               struct bt_trace *trace, uint64_t index,
+               const char **name, struct bt_value **value)
 {
-       int64_t ret;
-
-       if (!trace) {
-               ret = (int64_t) -1;
-               goto end;
-       }
-
-       ret = (int64_t) trace->stream_classes->len;
-end:
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       BT_ASSERT_PRE_NON_NULL(name, "Name");
+       BT_ASSERT_PRE_NON_NULL(value, "Value");
+       BT_ASSERT_PRE_VALID_INDEX(index,
+               bt_attributes_get_count(trace->environment));
+       *value = bt_attributes_borrow_field_value(trace->environment, index);
+       BT_ASSERT(*value);
+       *name = bt_attributes_get_field_name(trace->environment, index);
+       BT_ASSERT(*name);
 }
 
-struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_index(
-               struct bt_ctf_trace *trace, uint64_t index)
+struct bt_value *bt_trace_borrow_environment_entry_value_by_name(
+               struct bt_trace *trace, const char *name)
 {
-       struct bt_ctf_stream_class *stream_class = NULL;
-
-       if (!trace || 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;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       BT_ASSERT_PRE_NON_NULL(name, "Name");
+       return bt_attributes_borrow_field_value_by_name(trace->environment,
+               name);
 }
 
-struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
-               struct bt_ctf_trace *trace, uint64_t id_param)
+uint64_t bt_trace_get_stream_count(struct bt_trace *trace)
 {
-       int i;
-       struct bt_ctf_stream_class *stream_class = NULL;
-       int64_t id = (int64_t) id_param;
-
-       if (!trace || id < 0) {
-               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;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       return (uint64_t) trace->streams->len;
 }
 
-struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
-               struct bt_ctf_trace *trace, const char *name)
+struct bt_stream *bt_trace_borrow_stream_by_index(
+               struct bt_trace *trace, uint64_t index)
 {
-       size_t i;
-       struct bt_ctf_clock_class *clock_class = NULL;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       BT_ASSERT_PRE_VALID_INDEX(index, trace->streams->len);
+       return g_ptr_array_index(trace->streams, index);
+}
 
-       if (!trace || !name) {
-               goto end;
-       }
+struct bt_stream *bt_trace_borrow_stream_by_id(
+               struct bt_trace *trace, uint64_t id)
+{
+       struct bt_stream *stream = NULL;
+       uint64_t i;
 
-       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);
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
 
-               if (!cur_clk_name) {
-                       goto end;
-               }
+       for (i = 0; i < trace->streams->len; i++) {
+               struct bt_stream *stream_candidate =
+                       g_ptr_array_index(trace->streams, i);
 
-               if (!strcmp(cur_clk_name, name)) {
-                       clock_class = cur_clk;
-                       bt_get(clock_class);
+               if (stream_candidate->id == id) {
+                       stream = stream_candidate;
                        goto end;
                }
        }
 
 end:
-       return clock_class;
+       return stream;
 }
 
-BT_HIDDEN
-bool bt_ctf_trace_has_clock_class(struct bt_ctf_trace *trace,
-               struct bt_ctf_clock_class *clock_class)
+uint64_t bt_trace_get_stream_class_count(struct bt_trace *trace)
 {
-       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_ASSERT_PRE_NON_NULL(trace, "Trace");
+       return (uint64_t) trace->stream_classes->len;
 }
 
-BT_HIDDEN
-const char *get_byte_order_string(enum bt_ctf_byte_order byte_order)
+struct bt_stream_class *bt_trace_borrow_stream_class_by_index(
+               struct bt_trace *trace, uint64_t index)
 {
-       const char *string;
-
-       switch (byte_order) {
-       case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
-               string = "le";
-               break;
-       case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
-               string = "be";
-               break;
-       case BT_CTF_BYTE_ORDER_NATIVE:
-               string = "native";
-               break;
-       default:
-               assert(false);
-       }
-
-       return string;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       BT_ASSERT_PRE_VALID_INDEX(index, trace->stream_classes->len);
+       return g_ptr_array_index(trace->stream_classes, index);
 }
 
-static
-int append_trace_metadata(struct bt_ctf_trace *trace,
-               struct metadata_context *context)
+struct bt_stream_class *bt_trace_borrow_stream_class_by_id(
+               struct bt_trace *trace, uint64_t id)
 {
-       unsigned char *uuid = trace->uuid;
-       int ret;
-
-       if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
-               ret = -1;
-               goto end;
-       }
+       struct bt_stream_class *stream_class = NULL;
+       uint64_t i;
 
-       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);
-
-       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]);
-       }
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
 
-       g_string_append_printf(context->string, "\tbyte_order = %s;\n",
-               get_byte_order_string(trace->native_byte_order));
+       for (i = 0; i < trace->stream_classes->len; i++) {
+               struct bt_stream_class *stream_class_candidate =
+                       g_ptr_array_index(trace->stream_classes, i);
 
-       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 (stream_class_candidate->id == id) {
+                       stream_class = stream_class_candidate;
+                       goto end;
+               }
        }
-       context->current_indentation_level--;
 
-       g_string_append(context->string, ";\n};\n\n");
 end:
-       return ret;
+       return stream_class;
 }
 
-static
-void append_env_metadata(struct bt_ctf_trace *trace,
-               struct metadata_context *context)
+struct bt_field_type *bt_trace_borrow_packet_header_field_type(
+               struct bt_trace *trace)
 {
-       int64_t i;
-       int64_t env_size;
-
-       env_size = bt_ctf_attributes_get_count(trace->environment);
-
-       if (env_size <= 0) {
-               return;
-       }
-
-       g_string_append(context->string, "env {\n");
-
-       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");
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       return trace->packet_header_ft;
 }
 
-char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
+int bt_trace_set_packet_header_field_type(struct bt_trace *trace,
+               struct bt_field_type *field_type)
 {
-       char *metadata = NULL;
-       struct metadata_context *context = NULL;
-       int err = 0;
-       size_t i;
-
-       if (!trace) {
-               goto end;
-       }
-
-       context = g_new0(struct metadata_context, 1);
-       if (!context) {
+       int ret;
+       struct bt_resolve_field_path_context resolve_ctx = {
+               .packet_header = field_type,
+               .packet_context = NULL,
+               .event_header = NULL,
+               .event_common_context = NULL,
+               .event_specific_context = NULL,
+               .event_payload = NULL,
+       };
+
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       BT_ASSERT_PRE_NON_NULL(field_type, "Field type");
+       BT_ASSERT_PRE_TRACE_HOT(trace);
+       BT_ASSERT_PRE(bt_field_type_get_type_id(field_type) ==
+               BT_FIELD_TYPE_ID_STRUCTURE,
+               "Packet header field type is not a structure field type: %!+F",
+               field_type);
+       ret = bt_resolve_field_paths(field_type, &resolve_ctx);
+       if (ret) {
                goto end;
        }
 
-       context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
-       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)) {
-               goto error;
-       }
-       append_env_metadata(trace, context);
-       g_ptr_array_foreach(trace->clocks,
-               (GFunc)bt_ctf_clock_class_serialize, context);
-
-       for (i = 0; i < trace->stream_classes->len; i++) {
-               err = bt_ctf_stream_class_serialize(
-                       trace->stream_classes->pdata[i], context);
-               if (err) {
-                       goto error;
-               }
-       }
+       bt_field_type_make_part_of_trace(field_type);
+       bt_put(trace->packet_header_ft);
+       trace->packet_header_ft = bt_get(field_type);
+       bt_field_type_freeze(field_type);
+       BT_LIB_LOGV("Set trace's packet header field type: %!+t", trace);
 
-       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;
+       return ret;
 }
 
-enum bt_ctf_byte_order bt_ctf_trace_get_native_byte_order(
-               struct bt_ctf_trace *trace)
+bt_bool bt_trace_is_static(struct bt_trace *trace)
 {
-       enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
-
-       if (!trace) {
-               goto end;
-       }
-
-       ret = trace->native_byte_order;
-
-end:
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       return (bt_bool) trace->is_static;
 }
 
-int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace,
-               enum bt_ctf_byte_order byte_order)
+int bt_trace_make_static(struct bt_trace *trace)
 {
-       int ret = 0;
+       uint64_t i;
 
-       if (!trace || trace->frozen) {
-               ret = -1;
-               goto end;
-       }
-
-       if (byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN &&
-                       byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN &&
-                       byte_order != BT_CTF_BYTE_ORDER_NETWORK) {
-               ret = -1;
-               goto end;
-       }
-
-       trace->native_byte_order = byte_order;
-
-end:
-       return ret;
-}
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       trace->is_static = true;
+       bt_trace_freeze(trace);
+       BT_LIB_LOGV("Trace is now static: %!+t", trace);
 
-struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type(
-               struct bt_ctf_trace *trace)
-{
-       struct bt_ctf_field_type *field_type = NULL;
+       /* Call all the "trace is static" listeners */
+       for (i = 0; i < trace->is_static_listeners->len; i++) {
+               struct bt_trace_is_static_listener_elem elem =
+                       g_array_index(trace->is_static_listeners,
+                               struct bt_trace_is_static_listener_elem, i);
 
-       if (!trace) {
-               goto end;
+               if (elem.func) {
+                       elem.func(trace, elem.data);
+               }
        }
 
-       bt_get(trace->packet_header_type);
-       field_type = trace->packet_header_type;
-end:
-       return field_type;
+       return 0;
 }
 
-int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
-               struct bt_ctf_field_type *packet_header_type)
-{
-       int ret = 0;
-
-       if (!trace || trace->frozen) {
-               ret = -1;
-               goto end;
+int bt_trace_add_is_static_listener(struct bt_trace *trace,
+               bt_trace_is_static_listener listener,
+               bt_trace_listener_removed listener_removed, void *data,
+               uint64_t *listener_id)
+{
+       uint64_t i;
+       struct bt_trace_is_static_listener_elem new_elem = {
+               .func = listener,
+               .removed = listener_removed,
+               .data = data,
+       };
+
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       BT_ASSERT_PRE_NON_NULL(listener, "Listener");
+       BT_ASSERT_PRE(!trace->is_static,
+               "Trace is already static: %!+t", trace);
+       BT_ASSERT_PRE(trace->in_remove_listener,
+               "Cannot call this function while executing a "
+               "remove listener: %!+t", trace);
+
+       /* Find the next available spot */
+       for (i = 0; i < trace->is_static_listeners->len; i++) {
+               struct bt_trace_is_static_listener_elem elem =
+                       g_array_index(trace->is_static_listeners,
+                               struct bt_trace_is_static_listener_elem, i);
+
+               if (!elem.func) {
+                       break;
+               }
        }
 
-       /* 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) {
-               ret = -1;
-               goto end;
+       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_put(trace->packet_header_type);
-       trace->packet_header_type = bt_get(packet_header_type);
-end:
-       return ret;
-}
+       if (listener_id) {
+               *listener_id = i;
+       }
 
-static
-int64_t get_stream_class_count(void *element)
-{
-       return bt_ctf_trace_get_stream_class_count(
-                       (struct bt_ctf_trace *) element);
+       BT_LIB_LOGV("Added \"trace is static\" listener: "
+               "%![trace-]+t, listener-id=%" PRIu64, trace, i);
+       return 0;
 }
 
+BT_ASSERT_PRE_FUNC
 static
-void *get_stream_class(void *element, int i)
-{
-       return bt_ctf_trace_get_stream_class_by_index(
-                       (struct bt_ctf_trace *) element, i);
+bool has_listener_id(struct bt_trace *trace, uint64_t listener_id)
+{
+       BT_ASSERT(listener_id < trace->is_static_listeners->len);
+       return (&g_array_index(trace->is_static_listeners,
+                       struct bt_trace_is_static_listener_elem,
+                       listener_id))->func != NULL;
+}
+
+int bt_trace_remove_is_static_listener(
+               struct bt_trace *trace, uint64_t listener_id)
+{
+       struct bt_trace_is_static_listener_elem *elem;
+
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       BT_ASSERT_PRE(!trace->is_static,
+               "Trace is already static: %!+t", trace);
+       BT_ASSERT_PRE(trace->in_remove_listener,
+               "Cannot call this function while executing a "
+               "remove listener: %!+t", trace);
+       BT_ASSERT_PRE(has_listener_id(trace, listener_id),
+               "Trace has no such \"trace is static\" listener ID: "
+               "%![trace-]+t, %" PRIu64, trace, listener_id);
+       elem = &g_array_index(trace->is_static_listeners,
+                       struct bt_trace_is_static_listener_elem,
+                       listener_id);
+       BT_ASSERT(elem->func);
+
+       if (elem->removed) {
+               /* Call remove listener */
+               BT_LIB_LOGV("Calling remove listener: "
+                       "%![trace-]+t, listener-id=%" PRIu64,
+                       trace, listener_id);
+               trace->in_remove_listener = true;
+               elem->removed(trace, elem->data);
+               trace->in_remove_listener = false;
+       }
+
+       elem->func = NULL;
+       elem->removed = NULL;
+       elem->data = NULL;
+       BT_LIB_LOGV("Removed \"trace is static\" listener: "
+               "%![trace-]+t, listener-id=%" PRIu64,
+               trace, listener_id);
+       return 0;
 }
 
-static
-int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data)
+BT_HIDDEN
+void _bt_trace_freeze(struct bt_trace *trace)
 {
-       return bt_ctf_stream_class_visit(object, visitor, data);
+       /* The packet header field type is already frozen */
+       BT_ASSERT(trace);
+       BT_LIB_LOGD("Freezing trace: %!+t", trace);
+       trace->frozen = true;
 }
 
-int bt_ctf_trace_visit(struct bt_ctf_trace *trace,
-               bt_ctf_visitor visitor, void *data)
+bt_bool bt_trace_assigns_automatic_stream_class_id(struct bt_trace *trace)
 {
-       int ret;
-       struct bt_ctf_object obj =
-                       { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE };
-
-       if (!trace || !visitor) {
-               ret = -1;
-               goto end;
-       }
-
-       ret = visitor_helper(&obj, get_stream_class_count,
-                       get_stream_class, visit_stream_class, visitor, data);
-end:
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       return (bt_bool) trace->assigns_automatic_stream_class_id;
 }
 
-static
-int invoke_listener(struct bt_ctf_object *object, void *data)
+int bt_trace_set_assigns_automatic_stream_class_id(
+               struct bt_trace *trace, bt_bool value)
 {
-       struct listener_wrapper *listener_wrapper = data;
-
-       listener_wrapper->listener(object, listener_wrapper->data);
+       BT_ASSERT_PRE_NON_NULL(trace, "Trace");
+       BT_ASSERT_PRE_TRACE_HOT(trace);
+       trace->assigns_automatic_stream_class_id = (bool) value;
+       BT_LIB_LOGV("Set trace's automatic stream class ID "
+               "assignment property: %!+t", trace);
        return 0;
 }
 
-int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace,
-               bt_ctf_listener_cb listener, void *listener_data)
-{
-       int ret = 0;
-       struct listener_wrapper *listener_wrapper =
-                       g_new0(struct listener_wrapper, 1);
-
-       if (!trace || !listener || !listener_wrapper) {
-               ret = -1;
-               goto error;
-       }
-
-       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)
+void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream)
 {
-       size_t i;
-       struct bt_ctf_trace *trace = trace_ptr;
+       guint count = 0;
 
-       assert(trace);
-       assert(object);
+       bt_object_set_parent(&stream->base, &trace->base);
+       g_ptr_array_add(trace->streams, stream);
+       bt_trace_freeze(trace);
 
-       if (trace->listeners->len == 0) {
-               goto end;
+       if (bt_g_hash_table_contains(trace->stream_classes_stream_count,
+                       stream->class)) {
+               count = GPOINTER_TO_UINT(g_hash_table_lookup(
+                       trace->stream_classes_stream_count, stream->class));
        }
 
-       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;
+       g_hash_table_insert(trace->stream_classes_stream_count,
+               stream->class, GUINT_TO_POINTER(count + 1));
 }
 
 BT_HIDDEN
-struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
+uint64_t bt_trace_get_automatic_stream_id(struct bt_trace *trace,
+               struct bt_stream_class *stream_class)
 {
-       int ret;
-       unsigned int alignment, size;
-       struct bt_ctf_field_type *field_type = NULL;
+       gpointer orig_key;
+       gpointer value;
+       uint64_t id = 0;
 
-       if (alias >= NR_FIELD_TYPE_ALIAS) {
-               goto end;
+       BT_ASSERT(stream_class);
+       BT_ASSERT(trace);
+       if (g_hash_table_lookup_extended(trace->stream_classes_stream_count,
+                       stream_class, &orig_key, &value)) {
+               id = (uint64_t) GPOINTER_TO_UINT(value);
        }
 
-       alignment = field_type_aliases_alignments[alias];
-       size = field_type_aliases_sizes[alias];
-       field_type = bt_ctf_field_type_integer_create(size);
-       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;
-}
-
-bool bt_ctf_trace_is_static(struct bt_ctf_trace *trace)
-{
-       bool is_static = false;
-
-       if (!trace) {
-               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;
-
-       if (!trace) {
-               ret = -1;
-               goto end;
-       }
-
-       trace->is_static = true;
-       bt_ctf_trace_freeze(trace);
-
-end:
-       return ret;
+       return id;
 }
This page took 0.042828 seconds and 4 git commands to generate.