Use compiler-agnostic defines to silence warning
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.cpp
index 30e771e1cc21f14c61e60fa3e9c4bf0d4ccdb7c5..f1b29da9ccdd9c9f937209df70061bbcb76607e0 100644 (file)
@@ -12,6 +12,7 @@
 #include "buffer-registry.hpp"
 #include "channel.hpp"
 #include "cmd.hpp"
+#include "consumer-output.hpp"
 #include "consumer.hpp"
 #include "event-notifier-error-accounting.hpp"
 #include "event.hpp"
@@ -22,7 +23,6 @@
 #include "lttng-syscall.hpp"
 #include "notification-thread-commands.hpp"
 #include "notification-thread.hpp"
-#include "rotate.hpp"
 #include "rotation-thread.hpp"
 #include "session.hpp"
 #include "timer.hpp"
@@ -41,6 +41,7 @@
 #include <common/sessiond-comm/sessiond-comm.hpp>
 #include <common/string-utils/string-utils.hpp>
 #include <common/trace-chunk.hpp>
+#include <common/urcu.hpp>
 #include <common/utils.hpp>
 
 #include <lttng/action/action-internal.hpp>
@@ -55,6 +56,7 @@
 #include <lttng/event-internal.hpp>
 #include <lttng/event-rule/event-rule-internal.hpp>
 #include <lttng/event-rule/event-rule.h>
+#include <lttng/kernel.h>
 #include <lttng/location-internal.hpp>
 #include <lttng/lttng-error.h>
 #include <lttng/rotate-internal.hpp>
@@ -115,7 +117,7 @@ uint64_t relayd_net_seq_idx;
 
 static struct cmd_completion_handler *current_completion_handler;
 static int validate_ust_event_name(const char *);
-static int cmd_enable_event_internal(struct ltt_session *session,
+static int cmd_enable_event_internal(ltt_session::locked_ref& session,
                                     const struct lttng_domain *domain,
                                     char *channel_name,
                                     struct lttng_event *event,
@@ -123,7 +125,7 @@ static int cmd_enable_event_internal(struct ltt_session *session,
                                     struct lttng_bytecode *filter,
                                     struct lttng_event_exclusion *exclusion,
                                     int wpipe);
-static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session,
+static enum lttng_error_code cmd_enable_channel_internal(ltt_session::locked_ref& session,
                                                         const struct lttng_domain *domain,
                                                         const struct lttng_channel *_attr,
                                                         int wpipe);
@@ -132,13 +134,13 @@ static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *ses
  * Create a session path used by list_lttng_sessions for the case that the
  * session consumer is on the network.
  */
-static int build_network_session_path(char *dst, size_t size, struct ltt_session *session)
+static int
+build_network_session_path(char *dst, size_t size, const ltt_session::locked_ref& session)
 {
        int ret, kdata_port, udata_port;
-       struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
+       struct lttng_uri *kuri = nullptr, *uuri = nullptr, *uri = nullptr;
        char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
 
-       LTTNG_ASSERT(session);
        LTTNG_ASSERT(dst);
 
        memset(tmp_urls, 0, sizeof(tmp_urls));
@@ -156,7 +158,7 @@ static int build_network_session_path(char *dst, size_t size, struct ltt_session
                udata_port = session->ust_session->consumer->dst.net.data.port;
        }
 
-       if (uuri == NULL && kuri == NULL) {
+       if (uuri == nullptr && kuri == nullptr) {
                uri = &session->consumer->dst.net.control;
                kdata_port = session->consumer->dst.net.data.port;
        } else if (kuri && uuri) {
@@ -172,9 +174,9 @@ static int build_network_session_path(char *dst, size_t size, struct ltt_session
                } else {
                        uri = kuri;
                }
-       } else if (kuri && uuri == NULL) {
+       } else if (kuri && uuri == nullptr) {
                uri = kuri;
-       } else if (uuri && kuri == NULL) {
+       } else if (uuri && kuri == nullptr) {
                uri = uuri;
        }
 
@@ -214,7 +216,7 @@ error:
  * Get run-time attributes if the session has been started (discarded events,
  * lost packets).
  */
-static int get_kernel_runtime_stats(struct ltt_session *session,
+static int get_kernel_runtime_stats(const ltt_session::locked_ref& session,
                                    struct ltt_kernel_channel *kchan,
                                    uint64_t *discarded_events,
                                    uint64_t *lost_packets)
@@ -248,7 +250,7 @@ end:
  * Get run-time attributes if the session has been started (discarded events,
  * lost packets).
  */
-static int get_ust_runtime_stats(struct ltt_session *session,
+static int get_ust_runtime_stats(const ltt_session::locked_ref& session,
                                 struct ltt_ust_channel *uchan,
                                 uint64_t *discarded_events,
                                 uint64_t *lost_packets)
@@ -315,8 +317,6 @@ static enum lttng_error_code list_lttng_agent_events(struct agent *agt,
        enum lttng_error_code ret_code;
        int ret = 0;
        unsigned int local_nb_events = 0;
-       struct agent_event *event;
-       struct lttng_ht_iter iter;
        unsigned long agent_event_count;
 
        assert(agt);
@@ -324,7 +324,6 @@ static enum lttng_error_code list_lttng_agent_events(struct agent *agt,
 
        DBG3("Listing agent events");
 
-       rcu_read_lock();
        agent_event_count = lttng_ht_get_count(agt->events);
        if (agent_event_count == 0) {
                /* Early exit. */
@@ -338,7 +337,10 @@ static enum lttng_error_code list_lttng_agent_events(struct agent *agt,
 
        local_nb_events = (unsigned int) agent_event_count;
 
-       cds_lfht_for_each_entry (agt->events->ht, &iter.iter, event, node.node) {
+       for (auto *event :
+            lttng::urcu::lfht_iteration_adapter<agent_event,
+                                                decltype(agent_event::node),
+                                                &agent_event::node>(*agt->events->ht)) {
                struct lttng_event *tmp_event = lttng_event_create();
 
                if (!tmp_event) {
@@ -358,19 +360,17 @@ static enum lttng_error_code list_lttng_agent_events(struct agent *agt,
                tmp_event->loglevel_type = event->loglevel_type;
 
                ret = lttng_event_serialize(
-                       tmp_event, 0, NULL, event->filter_expression, 0, NULL, reply_payload);
+                       tmp_event, 0, nullptr, event->filter_expression, 0, nullptr, reply_payload);
                lttng_event_destroy(tmp_event);
                if (ret) {
                        ret_code = LTTNG_ERR_FATAL;
                        goto error;
                }
        }
-
 end:
        ret_code = LTTNG_OK;
        *nb_events = local_nb_events;
 error:
-       rcu_read_unlock();
        return ret_code;
 }
 
@@ -387,7 +387,6 @@ static enum lttng_error_code list_lttng_ust_global_events(char *channel_name,
        struct lttng_ht_iter iter;
        struct lttng_ht_node_str *node;
        struct ltt_ust_channel *uchan;
-       struct ltt_ust_event *uevent;
        unsigned long channel_event_count;
        unsigned int local_nb_events = 0;
 
@@ -396,16 +395,16 @@ static enum lttng_error_code list_lttng_ust_global_events(char *channel_name,
 
        DBG("Listing UST global events for channel %s", channel_name);
 
-       rcu_read_lock();
+       const lttng::urcu::read_lock_guard read_lock;
 
        lttng_ht_lookup(ust_global->channels, (void *) channel_name, &iter);
-       node = lttng_ht_iter_get_node_str(&iter);
-       if (node == NULL) {
+       node = lttng_ht_iter_get_node<lttng_ht_node_str>(&iter);
+       if (node == nullptr) {
                ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND;
                goto error;
        }
 
-       uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
+       uchan = lttng::utils::container_of(node, &ltt_ust_channel::node);
 
        channel_event_count = lttng_ht_get_count(uchan->events);
        if (channel_event_count == 0) {
@@ -423,8 +422,11 @@ static enum lttng_error_code list_lttng_ust_global_events(char *channel_name,
 
        DBG3("Listing UST global %d events", *nb_events);
 
-       cds_lfht_for_each_entry (uchan->events->ht, &iter.iter, uevent, node.node) {
-               struct lttng_event *tmp_event = NULL;
+       for (auto *uevent :
+            lttng::urcu::lfht_iteration_adapter<ltt_ust_event,
+                                                decltype(ltt_ust_event::node),
+                                                &ltt_ust_event::node>(*uchan->events->ht)) {
+               struct lttng_event *tmp_event = nullptr;
 
                if (uevent->internal) {
                        /* This event should remain hidden from clients */
@@ -478,17 +480,25 @@ static enum lttng_error_code list_lttng_ust_global_events(char *channel_name,
                        tmp_event->exclusion = 1;
                }
 
+               std::vector<const char *> exclusion_names;
+               if (uevent->exclusion) {
+                       for (int i = 0; i < uevent->exclusion->count; i++) {
+                               exclusion_names.emplace_back(
+                                       LTTNG_EVENT_EXCLUSION_NAME_AT(uevent->exclusion, i));
+                       }
+               }
+
                /*
                 * We do not care about the filter bytecode and the fd from the
                 * userspace_probe_location.
                 */
                ret = lttng_event_serialize(tmp_event,
-                                           uevent->exclusion ? uevent->exclusion->count : 0,
-                                           uevent->exclusion ? (char **) uevent->exclusion->names :
-                                                               NULL,
+                                           exclusion_names.size(),
+                                           exclusion_names.size() ? exclusion_names.data() :
+                                                                    nullptr,
                                            uevent->filter_expression,
                                            0,
-                                           NULL,
+                                           nullptr,
                                            reply_payload);
                lttng_event_destroy(tmp_event);
                if (ret) {
@@ -502,7 +512,6 @@ end:
        ret_code = LTTNG_OK;
        *nb_events = local_nb_events;
 error:
-       rcu_read_unlock();
        return ret_code;
 }
 
@@ -516,13 +525,12 @@ static enum lttng_error_code list_lttng_kernel_events(char *channel_name,
 {
        enum lttng_error_code ret_code;
        int ret;
-       struct ltt_kernel_event *event;
        struct ltt_kernel_channel *kchan;
 
        assert(reply_payload);
 
        kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
-       if (kchan == NULL) {
+       if (kchan == nullptr) {
                ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
                goto end;
        }
@@ -537,7 +545,9 @@ static enum lttng_error_code list_lttng_kernel_events(char *channel_name,
        }
 
        /* Kernel channels */
-       cds_list_for_each_entry (event, &kchan->events_list.head, list) {
+       for (auto event :
+            lttng::urcu::list_iteration_adapter<ltt_kernel_event, &ltt_kernel_event::list>(
+                    kchan->events_list.head)) {
                struct lttng_event *tmp_event = lttng_event_create();
 
                if (!tmp_event) {
@@ -614,7 +624,7 @@ static enum lttng_error_code list_lttng_kernel_events(char *channel_name,
                }
 
                ret = lttng_event_serialize(
-                       tmp_event, 0, NULL, event->filter_expression, 0, NULL, reply_payload);
+                       tmp_event, 0, nullptr, event->filter_expression, 0, nullptr, reply_payload);
                lttng_event_destroy(tmp_event);
                if (ret) {
                        ret_code = LTTNG_ERR_FATAL;
@@ -631,7 +641,7 @@ end:
  * Add URI so the consumer output object. Set the correct path depending on the
  * domain adding the default trace directory.
  */
-static enum lttng_error_code add_uri_to_consumer(const struct ltt_session *session,
+static enum lttng_error_code add_uri_to_consumer(const ltt_session::locked_ref& session,
                                                 struct consumer_output *consumer,
                                                 struct lttng_uri *uri,
                                                 enum lttng_domain_type domain)
@@ -641,7 +651,7 @@ static enum lttng_error_code add_uri_to_consumer(const struct ltt_session *sessi
 
        LTTNG_ASSERT(uri);
 
-       if (consumer == NULL) {
+       if (consumer == nullptr) {
                DBG("No consumer detected. Don't add URI. Stopping.");
                ret_code = LTTNG_ERR_NO_CONSUMER;
                goto error;
@@ -733,16 +743,15 @@ error:
 static int init_kernel_tracing(struct ltt_kernel_session *session)
 {
        int ret = 0;
-       struct lttng_ht_iter iter;
-       struct consumer_socket *socket;
 
        LTTNG_ASSERT(session);
 
-       rcu_read_lock();
-
-       if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
-               cds_lfht_for_each_entry (
-                       session->consumer->socks->ht, &iter.iter, socket, node.node) {
+       if (session->consumer_fds_sent == 0 && session->consumer != nullptr) {
+               for (auto *socket :
+                    lttng::urcu::lfht_iteration_adapter<consumer_socket,
+                                                        decltype(consumer_socket::node),
+                                                        &consumer_socket::node>(
+                            *session->consumer->socks->ht)) {
                        pthread_mutex_lock(socket->lock);
                        ret = kernel_consumer_send_session(socket, session);
                        pthread_mutex_unlock(socket->lock);
@@ -754,7 +763,6 @@ static int init_kernel_tracing(struct ltt_kernel_session *session)
        }
 
 error:
-       rcu_read_unlock();
        return ret;
 }
 
@@ -862,7 +870,7 @@ static enum lttng_error_code send_consumer_relayd_socket(unsigned int session_id
                                                         bool session_name_contains_creation_time)
 {
        int ret;
-       struct lttcomm_relayd_sock *rsock = NULL;
+       struct lttcomm_relayd_sock *rsock = nullptr;
        enum lttng_error_code status;
 
        /* Connect to relayd and make version check if uri is the control. */
@@ -921,7 +929,7 @@ close_sock:
                 * since the relayd connection failed thus making any tracing or/and
                 * streaming not usable.
                 */
-               consumer->enabled = 0;
+               consumer->enabled = false;
        }
        (void) relayd_close(rsock);
        free(rsock);
@@ -1000,25 +1008,20 @@ error:
  * the relayd and send them to the right domain consumer. Consumer type MUST be
  * network.
  */
-int cmd_setup_relayd(struct ltt_session *session)
+int cmd_setup_relayd(const ltt_session::locked_ref& session)
 {
        int ret = LTTNG_OK;
        struct ltt_ust_session *usess;
        struct ltt_kernel_session *ksess;
-       struct consumer_socket *socket;
-       struct lttng_ht_iter iter;
        LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
 
-       LTTNG_ASSERT(session);
-
        usess = session->ust_session;
        ksess = session->kernel_session;
 
        DBG("Setting relayd for session %s", session->name);
 
-       rcu_read_lock();
        if (session->current_trace_chunk) {
-               enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
+               const lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
                        session->current_trace_chunk, &current_chunk_id.value);
 
                if (status == LTTNG_TRACE_CHUNK_STATUS_OK) {
@@ -1033,8 +1036,11 @@ int cmd_setup_relayd(struct ltt_session *session)
        if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET &&
            usess->consumer->enabled) {
                /* For each consumer socket, send relayd sockets */
-               cds_lfht_for_each_entry (
-                       usess->consumer->socks->ht, &iter.iter, socket, node.node) {
+               for (auto *socket :
+                    lttng::urcu::lfht_iteration_adapter<consumer_socket,
+                                                        decltype(consumer_socket::node),
+                                                        &consumer_socket::node>(
+                            *usess->consumer->socks->ht)) {
                        pthread_mutex_lock(socket->lock);
                        ret = send_consumer_relayd_sockets(
                                session->id,
@@ -1044,7 +1050,7 @@ int cmd_setup_relayd(struct ltt_session *session)
                                session->hostname,
                                session->base_path,
                                session->live_timer,
-                               current_chunk_id.is_set ? &current_chunk_id.value : NULL,
+                               current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
                                session->creation_time,
                                session->name_contains_creation_time);
                        pthread_mutex_unlock(socket->lock);
@@ -1054,6 +1060,7 @@ int cmd_setup_relayd(struct ltt_session *session)
                        /* Session is now ready for network streaming. */
                        session->net_handle = 1;
                }
+
                session->consumer->relay_major_version = usess->consumer->relay_major_version;
                session->consumer->relay_minor_version = usess->consumer->relay_minor_version;
                session->consumer->relay_allows_clear = usess->consumer->relay_allows_clear;
@@ -1061,8 +1068,13 @@ int cmd_setup_relayd(struct ltt_session *session)
 
        if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET &&
            ksess->consumer->enabled) {
-               cds_lfht_for_each_entry (
-                       ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
+               const lttng::urcu::read_lock_guard read_lock;
+
+               for (auto *socket :
+                    lttng::urcu::lfht_iteration_adapter<consumer_socket,
+                                                        decltype(consumer_socket::node),
+                                                        &consumer_socket::node>(
+                            *ksess->consumer->socks->ht)) {
                        pthread_mutex_lock(socket->lock);
                        ret = send_consumer_relayd_sockets(
                                session->id,
@@ -1072,7 +1084,7 @@ int cmd_setup_relayd(struct ltt_session *session)
                                session->hostname,
                                session->base_path,
                                session->live_timer,
-                               current_chunk_id.is_set ? &current_chunk_id.value : NULL,
+                               current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
                                session->creation_time,
                                session->name_contains_creation_time);
                        pthread_mutex_unlock(socket->lock);
@@ -1082,13 +1094,13 @@ int cmd_setup_relayd(struct ltt_session *session)
                        /* Session is now ready for network streaming. */
                        session->net_handle = 1;
                }
+
                session->consumer->relay_major_version = ksess->consumer->relay_major_version;
                session->consumer->relay_minor_version = ksess->consumer->relay_minor_version;
                session->consumer->relay_allows_clear = ksess->consumer->relay_allows_clear;
        }
 
 error:
-       rcu_read_unlock();
        return ret;
 }
 
@@ -1098,10 +1110,9 @@ error:
 int start_kernel_session(struct ltt_kernel_session *ksess)
 {
        int ret;
-       struct ltt_kernel_channel *kchan;
 
        /* Open kernel metadata */
-       if (ksess->metadata == NULL && ksess->output_traces) {
+       if (ksess->metadata == nullptr && ksess->output_traces) {
                ret = kernel_open_metadata(ksess);
                if (ret < 0) {
                        ret = LTTNG_ERR_KERN_META_FAIL;
@@ -1120,7 +1131,9 @@ int start_kernel_session(struct ltt_kernel_session *ksess)
        }
 
        /* For each channel */
-       cds_list_for_each_entry (kchan, &ksess->channel_list.head, list) {
+       for (auto kchan :
+            lttng::urcu::list_iteration_adapter<ltt_kernel_channel, &ltt_kernel_channel::list>(
+                    ksess->channel_list.head)) {
                if (kchan->stream_count == 0) {
                        ret = kernel_open_channel_stream(kchan);
                        if (ret < 0) {
@@ -1149,7 +1162,7 @@ int start_kernel_session(struct ltt_kernel_session *ksess)
        /* Quiescent wait after starting trace */
        kernel_wait_quiescent();
 
-       ksess->active = 1;
+       ksess->active = true;
 
        ret = LTTNG_OK;
 
@@ -1159,7 +1172,6 @@ error:
 
 int stop_kernel_session(struct ltt_kernel_session *ksess)
 {
-       struct ltt_kernel_channel *kchan;
        bool error_occurred = false;
        int ret;
 
@@ -1186,7 +1198,9 @@ int stop_kernel_session(struct ltt_kernel_session *ksess)
        }
 
        /* Flush all buffers after stopping */
-       cds_list_for_each_entry (kchan, &ksess->channel_list.head, list) {
+       for (auto kchan :
+            lttng::urcu::list_iteration_adapter<ltt_kernel_channel, &ltt_kernel_channel::list>(
+                    ksess->channel_list.head)) {
                ret = kernel_flush_buffer(kchan);
                if (ret < 0) {
                        ERR("Kernel flush buffer error");
@@ -1194,7 +1208,7 @@ int stop_kernel_session(struct ltt_kernel_session *ksess)
                }
        }
 
-       ksess->active = 0;
+       ksess->active = false;
        if (error_occurred) {
                ret = LTTNG_ERR_UNK;
        } else {
@@ -1207,7 +1221,7 @@ error:
 /*
  * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
  */
-int cmd_disable_channel(struct ltt_session *session,
+int cmd_disable_channel(const ltt_session::locked_ref& session,
                        enum lttng_domain_type domain,
                        char *channel_name)
 {
@@ -1216,7 +1230,7 @@ int cmd_disable_channel(struct ltt_session *session,
 
        usess = session->ust_session;
 
-       rcu_read_lock();
+       const lttng::urcu::read_lock_guard read_lock;
 
        switch (domain) {
        case LTTNG_DOMAIN_KERNEL:
@@ -1237,7 +1251,7 @@ int cmd_disable_channel(struct ltt_session *session,
                chan_ht = usess->domain_global.channels;
 
                uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
-               if (uchan == NULL) {
+               if (uchan == nullptr) {
                        ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
                        goto error;
                }
@@ -1256,7 +1270,6 @@ int cmd_disable_channel(struct ltt_session *session,
        ret = LTTNG_OK;
 
 error:
-       rcu_read_unlock();
        return ret;
 }
 
@@ -1265,12 +1278,12 @@ error:
  *
  * The wpipe arguments is used as a notifier for the kernel thread.
  */
-int cmd_enable_channel(struct command_ctx *cmd_ctx, int sock, int wpipe)
+int cmd_enable_channel(command_ctx *cmd_ctx, ltt_session::locked_ref& session, int sock, int wpipe)
 {
        int ret;
        size_t channel_len;
        ssize_t sock_recv_len;
-       struct lttng_channel *channel = NULL;
+       struct lttng_channel *channel = nullptr;
        struct lttng_buffer_view view;
        struct lttng_dynamic_buffer channel_buffer;
        const struct lttng_domain command_domain = cmd_ctx->lsm.domain;
@@ -1302,7 +1315,7 @@ int cmd_enable_channel(struct command_ctx *cmd_ctx, int sock, int wpipe)
                goto end;
        }
 
-       ret = cmd_enable_channel_internal(cmd_ctx->session, &command_domain, channel, wpipe);
+       ret = cmd_enable_channel_internal(session, &command_domain, channel, wpipe);
 
 end:
        lttng_dynamic_buffer_reset(&channel_buffer);
@@ -1310,7 +1323,7 @@ end:
        return ret;
 }
 
-static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session,
+static enum lttng_error_code cmd_enable_channel_internal(ltt_session::locked_ref& session,
                                                         const struct lttng_domain *domain,
                                                         const struct lttng_channel *_attr,
                                                         int wpipe)
@@ -1319,12 +1332,13 @@ static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *ses
        struct ltt_ust_session *usess = session->ust_session;
        struct lttng_ht *chan_ht;
        size_t len;
-       struct lttng_channel *attr = NULL;
+       struct lttng_channel *attr = nullptr;
 
-       LTTNG_ASSERT(session);
        LTTNG_ASSERT(_attr);
        LTTNG_ASSERT(domain);
 
+       const lttng::urcu::read_lock_guard read_lock;
+
        attr = lttng_channel_copy(_attr);
        if (!attr) {
                ret_code = LTTNG_ERR_NOMEM;
@@ -1334,15 +1348,13 @@ static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *ses
        len = lttng_strnlen(attr->name, sizeof(attr->name));
 
        /* Validate channel name */
-       if (attr->name[0] == '.' || memchr(attr->name, '/', len) != NULL) {
+       if (attr->name[0] == '.' || memchr(attr->name, '/', len) != nullptr) {
                ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
                goto end;
        }
 
        DBG("Enabling channel %s for session %s", attr->name, session->name);
 
-       rcu_read_lock();
-
        /*
         * If the session is a live session, remove the switch timer, the
         * live timer does the same thing but sends also synchronisation
@@ -1372,6 +1384,7 @@ static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *ses
                break;
        case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_LOG4J:
+       case LTTNG_DOMAIN_LOG4J2:
        case LTTNG_DOMAIN_PYTHON:
                if (!agent_tracing_is_enabled()) {
                        DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
@@ -1390,7 +1403,7 @@ static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *ses
                struct ltt_kernel_channel *kchan;
 
                kchan = trace_kernel_get_channel_by_name(attr->name, session->kernel_session);
-               if (kchan == NULL) {
+               if (kchan == nullptr) {
                        /*
                         * Don't try to create a channel if the session has been started at
                         * some point in time before. The tracer does not allow it.
@@ -1422,6 +1435,7 @@ static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *ses
        case LTTNG_DOMAIN_UST:
        case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_LOG4J:
+       case LTTNG_DOMAIN_LOG4J2:
        case LTTNG_DOMAIN_PYTHON:
        {
                struct ltt_ust_channel *uchan;
@@ -1435,19 +1449,30 @@ static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *ses
                 * adhered to.
                 */
                if (domain->type == LTTNG_DOMAIN_JUL) {
-                       if (strncmp(attr->name, DEFAULT_JUL_CHANNEL_NAME, LTTNG_SYMBOL_NAME_LEN)) {
+                       if (strncmp(attr->name,
+                                   DEFAULT_JUL_CHANNEL_NAME,
+                                   LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
                                ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
                                goto error;
                        }
                } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
-                       if (strncmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME, LTTNG_SYMBOL_NAME_LEN)) {
+                       if (strncmp(attr->name,
+                                   DEFAULT_LOG4J_CHANNEL_NAME,
+                                   LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
+                               ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
+                               goto error;
+                       }
+               } else if (domain->type == LTTNG_DOMAIN_LOG4J2) {
+                       if (strncmp(attr->name,
+                                   DEFAULT_LOG4J2_CHANNEL_NAME,
+                                   LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
                                ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
                                goto error;
                        }
                } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
                        if (strncmp(attr->name,
                                    DEFAULT_PYTHON_CHANNEL_NAME,
-                                   LTTNG_SYMBOL_NAME_LEN)) {
+                                   LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
                                ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
                                goto error;
                        }
@@ -1456,7 +1481,7 @@ static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *ses
                chan_ht = usess->domain_global.channels;
 
                uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
-               if (uchan == NULL) {
+               if (uchan == nullptr) {
                        /*
                         * Don't try to create a channel if the session has been started at
                         * some point in time before. The tracer does not allow it.
@@ -1484,14 +1509,13 @@ static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *ses
                session->has_non_mmap_channel = true;
        }
 error:
-       rcu_read_unlock();
 end:
        lttng_channel_destroy(attr);
        return ret_code;
 }
 
 enum lttng_error_code
-cmd_process_attr_tracker_get_tracking_policy(struct ltt_session *session,
+cmd_process_attr_tracker_get_tracking_policy(const ltt_session::locked_ref& session,
                                             enum lttng_domain_type domain,
                                             enum lttng_process_attr process_attr,
                                             enum lttng_tracking_policy *policy)
@@ -1528,7 +1552,7 @@ end:
 }
 
 enum lttng_error_code
-cmd_process_attr_tracker_set_tracking_policy(struct ltt_session *session,
+cmd_process_attr_tracker_set_tracking_policy(const ltt_session::locked_ref& session,
                                             enum lttng_domain_type domain,
                                             enum lttng_process_attr process_attr,
                                             enum lttng_tracking_policy policy)
@@ -1571,7 +1595,7 @@ end:
 }
 
 enum lttng_error_code
-cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session *session,
+cmd_process_attr_tracker_inclusion_set_add_value(const ltt_session::locked_ref& session,
                                                 enum lttng_domain_type domain,
                                                 enum lttng_process_attr process_attr,
                                                 const struct process_attr_value *value)
@@ -1604,7 +1628,7 @@ end:
 }
 
 enum lttng_error_code
-cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session *session,
+cmd_process_attr_tracker_inclusion_set_remove_value(const ltt_session::locked_ref& session,
                                                    enum lttng_domain_type domain,
                                                    enum lttng_process_attr process_attr,
                                                    const struct process_attr_value *value)
@@ -1637,7 +1661,7 @@ end:
 }
 
 enum lttng_error_code
-cmd_process_attr_tracker_get_inclusion_set(struct ltt_session *session,
+cmd_process_attr_tracker_get_inclusion_set(const ltt_session::locked_ref& session,
                                           enum lttng_domain_type domain,
                                           enum lttng_process_attr process_attr,
                                           struct lttng_process_attr_values **values)
@@ -1695,14 +1719,15 @@ end:
  * Command LTTNG_DISABLE_EVENT processed by the client thread.
  */
 int cmd_disable_event(struct command_ctx *cmd_ctx,
+                     ltt_session::locked_ref& locked_session,
                      struct lttng_event *event,
                      char *filter_expression,
                      struct lttng_bytecode *bytecode,
                      struct lttng_event_exclusion *exclusion)
 {
        int ret;
+       const ltt_session& session = *locked_session;
        const char *event_name;
-       const struct ltt_session *session = cmd_ctx->session;
        const char *channel_name = cmd_ctx->lsm.u.disable.channel_name;
        const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type;
 
@@ -1723,6 +1748,8 @@ int cmd_disable_event(struct command_ctx *cmd_ctx,
 
        event_name = event->name;
 
+       const lttng::urcu::read_lock_guard read_lock;
+
        /* Error out on unhandled search criteria */
        if (event->loglevel_type || event->loglevel != -1 || event->enabled || event->pid ||
            event->filter || event->exclusion) {
@@ -1730,15 +1757,13 @@ int cmd_disable_event(struct command_ctx *cmd_ctx,
                goto error;
        }
 
-       rcu_read_lock();
-
        switch (domain) {
        case LTTNG_DOMAIN_KERNEL:
        {
                struct ltt_kernel_channel *kchan;
                struct ltt_kernel_session *ksess;
 
-               ksess = session->kernel_session;
+               ksess = session.kernel_session;
 
                /*
                 * If a non-default channel has been created in the
@@ -1751,7 +1776,7 @@ int cmd_disable_event(struct command_ctx *cmd_ctx,
                }
 
                kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
-               if (kchan == NULL) {
+               if (kchan == nullptr) {
                        ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
                        goto error_unlock;
                }
@@ -1764,7 +1789,7 @@ int cmd_disable_event(struct command_ctx *cmd_ctx,
                case LTTNG_EVENT_FUNCTION:
                case LTTNG_EVENT_FUNCTION_ENTRY: /* fall-through */
                        if (event_name[0] == '\0') {
-                               ret = event_kernel_disable_event(kchan, NULL, event->type);
+                               ret = event_kernel_disable_event(kchan, nullptr, event->type);
                        } else {
                                ret = event_kernel_disable_event(kchan, event_name, event->type);
                        }
@@ -1785,7 +1810,7 @@ int cmd_disable_event(struct command_ctx *cmd_ctx,
                struct ltt_ust_channel *uchan;
                struct ltt_ust_session *usess;
 
-               usess = session->ust_session;
+               usess = session.ust_session;
 
                if (validate_ust_event_name(event_name)) {
                        ret = LTTNG_ERR_INVALID_EVENT_NAME;
@@ -1803,7 +1828,7 @@ int cmd_disable_event(struct command_ctx *cmd_ctx,
                }
 
                uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name);
-               if (uchan == NULL) {
+               if (uchan == nullptr) {
                        ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
                        goto error_unlock;
                }
@@ -1832,11 +1857,12 @@ int cmd_disable_event(struct command_ctx *cmd_ctx,
                break;
        }
        case LTTNG_DOMAIN_LOG4J:
+       case LTTNG_DOMAIN_LOG4J2:
        case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_PYTHON:
        {
                struct agent *agt;
-               struct ltt_ust_session *usess = session->ust_session;
+               struct ltt_ust_session *usess = session.ust_session;
 
                LTTNG_ASSERT(usess);
 
@@ -1876,7 +1902,6 @@ int cmd_disable_event(struct command_ctx *cmd_ctx,
        ret = LTTNG_OK;
 
 error_unlock:
-       rcu_read_unlock();
 error:
        free(exclusion);
        free(bytecode);
@@ -1888,12 +1913,13 @@ error:
  * Command LTTNG_ADD_CONTEXT processed by the client thread.
  */
 int cmd_add_context(struct command_ctx *cmd_ctx,
+                   ltt_session::locked_ref& locked_session,
                    const struct lttng_event_context *event_context,
                    int kwpipe)
 {
        int ret, chan_kern_created = 0, chan_ust_created = 0;
        const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type;
-       const struct ltt_session *session = cmd_ctx->session;
+       const struct ltt_session& session = *locked_session;
        const char *channel_name = cmd_ctx->lsm.u.context.channel_name;
 
        /*
@@ -1901,31 +1927,32 @@ int cmd_add_context(struct command_ctx *cmd_ctx,
         * some point in time before. The tracer does not allow it and would
         * result in a corrupted trace.
         */
-       if (cmd_ctx->session->has_been_started) {
+       if (session.has_been_started) {
                ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
                goto end;
        }
 
        switch (domain) {
        case LTTNG_DOMAIN_KERNEL:
-               LTTNG_ASSERT(session->kernel_session);
+               LTTNG_ASSERT(session.kernel_session);
 
-               if (session->kernel_session->channel_count == 0) {
+               if (session.kernel_session->channel_count == 0) {
                        /* Create default channel */
-                       ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
+                       ret = channel_kernel_create(session.kernel_session, nullptr, kwpipe);
                        if (ret != LTTNG_OK) {
                                goto error;
                        }
                        chan_kern_created = 1;
                }
                /* Add kernel context to kernel tracer */
-               ret = context_kernel_add(session->kernel_session, event_context, channel_name);
+               ret = context_kernel_add(session.kernel_session, event_context, channel_name);
                if (ret != LTTNG_OK) {
                        goto error;
                }
                break;
        case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_LOG4J:
+       case LTTNG_DOMAIN_LOG4J2:
        {
                /*
                 * Validate channel name.
@@ -1935,11 +1962,15 @@ int cmd_add_context(struct command_ctx *cmd_ctx,
                 * name, return an error.
                 */
                if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
-                   strcmp(channel_name, DEFAULT_JUL_CHANNEL_NAME)) {
+                   strcmp(channel_name, DEFAULT_JUL_CHANNEL_NAME) != 0) {
                        ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
                        goto error;
                } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
-                          strcmp(channel_name, DEFAULT_LOG4J_CHANNEL_NAME)) {
+                          strcmp(channel_name, DEFAULT_LOG4J_CHANNEL_NAME) != 0) {
+                       ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
+                       goto error;
+               } else if (domain == LTTNG_DOMAIN_LOG4J2 && *channel_name &&
+                          strcmp(channel_name, DEFAULT_LOG4J2_CHANNEL_NAME) != 0) {
                        ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
                        goto error;
                }
@@ -1947,7 +1978,7 @@ int cmd_add_context(struct command_ctx *cmd_ctx,
        /* fall through */
        case LTTNG_DOMAIN_UST:
        {
-               struct ltt_ust_session *usess = session->ust_session;
+               struct ltt_ust_session *usess = session.ust_session;
                unsigned int chan_count;
 
                LTTNG_ASSERT(usess);
@@ -1957,7 +1988,7 @@ int cmd_add_context(struct command_ctx *cmd_ctx,
                        struct lttng_channel *attr;
                        /* Create default channel */
                        attr = channel_new_default_attr(domain, usess->buffer_type);
-                       if (attr == NULL) {
+                       if (attr == nullptr) {
                                ret = LTTNG_ERR_FATAL;
                                goto error;
                        }
@@ -1988,7 +2019,7 @@ int cmd_add_context(struct command_ctx *cmd_ctx,
 error:
        if (chan_kern_created) {
                struct ltt_kernel_channel *kchan = trace_kernel_get_channel_by_name(
-                       DEFAULT_CHANNEL_NAME, session->kernel_session);
+                       DEFAULT_CHANNEL_NAME, session.kernel_session);
                /* Created previously, this should NOT fail. */
                LTTNG_ASSERT(kchan);
                kernel_destroy_channel(kchan);
@@ -1996,11 +2027,11 @@ error:
 
        if (chan_ust_created) {
                struct ltt_ust_channel *uchan = trace_ust_find_channel_by_name(
-                       session->ust_session->domain_global.channels, DEFAULT_CHANNEL_NAME);
+                       session.ust_session->domain_global.channels, DEFAULT_CHANNEL_NAME);
                /* Created previously, this should NOT fail. */
                LTTNG_ASSERT(uchan);
                /* Remove from the channel list of the session. */
-               trace_ust_delete_channel(session->ust_session->domain_global.channels, uchan);
+               trace_ust_delete_channel(session.ust_session->domain_global.channels, uchan);
                trace_ust_destroy_channel(uchan);
        }
 end:
@@ -2030,6 +2061,7 @@ static int validate_ust_event_name(const char *name)
         */
        if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
            name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
+           name_starts_with(name, DEFAULT_LOG4J2_EVENT_COMPONENT) ||
            name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
                ret = -1;
        }
@@ -2044,7 +2076,7 @@ end:
  * be hidden from clients. Such events are used in the agent implementation to
  * enable the events through which all "agent" events are funeled.
  */
-static int _cmd_enable_event(struct ltt_session *session,
+static int _cmd_enable_event(ltt_session::locked_ref& locked_session,
                             const struct lttng_domain *domain,
                             char *channel_name,
                             struct lttng_event *event,
@@ -2055,9 +2087,9 @@ static int _cmd_enable_event(struct ltt_session *session,
                             bool internal_event)
 {
        int ret = 0, channel_created = 0;
-       struct lttng_channel *attr = NULL;
+       struct lttng_channel *attr = nullptr;
+       const ltt_session& session = *locked_session;
 
-       LTTNG_ASSERT(session);
        LTTNG_ASSERT(event);
        LTTNG_ASSERT(channel_name);
 
@@ -2078,9 +2110,7 @@ static int _cmd_enable_event(struct ltt_session *session,
                }
        }
 
-       DBG("Enable event command for event \'%s\'", event->name);
-
-       rcu_read_lock();
+       const lttng::urcu::read_lock_guard read_lock;
 
        switch (domain->type) {
        case LTTNG_DOMAIN_KERNEL:
@@ -2092,15 +2122,15 @@ static int _cmd_enable_event(struct ltt_session *session,
                 * session, explicitely require that -c chan_name needs
                 * to be provided.
                 */
-               if (session->kernel_session->has_non_default_channel && channel_name[0] == '\0') {
+               if (session.kernel_session->has_non_default_channel && channel_name[0] == '\0') {
                        ret = LTTNG_ERR_NEED_CHANNEL_NAME;
                        goto error;
                }
 
-               kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session);
-               if (kchan == NULL) {
+               kchan = trace_kernel_get_channel_by_name(channel_name, session.kernel_session);
+               if (kchan == nullptr) {
                        attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, LTTNG_BUFFER_GLOBAL);
-                       if (attr == NULL) {
+                       if (attr == nullptr) {
                                ret = LTTNG_ERR_FATAL;
                                goto error;
                        }
@@ -2109,7 +2139,7 @@ static int _cmd_enable_event(struct ltt_session *session,
                                goto error;
                        }
 
-                       ret = cmd_enable_channel_internal(session, domain, attr, wpipe);
+                       ret = cmd_enable_channel_internal(locked_session, domain, attr, wpipe);
                        if (ret != LTTNG_OK) {
                                goto error;
                        }
@@ -2117,8 +2147,8 @@ static int _cmd_enable_event(struct ltt_session *session,
                }
 
                /* Get the newly created kernel channel pointer */
-               kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session);
-               if (kchan == NULL) {
+               kchan = trace_kernel_get_channel_by_name(channel_name, session.kernel_session);
+               if (kchan == nullptr) {
                        /* This sould not happen... */
                        ret = LTTNG_ERR_FATAL;
                        goto error;
@@ -2127,8 +2157,8 @@ static int _cmd_enable_event(struct ltt_session *session,
                switch (event->type) {
                case LTTNG_EVENT_ALL:
                {
-                       char *filter_expression_a = NULL;
-                       struct lttng_bytecode *filter_a = NULL;
+                       char *filter_expression_a = nullptr;
+                       struct lttng_bytecode *filter_a = nullptr;
 
                        /*
                         * We need to duplicate filter_expression and filter,
@@ -2154,8 +2184,8 @@ static int _cmd_enable_event(struct ltt_session *session,
                        event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
                        ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
                        /* We have passed ownership */
-                       filter_expression = NULL;
-                       filter = NULL;
+                       filter_expression = nullptr;
+                       filter = nullptr;
                        if (ret != LTTNG_OK) {
                                if (channel_created) {
                                        /* Let's not leak a useless channel. */
@@ -2169,8 +2199,8 @@ static int _cmd_enable_event(struct ltt_session *session,
                        ret = event_kernel_enable_event(
                                kchan, event, filter_expression_a, filter_a);
                        /* We have passed ownership */
-                       filter_expression_a = NULL;
-                       filter_a = NULL;
+                       filter_expression_a = nullptr;
+                       filter_a = nullptr;
                        if (ret != LTTNG_OK) {
                                goto error;
                        }
@@ -2183,8 +2213,8 @@ static int _cmd_enable_event(struct ltt_session *session,
                case LTTNG_EVENT_TRACEPOINT:
                        ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
                        /* We have passed ownership */
-                       filter_expression = NULL;
-                       filter = NULL;
+                       filter_expression = nullptr;
+                       filter = nullptr;
                        if (ret != LTTNG_OK) {
                                if (channel_created) {
                                        /* Let's not leak a useless channel. */
@@ -2196,8 +2226,8 @@ static int _cmd_enable_event(struct ltt_session *session,
                case LTTNG_EVENT_SYSCALL:
                        ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
                        /* We have passed ownership */
-                       filter_expression = NULL;
-                       filter = NULL;
+                       filter_expression = nullptr;
+                       filter = nullptr;
                        if (ret != LTTNG_OK) {
                                goto error;
                        }
@@ -2213,7 +2243,7 @@ static int _cmd_enable_event(struct ltt_session *session,
        case LTTNG_DOMAIN_UST:
        {
                struct ltt_ust_channel *uchan;
-               struct ltt_ust_session *usess = session->ust_session;
+               struct ltt_ust_session *usess = session.ust_session;
 
                LTTNG_ASSERT(usess);
 
@@ -2229,10 +2259,10 @@ static int _cmd_enable_event(struct ltt_session *session,
 
                /* Get channel from global UST domain */
                uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name);
-               if (uchan == NULL) {
+               if (uchan == nullptr) {
                        /* Create default channel */
                        attr = channel_new_default_attr(LTTNG_DOMAIN_UST, usess->buffer_type);
-                       if (attr == NULL) {
+                       if (attr == nullptr) {
                                ret = LTTNG_ERR_FATAL;
                                goto error;
                        }
@@ -2241,7 +2271,7 @@ static int _cmd_enable_event(struct ltt_session *session,
                                goto error;
                        }
 
-                       ret = cmd_enable_channel_internal(session, domain, attr, wpipe);
+                       ret = cmd_enable_channel_internal(locked_session, domain, attr, wpipe);
                        if (ret != LTTNG_OK) {
                                goto error;
                        }
@@ -2279,9 +2309,9 @@ static int _cmd_enable_event(struct ltt_session *session,
                ret = event_ust_enable_tracepoint(
                        usess, uchan, event, filter_expression, filter, exclusion, internal_event);
                /* We have passed ownership */
-               filter_expression = NULL;
-               filter = NULL;
-               exclusion = NULL;
+               filter_expression = nullptr;
+               filter = nullptr;
+               exclusion = nullptr;
                if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
                        goto already_enabled;
                } else if (ret != LTTNG_OK) {
@@ -2290,6 +2320,7 @@ static int _cmd_enable_event(struct ltt_session *session,
                break;
        }
        case LTTNG_DOMAIN_LOG4J:
+       case LTTNG_DOMAIN_LOG4J2:
        case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_PYTHON:
        {
@@ -2297,7 +2328,7 @@ static int _cmd_enable_event(struct ltt_session *session,
                struct agent *agt;
                struct lttng_event uevent;
                struct lttng_domain tmp_dom;
-               struct ltt_ust_session *usess = session->ust_session;
+               struct ltt_ust_session *usess = session.ust_session;
 
                LTTNG_ASSERT(usess);
 
@@ -2321,6 +2352,7 @@ static int _cmd_enable_event(struct ltt_session *session,
                memset(&uevent, 0, sizeof(uevent));
                uevent.type = LTTNG_EVENT_TRACEPOINT;
                uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
+               uevent.loglevel = -1;
                default_event_name = event_get_default_agent_ust_name(domain->type);
                if (!default_event_name) {
                        ret = LTTNG_ERR_FATAL;
@@ -2341,6 +2373,9 @@ static int _cmd_enable_event(struct ltt_session *session,
                case LTTNG_DOMAIN_LOG4J:
                        default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
                        break;
+               case LTTNG_DOMAIN_LOG4J2:
+                       default_chan_name = DEFAULT_LOG4J2_CHANNEL_NAME;
+                       break;
                case LTTNG_DOMAIN_JUL:
                        default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
                        break;
@@ -2353,8 +2388,8 @@ static int _cmd_enable_event(struct ltt_session *session,
                }
 
                {
-                       char *filter_expression_copy = NULL;
-                       struct lttng_bytecode *filter_copy = NULL;
+                       char *filter_expression_copy = nullptr;
+                       struct lttng_bytecode *filter_copy = nullptr;
 
                        if (filter) {
                                const size_t filter_size =
@@ -2379,13 +2414,13 @@ static int _cmd_enable_event(struct ltt_session *session,
                                }
                        }
 
-                       ret = cmd_enable_event_internal(session,
+                       ret = cmd_enable_event_internal(locked_session,
                                                        &tmp_dom,
                                                        (char *) default_chan_name,
                                                        &uevent,
                                                        filter_expression_copy,
                                                        filter_copy,
-                                                       NULL,
+                                                       nullptr,
                                                        wpipe);
                }
 
@@ -2401,8 +2436,8 @@ static int _cmd_enable_event(struct ltt_session *session,
                } else {
                        ret = event_agent_enable(usess, agt, event, filter, filter_expression);
                }
-               filter = NULL;
-               filter_expression = NULL;
+               filter = nullptr;
+               filter_expression = nullptr;
                if (ret != LTTNG_OK) {
                        goto error;
                }
@@ -2422,7 +2457,6 @@ error:
        free(filter);
        free(exclusion);
        channel_attr_destroy(attr);
-       rcu_read_unlock();
        return ret;
 }
 
@@ -2431,6 +2465,7 @@ error:
  * We own filter, exclusion, and filter_expression.
  */
 int cmd_enable_event(struct command_ctx *cmd_ctx,
+                    ltt_session::locked_ref& locked_session,
                     struct lttng_event *event,
                     char *filter_expression,
                     struct lttng_event_exclusion *exclusion,
@@ -2451,7 +2486,7 @@ int cmd_enable_event(struct command_ctx *cmd_ctx,
         *  - bytecode,
         *  - exclusion
         */
-       ret = _cmd_enable_event(cmd_ctx->session,
+       ret = _cmd_enable_event(locked_session,
                                &command_domain,
                                cmd_ctx->lsm.u.enable.channel_name,
                                event,
@@ -2460,9 +2495,9 @@ int cmd_enable_event(struct command_ctx *cmd_ctx,
                                exclusion,
                                wpipe,
                                false);
-       filter_expression = NULL;
-       bytecode = NULL;
-       exclusion = NULL;
+       filter_expression = nullptr;
+       bytecode = nullptr;
+       exclusion = nullptr;
        return ret;
 }
 
@@ -2471,7 +2506,7 @@ int cmd_enable_event(struct command_ctx *cmd_ctx,
  * never be made visible to clients and are immune to checks such as
  * reserved names.
  */
-static int cmd_enable_event_internal(struct ltt_session *session,
+static int cmd_enable_event_internal(ltt_session::locked_ref& locked_session,
                                     const struct lttng_domain *domain,
                                     char *channel_name,
                                     struct lttng_event *event,
@@ -2480,7 +2515,7 @@ static int cmd_enable_event_internal(struct ltt_session *session,
                                     struct lttng_event_exclusion *exclusion,
                                     int wpipe)
 {
-       return _cmd_enable_event(session,
+       return _cmd_enable_event(locked_session,
                                 domain,
                                 channel_name,
                                 event,
@@ -2500,7 +2535,7 @@ enum lttng_error_code cmd_list_tracepoints(enum lttng_domain_type domain,
        enum lttng_error_code ret_code;
        int ret;
        ssize_t i, nb_events = 0;
-       struct lttng_event *events = NULL;
+       struct lttng_event *events = nullptr;
        struct lttcomm_list_command_header reply_command_header = {};
        size_t reply_command_header_offset;
 
@@ -2532,6 +2567,7 @@ enum lttng_error_code cmd_list_tracepoints(enum lttng_domain_type domain,
                }
                break;
        case LTTNG_DOMAIN_LOG4J:
+       case LTTNG_DOMAIN_LOG4J2:
        case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_PYTHON:
                nb_events = agent_list_events(&events, domain);
@@ -2546,7 +2582,8 @@ enum lttng_error_code cmd_list_tracepoints(enum lttng_domain_type domain,
        }
 
        for (i = 0; i < nb_events; i++) {
-               ret = lttng_event_serialize(&events[i], 0, NULL, NULL, 0, NULL, reply_payload);
+               ret = lttng_event_serialize(
+                       &events[i], 0, nullptr, nullptr, 0, nullptr, reply_payload);
                if (ret) {
                        ret_code = LTTNG_ERR_NOMEM;
                        goto error;
@@ -2580,7 +2617,7 @@ enum lttng_error_code cmd_list_tracepoint_fields(enum lttng_domain_type domain,
        enum lttng_error_code ret_code;
        int ret;
        unsigned int i, nb_fields;
-       struct lttng_event_field *fields = NULL;
+       struct lttng_event_field *fields = nullptr;
        struct lttcomm_list_command_header reply_command_header = {};
        size_t reply_command_header_offset;
 
@@ -2646,7 +2683,7 @@ enum lttng_error_code cmd_list_syscalls(struct lttng_payload *reply_payload)
        enum lttng_error_code ret_code;
        ssize_t nb_events, i;
        int ret;
-       struct lttng_event *events = NULL;
+       struct lttng_event *events = nullptr;
        struct lttcomm_list_command_header reply_command_header = {};
        size_t reply_command_header_offset;
 
@@ -2669,7 +2706,8 @@ enum lttng_error_code cmd_list_syscalls(struct lttng_payload *reply_payload)
        }
 
        for (i = 0; i < nb_events; i++) {
-               ret = lttng_event_serialize(&events[i], 0, NULL, NULL, 0, NULL, reply_payload);
+               ret = lttng_event_serialize(
+                       &events[i], 0, nullptr, nullptr, 0, nullptr, reply_payload);
                if (ret) {
                        ret_code = LTTNG_ERR_NOMEM;
                        goto end;
@@ -2696,10 +2734,8 @@ end:
 
 /*
  * Command LTTNG_START_TRACE processed by the client thread.
- *
- * Called with session mutex held.
  */
-int cmd_start_trace(struct ltt_session *session)
+int cmd_start_trace(const ltt_session::locked_ref& session)
 {
        enum lttng_error_code ret;
        unsigned long nb_chan = 0;
@@ -2708,8 +2744,6 @@ int cmd_start_trace(struct ltt_session *session)
        const bool session_rotated_after_last_stop = session->rotated_after_last_stop;
        const bool session_cleared_after_last_stop = session->cleared_after_last_stop;
 
-       LTTNG_ASSERT(session);
-
        /* Ease our life a bit ;) */
        ksession = session->kernel_session;
        usess = session->ust_session;
@@ -2752,7 +2786,7 @@ int cmd_start_trace(struct ltt_session *session)
                goto error;
        }
 
-       session->active = 1;
+       session->active = true;
        session->rotated_after_last_stop = false;
        session->cleared_after_last_stop = false;
        if (session->output_traces && !session->current_trace_chunk) {
@@ -2760,14 +2794,15 @@ int cmd_start_trace(struct ltt_session *session)
                        struct lttng_trace_chunk *trace_chunk;
 
                        DBG("Creating initial trace chunk of session \"%s\"", session->name);
-                       trace_chunk = session_create_new_trace_chunk(session, NULL, NULL, NULL);
+                       trace_chunk =
+                               session_create_new_trace_chunk(session, nullptr, nullptr, nullptr);
                        if (!trace_chunk) {
                                ret = LTTNG_ERR_CREATE_DIR_FAIL;
                                goto error;
                        }
                        LTTNG_ASSERT(!session->current_trace_chunk);
                        ret = (lttng_error_code) session_set_trace_chunk(
-                               session, trace_chunk, NULL);
+                               session, trace_chunk, nullptr);
                        lttng_trace_chunk_put(trace_chunk);
                        if (ret) {
                                ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
@@ -2787,7 +2822,7 @@ int cmd_start_trace(struct ltt_session *session)
                         * rotation should happen on reception of the command.
                         */
                        ret = (lttng_error_code) cmd_rotate_session(
-                               session, NULL, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
+                               session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
                        if (ret != LTTNG_OK) {
                                goto error;
                        }
@@ -2795,7 +2830,7 @@ int cmd_start_trace(struct ltt_session *session)
        }
 
        /* Kernel tracing */
-       if (ksession != NULL) {
+       if (ksession != nullptr) {
                DBG("Start kernel tracing session %s", session->name);
                ret = (lttng_error_code) start_kernel_session(ksession);
                if (ret != LTTNG_OK) {
@@ -2805,7 +2840,7 @@ int cmd_start_trace(struct ltt_session *session)
 
        /* Flag session that trace should start automatically */
        if (usess) {
-               int int_ret = ust_app_start_trace_all(usess);
+               const int int_ret = ust_app_start_trace_all(usess);
 
                if (int_ret < 0) {
                        ret = LTTNG_ERR_UST_START_FAIL;
@@ -2830,7 +2865,7 @@ int cmd_start_trace(struct ltt_session *session)
        session->rotated_after_last_stop = false;
 
        if (session->rotate_timer_period && !session->rotation_schedule_timer_enabled) {
-               int int_ret = timer_session_rotation_schedule_timer_start(
+               const int int_ret = timer_session_rotation_schedule_timer_start(
                        session, session->rotate_timer_period);
 
                if (int_ret < 0) {
@@ -2845,9 +2880,9 @@ int cmd_start_trace(struct ltt_session *session)
 error:
        if (ret == LTTNG_OK) {
                /* Flag this after a successful start. */
-               session->has_been_started |= 1;
+               session->has_been_started = true;
        } else {
-               session->active = 0;
+               session->active = false;
                /* Restore initial state on error. */
                session->rotated_after_last_stop = session_rotated_after_last_stop;
                session->cleared_after_last_stop = session_cleared_after_last_stop;
@@ -2859,14 +2894,12 @@ end:
 /*
  * Command LTTNG_STOP_TRACE processed by the client thread.
  */
-int cmd_stop_trace(struct ltt_session *session)
+int cmd_stop_trace(const ltt_session::locked_ref& session)
 {
        int ret;
        struct ltt_kernel_session *ksession;
        struct ltt_ust_session *usess;
 
-       LTTNG_ASSERT(session);
-
        DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
        /* Short cut */
        ksession = session->kernel_session;
@@ -2893,7 +2926,7 @@ int cmd_stop_trace(struct ltt_session *session)
 
        DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
        /* Flag inactive after a successful stop. */
-       session->active = 0;
+       session->active = false;
        ret = LTTNG_OK;
 
 error:
@@ -2904,8 +2937,9 @@ error:
  * Set the base_path of the session only if subdir of a control uris is set.
  * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
  */
-static int
-set_session_base_path_from_uris(struct ltt_session *session, size_t nb_uri, struct lttng_uri *uris)
+static int set_session_base_path_from_uris(const ltt_session::locked_ref& session,
+                                          size_t nb_uri,
+                                          struct lttng_uri *uris)
 {
        int ret;
        size_t i;
@@ -2916,9 +2950,9 @@ set_session_base_path_from_uris(struct ltt_session *session, size_t nb_uri, stru
                        continue;
                }
 
-               if (session->base_path != NULL) {
+               if (session->base_path != nullptr) {
                        free(session->base_path);
-                       session->base_path = NULL;
+                       session->base_path = nullptr;
                }
 
                /* Set session base_path */
@@ -2942,13 +2976,14 @@ error:
 /*
  * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
  */
-int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, struct lttng_uri *uris)
+int cmd_set_consumer_uri(const ltt_session::locked_ref& session,
+                        size_t nb_uri,
+                        struct lttng_uri *uris)
 {
        int ret, i;
        struct ltt_kernel_session *ksess = session->kernel_session;
        struct ltt_ust_session *usess = session->ust_session;
 
-       LTTNG_ASSERT(session);
        LTTNG_ASSERT(uris);
        LTTNG_ASSERT(nb_uri > 0);
 
@@ -3023,14 +3058,14 @@ error:
 }
 
 static enum lttng_error_code
-set_session_output_from_descriptor(struct ltt_session *session,
+set_session_output_from_descriptor(const ltt_session::locked_ref& session,
                                   const struct lttng_session_descriptor *descriptor)
 {
        int ret;
        enum lttng_error_code ret_code = LTTNG_OK;
-       enum lttng_session_descriptor_type session_type =
+       const lttng_session_descriptor_type session_type =
                lttng_session_descriptor_get_type(descriptor);
-       enum lttng_session_descriptor_output_type output_type =
+       const lttng_session_descriptor_output_type output_type =
                lttng_session_descriptor_get_output_type(descriptor);
        struct lttng_uri uris[2] = {};
        size_t uri_count = 0;
@@ -3054,7 +3089,7 @@ set_session_output_from_descriptor(struct ltt_session *session,
        switch (session_type) {
        case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
        {
-               struct snapshot_output *new_output = NULL;
+               struct snapshot_output *new_output = nullptr;
 
                new_output = snapshot_output_alloc();
                if (!new_output) {
@@ -3064,7 +3099,7 @@ set_session_output_from_descriptor(struct ltt_session *session,
 
                ret = snapshot_output_init_with_uri(session,
                                                    DEFAULT_SNAPSHOT_MAX_SIZE,
-                                                   NULL,
+                                                   nullptr,
                                                    uris,
                                                    uri_count,
                                                    session->consumer,
@@ -3100,10 +3135,10 @@ cmd_create_session_from_descriptor(struct lttng_session_descriptor *descriptor,
        int ret;
        enum lttng_error_code ret_code;
        const char *session_name;
-       struct ltt_session *new_session = NULL;
+       struct ltt_session *new_session = nullptr;
        enum lttng_session_descriptor_status descriptor_status;
 
-       session_lock_list();
+       const auto list_lock = lttng::sessiond::lock_session_list();
        if (home_path) {
                if (*home_path != '/') {
                        ERR("Home path provided by client is not absolute");
@@ -3117,7 +3152,7 @@ cmd_create_session_from_descriptor(struct lttng_session_descriptor *descriptor,
        case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
                break;
        case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
-               session_name = NULL;
+               session_name = nullptr;
                break;
        default:
                ret_code = LTTNG_ERR_INVALID;
@@ -3140,12 +3175,17 @@ cmd_create_session_from_descriptor(struct lttng_session_descriptor *descriptor,
 
        /* Announce the session's destruction to the notification thread when it is destroyed. */
        ret = session_add_destroy_notifier(
-               new_session,
-               [](const struct ltt_session *session, void *user_data __attribute__((unused))) {
+               [new_session]() {
+                       session_get(new_session);
+                       new_session->lock();
+                       return ltt_session::make_locked_ref(*new_session);
+               }(),
+               [](const ltt_session::locked_ref& session,
+                  void *user_data __attribute__((unused))) {
                        (void) notification_thread_command_remove_session(
                                the_notification_thread_handle, session->id);
                },
-               NULL);
+               nullptr);
        if (ret) {
                PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
                       new_session->name);
@@ -3168,7 +3208,9 @@ cmd_create_session_from_descriptor(struct lttng_session_descriptor *descriptor,
                 * not auto-generated.
                 */
                ret_code = lttng_session_descriptor_set_default_output(
-                       descriptor, session_name ? &new_session->creation_time : NULL, home_path);
+                       descriptor,
+                       session_name ? &new_session->creation_time : nullptr,
+                       home_path);
                if (ret_code != LTTNG_OK) {
                        goto end;
                }
@@ -3189,11 +3231,17 @@ cmd_create_session_from_descriptor(struct lttng_session_descriptor *descriptor,
                break;
        }
 
-       ret_code = set_session_output_from_descriptor(new_session, descriptor);
+       ret_code = set_session_output_from_descriptor(
+               [new_session]() {
+                       session_get(new_session);
+                       new_session->lock();
+                       return ltt_session::make_locked_ref(*new_session);
+               }(),
+               descriptor);
        if (ret_code != LTTNG_OK) {
                goto end;
        }
-       new_session->consumer->enabled = 1;
+       new_session->consumer->enabled = true;
        ret_code = LTTNG_OK;
 end:
        /* Release reference provided by the session_create function. */
@@ -3202,7 +3250,7 @@ end:
                /* Release the global reference on error. */
                session_destroy(new_session);
        }
-       session_unlock_list();
+
        return ret_code;
 }
 
@@ -3215,7 +3263,7 @@ enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx,
        struct lttng_dynamic_buffer payload;
        struct lttng_buffer_view home_dir_view;
        struct lttng_buffer_view session_descriptor_view;
-       struct lttng_session_descriptor *session_descriptor = NULL;
+       struct lttng_session_descriptor *session_descriptor = nullptr;
        enum lttng_error_code ret_code;
 
        lttng_dynamic_buffer_init(&payload);
@@ -3275,24 +3323,24 @@ enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx,
         * Sets the descriptor's auto-generated properties (name, output) if
         * needed.
         */
-       ret_code =
-               cmd_create_session_from_descriptor(session_descriptor,
-                                                  &cmd_ctx->creds,
-                                                  home_dir_view.size ? home_dir_view.data : NULL);
+       ret_code = cmd_create_session_from_descriptor(session_descriptor,
+                                                     &cmd_ctx->creds,
+                                                     home_dir_view.size ? home_dir_view.data :
+                                                                          nullptr);
        if (ret_code != LTTNG_OK) {
                goto error;
        }
 
        ret_code = LTTNG_OK;
        *return_descriptor = session_descriptor;
-       session_descriptor = NULL;
+       session_descriptor = nullptr;
 error:
        lttng_dynamic_buffer_reset(&payload);
        lttng_session_descriptor_destroy(session_descriptor);
        return ret_code;
 }
 
-static void cmd_destroy_session_reply(const struct ltt_session *session, void *_reply_context)
+static void cmd_destroy_session_reply(const ltt_session::locked_ref& session, void *_reply_context)
 {
        int ret;
        ssize_t comm_ret;
@@ -3300,7 +3348,7 @@ static void cmd_destroy_session_reply(const struct ltt_session *session, void *_
                (cmd_destroy_session_reply_context *) _reply_context;
        struct lttng_dynamic_buffer payload;
        struct lttcomm_session_destroy_command_header cmd_header;
-       struct lttng_trace_archive_location *location = NULL;
+       struct lttng_trace_archive_location *location = nullptr;
        struct lttcomm_lttng_msg llm = {
                .cmd_type = LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION,
                .ret_code = reply_context->destruction_status,
@@ -3377,13 +3425,11 @@ error:
  *
  * Called with session lock held.
  */
-int cmd_destroy_session(struct ltt_session *session,
-                       struct notification_thread_handle *notification_thread_handle,
-                       int *sock_fd)
+int cmd_destroy_session(const ltt_session::locked_ref& session, int *sock_fd)
 {
        int ret;
        enum lttng_error_code destruction_last_error = LTTNG_OK;
-       struct cmd_destroy_session_reply_context *reply_context = NULL;
+       struct cmd_destroy_session_reply_context *reply_context = nullptr;
 
        if (sock_fd) {
                reply_context = zmalloc<cmd_destroy_session_reply_context>();
@@ -3395,9 +3441,6 @@ int cmd_destroy_session(struct ltt_session *session,
                reply_context->reply_sock_fd = *sock_fd;
        }
 
-       /* Safety net */
-       LTTNG_ASSERT(session);
-
        DBG("Begin destroy session %s (id %" PRIu64 ")", session->name, session->id);
        if (session->active) {
                DBG("Session \"%s\" is active, attempting to stop it before destroying it",
@@ -3421,7 +3464,15 @@ int cmd_destroy_session(struct ltt_session *session,
        }
 
        if (session->rotate_size) {
-               unsubscribe_session_consumed_size_rotation(session, notification_thread_handle);
+               try {
+                       the_rotation_thread_handle->unsubscribe_session_consumed_size_rotation(
+                               *session);
+               } catch (const std::exception& e) {
+                       /* Continue the destruction of the session anyway. */
+                       ERR("Failed to unsubscribe rotation thread notification channel from consumed size condition during session destruction: %s",
+                           e.what());
+               }
+
                session->rotate_size = 0;
        }
 
@@ -3431,7 +3482,7 @@ int cmd_destroy_session(struct ltt_session *session,
                 * occurred during the session's lifetime.
                 */
                ret = cmd_rotate_session(
-                       session, NULL, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
+                       session, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
                if (ret != LTTNG_OK) {
                        ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
                            session->name,
@@ -3452,7 +3503,7 @@ int cmd_destroy_session(struct ltt_session *session,
                 * place.
                 */
                ret = cmd_rotate_session(
-                       session, NULL, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
+                       session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
                /*
                 * Rotation operations may not be supported by the kernel
                 * tracer. Hence, do not consider this implicit rotation as
@@ -3527,7 +3578,7 @@ int cmd_destroy_session(struct ltt_session *session,
         * still holds a reference to the session, thus delaying its destruction
         * _at least_ up to the point when that reference is released.
         */
-       session_destroy(session);
+       session_destroy(&session.get());
        if (reply_context) {
                reply_context->destruction_status = destruction_last_error;
                ret = session_add_destroy_notifier(
@@ -3547,15 +3598,14 @@ end:
 /*
  * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
  */
-int cmd_register_consumer(struct ltt_session *session,
+int cmd_register_consumer(const ltt_session::locked_ref& session,
                          enum lttng_domain_type domain,
                          const char *sock_path,
                          struct consumer_data *cdata)
 {
        int ret, sock;
-       struct consumer_socket *socket = NULL;
+       struct consumer_socket *socket = nullptr;
 
-       LTTNG_ASSERT(session);
        LTTNG_ASSERT(cdata);
        LTTNG_ASSERT(sock_path);
 
@@ -3580,7 +3630,7 @@ int cmd_register_consumer(struct ltt_session *session,
                cdata->cmd_sock = sock;
 
                socket = consumer_allocate_socket(&cdata->cmd_sock);
-               if (socket == NULL) {
+               if (socket == nullptr) {
                        ret = close(sock);
                        if (ret < 0) {
                                PERROR("close register consumer");
@@ -3591,18 +3641,17 @@ int cmd_register_consumer(struct ltt_session *session,
                }
 
                socket->lock = zmalloc<pthread_mutex_t>();
-               if (socket->lock == NULL) {
+               if (socket->lock == nullptr) {
                        PERROR("zmalloc pthread mutex");
                        ret = LTTNG_ERR_FATAL;
                        goto error;
                }
 
-               pthread_mutex_init(socket->lock, NULL);
+               pthread_mutex_init(socket->lock, nullptr);
                socket->registered = 1;
 
-               rcu_read_lock();
+               const lttng::urcu::read_lock_guard read_lock;
                consumer_add_socket(socket, ksess->consumer);
-               rcu_read_unlock();
 
                pthread_mutex_lock(&cdata->pid_mutex);
                cdata->pid = -1;
@@ -3628,30 +3677,27 @@ error:
 /*
  * Command LTTNG_LIST_DOMAINS processed by the client thread.
  */
-ssize_t cmd_list_domains(struct ltt_session *session, struct lttng_domain **domains)
+ssize_t cmd_list_domains(const ltt_session::locked_ref& session, struct lttng_domain **domains)
 {
        int ret, index = 0;
        ssize_t nb_dom = 0;
-       struct agent *agt;
-       struct lttng_ht_iter iter;
 
-       if (session->kernel_session != NULL) {
+       if (session->kernel_session != nullptr) {
                DBG3("Listing domains found kernel domain");
                nb_dom++;
        }
 
-       if (session->ust_session != NULL) {
+       if (session->ust_session != nullptr) {
                DBG3("Listing domains found UST global domain");
                nb_dom++;
 
-               rcu_read_lock();
-               cds_lfht_for_each_entry (
-                       session->ust_session->agents->ht, &iter.iter, agt, node.node) {
+               for (auto *agt :
+                    lttng::urcu::lfht_iteration_adapter<agent, decltype(agent::node), &agent::node>(
+                            *session->ust_session->agents->ht)) {
                        if (agt->being_used) {
                                nb_dom++;
                        }
                }
-               rcu_read_unlock();
        }
 
        if (!nb_dom) {
@@ -3659,12 +3705,12 @@ ssize_t cmd_list_domains(struct ltt_session *session, struct lttng_domain **doma
        }
 
        *domains = calloc<lttng_domain>(nb_dom);
-       if (*domains == NULL) {
+       if (*domains == nullptr) {
                ret = LTTNG_ERR_FATAL;
                goto error;
        }
 
-       if (session->kernel_session != NULL) {
+       if (session->kernel_session != nullptr) {
                (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
 
                /* Kernel session buffer type is always GLOBAL */
@@ -3673,21 +3719,26 @@ ssize_t cmd_list_domains(struct ltt_session *session, struct lttng_domain **doma
                index++;
        }
 
-       if (session->ust_session != NULL) {
+       if (session->ust_session != nullptr) {
                (*domains)[index].type = LTTNG_DOMAIN_UST;
                (*domains)[index].buf_type = session->ust_session->buffer_type;
                index++;
 
-               rcu_read_lock();
-               cds_lfht_for_each_entry (
-                       session->ust_session->agents->ht, &iter.iter, agt, node.node) {
-                       if (agt->being_used) {
-                               (*domains)[index].type = agt->domain;
-                               (*domains)[index].buf_type = session->ust_session->buffer_type;
-                               index++;
+               {
+                       const lttng::urcu::read_lock_guard read_lock;
+
+                       for (auto *agt : lttng::urcu::lfht_iteration_adapter<agent,
+                                                                            decltype(agent::node),
+                                                                            &agent::node>(
+                                    *session->ust_session->agents->ht)) {
+                               if (agt->being_used) {
+                                       (*domains)[index].type = agt->domain;
+                                       (*domains)[index].buf_type =
+                                               session->ust_session->buffer_type;
+                                       index++;
+                               }
                        }
                }
-               rcu_read_unlock();
        }
 end:
        return nb_dom;
@@ -3701,7 +3752,7 @@ error:
  * Command LTTNG_LIST_CHANNELS processed by the client thread.
  */
 enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain,
-                                       struct ltt_session *session,
+                                       const ltt_session::locked_ref& session,
                                        struct lttng_payload *payload)
 {
        int ret = 0;
@@ -3710,8 +3761,7 @@ enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain,
        size_t cmd_header_offset;
        enum lttng_error_code ret_code;
 
-       assert(session);
-       assert(payload);
+       LTTNG_ASSERT(payload);
 
        DBG("Listing channels for session %s", session->name);
 
@@ -3729,10 +3779,11 @@ enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain,
        case LTTNG_DOMAIN_KERNEL:
        {
                /* Kernel channels */
-               struct ltt_kernel_channel *kchan;
-               if (session->kernel_session != NULL) {
-                       cds_list_for_each_entry (
-                               kchan, &session->kernel_session->channel_list.head, list) {
+               if (session->kernel_session != nullptr) {
+                       for (auto kchan :
+                            lttng::urcu::list_iteration_adapter<ltt_kernel_channel,
+                                                                &ltt_kernel_channel::list>(
+                                    session->kernel_session->channel_list.head)) {
                                uint64_t discarded_events, lost_packets;
                                struct lttng_channel_extended *extended;
 
@@ -3768,16 +3819,13 @@ enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain,
        }
        case LTTNG_DOMAIN_UST:
        {
-               struct lttng_ht_iter iter;
-               struct ltt_ust_channel *uchan;
-
-               rcu_read_lock();
-               cds_lfht_for_each_entry (session->ust_session->domain_global.channels->ht,
-                                        &iter.iter,
-                                        uchan,
-                                        node.node) {
+               for (auto *uchan :
+                    lttng::urcu::lfht_iteration_adapter<ltt_ust_channel,
+                                                        decltype(ltt_ust_channel::node),
+                                                        &ltt_ust_channel::node>(
+                            *session->ust_session->domain_global.channels->ht)) {
                        uint64_t discarded_events = 0, lost_packets = 0;
-                       struct lttng_channel *channel = NULL;
+                       struct lttng_channel *channel = nullptr;
                        struct lttng_channel_extended *extended;
 
                        channel = trace_ust_channel_to_lttng_channel(uchan);
@@ -3811,7 +3859,7 @@ enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain,
                        lttng_channel_destroy(channel);
                        i++;
                }
-               rcu_read_unlock();
+
                break;
        }
        default:
@@ -3837,7 +3885,7 @@ end:
  * Command LTTNG_LIST_EVENTS processed by the client thread.
  */
 enum lttng_error_code cmd_list_events(enum lttng_domain_type domain,
-                                     struct ltt_session *session,
+                                     const ltt_session::locked_ref& session,
                                      char *channel_name,
                                      struct lttng_payload *reply_payload)
 {
@@ -3861,7 +3909,7 @@ enum lttng_error_code cmd_list_events(enum lttng_domain_type domain,
 
        switch (domain) {
        case LTTNG_DOMAIN_KERNEL:
-               if (session->kernel_session != NULL) {
+               if (session->kernel_session != nullptr) {
                        ret_code = list_lttng_kernel_events(
                                channel_name, session->kernel_session, reply_payload, &nb_events);
                }
@@ -3869,7 +3917,7 @@ enum lttng_error_code cmd_list_events(enum lttng_domain_type domain,
                break;
        case LTTNG_DOMAIN_UST:
        {
-               if (session->ust_session != NULL) {
+               if (session->ust_session != nullptr) {
                        ret_code =
                                list_lttng_ust_global_events(channel_name,
                                                             &session->ust_session->domain_global,
@@ -3880,23 +3928,20 @@ enum lttng_error_code cmd_list_events(enum lttng_domain_type domain,
                break;
        }
        case LTTNG_DOMAIN_LOG4J:
+       case LTTNG_DOMAIN_LOG4J2:
        case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_PYTHON:
                if (session->ust_session) {
-                       struct lttng_ht_iter iter;
-                       struct agent *agt;
-
-                       rcu_read_lock();
-                       cds_lfht_for_each_entry (
-                               session->ust_session->agents->ht, &iter.iter, agt, node.node) {
+                       for (auto *agt : lttng::urcu::lfht_iteration_adapter<agent,
+                                                                            decltype(agent::node),
+                                                                            &agent::node>(
+                                    *session->ust_session->agents->ht)) {
                                if (agt->domain == domain) {
                                        ret_code = list_lttng_agent_events(
                                                agt, reply_payload, &nb_events);
                                        break;
                                }
                        }
-
-                       rcu_read_unlock();
                }
                break;
        default:
@@ -3923,8 +3968,7 @@ end:
  * Using the session list, filled a lttng_session array to send back to the
  * client for session listing.
  *
- * The session list lock MUST be acquired before calling this function. Use
- * session_lock_list() and session_unlock_list().
+ * The session list lock MUST be acquired before calling this function.
  */
 void cmd_list_lttng_sessions(struct lttng_session *sessions,
                             size_t session_count,
@@ -3933,7 +3977,6 @@ void cmd_list_lttng_sessions(struct lttng_session *sessions,
 {
        int ret;
        unsigned int i = 0;
-       struct ltt_session *session;
        struct ltt_session_list *list = session_get_list();
        struct lttng_session_extended *extended = (typeof(extended)) (&sessions[session_count]);
 
@@ -3942,15 +3985,18 @@ void cmd_list_lttng_sessions(struct lttng_session *sessions,
         * Iterate over session list and append data after the control struct in
         * the buffer.
         */
-       cds_list_for_each_entry (session, &list->head, list) {
-               if (!session_get(session)) {
-                       continue;
-               }
+       for (auto raw_session_ptr :
+            lttng::urcu::list_iteration_adapter<ltt_session, &ltt_session::list>(list->head)) {
+               auto session = [raw_session_ptr]() {
+                       session_get(raw_session_ptr);
+                       raw_session_ptr->lock();
+                       return ltt_session::make_locked_ref(*raw_session_ptr);
+               }();
+
                /*
                 * Only list the sessions the user can control.
                 */
                if (!session_access_ok(session, uid) || session->destroyed) {
-                       session_put(session);
                        continue;
                }
 
@@ -3970,7 +4016,6 @@ void cmd_list_lttng_sessions(struct lttng_session *sessions,
                }
                if (ret < 0) {
                        PERROR("snprintf session path");
-                       session_put(session);
                        continue;
                }
 
@@ -3982,22 +4027,32 @@ void cmd_list_lttng_sessions(struct lttng_session *sessions,
                extended[i].creation_time.value = (uint64_t) session->creation_time;
                extended[i].creation_time.is_set = 1;
                i++;
-               session_put(session);
        }
 }
 
+/*
+ * Command LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS
+ */
+enum lttng_error_code cmd_kernel_tracer_status(enum lttng_kernel_tracer_status *status)
+{
+       if (status == nullptr) {
+               return LTTNG_ERR_INVALID;
+       }
+
+       *status = get_kernel_tracer_status();
+       return LTTNG_OK;
+}
+
 /*
  * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
  * ready for trace analysis (or any kind of reader) or else 1 for pending data.
  */
-int cmd_data_pending(struct ltt_session *session)
+int cmd_data_pending(const ltt_session::locked_ref& session)
 {
        int ret;
        struct ltt_kernel_session *ksess = session->kernel_session;
        struct ltt_ust_session *usess = session->ust_session;
 
-       LTTNG_ASSERT(session);
-
        DBG("Data pending for session %s", session->name);
 
        /* Session MUST be stopped to ask for data availability. */
@@ -4056,14 +4111,13 @@ error:
  *
  * Return LTTNG_OK on success or else a LTTNG_ERR code.
  */
-int cmd_snapshot_add_output(struct ltt_session *session,
+int cmd_snapshot_add_output(const ltt_session::locked_ref& session,
                            const struct lttng_snapshot_output *output,
                            uint32_t *id)
 {
        int ret;
        struct snapshot_output *new_output;
 
-       LTTNG_ASSERT(session);
        LTTNG_ASSERT(output);
 
        DBG("Cmd snapshot add output for session %s", session->name);
@@ -4110,12 +4164,10 @@ int cmd_snapshot_add_output(struct ltt_session *session,
                goto free_error;
        }
 
-       rcu_read_lock();
        snapshot_add_output(&session->snapshot, new_output);
        if (id) {
                *id = new_output->id;
        }
-       rcu_read_unlock();
 
        return LTTNG_OK;
 
@@ -4130,15 +4182,15 @@ error:
  *
  * Return LTTNG_OK on success or else a LTTNG_ERR code.
  */
-int cmd_snapshot_del_output(struct ltt_session *session, const struct lttng_snapshot_output *output)
+int cmd_snapshot_del_output(const ltt_session::locked_ref& session,
+                           const struct lttng_snapshot_output *output)
 {
        int ret;
-       struct snapshot_output *sout = NULL;
+       struct snapshot_output *sout = nullptr;
 
-       LTTNG_ASSERT(session);
        LTTNG_ASSERT(output);
 
-       rcu_read_lock();
+       const lttng::urcu::read_lock_guard read_lock;
 
        /*
         * Permission denied to create an output if the session is not
@@ -4168,7 +4220,6 @@ int cmd_snapshot_del_output(struct ltt_session *session, const struct lttng_snap
        ret = LTTNG_OK;
 
 error:
-       rcu_read_unlock();
        return ret;
 }
 
@@ -4179,15 +4230,12 @@ error:
  *
  * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
  */
-ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
+ssize_t cmd_snapshot_list_outputs(const ltt_session::locked_ref& session,
                                  struct lttng_snapshot_output **outputs)
 {
        int ret, idx = 0;
-       struct lttng_snapshot_output *list = NULL;
-       struct lttng_ht_iter iter;
-       struct snapshot_output *output;
+       struct lttng_snapshot_output *list = nullptr;
 
-       LTTNG_ASSERT(session);
        LTTNG_ASSERT(outputs);
 
        DBG("Cmd snapshot list outputs for session %s", session->name);
@@ -4213,8 +4261,10 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
        }
 
        /* Copy list from session to the new list object. */
-       rcu_read_lock();
-       cds_lfht_for_each_entry (session->snapshot.output_ht->ht, &iter.iter, output, node.node) {
+       for (auto *output : lttng::urcu::lfht_iteration_adapter<snapshot_output,
+                                                               decltype(snapshot_output::node),
+                                                               &snapshot_output::node>(
+                    *session->snapshot.output_ht->ht)) {
                LTTNG_ASSERT(output->consumer);
                list[idx].id = output->id;
                list[idx].max_size = output->max_size;
@@ -4222,6 +4272,7 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
                        ret = -LTTNG_ERR_INVALID;
                        goto error;
                }
+
                if (output->consumer->type == CONSUMER_DST_LOCAL) {
                        if (lttng_strncpy(list[idx].ctrl_url,
                                          output->consumer->dst.session_root_path,
@@ -4248,14 +4299,14 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
                                goto error;
                        }
                }
+
                idx++;
        }
 
        *outputs = list;
-       list = NULL;
+       list = nullptr;
        ret = session->snapshot.nb_output;
 error:
-       rcu_read_unlock();
        free(list);
 end:
        return ret;
@@ -4267,12 +4318,10 @@ end:
  *
  * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
  */
-static int check_regenerate_metadata_support(struct ltt_session *session)
+static int check_regenerate_metadata_support(const ltt_session::locked_ref& session)
 {
        int ret;
 
-       LTTNG_ASSERT(session);
-
        if (session->live_timer != 0) {
                ret = LTTNG_ERR_LIVE_SESSION;
                goto end;
@@ -4314,12 +4363,10 @@ end:
  *
  * Return LTTNG_OK on success or else a LTTNG_ERR code.
  */
-int cmd_regenerate_metadata(struct ltt_session *session)
+int cmd_regenerate_metadata(const ltt_session::locked_ref& session)
 {
        int ret;
 
-       LTTNG_ASSERT(session);
-
        ret = check_regenerate_metadata_support(session);
        if (ret) {
                goto end;
@@ -4354,12 +4401,10 @@ end:
  *
  * Return LTTNG_OK on success or else a LTTNG_ERR code.
  */
-int cmd_regenerate_statedump(struct ltt_session *session)
+int cmd_regenerate_statedump(const ltt_session::locked_ref& session)
 {
        int ret;
 
-       LTTNG_ASSERT(session);
-
        if (!session->active) {
                ret = LTTNG_ERR_SESSION_NOT_STARTED;
                goto end;
@@ -4423,7 +4468,7 @@ synchronize_tracer_notifier_register(struct notification_thread_handle *notifica
        trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
        trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
 
-       session_lock_list();
+       const auto list_lock = lttng::sessiond::lock_session_list();
        switch (trigger_domain) {
        case LTTNG_DOMAIN_KERNEL:
        {
@@ -4442,6 +4487,8 @@ synchronize_tracer_notifier_register(struct notification_thread_handle *notifica
                                    (int) trigger_owner,
                                    ret_code);
                        }
+
+                       return ret_code;
                }
                break;
        }
@@ -4450,6 +4497,7 @@ synchronize_tracer_notifier_register(struct notification_thread_handle *notifica
                break;
        case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_LOG4J:
+       case LTTNG_DOMAIN_LOG4J2:
        case LTTNG_DOMAIN_PYTHON:
        {
                /* Agent domains. */
@@ -4459,7 +4507,7 @@ synchronize_tracer_notifier_register(struct notification_thread_handle *notifica
                        agt = agent_create(trigger_domain);
                        if (!agt) {
                                ret_code = LTTNG_ERR_NOMEM;
-                               goto end_unlock_session_list;
+                               return ret_code;
                        }
 
                        agent_add(agt, the_trigger_agents_ht_by_domain);
@@ -4467,7 +4515,7 @@ synchronize_tracer_notifier_register(struct notification_thread_handle *notifica
 
                ret_code = (lttng_error_code) trigger_agent_enable(trigger, agt);
                if (ret_code != LTTNG_OK) {
-                       goto end_unlock_session_list;
+                       return ret_code;
                }
 
                break;
@@ -4477,17 +4525,13 @@ synchronize_tracer_notifier_register(struct notification_thread_handle *notifica
                abort();
        }
 
-       ret_code = LTTNG_OK;
-end_unlock_session_list:
-       session_unlock_list();
-       return ret_code;
+       return LTTNG_OK;
 }
 
-enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds,
-                                          struct lttng_trigger *trigger,
-                                          bool is_trigger_anonymous,
-                                          struct notification_thread_handle *notification_thread,
-                                          struct lttng_trigger **return_trigger)
+lttng::ctl::trigger cmd_register_trigger(const struct lttng_credentials *cmd_creds,
+                                        struct lttng_trigger *trigger,
+                                        bool is_trigger_anonymous,
+                                        struct notification_thread_handle *notification_thread)
 {
        enum lttng_error_code ret_code;
        const char *trigger_name;
@@ -4512,12 +4556,13 @@ enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_c
         */
        if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) {
                if (lttng_credentials_get_uid(cmd_creds) != 0) {
-                       ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
-                           trigger_name,
-                           (int) trigger_owner,
-                           (int) lttng_credentials_get_uid(cmd_creds));
-                       ret_code = LTTNG_ERR_INVALID_TRIGGER;
-                       goto end;
+                       LTTNG_THROW_CTL(
+                               fmt::format(
+                                       "Trigger credentials do not match the command credentials: trigger_name = `{}`, trigger_owner_uid={}, command_creds_uid={}",
+                                       trigger_name,
+                                       trigger_owner,
+                                       lttng_credentials_get_uid(cmd_creds)),
+                               LTTNG_ERR_INVALID_TRIGGER);
                }
        }
 
@@ -4527,11 +4572,12 @@ enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_c
         */
        ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds);
        if (ret_code != LTTNG_OK) {
-               ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
-                   trigger_name,
-                   (int) trigger_owner,
-                   ret_code);
-               goto end;
+               LTTNG_THROW_CTL(
+                       fmt::format(
+                               "Failed to generate bytecode of trigger: trigger_name=`{}`, trigger_owner_uid={}",
+                               trigger_name,
+                               trigger_owner),
+                       ret_code);
        }
 
        /*
@@ -4547,11 +4593,12 @@ enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_c
        ret_code = notification_thread_command_register_trigger(
                notification_thread, trigger, is_trigger_anonymous);
        if (ret_code != LTTNG_OK) {
-               DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
-                   trigger_name,
-                   (int) trigger_owner,
-                   ret_code);
-               goto end;
+               LTTNG_THROW_CTL(
+                       fmt::format(
+                               "Failed to register trigger to notification thread: trigger_name=`{}`, trigger_owner_uid={}",
+                               trigger_name,
+                               trigger_owner),
+                       ret_code);
        }
 
        trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
@@ -4564,8 +4611,7 @@ enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_c
                ret_code = synchronize_tracer_notifier_register(
                        notification_thread, trigger, cmd_creds);
                if (ret_code != LTTNG_OK) {
-                       ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code));
-                       goto end;
+                       LTTNG_THROW_CTL("Failed to register tracer notifier", ret_code);
                }
        }
 
@@ -4576,14 +4622,9 @@ enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_c
         * reference to the trigger so the caller doesn't have to care if those
         * are distinct instances or not.
         */
-       if (ret_code == LTTNG_OK) {
-               lttng_trigger_get(trigger);
-               *return_trigger = trigger;
-               /* Ownership of trigger was transferred to caller. */
-               trigger = NULL;
-       }
-end:
-       return ret_code;
+       LTTNG_ASSERT(ret_code == LTTNG_OK);
+       lttng_trigger_get(trigger);
+       return lttng::ctl::trigger(trigger);
 }
 
 static enum lttng_error_code
@@ -4598,12 +4639,12 @@ synchronize_tracer_notifier_unregister(const struct lttng_trigger *trigger)
        LTTNG_ASSERT(lttng_condition_get_type(condition) ==
                     LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
 
-       session_lock_list();
+       const auto list_lock = lttng::sessiond::lock_session_list();
        switch (trigger_domain) {
        case LTTNG_DOMAIN_KERNEL:
                ret_code = kernel_unregister_event_notifier(trigger);
                if (ret_code != LTTNG_OK) {
-                       goto end_unlock_session_list;
+                       return ret_code;
                }
 
                break;
@@ -4612,6 +4653,7 @@ synchronize_tracer_notifier_unregister(const struct lttng_trigger *trigger)
                break;
        case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_LOG4J:
+       case LTTNG_DOMAIN_LOG4J2:
        case LTTNG_DOMAIN_PYTHON:
        {
                /* Agent domains. */
@@ -4624,7 +4666,7 @@ synchronize_tracer_notifier_unregister(const struct lttng_trigger *trigger)
                LTTNG_ASSERT(agt);
                ret_code = (lttng_error_code) trigger_agent_disable(trigger, agt);
                if (ret_code != LTTNG_OK) {
-                       goto end_unlock_session_list;
+                       return ret_code;
                }
 
                break;
@@ -4634,11 +4676,7 @@ synchronize_tracer_notifier_unregister(const struct lttng_trigger *trigger)
                abort();
        }
 
-       ret_code = LTTNG_OK;
-
-end_unlock_session_list:
-       session_unlock_list();
-       return ret_code;
+       return LTTNG_OK;
 }
 
 enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
@@ -4649,7 +4687,7 @@ enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd
        const char *trigger_name;
        uid_t trigger_owner;
        enum lttng_trigger_status trigger_status;
-       struct lttng_trigger *sessiond_trigger = NULL;
+       struct lttng_trigger *sessiond_trigger = nullptr;
 
        trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
        trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
@@ -4735,7 +4773,7 @@ enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx,
 {
        int ret;
        enum lttng_error_code ret_code;
-       struct lttng_triggers *triggers = NULL;
+       struct lttng_triggers *triggers = nullptr;
 
        /* Get the set of triggers from the notification thread. */
        ret_code = notification_thread_command_list_triggers(
@@ -4751,7 +4789,7 @@ enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx,
        }
 
        *return_triggers = triggers;
-       triggers = NULL;
+       triggers = nullptr;
        ret_code = LTTNG_OK;
 end:
        lttng_triggers_destroy(triggers);
@@ -4766,12 +4804,12 @@ cmd_execute_error_query(const struct lttng_credentials *cmd_creds,
 {
        enum lttng_error_code ret_code;
        const struct lttng_trigger *query_target_trigger;
-       const struct lttng_action *query_target_action = NULL;
-       struct lttng_trigger *matching_trigger = NULL;
+       const struct lttng_action *query_target_action = nullptr;
+       struct lttng_trigger *matching_trigger = nullptr;
        const char *trigger_name;
        uid_t trigger_owner;
        enum lttng_trigger_status trigger_status;
-       struct lttng_error_query_results *results = NULL;
+       struct lttng_error_query_results *results = nullptr;
 
        switch (lttng_error_query_get_target_type(query)) {
        case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
@@ -4796,7 +4834,7 @@ cmd_execute_error_query(const struct lttng_credentials *cmd_creds,
        }
 
        /* No longer needed. */
-       query_target_trigger = NULL;
+       query_target_trigger = nullptr;
 
        if (lttng_error_query_get_target_type(query) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) {
                /* Get the sessiond-side version of the target action. */
@@ -4886,7 +4924,7 @@ cmd_execute_error_query(const struct lttng_credentials *cmd_creds,
        }
 
        *_results = results;
-       results = NULL;
+       results = nullptr;
        ret_code = LTTNG_OK;
 end:
        lttng_trigger_put(matching_trigger);
@@ -4901,21 +4939,18 @@ end:
  * Return LTTNG_OK on success or a LTTNG_ERR code.
  */
 static enum lttng_error_code set_relayd_for_snapshot(struct consumer_output *output,
-                                                    const struct ltt_session *session)
+                                                    const ltt_session::locked_ref& session)
 {
        enum lttng_error_code status = LTTNG_OK;
-       struct lttng_ht_iter iter;
-       struct consumer_socket *socket;
        LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
        const char *base_path;
 
        LTTNG_ASSERT(output);
-       LTTNG_ASSERT(session);
 
        DBG2("Set relayd object from snapshot output");
 
        if (session->current_trace_chunk) {
-               enum lttng_trace_chunk_status chunk_status = lttng_trace_chunk_get_id(
+               const lttng_trace_chunk_status chunk_status = lttng_trace_chunk_get_id(
                        session->current_trace_chunk, &current_chunk_id.value);
 
                if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) {
@@ -4946,8 +4981,10 @@ static enum lttng_error_code set_relayd_for_snapshot(struct consumer_output *out
         * For each consumer socket, create and send the relayd object of the
         * snapshot output.
         */
-       rcu_read_lock();
-       cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) {
+       for (auto *socket :
+            lttng::urcu::lfht_iteration_adapter<consumer_socket,
+                                                decltype(consumer_socket::node),
+                                                &consumer_socket::node>(*output->socks->ht)) {
                pthread_mutex_lock(socket->lock);
                status = send_consumer_relayd_sockets(
                        session->id,
@@ -4957,16 +4994,14 @@ static enum lttng_error_code set_relayd_for_snapshot(struct consumer_output *out
                        session->hostname,
                        base_path,
                        session->live_timer,
-                       current_chunk_id.is_set ? &current_chunk_id.value : NULL,
+                       current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
                        session->creation_time,
                        session->name_contains_creation_time);
                pthread_mutex_unlock(socket->lock);
                if (status != LTTNG_OK) {
-                       rcu_read_unlock();
                        goto error;
                }
        }
-       rcu_read_unlock();
 
 error:
        return status;
@@ -4979,14 +5014,12 @@ error:
  */
 static enum lttng_error_code record_kernel_snapshot(struct ltt_kernel_session *ksess,
                                                    const struct consumer_output *output,
-                                                   const struct ltt_session *session,
                                                    uint64_t nb_packets_per_stream)
 {
        enum lttng_error_code status;
 
        LTTNG_ASSERT(ksess);
        LTTNG_ASSERT(output);
-       LTTNG_ASSERT(session);
 
        status = kernel_snapshot_record(ksess, output, nb_packets_per_stream);
        return status;
@@ -4999,29 +5032,28 @@ static enum lttng_error_code record_kernel_snapshot(struct ltt_kernel_session *k
  */
 static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
                                                 const struct consumer_output *output,
-                                                const struct ltt_session *session,
                                                 uint64_t nb_packets_per_stream)
 {
        enum lttng_error_code status;
 
        LTTNG_ASSERT(usess);
        LTTNG_ASSERT(output);
-       LTTNG_ASSERT(session);
 
        status = ust_app_snapshot_record(usess, output, nb_packets_per_stream);
        return status;
 }
 
-static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session *session,
+static uint64_t get_session_size_one_more_packet_per_stream(const ltt_session::locked_ref& session,
                                                            uint64_t cur_nr_packets)
 {
        uint64_t tot_size = 0;
 
        if (session->kernel_session) {
-               struct ltt_kernel_channel *chan;
-               const struct ltt_kernel_session *ksess = session->kernel_session;
+               struct ltt_kernel_session *ksess = session->kernel_session;
 
-               cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
+               for (auto chan : lttng::urcu::list_iteration_adapter<ltt_kernel_channel,
+                                                                    &ltt_kernel_channel::list>(
+                            ksess->channel_list.head)) {
                        if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
                                /*
                                 * Don't take channel into account if we
@@ -5062,7 +5094,7 @@ static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_ses
  * an approximation: for instance, applications could appear/disappear
  * in between this call and actually grabbing data.
  */
-static int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
+static int64_t get_session_nb_packets_per_stream(const ltt_session::locked_ref& session,
                                                 uint64_t max_size)
 {
        int64_t size_left;
@@ -5095,7 +5127,7 @@ static int64_t get_session_nb_packets_per_stream(const struct ltt_session *sessi
        return cur_nb_packets;
 }
 
-static enum lttng_error_code snapshot_record(struct ltt_session *session,
+static enum lttng_error_code snapshot_record(const ltt_session::locked_ref& session,
                                             const struct snapshot_output *snapshot_output)
 {
        int64_t nb_packets_per_stream;
@@ -5103,10 +5135,10 @@ static enum lttng_error_code snapshot_record(struct ltt_session *session,
        int ret;
        enum lttng_error_code ret_code = LTTNG_OK;
        struct lttng_trace_chunk *snapshot_trace_chunk;
-       struct consumer_output *original_ust_consumer_output = NULL;
-       struct consumer_output *original_kernel_consumer_output = NULL;
-       struct consumer_output *snapshot_ust_consumer_output = NULL;
-       struct consumer_output *snapshot_kernel_consumer_output = NULL;
+       struct consumer_output *original_ust_consumer_output = nullptr;
+       struct consumer_output *original_kernel_consumer_output = nullptr;
+       struct consumer_output *snapshot_ust_consumer_output = nullptr;
+       struct consumer_output *snapshot_kernel_consumer_output = nullptr;
 
        ret = snprintf(snapshot_chunk_name,
                       sizeof(snapshot_chunk_name),
@@ -5188,9 +5220,9 @@ static enum lttng_error_code snapshot_record(struct ltt_session *session,
                goto error;
        }
        LTTNG_ASSERT(!session->current_trace_chunk);
-       ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL);
+       ret = session_set_trace_chunk(session, snapshot_trace_chunk, nullptr);
        lttng_trace_chunk_put(snapshot_trace_chunk);
-       snapshot_trace_chunk = NULL;
+       snapshot_trace_chunk = nullptr;
        if (ret) {
                ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
                    session->name);
@@ -5208,7 +5240,6 @@ static enum lttng_error_code snapshot_record(struct ltt_session *session,
        if (session->kernel_session) {
                ret_code = record_kernel_snapshot(session->kernel_session,
                                                  snapshot_kernel_consumer_output,
-                                                 session,
                                                  nb_packets_per_stream);
                if (ret_code != LTTNG_OK) {
                        goto error_close_trace_chunk;
@@ -5216,17 +5247,15 @@ static enum lttng_error_code snapshot_record(struct ltt_session *session,
        }
 
        if (session->ust_session) {
-               ret_code = record_ust_snapshot(session->ust_session,
-                                              snapshot_ust_consumer_output,
-                                              session,
-                                              nb_packets_per_stream);
+               ret_code = record_ust_snapshot(
+                       session->ust_session, snapshot_ust_consumer_output, nb_packets_per_stream);
                if (ret_code != LTTNG_OK) {
                        goto error_close_trace_chunk;
                }
        }
 
 error_close_trace_chunk:
-       if (session_set_trace_chunk(session, NULL, &snapshot_trace_chunk)) {
+       if (session_set_trace_chunk(session, nullptr, &snapshot_trace_chunk)) {
                ERR("Failed to release the current trace chunk of session \"%s\"", session->name);
                ret_code = LTTNG_ERR_UNK;
        }
@@ -5234,7 +5263,7 @@ error_close_trace_chunk:
        if (session_close_trace_chunk(session,
                                      snapshot_trace_chunk,
                                      LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION,
-                                     NULL)) {
+                                     nullptr)) {
                /*
                 * Don't goto end; make sure the chunk is closed for the session
                 * to allow future snapshots.
@@ -5244,7 +5273,7 @@ error_close_trace_chunk:
        }
 
        lttng_trace_chunk_put(snapshot_trace_chunk);
-       snapshot_trace_chunk = NULL;
+       snapshot_trace_chunk = nullptr;
 error:
        if (original_ust_consumer_output) {
                session->ust_session->consumer = original_ust_consumer_output;
@@ -5265,7 +5294,7 @@ error:
  *
  * Return LTTNG_OK on success or else a LTTNG_ERR code.
  */
-int cmd_snapshot_record(struct ltt_session *session,
+int cmd_snapshot_record(const ltt_session::locked_ref& session,
                        const struct lttng_snapshot_output *output,
                        int wait __attribute__((unused)))
 {
@@ -5273,9 +5302,8 @@ int cmd_snapshot_record(struct ltt_session *session,
        int ret;
        unsigned int snapshot_success = 0;
        char datetime[16];
-       struct snapshot_output *tmp_output = NULL;
+       struct snapshot_output *tmp_output = nullptr;
 
-       LTTNG_ASSERT(session);
        LTTNG_ASSERT(output);
 
        DBG("Cmd snapshot record for session %s", session->name);
@@ -5317,7 +5345,7 @@ int cmd_snapshot_record(struct ltt_session *session,
                                           output->data_url,
                                           session->consumer,
                                           tmp_output,
-                                          NULL);
+                                          nullptr);
                if (ret < 0) {
                        if (ret == -ENOMEM) {
                                cmd_ret = LTTNG_ERR_NOMEM;
@@ -5337,12 +5365,11 @@ int cmd_snapshot_record(struct ltt_session *session,
                }
                snapshot_success = 1;
        } else {
-               struct snapshot_output *sout;
-               struct lttng_ht_iter iter;
-
-               rcu_read_lock();
-               cds_lfht_for_each_entry (
-                       session->snapshot.output_ht->ht, &iter.iter, sout, node.node) {
+               for (auto *sout :
+                    lttng::urcu::lfht_iteration_adapter<snapshot_output,
+                                                        decltype(snapshot_output::node),
+                                                        &snapshot_output::node>(
+                            *session->snapshot.output_ht->ht)) {
                        struct snapshot_output output_copy;
 
                        /*
@@ -5365,19 +5392,17 @@ int cmd_snapshot_record(struct ltt_session *session,
                                                  output->name,
                                                  sizeof(output_copy.name))) {
                                        cmd_ret = LTTNG_ERR_INVALID;
-                                       rcu_read_unlock();
                                        goto error;
                                }
                        }
 
                        cmd_ret = snapshot_record(session, &output_copy);
                        if (cmd_ret != LTTNG_OK) {
-                               rcu_read_unlock();
                                goto error;
                        }
+
                        snapshot_success = 1;
                }
-               rcu_read_unlock();
        }
 
        if (snapshot_success) {
@@ -5390,17 +5415,15 @@ error:
        if (tmp_output) {
                snapshot_output_destroy(tmp_output);
        }
+
        return cmd_ret;
 }
 
 /*
  * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
  */
-int cmd_set_session_shm_path(struct ltt_session *session, const char *shm_path)
+int cmd_set_session_shm_path(const ltt_session::locked_ref& session, const char *shm_path)
 {
-       /* Safety net */
-       LTTNG_ASSERT(session);
-
        /*
         * Can only set shm path before session is started.
         */
@@ -5408,10 +5431,9 @@ int cmd_set_session_shm_path(struct ltt_session *session, const char *shm_path)
                return LTTNG_ERR_SESSION_STARTED;
        }
 
-       strncpy(session->shm_path, shm_path, sizeof(session->shm_path));
-       session->shm_path[sizeof(session->shm_path) - 1] = '\0';
-
-       return LTTNG_OK;
+       /* Report an error if shm_path is too long or not null-terminated. */
+       const auto copy_ret = lttng_strncpy(session->shm_path, shm_path, sizeof(session->shm_path));
+       return copy_ret == 0 ? LTTNG_OK : LTTNG_ERR_INVALID;
 }
 
 /*
@@ -5422,7 +5444,7 @@ int cmd_set_session_shm_path(struct ltt_session *session, const char *shm_path)
  *
  * Returns LTTNG_OK on success or else a negative LTTng error code.
  */
-int cmd_rotate_session(struct ltt_session *session,
+int cmd_rotate_session(const ltt_session::locked_ref& session,
                       struct lttng_rotate_session_return *rotate_return,
                       bool quiet_rotation,
                       enum lttng_trace_chunk_command_type command)
@@ -5430,14 +5452,12 @@ int cmd_rotate_session(struct ltt_session *session,
        int ret;
        uint64_t ongoing_rotation_chunk_id;
        enum lttng_error_code cmd_ret = LTTNG_OK;
-       struct lttng_trace_chunk *chunk_being_archived = NULL;
-       struct lttng_trace_chunk *new_trace_chunk = NULL;
+       struct lttng_trace_chunk *chunk_being_archived = nullptr;
+       struct lttng_trace_chunk *new_trace_chunk = nullptr;
        enum lttng_trace_chunk_status chunk_status;
        bool failed_to_rotate = false;
        enum lttng_error_code rotation_fail_code = LTTNG_OK;
 
-       LTTNG_ASSERT(session);
-
        if (!session->has_been_started) {
                cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
                goto end;
@@ -5498,7 +5518,8 @@ int cmd_rotate_session(struct ltt_session *session,
        }
 
        if (session->active) {
-               new_trace_chunk = session_create_new_trace_chunk(session, NULL, NULL, NULL);
+               new_trace_chunk =
+                       session_create_new_trace_chunk(session, nullptr, nullptr, nullptr);
                if (!new_trace_chunk) {
                        cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
                        goto error;
@@ -5576,7 +5597,7 @@ int cmd_rotate_session(struct ltt_session *session,
        }
 
        session->chunk_being_archived = chunk_being_archived;
-       chunk_being_archived = NULL;
+       chunk_being_archived = nullptr;
        if (!quiet_rotation) {
                ret = notification_thread_command_session_rotation_ongoing(
                        the_notification_thread_handle, session->id, ongoing_rotation_chunk_id);
@@ -5609,7 +5630,7 @@ error:
  *
  * Return LTTNG_OK on success or else an LTTNG_ERR code.
  */
-int cmd_rotate_get_info(struct ltt_session *session,
+int cmd_rotate_get_info(const ltt_session::locked_ref& session,
                        struct lttng_rotation_get_info_return *info_return,
                        uint64_t rotation_id)
 {
@@ -5760,17 +5781,14 @@ end:
  *
  * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
  */
-int cmd_rotation_set_schedule(struct ltt_session *session,
+int cmd_rotation_set_schedule(const ltt_session::locked_ref& session,
                              bool activate,
                              enum lttng_rotation_schedule_type schedule_type,
-                             uint64_t new_value,
-                             struct notification_thread_handle *notification_thread_handle)
+                             uint64_t new_value)
 {
        int ret;
        uint64_t *parameter_value;
 
-       LTTNG_ASSERT(session);
-
        DBG("Cmd rotate set schedule session %s", session->name);
 
        if (session->live_timer || !session->output_traces) {
@@ -5866,18 +5884,22 @@ int cmd_rotation_set_schedule(struct ltt_session *session,
                break;
        case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
                if (activate) {
-                       ret = subscribe_session_consumed_size_rotation(
-                               session, new_value, notification_thread_handle);
-                       if (ret) {
-                               ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
+                       try {
+                               the_rotation_thread_handle->subscribe_session_consumed_size_rotation(
+                                       *session, new_value);
+                       } catch (const std::exception& e) {
+                               ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
+                                   e.what());
                                ret = LTTNG_ERR_UNK;
                                goto end;
                        }
                } else {
-                       ret = unsubscribe_session_consumed_size_rotation(
-                               session, notification_thread_handle);
-                       if (ret) {
-                               ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
+                       try {
+                               the_rotation_thread_handle
+                                       ->unsubscribe_session_consumed_size_rotation(*session);
+                       } catch (const std::exception& e) {
+                               ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
+                                   e.what());
                                ret = LTTNG_ERR_UNK;
                                goto end;
                        }
@@ -5931,18 +5953,18 @@ static enum lttng_error_code wait_on_path(void *path_data)
  * Returns a pointer to a handler to run on completion of a command.
  * Returns NULL if no handler has to be run for the last command executed.
  */
-const struct cmd_completion_handler *cmd_pop_completion_handler(void)
+const struct cmd_completion_handler *cmd_pop_completion_handler()
 {
        struct cmd_completion_handler *handler = current_completion_handler;
 
-       current_completion_handler = NULL;
+       current_completion_handler = nullptr;
        return handler;
 }
 
 /*
  * Init command subsystem.
  */
-void cmd_init(void)
+void cmd_init()
 {
        /*
         * Set network sequence index to 1 for streams to match a relayd
This page took 0.074741 seconds and 4 git commands to generate.