+ return locked_ptr(this);
+}
+
+/*
+ * Initialize registry with default values.
+ */
+void lsu::registry_session::add_channel(uint64_t key)
+{
+ lttng::pthread::lock_guard session_lock_guard(_lock);
+
+ /*
+ * Assign a channel ID right now since the event notification comes
+ * *before* the channel notify so the ID needs to be set at this point so
+ * the metadata can be dumped for that event.
+ */
+ if (is_max_channel_id(_used_channel_id)) {
+ LTTNG_THROW_ERROR(lttng::format(
+ "Failed to allocate unique id for channel under session while adding channel"));
+ }
+
+ auto chan = new lsu::registry_channel(
+ _get_next_channel_id(),
+ abi,
+ _clock->name,
+ /* Registered channel listener. */
+ [this](const lsu::registry_channel& registered_channel) {
+ /*
+ * Channel registration completed, serialize it's layout's
+ * description.
+ */
+ registered_channel.accept(*_metadata_generating_visitor);
+ },
+ /* Added event listener. */
+ [this](const lsu::registry_channel& channel,
+ const lsu::registry_event& added_event) {
+ /*
+ * The channel and its event classes will be dumped at once when
+ * it is registered. This check prevents event classes from being
+ * declared before their stream class.
+ */
+ if (channel.is_registered()) {
+ added_event.accept(*_metadata_generating_visitor);
+ }
+ });
+
+ lttng::urcu::read_lock_guard rcu_read_lock_guard;
+ lttng_ht_node_init_u64(&chan->_node, key);
+ lttng_ht_add_unique_u64(_channels.get(), &chan->_node);
+}
+
+lttng::sessiond::ust::registry_channel& lsu::registry_session::channel(uint64_t channel_key) const
+{
+ lttng::urcu::read_lock_guard read_lock_guard;
+ struct lttng_ht_node_u64 *node;
+ struct lttng_ht_iter iter;
+
+ ASSERT_LOCKED(_lock);
+
+ lttng_ht_lookup(_channels.get(), &channel_key, &iter);
+ node = lttng_ht_iter_get_node_u64(&iter);
+ if (!node) {
+ LTTNG_THROW_INVALID_ARGUMENT_ERROR(lttng::format(
+ "Invalid channel key provided: channel key = {}", channel_key));
+ }
+
+ DIAGNOSTIC_PUSH
+ DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
+ auto chan = lttng::utils::container_of(node, &lsu::registry_channel::_node);
+ DIAGNOSTIC_POP
+ return *chan;
+}
+
+void lsu::registry_session::remove_channel(uint64_t channel_key, bool notify)
+{
+ struct lttng_ht_iter iter;
+ int ret;
+ lttng::urcu::read_lock_guard read_lock_guard;
+
+ ASSERT_LOCKED(_lock);
+ auto& channel_to_remove = channel(channel_key);
+
+ iter.iter.node = &channel_to_remove._node.node;
+ ret = lttng_ht_del(_channels.get(), &iter);
+ LTTNG_ASSERT(!ret);
+ destroy_channel(&channel_to_remove, notify);
+}
+
+void lsu::registry_session::accept(
+ lttng::sessiond::trace::trace_class_environment_visitor& visitor) const
+{
+ ASSERT_LOCKED(_lock);
+
+ visitor.visit(lst::environment_field<const char *>("domain", "ust"));
+ visitor.visit(lst::environment_field<const char *>("tracer_name", "lttng-ust"));
+ visitor.visit(lst::environment_field<int64_t>("tracer_major", _app_tracer_version.major));
+ visitor.visit(lst::environment_field<int64_t>("tracer_minor", _app_tracer_version.minor));
+ visitor.visit(lst::environment_field<const char *>(
+ "tracer_buffering_scheme",
+ buffering_scheme() == LTTNG_BUFFER_PER_PID ? "pid" : "uid"));
+ visitor.visit(lst::environment_field<int64_t>("architecture_bit_width", abi.bits_per_long));
+
+ {
+ /* The caller already holds the session and session list locks. */
+ ASSERT_SESSION_LIST_LOCKED();
+ const auto session = lttng::sessiond::find_session_by_id(_tracing_id);
+
+ LTTNG_ASSERT(session);
+ ASSERT_LOCKED(session->lock);
+
+ visitor.visit(lst::environment_field<const char *>(
+ "trace_name",
+ session->has_auto_generated_name ? DEFAULT_SESSION_NAME : session->name));
+ visitor.visit(lst::environment_field<std::string>(
+ "trace_creation_datetime",
+ lttng::utils::time_to_iso8601_str(session->creation_time)));
+ visitor.visit(lst::environment_field<const char *>("hostname", session->hostname));
+ }
+}
+
+void lsu::registry_session::_accept_on_clock_classes(lst::trace_class_visitor& visitor) const
+{
+ ASSERT_LOCKED(_lock);
+ _clock->accept(visitor);
+}
+
+void lsu::registry_session::_accept_on_stream_classes(lst::trace_class_visitor& visitor) const
+{
+ ASSERT_LOCKED(_lock);
+
+ std::vector<const lttng::sessiond::ust::registry_channel *> sorted_stream_classes;
+
+ {
+ lttng::urcu::read_lock_guard rcu_lock_guard;
+ const lsu::registry_channel *channel;
+ lttng_ht_iter channel_it;
+
+ DIAGNOSTIC_PUSH
+ DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
+ cds_lfht_for_each_entry (_channels->ht, &channel_it.iter, channel, _node.node) {
+ sorted_stream_classes.emplace_back(channel);
+ }
+ DIAGNOSTIC_POP
+ }
+
+ std::sort(sorted_stream_classes.begin(),
+ sorted_stream_classes.end(),
+ [](const lttng::sessiond::ust::registry_channel *a,
+ const lttng::sessiond::ust::registry_channel *b) { return a->id < b->id; });
+
+ for (const auto stream_class : sorted_stream_classes) {
+ stream_class->accept(visitor);
+ }
+}
+
+/*
+ * Return next available channel id and increment the used counter. The
+ * is_max_channel_id function MUST be called before in order to validate
+ * if the maximum number of IDs have been reached. If not, it is safe to call
+ * this function.
+ *
+ * Return a unique channel ID. If max is reached, the used_channel_id counter
+ * is returned.
+ */
+uint32_t lsu::registry_session::_get_next_channel_id()
+{
+ if (is_max_channel_id(_used_channel_id)) {
+ return _used_channel_id;
+ }
+
+ _used_channel_id++;
+ return _next_channel_id++;
+}
+
+void lsu::registry_session::_increase_metadata_size(size_t reservation_length)
+{
+ const auto new_len = _metadata_len + reservation_length;
+ auto new_alloc_len = new_len;
+ const auto old_alloc_len = _metadata_alloc_len;
+
+ /* Rounding the new allocation length to the next power of 2 would overflow. */
+ if (new_alloc_len > (UINT32_MAX >> 1)) {
+ LTTNG_THROW_ERROR(
+ "Failed to reserve trace metadata storage as the new size would overflow");
+ }
+
+ /* The current allocation length is already the largest we can afford. */
+ if ((old_alloc_len << 1) > (UINT32_MAX >> 1)) {