Fix: syscall event rule: emission sites not compared in is_equal
[lttng-tools.git] / src / bin / lttng-sessiond / buffer-registry.cpp
index 360af749d587d6ebe4c5d09807d55d116295100c..b840a7fa91ef0b69931a0f8c2f59c97389d31073 100644 (file)
@@ -6,18 +6,19 @@
  */
 
 #define _LGPL_SOURCE
-#include <inttypes.h>
-
-#include <common/common.hpp>
-#include <common/hashtable/utils.hpp>
-
 #include "buffer-registry.hpp"
 #include "fd-limit.hpp"
-#include "ust-consumer.hpp"
 #include "lttng-ust-ctl.hpp"
 #include "lttng-ust-error.hpp"
+#include "ust-consumer.hpp"
 #include "utils.hpp"
 
+#include <common/common.hpp>
+#include <common/hashtable/utils.hpp>
+#include <common/urcu.hpp>
+
+#include <inttypes.h>
+
 /*
  * Set in main.c during initialization process of the daemon. This contains
  * buffer_reg_uid object which are global registry for per UID buffer. Object
@@ -48,9 +49,8 @@ static int ht_match_reg_uid(struct cds_lfht_node *node, const void *_key)
        LTTNG_ASSERT(reg);
        key = (buffer_reg_uid *) _key;
 
-       if (key->session_id != reg->session_id ||
-                       key->bits_per_long != reg->bits_per_long ||
-                       key->uid != reg->uid) {
+       if (key->session_id != reg->session_id || key->bits_per_long != reg->bits_per_long ||
+           key->uid != reg->uid) {
                goto no_match;
        }
 
@@ -71,14 +71,14 @@ static unsigned long ht_hash_reg_uid(const void *_key, unsigned long seed)
 
        LTTNG_ASSERT(key);
 
-       xored_key = (uint64_t)(key->session_id ^ key->bits_per_long ^ key->uid);
+       xored_key = (uint64_t) (key->session_id ^ key->bits_per_long ^ key->uid);
        return hash_key_u64(&xored_key, seed);
 }
 
 /*
  * Initialize global buffer per UID registry. Should only be called ONCE!.
  */
-void buffer_reg_init_uid_registry(void)
+void buffer_reg_init_uid_registry()
 {
        /* Should be called once. */
        LTTNG_ASSERT(!buffer_registry_uid);
@@ -95,12 +95,16 @@ void buffer_reg_init_uid_registry(void)
  *
  * Return 0 on success else a negative value and regp is untouched.
  */
-int buffer_reg_uid_create(uint64_t session_id, uint32_t bits_per_long, uid_t uid,
-               enum lttng_domain_type domain, struct buffer_reg_uid **regp,
-               const char *root_shm_path, const char *shm_path)
+int buffer_reg_uid_create(uint64_t session_id,
+                         uint32_t bits_per_long,
+                         uid_t uid,
+                         enum lttng_domain_type domain,
+                         struct buffer_reg_uid **regp,
+                         const char *root_shm_path,
+                         const char *shm_path)
 {
        int ret = 0;
-       struct buffer_reg_uid *reg = NULL;
+       struct buffer_reg_uid *reg = nullptr;
 
        LTTNG_ASSERT(regp);
 
@@ -128,7 +132,8 @@ int buffer_reg_uid_create(uint64_t session_id, uint32_t bits_per_long, uid_t uid
                strncpy(reg->shm_path, shm_path, sizeof(reg->shm_path));
                reg->shm_path[sizeof(reg->shm_path) - 1] = '\0';
                DBG3("shm path '%s' is assigned to uid buffer registry for session id %" PRIu64,
-                       reg->shm_path, session_id);
+                    reg->shm_path,
+                    session_id);
        }
        reg->registry->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
        if (!reg->registry->channels) {
@@ -140,7 +145,10 @@ int buffer_reg_uid_create(uint64_t session_id, uint32_t bits_per_long, uid_t uid
        *regp = reg;
 
        DBG3("Buffer registry per UID created id: %" PRIu64 ", ABI: %u, uid: %d, domain: %d",
-                       session_id, bits_per_long, uid, domain);
+            session_id,
+            bits_per_long,
+            uid,
+            domain);
 
        return 0;
 
@@ -161,14 +169,13 @@ void buffer_reg_uid_add(struct buffer_reg_uid *reg)
 
        LTTNG_ASSERT(reg);
 
-       DBG3("Buffer registry per UID adding to global registry with id: %" PRIu64 ,
-                       reg->session_id);
+       DBG3("Buffer registry per UID adding to global registry with id: %" PRIu64,
+            reg->session_id);
 
-       rcu_read_lock();
-       nodep = cds_lfht_add_unique(ht->ht, ht->hash_fct(reg, lttng_ht_seed),
-                       ht->match_fct, reg, &reg->node.node);
+       lttng::urcu::read_lock_guard read_lock;
+       nodep = cds_lfht_add_unique(
+               ht->ht, ht->hash_fct(reg, lttng_ht_seed), ht->match_fct, reg, &reg->node.node);
        LTTNG_ASSERT(nodep == &reg->node.node);
-       rcu_read_unlock();
 }
 
 /*
@@ -177,12 +184,11 @@ void buffer_reg_uid_add(struct buffer_reg_uid *reg)
  *
  * Return the object pointer or NULL on error.
  */
-struct buffer_reg_uid *buffer_reg_uid_find(uint64_t session_id,
-               uint32_t bits_per_long, uid_t uid)
+struct buffer_reg_uid *buffer_reg_uid_find(uint64_t session_id, uint32_t bits_per_long, uid_t uid)
 {
        struct lttng_ht_node_u64 *node;
        struct lttng_ht_iter iter;
-       struct buffer_reg_uid *reg = NULL, key;
+       struct buffer_reg_uid *reg = nullptr, key;
        struct lttng_ht *ht = buffer_registry_uid;
 
        ASSERT_RCU_READ_LOCKED();
@@ -193,16 +199,17 @@ struct buffer_reg_uid *buffer_reg_uid_find(uint64_t session_id,
        key.uid = uid;
 
        DBG3("Buffer registry per UID find id: %" PRIu64 ", ABI: %u, uid: %d",
-                       session_id, bits_per_long, uid);
+            session_id,
+            bits_per_long,
+            uid);
 
        /* Custom lookup function since it's a different key. */
-       cds_lfht_lookup(ht->ht, ht->hash_fct(&key, lttng_ht_seed), ht->match_fct,
-                       &key, &iter.iter);
+       cds_lfht_lookup(ht->ht, ht->hash_fct(&key, lttng_ht_seed), ht->match_fct, &key, &iter.iter);
        node = lttng_ht_iter_get_node_u64(&iter);
        if (!node) {
                goto end;
        }
-       reg = caa_container_of(node, struct buffer_reg_uid, node);
+       reg = lttng::utils::container_of(node, &buffer_reg_uid::node);
 
 end:
        return reg;
@@ -211,7 +218,7 @@ end:
 /*
  * Initialize global buffer per PID registry. Should only be called ONCE!.
  */
-void buffer_reg_init_pid_registry(void)
+void buffer_reg_init_pid_registry()
 {
        /* Should be called once. */
        LTTNG_ASSERT(!buffer_registry_pid);
@@ -226,11 +233,13 @@ void buffer_reg_init_pid_registry(void)
  *
  * Return 0 on success else a negative value and regp is untouched.
  */
-int buffer_reg_pid_create(uint64_t session_id, struct buffer_reg_pid **regp,
-               const char *root_shm_path, const char *shm_path)
+int buffer_reg_pid_create(uint64_t session_id,
+                         struct buffer_reg_pid **regp,
+                         const char *root_shm_path,
+                         const char *shm_path)
 {
        int ret = 0;
-       struct buffer_reg_pid *reg = NULL;
+       struct buffer_reg_pid *reg = nullptr;
 
        LTTNG_ASSERT(regp);
 
@@ -256,7 +265,8 @@ int buffer_reg_pid_create(uint64_t session_id, struct buffer_reg_pid **regp,
                strncpy(reg->shm_path, shm_path, sizeof(reg->shm_path));
                reg->shm_path[sizeof(reg->shm_path) - 1] = '\0';
                DBG3("shm path '%s' is assigned to pid buffer registry for session id %" PRIu64,
-                               reg->shm_path, session_id);
+                    reg->shm_path,
+                    session_id);
        }
        reg->registry->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
        if (!reg->registry->channels) {
@@ -267,8 +277,7 @@ int buffer_reg_pid_create(uint64_t session_id, struct buffer_reg_pid **regp,
        lttng_ht_node_init_u64(&reg->node, reg->session_id);
        *regp = reg;
 
-       DBG3("Buffer registry per PID created with session id: %" PRIu64,
-                       session_id);
+       DBG3("Buffer registry per PID created with session id: %" PRIu64, session_id);
 
        return 0;
 
@@ -287,11 +296,10 @@ void buffer_reg_pid_add(struct buffer_reg_pid *reg)
        LTTNG_ASSERT(reg);
 
        DBG3("Buffer registry per PID adding to global registry with id: %" PRIu64,
-                       reg->session_id);
+            reg->session_id);
 
-       rcu_read_lock();
+       lttng::urcu::read_lock_guard read_lock;
        lttng_ht_add_unique_u64(buffer_registry_pid, &reg->node);
-       rcu_read_unlock();
 }
 
 /*
@@ -304,7 +312,7 @@ struct buffer_reg_pid *buffer_reg_pid_find(uint64_t session_id)
 {
        struct lttng_ht_node_u64 *node;
        struct lttng_ht_iter iter;
-       struct buffer_reg_pid *reg = NULL;
+       struct buffer_reg_pid *reg = nullptr;
        struct lttng_ht *ht = buffer_registry_pid;
 
        DBG3("Buffer registry per PID find id: %" PRIu64, session_id);
@@ -314,7 +322,7 @@ struct buffer_reg_pid *buffer_reg_pid_find(uint64_t session_id)
        if (!node) {
                goto end;
        }
-       reg = caa_container_of(node, struct buffer_reg_pid, node);
+       reg = lttng::utils::container_of(node, &buffer_reg_pid::node);
 
 end:
        return reg;
@@ -325,35 +333,36 @@ end:
  *
  * Return the matching key or -1 if not found.
  */
-int buffer_reg_uid_consumer_channel_key(
-               struct cds_list_head *buffer_reg_uid_list,
-               uint64_t chan_key, uint64_t *consumer_chan_key)
+int buffer_reg_uid_consumer_channel_key(struct cds_list_head *buffer_reg_uid_list,
+                                       uint64_t chan_key,
+                                       uint64_t *consumer_chan_key)
 {
        struct lttng_ht_iter iter;
-       struct buffer_reg_uid *uid_reg = NULL;
-       struct buffer_reg_session *session_reg = NULL;
+       struct buffer_reg_uid *uid_reg = nullptr;
+       struct buffer_reg_session *session_reg = nullptr;
        struct buffer_reg_channel *reg_chan;
        int ret = -1;
 
-       rcu_read_lock();
-       /*
-        * For the per-uid registry, we have to iterate since we don't have the
-        * uid and bitness key.
-        */
-       cds_list_for_each_entry(uid_reg, buffer_reg_uid_list, lnode) {
-               session_reg = uid_reg->registry;
-               cds_lfht_for_each_entry(session_reg->channels->ht,
-                               &iter.iter, reg_chan, node.node) {
-                       if (reg_chan->key == chan_key) {
-                               *consumer_chan_key = reg_chan->consumer_key;
-                               ret = 0;
-                               goto end;
+       {
+               lttng::urcu::read_lock_guard read_lock;
+
+               /*
+                * For the per-uid registry, we have to iterate since we don't have the
+                * uid and bitness key.
+                */
+               cds_list_for_each_entry (uid_reg, buffer_reg_uid_list, lnode) {
+                       session_reg = uid_reg->registry;
+                       cds_lfht_for_each_entry (
+                               session_reg->channels->ht, &iter.iter, reg_chan, node.node) {
+                               if (reg_chan->key == chan_key) {
+                                       *consumer_chan_key = reg_chan->consumer_key;
+                                       ret = 0;
+                                       goto end;
+                               }
                        }
                }
        }
-
 end:
-       rcu_read_unlock();
        return ret;
 }
 
@@ -379,7 +388,7 @@ int buffer_reg_channel_create(uint64_t key, struct buffer_reg_channel **regp)
 
        reg->key = key;
        CDS_INIT_LIST_HEAD(&reg->streams);
-       pthread_mutex_init(&reg->stream_list_lock, NULL);
+       pthread_mutex_init(&reg->stream_list_lock, nullptr);
 
        lttng_ht_node_init_u64(&reg->node, key);
        *regp = reg;
@@ -415,8 +424,7 @@ int buffer_reg_stream_create(struct buffer_reg_stream **regp)
 /*
  * Add stream to the list in the channel.
  */
-void buffer_reg_stream_add(struct buffer_reg_stream *stream,
-               struct buffer_reg_channel *channel)
+void buffer_reg_stream_add(struct buffer_reg_stream *stream, struct buffer_reg_channel *channel)
 {
        LTTNG_ASSERT(stream);
        LTTNG_ASSERT(channel);
@@ -430,15 +438,13 @@ void buffer_reg_stream_add(struct buffer_reg_stream *stream,
 /*
  * Add a buffer registry channel object to the given session.
  */
-void buffer_reg_channel_add(struct buffer_reg_session *session,
-               struct buffer_reg_channel *channel)
+void buffer_reg_channel_add(struct buffer_reg_session *session, struct buffer_reg_channel *channel)
 {
        LTTNG_ASSERT(session);
        LTTNG_ASSERT(channel);
 
-       rcu_read_lock();
+       lttng::urcu::read_lock_guard read_lock;
        lttng_ht_add_unique_u64(session->channels, &channel->node);
-       rcu_read_unlock();
 }
 
 /*
@@ -448,12 +454,11 @@ void buffer_reg_channel_add(struct buffer_reg_session *session,
  *
  * Return the object pointer or NULL on error.
  */
-struct buffer_reg_channel *buffer_reg_channel_find(uint64_t key,
-               struct buffer_reg_uid *reg)
+struct buffer_reg_channel *buffer_reg_channel_find(uint64_t key, struct buffer_reg_uid *reg)
 {
        struct lttng_ht_node_u64 *node;
        struct lttng_ht_iter iter;
-       struct buffer_reg_channel *chan = NULL;
+       struct buffer_reg_channel *chan = nullptr;
        struct lttng_ht *ht;
 
        LTTNG_ASSERT(reg);
@@ -472,7 +477,7 @@ struct buffer_reg_channel *buffer_reg_channel_find(uint64_t key,
        if (!node) {
                goto end;
        }
-       chan = caa_container_of(node, struct buffer_reg_channel, node);
+       chan = lttng::utils::container_of(node, &buffer_reg_channel::node);
 
 end:
        return chan;
@@ -481,25 +486,24 @@ end:
 /*
  * Destroy a buffer registry stream with the given domain.
  */
-void buffer_reg_stream_destroy(struct buffer_reg_stream *regp,
-               enum lttng_domain_type domain)
+void buffer_reg_stream_destroy(struct buffer_reg_stream *regp, enum lttng_domain_type domain)
 {
        if (!regp) {
                return;
        }
 
-       DBG3("Buffer registry stream destroy with handle %d",
-                       regp->obj.ust->handle);
+       DBG3("Buffer registry stream destroy with handle %d", regp->obj.ust->handle);
 
        switch (domain) {
        case LTTNG_DOMAIN_UST:
        {
                int ret;
 
-               ret = ust_app_release_object(NULL, regp->obj.ust);
+               ret = ust_app_release_object(nullptr, regp->obj.ust);
                if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
                        ERR("Buffer reg stream release obj handle %d failed with ret %d",
-                                       regp->obj.ust->handle, ret);
+                           regp->obj.ust->handle,
+                           ret);
                }
                free(regp->obj.ust);
                lttng_fd_put(LTTNG_FD_APPS, 2);
@@ -517,8 +521,7 @@ void buffer_reg_stream_destroy(struct buffer_reg_stream *regp,
  * Remove buffer registry channel object from the session hash table. RCU read
  * side lock MUST be acquired before calling this.
  */
-void buffer_reg_channel_remove(struct buffer_reg_session *session,
-               struct buffer_reg_channel *regp)
+void buffer_reg_channel_remove(struct buffer_reg_session *session, struct buffer_reg_channel *regp)
 {
        int ret;
        struct lttng_ht_iter iter;
@@ -534,8 +537,7 @@ void buffer_reg_channel_remove(struct buffer_reg_session *session,
 /*
  * Destroy a buffer registry channel with the given domain.
  */
-void buffer_reg_channel_destroy(struct buffer_reg_channel *regp,
-               enum lttng_domain_type domain)
+void buffer_reg_channel_destroy(struct buffer_reg_channel *regp, enum lttng_domain_type domain)
 {
        if (!regp) {
                return;
@@ -549,17 +551,18 @@ void buffer_reg_channel_destroy(struct buffer_reg_channel *regp,
                int ret;
                struct buffer_reg_stream *sreg, *stmp;
                /* Wipe stream */
-               cds_list_for_each_entry_safe(sreg, stmp, &regp->streams, lnode) {
+               cds_list_for_each_entry_safe (sreg, stmp, &regp->streams, lnode) {
                        cds_list_del(&sreg->lnode);
                        regp->stream_count--;
                        buffer_reg_stream_destroy(sreg, domain);
                }
 
                if (regp->obj.ust) {
-                       ret = ust_app_release_object(NULL, regp->obj.ust);
+                       ret = ust_app_release_object(nullptr, regp->obj.ust);
                        if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
                                ERR("Buffer reg channel release obj handle %d failed with ret %d",
-                                               regp->obj.ust->handle, ret);
+                                   regp->obj.ust->handle,
+                                   ret);
                        }
                        free(regp->obj.ust);
                }
@@ -578,7 +581,7 @@ void buffer_reg_channel_destroy(struct buffer_reg_channel *regp,
  * Destroy a buffer registry session with the given domain.
  */
 static void buffer_reg_session_destroy(struct buffer_reg_session *regp,
-               enum lttng_domain_type domain)
+                                      enum lttng_domain_type domain)
 {
        int ret;
        struct lttng_ht_iter iter;
@@ -587,14 +590,15 @@ static void buffer_reg_session_destroy(struct buffer_reg_session *regp,
        DBG3("Buffer registry session destroy");
 
        /* Destroy all channels. */
-       rcu_read_lock();
-       cds_lfht_for_each_entry(regp->channels->ht, &iter.iter, reg_chan,
-                       node.node) {
-               ret = lttng_ht_del(regp->channels, &iter);
-               LTTNG_ASSERT(!ret);
-               buffer_reg_channel_destroy(reg_chan, domain);
+       {
+               lttng::urcu::read_lock_guard read_lock;
+
+               cds_lfht_for_each_entry (regp->channels->ht, &iter.iter, reg_chan, node.node) {
+                       ret = lttng_ht_del(regp->channels, &iter);
+                       LTTNG_ASSERT(!ret);
+                       buffer_reg_channel_destroy(reg_chan, domain);
+               }
        }
-       rcu_read_unlock();
 
        lttng_ht_destroy(regp->channels);
 
@@ -620,19 +624,16 @@ void buffer_reg_uid_remove(struct buffer_reg_uid *regp)
 
        LTTNG_ASSERT(regp);
 
-       rcu_read_lock();
+       lttng::urcu::read_lock_guard read_lock;
        iter.iter.node = &regp->node.node;
        ret = lttng_ht_del(buffer_registry_uid, &iter);
        LTTNG_ASSERT(!ret);
-       rcu_read_unlock();
 }
 
 static void rcu_free_buffer_reg_uid(struct rcu_head *head)
 {
-       struct lttng_ht_node_u64 *node =
-               caa_container_of(head, struct lttng_ht_node_u64, head);
-       struct buffer_reg_uid *reg =
-               caa_container_of(node, struct buffer_reg_uid, node);
+       struct lttng_ht_node_u64 *node = lttng::utils::container_of(head, &lttng_ht_node_u64::head);
+       struct buffer_reg_uid *reg = lttng::utils::container_of(node, &buffer_reg_uid::node);
 
        buffer_reg_session_destroy(reg->registry, reg->domain);
        free(reg);
@@ -640,10 +641,8 @@ static void rcu_free_buffer_reg_uid(struct rcu_head *head)
 
 static void rcu_free_buffer_reg_pid(struct rcu_head *head)
 {
-       struct lttng_ht_node_u64 *node =
-               caa_container_of(head, struct lttng_ht_node_u64, head);
-       struct buffer_reg_pid *reg =
-               caa_container_of(node, struct buffer_reg_pid, node);
+       struct lttng_ht_node_u64 *node = lttng::utils::container_of(head, &lttng_ht_node_u64::head);
+       struct buffer_reg_pid *reg = lttng::utils::container_of(node, &buffer_reg_pid::node);
 
        buffer_reg_session_destroy(reg->registry, LTTNG_DOMAIN_UST);
        free(reg);
@@ -654,8 +653,7 @@ static void rcu_free_buffer_reg_pid(struct rcu_head *head)
  * list or hash table. Use buffer_reg_pid_remove() before calling this function
  * for the case that the object is in the global hash table.
  */
-void buffer_reg_uid_destroy(struct buffer_reg_uid *regp,
-               struct consumer_output *consumer)
+void buffer_reg_uid_destroy(struct buffer_reg_uid *regp, struct consumer_output *consumer)
 {
        struct consumer_socket *socket;
 
@@ -664,36 +662,36 @@ void buffer_reg_uid_destroy(struct buffer_reg_uid *regp,
        }
 
        DBG3("Buffer registry per UID destroy with id: %" PRIu64 ", ABI: %u, uid: %d",
-                       regp->session_id, regp->bits_per_long, regp->uid);
+            regp->session_id,
+            regp->bits_per_long,
+            regp->uid);
 
        if (!consumer) {
                goto destroy;
        }
 
-       rcu_read_lock();
-       /* Get the right socket from the consumer object. */
-       socket = consumer_find_socket_by_bitness(regp->bits_per_long,
-                       consumer);
-       if (!socket) {
-               goto unlock;
-       }
+       {
+               lttng::urcu::read_lock_guard read_lock;
+               /* Get the right socket from the consumer object. */
+               socket = consumer_find_socket_by_bitness(regp->bits_per_long, consumer);
+               if (!socket) {
+                       goto destroy;
+               }
 
-       switch (regp->domain) {
-       case LTTNG_DOMAIN_UST:
-               if (regp->registry->reg.ust->_metadata_key) {
-                       /* Return value does not matter. This call will print errors. */
-                       (void) consumer_close_metadata(socket,
-                                       regp->registry->reg.ust->_metadata_key);
+               switch (regp->domain) {
+               case LTTNG_DOMAIN_UST:
+                       if (regp->registry->reg.ust->_metadata_key) {
+                               /* Return value does not matter. This call will print errors. */
+                               (void) consumer_close_metadata(
+                                       socket, regp->registry->reg.ust->_metadata_key);
+                       }
+                       break;
+               default:
+                       abort();
+                       return;
                }
-               break;
-       default:
-               abort();
-               rcu_read_unlock();
-               return;
        }
 
-unlock:
-       rcu_read_unlock();
 destroy:
        call_rcu(&regp->node.head, rcu_free_buffer_reg_uid);
 }
@@ -725,8 +723,7 @@ void buffer_reg_pid_destroy(struct buffer_reg_pid *regp)
                return;
        }
 
-       DBG3("Buffer registry per PID destroy with id: %" PRIu64,
-                       regp->session_id);
+       DBG3("Buffer registry per PID destroy with id: %" PRIu64, regp->session_id);
 
        /* This registry is only used by UST. */
        call_rcu(&regp->node.head, rcu_free_buffer_reg_pid);
@@ -735,7 +732,7 @@ void buffer_reg_pid_destroy(struct buffer_reg_pid *regp)
 /*
  * Destroy per PID and UID registry hash table.
  */
-void buffer_reg_destroy_registries(void)
+void buffer_reg_destroy_registries()
 {
        DBG3("Buffer registry destroy all registry");
        lttng_ht_destroy(buffer_registry_uid);
This page took 0.031297 seconds and 4 git commands to generate.