Use compiler-agnostic defines to silence warning
[lttng-tools.git] / src / bin / lttng-sessiond / agent.cpp
index 15b3b9db97e0c15117162e37814fe1db65da9ec3..2beb7e9aa7a0b1e3634d08c861eb4a6c2f3a8936 100644 (file)
@@ -15,6 +15,7 @@
 #include <common/common.hpp>
 #include <common/compat/endian.hpp>
 #include <common/sessiond-comm/agent.hpp>
+#include <common/urcu.hpp>
 
 #include <lttng/condition/condition.h>
 #include <lttng/condition/event-rule-matches.h>
 #include <lttng/event-rule/event-rule.h>
 #include <lttng/event-rule/jul-logging.h>
 #include <lttng/event-rule/log4j-logging.h>
+#include <lttng/event-rule/log4j2-logging.h>
 #include <lttng/event-rule/python-logging.h>
 #include <lttng/log-level-rule-internal.hpp>
 
 #include <urcu/rculist.h>
 #include <urcu/uatomic.h>
 
-typedef enum lttng_event_rule_status (*event_rule_logging_get_name_pattern)(
-       const struct lttng_event_rule *rule, const char **pattern);
-typedef enum lttng_event_rule_status (*event_rule_logging_get_log_level_rule)(
-       const struct lttng_event_rule *rule, const struct lttng_log_level_rule **log_level_rule);
+using event_rule_logging_get_name_pattern =
+       enum lttng_event_rule_status (*)(const struct lttng_event_rule *, const char **);
+using event_rule_logging_get_log_level_rule = enum lttng_event_rule_status (*)(
+       const struct lttng_event_rule *, const struct lttng_log_level_rule **);
 
 /*
  * Agent application context representation.
@@ -93,14 +95,11 @@ static void log_reply_code(uint32_t in_reply_ret_code)
  */
 static int ht_match_event_by_name(struct cds_lfht_node *node, const void *_key)
 {
-       struct agent_event *event;
-       const struct agent_ht_key *key;
-
        LTTNG_ASSERT(node);
        LTTNG_ASSERT(_key);
 
-       event = caa_container_of(node, struct agent_event, node.node);
-       key = (agent_ht_key *) _key;
+       auto *event = lttng_ht_node_container_of(node, &agent_event::node);
+       const auto *key = static_cast<const agent_ht_key *>(_key);
 
        /* Match 1 elements of the key: name. */
 
@@ -121,15 +120,12 @@ no_match:
  */
 static int ht_match_event(struct cds_lfht_node *node, const void *_key)
 {
-       struct agent_event *event;
-       const struct agent_ht_key *key;
-       int ll_match;
-
        LTTNG_ASSERT(node);
        LTTNG_ASSERT(_key);
 
-       event = caa_container_of(node, struct agent_event, node.node);
-       key = (agent_ht_key *) _key;
+       const auto *event = lttng_ht_node_container_of(node, &agent_event::node);
+       const auto *key = (agent_ht_key *) _key;
+       bool ll_match;
 
        /* Match 2 elements of the key: name and loglevel. */
 
@@ -311,8 +307,8 @@ static ssize_t list_events(struct agent_app *app, struct lttng_event **events)
        uint32_t nb_event;
        size_t data_size;
        uint32_t reply_ret_code;
-       struct lttng_event *tmp_events = NULL;
-       struct lttcomm_agent_list_reply *reply = NULL;
+       struct lttng_event *tmp_events = nullptr;
+       struct lttcomm_agent_list_reply *reply = nullptr;
        struct lttcomm_agent_list_reply_hdr reply_hdr;
 
        LTTNG_ASSERT(app);
@@ -677,14 +673,12 @@ int agent_send_registration_done(struct agent_app *app)
 int agent_enable_event(struct agent_event *event, enum lttng_domain_type domain)
 {
        int ret;
-       struct agent_app *app;
-       struct lttng_ht_iter iter;
 
        LTTNG_ASSERT(event);
 
-       rcu_read_lock();
-
-       cds_lfht_for_each_entry (the_agent_apps_ht_by_sock->ht, &iter.iter, app, node.node) {
+       for (auto *app : lttng::urcu::
+                    lfht_iteration_adapter<agent_app, decltype(agent_app::node), &agent_app::node>(
+                            *the_agent_apps_ht_by_sock->ht)) {
                if (app->domain != domain) {
                        continue;
                }
@@ -700,7 +694,6 @@ int agent_enable_event(struct agent_event *event, enum lttng_domain_type domain)
        ret = LTTNG_OK;
 
 error:
-       rcu_read_unlock();
        return ret;
 }
 
@@ -713,7 +706,7 @@ static void destroy_app_ctx(struct agent_app_ctx *ctx)
 
 static struct agent_app_ctx *create_app_ctx(const struct lttng_event_context *ctx)
 {
-       struct agent_app_ctx *agent_ctx = NULL;
+       struct agent_app_ctx *agent_ctx = nullptr;
 
        if (!ctx) {
                goto end;
@@ -729,7 +722,7 @@ static struct agent_app_ctx *create_app_ctx(const struct lttng_event_context *ct
        agent_ctx->ctx_name = strdup(ctx->u.app_ctx.ctx_name);
        if (!agent_ctx->provider_name || !agent_ctx->ctx_name) {
                destroy_app_ctx(agent_ctx);
-               agent_ctx = NULL;
+               agent_ctx = nullptr;
        }
 end:
        return agent_ctx;
@@ -744,8 +737,6 @@ end:
 int agent_enable_context(const struct lttng_event_context *ctx, enum lttng_domain_type domain)
 {
        int ret;
-       struct agent_app *app;
-       struct lttng_ht_iter iter;
 
        LTTNG_ASSERT(ctx);
        if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
@@ -753,9 +744,9 @@ int agent_enable_context(const struct lttng_event_context *ctx, enum lttng_domai
                goto error;
        }
 
-       rcu_read_lock();
-
-       cds_lfht_for_each_entry (the_agent_apps_ht_by_sock->ht, &iter.iter, app, node.node) {
+       for (auto *app : lttng::urcu::
+                    lfht_iteration_adapter<agent_app, decltype(agent_app::node), &agent_app::node>(
+                            *the_agent_apps_ht_by_sock->ht)) {
                struct agent_app_ctx *agent_ctx;
 
                if (app->domain != domain) {
@@ -779,7 +770,6 @@ int agent_enable_context(const struct lttng_event_context *ctx, enum lttng_domai
        ret = LTTNG_OK;
 
 error_unlock:
-       rcu_read_unlock();
 error:
        return ret;
 }
@@ -793,8 +783,6 @@ error:
 int agent_disable_event(struct agent_event *event, enum lttng_domain_type domain)
 {
        int ret = LTTNG_OK;
-       struct agent_app *app;
-       struct lttng_ht_iter iter;
 
        LTTNG_ASSERT(event);
        if (!AGENT_EVENT_IS_ENABLED(event)) {
@@ -811,9 +799,9 @@ int agent_disable_event(struct agent_event *event, enum lttng_domain_type domain
                goto end;
        }
 
-       rcu_read_lock();
-
-       cds_lfht_for_each_entry (the_agent_apps_ht_by_sock->ht, &iter.iter, app, node.node) {
+       for (auto *app : lttng::urcu::
+                    lfht_iteration_adapter<agent_app, decltype(agent_app::node), &agent_app::node>(
+                            *the_agent_apps_ht_by_sock->ht)) {
                if (app->domain != domain) {
                        continue;
                }
@@ -829,7 +817,6 @@ int agent_disable_event(struct agent_event *event, enum lttng_domain_type domain
        LTTNG_ASSERT(!AGENT_EVENT_IS_ENABLED(event));
 
 error:
-       rcu_read_unlock();
 end:
        return ret;
 }
@@ -843,14 +830,13 @@ end:
 static int disable_context(struct agent_app_ctx *ctx, enum lttng_domain_type domain)
 {
        int ret = LTTNG_OK;
-       struct agent_app *app;
-       struct lttng_ht_iter iter;
 
        LTTNG_ASSERT(ctx);
-
-       rcu_read_lock();
        DBG2("Disabling agent application context %s:%s", ctx->provider_name, ctx->ctx_name);
-       cds_lfht_for_each_entry (the_agent_apps_ht_by_sock->ht, &iter.iter, app, node.node) {
+
+       for (auto *app : lttng::urcu::
+                    lfht_iteration_adapter<agent_app, decltype(agent_app::node), &agent_app::node>(
+                            *the_agent_apps_ht_by_sock->ht)) {
                if (app->domain != domain) {
                        continue;
                }
@@ -861,7 +847,6 @@ static int disable_context(struct agent_app_ctx *ctx, enum lttng_domain_type dom
                }
        }
 end:
-       rcu_read_unlock();
        return ret;
 }
 
@@ -875,9 +860,7 @@ int agent_list_events(struct lttng_event **events, enum lttng_domain_type domain
 {
        int ret;
        size_t nbmem, count = 0;
-       struct agent_app *app;
-       struct lttng_event *tmp_events = NULL;
-       struct lttng_ht_iter iter;
+       struct lttng_event *tmp_events = nullptr;
 
        LTTNG_ASSERT(events);
 
@@ -891,8 +874,9 @@ int agent_list_events(struct lttng_event **events, enum lttng_domain_type domain
                goto error;
        }
 
-       rcu_read_lock();
-       cds_lfht_for_each_entry (the_agent_apps_ht_by_sock->ht, &iter.iter, app, node.node) {
+       for (auto *app : lttng::urcu::
+                    lfht_iteration_adapter<agent_app, decltype(agent_app::node), &agent_app::node>(
+                            *the_agent_apps_ht_by_sock->ht)) {
                ssize_t nb_ev;
                struct lttng_event *agent_events;
 
@@ -904,7 +888,7 @@ int agent_list_events(struct lttng_event **events, enum lttng_domain_type domain
                nb_ev = list_events(app, &agent_events);
                if (nb_ev < 0) {
                        ret = nb_ev;
-                       goto error_unlock;
+                       goto error;
                }
 
                if (count + nb_ev > nbmem) {
@@ -922,8 +906,9 @@ int agent_list_events(struct lttng_event **events, enum lttng_domain_type domain
                                PERROR("realloc agent events");
                                ret = -ENOMEM;
                                free(agent_events);
-                               goto error_unlock;
+                               goto error;
                        }
+
                        /* Zero the new memory */
                        memset(new_tmp_events + nbmem,
                               0,
@@ -935,14 +920,11 @@ int agent_list_events(struct lttng_event **events, enum lttng_domain_type domain
                free(agent_events);
                count += nb_ev;
        }
-       rcu_read_unlock();
 
        ret = count;
        *events = tmp_events;
        return ret;
 
-error_unlock:
-       rcu_read_unlock();
 error:
        free(tmp_events);
        return ret;
@@ -992,8 +974,8 @@ struct agent_app *agent_find_app_by_sock(int sock)
        ASSERT_RCU_READ_LOCKED();
 
        lttng_ht_lookup(the_agent_apps_ht_by_sock, (void *) ((unsigned long) sock), &iter);
-       node = lttng_ht_iter_get_node_ulong(&iter);
-       if (node == NULL) {
+       node = lttng_ht_iter_get_node<lttng_ht_node_ulong>(&iter);
+       if (node == nullptr) {
                goto error;
        }
        app = lttng::utils::container_of(node, &agent_app::node);
@@ -1003,7 +985,7 @@ struct agent_app *agent_find_app_by_sock(int sock)
 
 error:
        DBG3("Agent app NOT found by sock %d.", sock);
-       return NULL;
+       return nullptr;
 }
 
 /*
@@ -1111,7 +1093,7 @@ struct agent *agent_create(enum lttng_domain_type domain)
        ret = agent_init(agt);
        if (ret < 0) {
                free(agt);
-               agt = NULL;
+               agt = nullptr;
                goto error;
        }
 
@@ -1131,7 +1113,7 @@ struct agent_event *agent_create_event(const char *name,
                                       struct lttng_bytecode *filter,
                                       char *filter_expression)
 {
-       struct agent_event *event = NULL;
+       struct agent_event *event = nullptr;
 
        DBG3("Agent create new event with name %s, loglevel type %d, \
                        loglevel value %d and filter %s",
@@ -1182,7 +1164,7 @@ void agent_add_event(struct agent_event *event, struct agent *agt)
 int agent_add_context(const struct lttng_event_context *ctx, struct agent *agt)
 {
        int ret = LTTNG_OK;
-       struct agent_app_ctx *agent_ctx = NULL;
+       struct agent_app_ctx *agent_ctx = nullptr;
 
        LTTNG_ASSERT(ctx);
        LTTNG_ASSERT(agt);
@@ -1276,6 +1258,10 @@ struct agent_event *agent_find_event_by_trigger(const struct lttng_trigger *trig
                logging_get_name_pattern = lttng_event_rule_log4j_logging_get_name_pattern;
                logging_get_log_level_rule = lttng_event_rule_log4j_logging_get_log_level_rule;
                break;
+       case LTTNG_EVENT_RULE_TYPE_LOG4J2_LOGGING:
+               logging_get_name_pattern = lttng_event_rule_log4j2_logging_get_name_pattern;
+               logging_get_log_level_rule = lttng_event_rule_log4j2_logging_get_log_level_rule;
+               break;
        case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
                logging_get_name_pattern = lttng_event_rule_python_logging_get_name_pattern;
                logging_get_log_level_rule = lttng_event_rule_python_logging_get_log_level_rule;
@@ -1287,7 +1273,7 @@ struct agent_event *agent_find_event_by_trigger(const struct lttng_trigger *trig
 
        domain = lttng_event_rule_get_domain_type(rule);
        LTTNG_ASSERT(domain == LTTNG_DOMAIN_JUL || domain == LTTNG_DOMAIN_LOG4J ||
-                    domain == LTTNG_DOMAIN_PYTHON);
+                    domain == LTTNG_DOMAIN_LOG4J2 || domain == LTTNG_DOMAIN_PYTHON);
 
        /* Get the event's pattern name ('name' in the legacy terminology). */
        er_status = logging_get_name_pattern(rule, &name);
@@ -1363,8 +1349,8 @@ struct agent_event *agent_find_event(const char *name,
                        ht_match_event,
                        &key,
                        &iter.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) {
                goto error;
        }
 
@@ -1373,7 +1359,7 @@ struct agent_event *agent_find_event(const char *name,
 
 error:
        DBG3("Agent event NOT found %s.", name);
-       return NULL;
+       return nullptr;
 }
 
 /*
@@ -1403,18 +1389,15 @@ static void destroy_app_ctx_rcu(struct rcu_head *head)
  */
 void agent_destroy(struct agent *agt)
 {
-       struct lttng_ht_node_str *node;
-       struct lttng_ht_iter iter;
-       struct agent_app_ctx *ctx;
-
        LTTNG_ASSERT(agt);
 
        DBG3("Agent destroy");
 
-       rcu_read_lock();
-       cds_lfht_for_each_entry (agt->events->ht, &iter.iter, node, node) {
+       for (auto *event :
+            lttng::urcu::lfht_iteration_adapter<agent_event,
+                                                decltype(agent_event::node),
+                                                &agent_event::node>(*agt->events->ht)) {
                int ret;
-               struct agent_event *event;
 
                /*
                 * When destroying an event, we have to try to disable it on the
@@ -1422,21 +1405,21 @@ void agent_destroy(struct agent *agt)
                 * value is not important since we have to continue anyway
                 * destroying the object.
                 */
-               event = lttng::utils::container_of(node, &agent_event::node);
                (void) agent_disable_event(event, agt->domain);
 
-               ret = lttng_ht_del(agt->events, &iter);
+               ret = cds_lfht_del(agt->events->ht, &event->node.node);
                LTTNG_ASSERT(!ret);
-               call_rcu(&node->head, destroy_event_agent_rcu);
+               call_rcu(&event->node.head, destroy_event_agent_rcu);
        }
 
+       agent_app_ctx *ctx;
        cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node)
        {
                (void) disable_context(ctx, agt->domain);
                cds_list_del(&ctx->list_node);
                call_rcu(&ctx->rcu_node, destroy_app_ctx_rcu);
        }
-       rcu_read_unlock();
+
        lttng_ht_destroy(agt->events);
        free(agt);
 }
@@ -1444,7 +1427,7 @@ void agent_destroy(struct agent *agt)
 /*
  * Allocate agent_apps_ht_by_sock.
  */
-int agent_app_ht_alloc(void)
+int agent_app_ht_alloc()
 {
        the_agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
        return the_agent_apps_ht_by_sock ? 0 : -1;
@@ -1464,7 +1447,7 @@ void agent_destroy_app_by_sock(int sock)
         * happen. The hash table deletion is ONLY done through this call when the
         * main sessiond thread is torn down.
         */
-       rcu_read_lock();
+       const lttng::urcu::read_lock_guard read_lock;
        app = agent_find_app_by_sock(sock);
        LTTNG_ASSERT(app);
 
@@ -1473,28 +1456,22 @@ void agent_destroy_app_by_sock(int sock)
 
        /* The application is freed in a RCU call but the socket is closed here. */
        agent_destroy_app(app);
-       rcu_read_unlock();
 }
 
 /*
  * Clean-up the agent app hash table and destroy it.
  */
-void agent_app_ht_clean(void)
+void agent_app_ht_clean()
 {
-       struct lttng_ht_node_ulong *node;
-       struct lttng_ht_iter iter;
-
        if (!the_agent_apps_ht_by_sock) {
                return;
        }
-       rcu_read_lock();
-       cds_lfht_for_each_entry (the_agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
-               struct agent_app *app;
 
-               app = lttng::utils::container_of(node, &agent_app::node);
+       for (auto *app : lttng::urcu::
+                    lfht_iteration_adapter<agent_app, decltype(agent_app::node), &agent_app::node>(
+                            *the_agent_apps_ht_by_sock->ht)) {
                agent_destroy_app_by_sock(app->sock->fd);
        }
-       rcu_read_unlock();
 
        lttng_ht_destroy(the_agent_apps_ht_by_sock);
 }
@@ -1508,8 +1485,6 @@ void agent_app_ht_clean(void)
 void agent_update(const struct agent *agt, const struct agent_app *app)
 {
        int ret;
-       struct agent_event *event;
-       struct lttng_ht_iter iter;
        struct agent_app_ctx *ctx;
 
        LTTNG_ASSERT(agt);
@@ -1517,13 +1492,14 @@ void agent_update(const struct agent *agt, const struct agent_app *app)
 
        DBG("Agent updating app: pid = %ld", (long) app->pid);
 
-       rcu_read_lock();
        /*
         * We are in the registration path thus if the application is gone,
         * there is a serious code flow error.
         */
-
-       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)) {
                /* Skip event if disabled. */
                if (!AGENT_EVENT_IS_ENABLED(event)) {
                        continue;
@@ -1552,15 +1528,13 @@ void agent_update(const struct agent *agt, const struct agent_app *app)
                        continue;
                }
        }
-
-       rcu_read_unlock();
 }
 
 /*
  * Allocate the per-event notifier domain agent hash table. It is lazily
  * populated as domains are used.
  */
-int agent_by_event_notifier_domain_ht_create(void)
+int agent_by_event_notifier_domain_ht_create()
 {
        the_trigger_agents_ht_by_domain = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
        return the_trigger_agents_ht_by_domain ? 0 : -1;
@@ -1569,31 +1543,28 @@ int agent_by_event_notifier_domain_ht_create(void)
 /*
  * Clean-up the per-event notifier domain agent hash table and destroy it.
  */
-void agent_by_event_notifier_domain_ht_destroy(void)
+void agent_by_event_notifier_domain_ht_destroy()
 {
-       struct lttng_ht_node_u64 *node;
-       struct lttng_ht_iter iter;
-
        if (!the_trigger_agents_ht_by_domain) {
                return;
        }
 
-       rcu_read_lock();
-       cds_lfht_for_each_entry (the_trigger_agents_ht_by_domain->ht, &iter.iter, node, node) {
-               struct agent *agent = lttng::utils::container_of(node, &agent::node);
-               const int ret = lttng_ht_del(the_trigger_agents_ht_by_domain, &iter);
+       for (struct agent *agent :
+            lttng::urcu::lfht_iteration_adapter<struct agent, decltype(agent::node), &agent::node>(
+                    *the_trigger_agents_ht_by_domain->ht)) {
+               const auto ret =
+                       cds_lfht_del(the_trigger_agents_ht_by_domain->ht, &agent->node.node);
 
                LTTNG_ASSERT(ret == 0);
                agent_destroy(agent);
        }
 
-       rcu_read_unlock();
        lttng_ht_destroy(the_trigger_agents_ht_by_domain);
 }
 
 struct agent *agent_find_by_event_notifier_domain(enum lttng_domain_type domain_type)
 {
-       struct agent *agt = NULL;
+       struct agent *agt = nullptr;
        struct lttng_ht_node_u64 *node;
        struct lttng_ht_iter iter;
        const uint64_t key = (uint64_t) domain_type;
@@ -1604,7 +1575,7 @@ struct agent *agent_find_by_event_notifier_domain(enum lttng_domain_type domain_
             lttng_domain_type_str(domain_type));
 
        lttng_ht_lookup(the_trigger_agents_ht_by_domain, &key, &iter);
-       node = lttng_ht_iter_get_node_u64(&iter);
+       node = lttng_ht_iter_get_node<lttng_ht_node_u64>(&iter);
        if (!node) {
                goto end;
        }
This page took 0.030496 seconds and 4 git commands to generate.