+ _destroy_enum(reg_enum);
+ }
+ DIAGNOSTIC_POP
+ }
+}
+
+lsu::registry_session::locked_ptr lsu::registry_session::lock() noexcept
+{
+ pthread_mutex_lock(&_lock);
+ 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(fmt::format("Failed to allocate unique id for channel under session while adding channel"));
+ }
+
+ auto chan = new lsu::registry_channel(
+ _get_next_channel_id(),
+ /* 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::get_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(fmt::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 = get_channel(channel_key);
+
+ iter.iter.node = &channel._node.node;
+ ret = lttng_ht_del(_channels.get(), &iter);
+ LTTNG_ASSERT(!ret);
+ destroy_channel(&channel, notify);
+}
+
+void lsu::registry_session::_visit_environment(
+ lttng::sessiond::trace::trace_class_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",
+ get_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);