Use compiler-agnostic defines to silence warning
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.cpp
index e27de2436c1cd994aed3bfbf6c1cd39a48cf3370..5d6a349ea413c3343aafe68f313d25c2170b3138 100644 (file)
@@ -16,7 +16,6 @@
 #include "lttng-syscall.hpp"
 #include "modprobe.hpp"
 #include "notification-thread-commands.hpp"
-#include "rotate.hpp"
 #include "sessiond-config.hpp"
 #include "tracker.hpp"
 #include "utils.hpp"
 #include <common/hashtable/utils.hpp>
 #include <common/kernel-ctl/kernel-ctl.hpp>
 #include <common/kernel-ctl/kernel-ioctl.hpp>
+#include <common/scope-exit.hpp>
 #include <common/sessiond-comm/sessiond-comm.hpp>
 #include <common/trace-chunk.hpp>
 #include <common/tracker.hpp>
+#include <common/urcu.hpp>
 #include <common/utils.hpp>
 
 #include <lttng/condition/event-rule-matches-internal.hpp>
 #include <sys/types.h>
 #include <unistd.h>
 
+namespace {
 /*
  * Key used to reference a channel between the sessiond and the consumer. This
  * is only read and updated with the session_list lock held.
  */
-static uint64_t next_kernel_channel_key;
+uint64_t next_kernel_channel_key;
 
-static const char *module_proc_lttng = "/proc/lttng";
+const char *module_proc_lttng = "/proc/lttng";
 
-static int kernel_tracer_fd = -1;
-static int kernel_tracer_event_notifier_group_fd = -1;
-static int kernel_tracer_event_notifier_group_notification_fd = -1;
-static struct cds_lfht *kernel_token_to_event_notifier_rule_ht;
+int kernel_tracer_fd = -1;
+nonstd::optional<enum lttng_kernel_tracer_status> kernel_tracer_status = nonstd::nullopt;
+int kernel_tracer_event_notifier_group_fd = -1;
+int kernel_tracer_event_notifier_group_notification_fd = -1;
+struct cds_lfht *kernel_token_to_event_notifier_rule_ht;
+
+const char *kernel_tracer_status_to_str(lttng_kernel_tracer_status status)
+{
+       switch (status) {
+       case LTTNG_KERNEL_TRACER_STATUS_INITIALIZED:
+               return "LTTNG_KERNEL_TRACER_STATUS_INITIALIZED";
+       case LTTNG_KERNEL_TRACER_STATUS_ERR_UNKNOWN:
+               return "LTTNG_KERNEL_TRACER_STATUS_ERR_UNKNOWN";
+       case LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT:
+               return "LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT";
+       case LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER:
+               return "LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER";
+       case LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG:
+               return "LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG";
+       case LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH:
+               return "LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH";
+       case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN:
+               return "LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN";
+       case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING:
+               return "LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING";
+       case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE:
+               return "LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE";
+       }
+
+       abort();
+}
+
+/*
+ * On some architectures, calling convention details are embedded in the symbol
+ * addresses. Uprobe requires a "clean" symbol offset (or at least, an address
+ * where an instruction boundary would be legal) to add
+ * instrumentation. sanitize_uprobe_offset implements that sanitization logic on
+ * a per-architecture basis.
+ */
+#if defined(__arm__) || defined(__aarch64__)
+static inline uint64_t sanitize_uprobe_offset(uint64_t raw_offset)
+{
+       /*
+        * The least significant bit is used when branching to switch to thumb
+        * ISA. However, it's an invalid address for us; mask the least
+        * significant bit.
+        */
+       return raw_offset &= ~0b1;
+}
+#else /* defined(__arm__) || defined(__aarch64__) */
+inline uint64_t sanitize_uprobe_offset(uint64_t raw_offset)
+{
+       return raw_offset;
+}
+#endif
+} /* namespace */
 
 /*
  * Add context on a kernel channel.
@@ -109,13 +163,11 @@ error:
  * Create a new kernel session, register it to the kernel tracer and add it to
  * the session daemon session.
  */
-int kernel_create_session(struct ltt_session *session)
+int kernel_create_session(const ltt_session::locked_ref& session)
 {
        int ret;
        struct ltt_kernel_session *lks;
 
-       LTTNG_ASSERT(session);
-
        /* Allocate data structure */
        lks = trace_kernel_create_session();
        if (lks == nullptr) {
@@ -451,7 +503,7 @@ static int userspace_probe_add_callsite(const struct lttng_userspace_probe_locat
                        goto end;
                }
 
-               callsite.u.uprobe.offset = offset;
+               callsite.u.uprobe.offset = sanitize_uprobe_offset(offset);
                ret = kernctl_add_callsite(fd, &callsite);
                if (ret) {
                        WARN("Failed to add callsite to ELF userspace probe.");
@@ -478,7 +530,7 @@ static int userspace_probe_add_callsite(const struct lttng_userspace_probe_locat
                        goto end;
                }
                for (i = 0; i < offsets_count; i++) {
-                       callsite.u.uprobe.offset = offsets[i];
+                       callsite.u.uprobe.offset = sanitize_uprobe_offset(offsets[i]);
                        ret = kernctl_add_callsite(fd, &callsite);
                        if (ret) {
                                WARN("Failed to add callsite to SDT userspace probe");
@@ -796,9 +848,8 @@ static int kernel_disable_event_notifier_rule(struct ltt_kernel_event_notifier_r
 
        LTTNG_ASSERT(event);
 
-       rcu_read_lock();
+       const lttng::urcu::read_lock_guard read_lock;
        cds_lfht_del(kernel_token_to_event_notifier_rule_ht, &event->ht_node);
-       rcu_read_unlock();
 
        ret = kernctl_disable(event->fd);
        if (ret < 0) {
@@ -1245,7 +1296,7 @@ error:
 void kernel_wait_quiescent()
 {
        int ret;
-       int fd = kernel_tracer_fd;
+       const int fd = kernel_tracer_fd;
 
        DBG("Kernel quiescent wait on %d", fd);
 
@@ -1585,26 +1636,22 @@ void kernel_destroy_session(struct ltt_kernel_session *ksess)
         * have to send a command to clean them up or else they leaked.
         */
        if (!ksess->output_traces && ksess->consumer_fds_sent) {
-               int ret;
-               struct consumer_socket *socket;
-               struct lttng_ht_iter iter;
-
-               /* For each consumer socket. */
-               rcu_read_lock();
-               cds_lfht_for_each_entry (
-                       ksess->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>(
+                            *ksess->consumer->socks->ht)) {
                        struct ltt_kernel_channel *chan;
 
                        /* For each channel, ask the consumer to destroy it. */
                        cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
-                               ret = kernel_consumer_destroy_channel(socket, chan);
+                               const auto ret = kernel_consumer_destroy_channel(socket, chan);
                                if (ret < 0) {
                                        /* Consumer is probably dead. Use next socket. */
                                        continue;
                                }
                        }
                }
-               rcu_read_unlock();
        }
 
        /* Close any relayd session */
@@ -1664,8 +1711,6 @@ enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess,
 {
        int err, ret, saved_metadata_fd;
        enum lttng_error_code status = LTTNG_OK;
-       struct consumer_socket *socket;
-       struct lttng_ht_iter iter;
        struct ltt_kernel_metadata *saved_metadata;
        char *trace_path = nullptr;
        size_t consumer_path_offset = 0;
@@ -1680,8 +1725,6 @@ enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess,
        saved_metadata = ksess->metadata;
        saved_metadata_fd = ksess->metadata_stream_fd;
 
-       rcu_read_lock();
-
        ret = kernel_open_metadata(ksess);
        if (ret < 0) {
                status = LTTNG_ERR_KERN_META_FAIL;
@@ -1699,8 +1742,11 @@ enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess,
                status = LTTNG_ERR_INVALID;
                goto error;
        }
-       /* Send metadata to consumer and snapshot everything. */
-       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)) {
                struct ltt_kernel_channel *chan;
 
                pthread_mutex_lock(socket->lock);
@@ -1757,7 +1803,6 @@ error:
        /* Restore metadata state.*/
        ksess->metadata = saved_metadata;
        ksess->metadata_stream_fd = saved_metadata_fd;
-       rcu_read_unlock();
        free(trace_path);
        return status;
 }
@@ -1841,12 +1886,10 @@ int kernel_supports_event_notifiers()
  *
  * Return LTTNG_OK on success or else an LTTng error code.
  */
-enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
+enum lttng_error_code kernel_rotate_session(const ltt_session::locked_ref& session)
 {
        int ret;
        enum lttng_error_code status = LTTNG_OK;
-       struct consumer_socket *socket;
-       struct lttng_ht_iter iter;
        struct ltt_kernel_session *ksess = session->kernel_session;
 
        LTTNG_ASSERT(ksess);
@@ -1854,13 +1897,14 @@ enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
 
        DBG("Rotate kernel session %s started (session %" PRIu64 ")", session->name, session->id);
 
-       rcu_read_lock();
-
        /*
         * Note that this loop will end after one iteration given that there is
         * only one kernel consumer.
         */
-       cds_lfht_for_each_entry (ksess->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>(
+                    *ksess->consumer->socks->ht)) {
                struct ltt_kernel_channel *chan;
 
                /* For each channel, ask the consumer to rotate it. */
@@ -1892,7 +1936,6 @@ enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
        }
 
 error:
-       rcu_read_unlock();
        return status;
 }
 
@@ -1901,7 +1944,7 @@ enum lttng_error_code kernel_create_channel_subdirectories(const struct ltt_kern
        enum lttng_error_code ret = LTTNG_OK;
        enum lttng_trace_chunk_status chunk_status;
 
-       rcu_read_lock();
+       const lttng::urcu::read_lock_guard read_lock;
        LTTNG_ASSERT(ksess->current_trace_chunk);
 
        /*
@@ -1915,21 +1958,68 @@ enum lttng_error_code kernel_create_channel_subdirectories(const struct ltt_kern
                goto error;
        }
 error:
-       rcu_read_unlock();
        return ret;
 }
 
+/*
+ * Get current kernel tracer status
+ */
+enum lttng_kernel_tracer_status get_kernel_tracer_status()
+{
+       if (!kernel_tracer_status) {
+               return LTTNG_KERNEL_TRACER_STATUS_ERR_UNKNOWN;
+       }
+
+       return *kernel_tracer_status;
+}
+
+/*
+ * Sets the kernel tracer status based on the positive errno code
+ */
+void set_kernel_tracer_status_from_modules_ret(int code)
+{
+       switch (code) {
+       case ENOENT:
+       {
+               kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
+                       LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING);
+               break;
+       }
+       case ENOKEY:
+       case EKEYEXPIRED:
+       case EKEYREVOKED:
+       case EKEYREJECTED:
+       {
+               kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
+                       LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE);
+               break;
+       }
+       default:
+       {
+               kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
+                       LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN);
+               break;
+       }
+       }
+}
+
 /*
  * Setup necessary data for kernel tracer action.
  */
 int init_kernel_tracer()
 {
        int ret;
-       bool is_root = !getuid();
+       const bool is_root = !getuid();
+
+       const auto log_status_on_exit = lttng::make_scope_exit([]() noexcept {
+               DBG_FMT("Kernel tracer status set to `{}`",
+                       kernel_tracer_status_to_str(*kernel_tracer_status));
+       });
 
        /* Modprobe lttng kernel modules */
        ret = modprobe_lttng_control();
        if (ret < 0) {
+               set_kernel_tracer_status_from_modules_ret(-ret);
                goto error;
        }
 
@@ -1937,17 +2027,22 @@ int init_kernel_tracer()
        kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
        if (kernel_tracer_fd < 0) {
                DBG("Failed to open %s", module_proc_lttng);
+               kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
+                       LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG);
                goto error_open;
        }
 
        /* Validate kernel version */
        ret = kernel_validate_version(&the_kernel_tracer_version, &the_kernel_tracer_abi_version);
        if (ret < 0) {
+               kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
+                       LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH);
                goto error_version;
        }
 
        ret = modprobe_lttng_data();
        if (ret < 0) {
+               set_kernel_tracer_status_from_modules_ret(-ret);
                goto error_modules;
        }
 
@@ -1964,6 +2059,8 @@ int init_kernel_tracer()
        ret = kernel_supports_event_notifiers();
        if (ret < 0) {
                ERR("Failed to check for kernel tracer event notifier support");
+               kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
+                       LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER);
                goto error_modules;
        }
        ret = kernel_create_event_notifier_group(&kernel_tracer_event_notifier_group_fd);
@@ -1978,6 +2075,8 @@ int init_kernel_tracer()
                                &kernel_tracer_event_notifier_group_notification_fd);
 
                if (error_code_ret != LTTNG_OK) {
+                       kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
+                               LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER);
                        goto error_modules;
                }
 
@@ -1986,12 +2085,16 @@ int init_kernel_tracer()
                if (error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
                        ERR("Failed to initialize event notifier error accounting for kernel tracer");
                        error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING;
+                       kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
+                               LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER);
                        goto error_modules;
                }
 
                kernel_token_to_event_notifier_rule_ht = cds_lfht_new(
                        DEFAULT_HT_SIZE, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, nullptr);
                if (!kernel_token_to_event_notifier_rule_ht) {
+                       kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
+                               LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER);
                        goto error_token_ht;
                }
        }
@@ -2007,6 +2110,8 @@ int init_kernel_tracer()
                    "work for this session daemon.");
        }
 
+       kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
+               LTTNG_KERNEL_TRACER_STATUS_INITIALIZED);
        return 0;
 
 error_version:
@@ -2051,6 +2156,8 @@ error:
        WARN("No kernel tracer available");
        kernel_tracer_fd = -1;
        if (!is_root) {
+               kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
+                       LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT);
                return LTTNG_ERR_NEED_ROOT_SESSIOND;
        } else {
                return LTTNG_ERR_KERN_NA;
@@ -2106,7 +2213,7 @@ void cleanup_kernel_tracer()
                kernel_tracer_fd = -1;
        }
 
-       free(syscall_table);
+       kernel_tracer_status = nonstd::nullopt;
 }
 
 bool kernel_tracer_is_initialized()
@@ -2119,12 +2226,10 @@ bool kernel_tracer_is_initialized()
  *
  * Return LTTNG_OK on success or else an LTTng error code.
  */
-enum lttng_error_code kernel_clear_session(struct ltt_session *session)
+enum lttng_error_code kernel_clear_session(const ltt_session::locked_ref& session)
 {
        int ret;
        enum lttng_error_code status = LTTNG_OK;
-       struct consumer_socket *socket;
-       struct lttng_ht_iter iter;
        struct ltt_kernel_session *ksess = session->kernel_session;
 
        LTTNG_ASSERT(ksess);
@@ -2132,8 +2237,6 @@ enum lttng_error_code kernel_clear_session(struct ltt_session *session)
 
        DBG("Clear kernel session %s (session %" PRIu64 ")", session->name, session->id);
 
-       rcu_read_lock();
-
        if (ksess->active) {
                ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
                status = LTTNG_ERR_FATAL;
@@ -2144,7 +2247,10 @@ enum lttng_error_code kernel_clear_session(struct ltt_session *session)
         * Note that this loop will end after one iteration given that there is
         * only one kernel consumer.
         */
-       cds_lfht_for_each_entry (ksess->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>(
+                    *ksess->consumer->socks->ht)) {
                struct ltt_kernel_channel *chan;
 
                /* For each channel, ask the consumer to clear it. */
@@ -2189,7 +2295,6 @@ error:
                break;
        }
 end:
-       rcu_read_unlock();
        return status;
 }
 
@@ -2240,7 +2345,7 @@ error:
 enum lttng_error_code
 kernel_destroy_event_notifier_group_notification_fd(int event_notifier_group_notification_fd)
 {
-       enum lttng_error_code ret_code = LTTNG_OK;
+       const lttng_error_code ret_code = LTTNG_OK;
 
        DBG("Closing event notifier group notification file descriptor: fd = %d",
            event_notifier_group_notification_fd);
@@ -2422,11 +2527,12 @@ static enum lttng_error_code kernel_create_event_notifier_rule(
        }
 
        /* Add trigger to kernel token mapping in the hash table. */
-       rcu_read_lock();
-       cds_lfht_add(kernel_token_to_event_notifier_rule_ht,
-                    hash_trigger(trigger),
-                    &event_notifier_rule->ht_node);
-       rcu_read_unlock();
+       {
+               const lttng::urcu::read_lock_guard read_lock;
+               cds_lfht_add(kernel_token_to_event_notifier_rule_ht,
+                            hash_trigger(trigger),
+                            &event_notifier_rule->ht_node);
+       }
 
        DBG("Created kernel event notifier: name = '%s', fd = %d",
            kernel_event_notifier.event.name,
@@ -2488,7 +2594,7 @@ enum lttng_error_code kernel_unregister_event_notifier(const struct lttng_trigge
        enum lttng_error_code error_code_ret;
        int ret;
 
-       rcu_read_lock();
+       const lttng::urcu::read_lock_guard read_lock;
 
        cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht,
                        hash_trigger(trigger),
@@ -2515,7 +2621,6 @@ enum lttng_error_code kernel_unregister_event_notifier(const struct lttng_trigge
        error_code_ret = LTTNG_OK;
 
 error:
-       rcu_read_unlock();
 
        return error_code_ret;
 }
This page took 0.029396 seconds and 4 git commands to generate.