SoW-2020-0002: Trace Hit Counters: trigger error reporting integration
[lttng-tools.git] / src / bin / lttng-sessiond / agent.c
index 0b8bc0251ce2f8976de7a93a62b6ed8c194605e3..66c55fc46e870275a659af9f543d2089b12b20aa 100644 (file)
@@ -1,24 +1,21 @@
 /*
- * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
+ * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
+ * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License, version 2 only, as
- * published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 51
- * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#define _GNU_SOURCE
 #define _LGPL_SOURCE
 #include <assert.h>
 #include <urcu/uatomic.h>
+#include <urcu/rculist.h>
+
+#include <lttng/event-rule/event-rule.h>
+#include <lttng/event-rule/event-rule-internal.h>
+#include <lttng/event-rule/tracepoint.h>
+#include <lttng/condition/condition.h>
+#include <lttng/condition/event-rule.h>
 
 #include <common/common.h>
 #include <common/sessiond-comm/agent.h>
 #include "agent.h"
 #include "ust-app.h"
 #include "utils.h"
-#include "error.h"
+#include "common/error.h"
 
 #define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
 
+/*
+ * Agent application context representation.
+ */
+struct agent_app_ctx {
+       char *provider_name;
+       char *ctx_name;
+
+       /* agent_app_ctx are part of the agent app_ctx_list. */
+       struct cds_list_head list_node;
+
+       /* For call_rcu teardown. */
+       struct rcu_head rcu_node;
+};
+
 /*
  * Human readable agent return code.
  */
@@ -96,13 +107,15 @@ no_match:
 }
 
 /*
- * Match function for the events hash table lookup by name and loglevel.
+ * Match function for the events hash table lookup by name, loglevel and
+ * filter_expression.
  */
 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;
 
        assert(node);
        assert(_key);
@@ -118,22 +131,27 @@ static int ht_match_event(struct cds_lfht_node *node,
        }
 
        /* Event loglevel value and type. */
-       if (event->loglevel_type == key->loglevel_type) {
-               /* Same loglevel type. */
-               if (key->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
-                       /*
-                        * Loglevel value must also match since the loglevel
-                        * type is not all.
-                        */
-                       if (event->loglevel_value != key->loglevel_value) {
-                               goto no_match;
-                       }
-               }
-       } else {
-               /* Loglevel type is different: no match. */
+       ll_match = loglevels_match(event->loglevel_type,
+               event->loglevel_value, key->loglevel_type,
+               key->loglevel_value, LTTNG_EVENT_LOGLEVEL_ALL);
+
+       if (!ll_match) {
                goto no_match;
        }
 
+       /* Filter expression */
+       if (!!event->filter_expression != !!key->filter_expression) {
+               /* One has a filter expression, the other does not */
+               goto no_match;
+       }
+
+       if (event->filter_expression) {
+               if (strncmp(event->filter_expression, key->filter_expression,
+                               strlen(event->filter_expression)) != 0) {
+                       goto no_match;
+               }
+       }
+
        return 1;
 
 no_match:
@@ -156,6 +174,7 @@ static void add_unique_agent_event(struct lttng_ht *ht,
        key.name = event->name;
        key.loglevel_value = event->loglevel_value;
        key.loglevel_type = event->loglevel_type;
+       key.filter_expression = event->filter_expression;
 
        node_ptr = cds_lfht_add_unique(ht->ht,
                        ht->hash_fct(event->node.key, lttng_ht_seed),
@@ -226,7 +245,7 @@ error:
  *
  * Return 0 on success or else a negative errno value of sendmsg() op.
  */
-static int send_payload(struct lttcomm_sock *sock, void *data,
+static int send_payload(struct lttcomm_sock *sock, const void *data,
                size_t size)
 {
        int ret;
@@ -338,8 +357,11 @@ static ssize_t list_events(struct agent_app *app, struct lttng_event **events)
 
        for (i = 0; i < nb_event; i++) {
                offset += len;
-               strncpy(tmp_events[i].name, reply->payload + offset,
-                               sizeof(tmp_events[i].name));
+               if (lttng_strncpy(tmp_events[i].name, reply->payload + offset,
+                               sizeof(tmp_events[i].name))) {
+                       ret = LTTNG_ERR_INVALID;
+                       goto error;
+               }
                tmp_events[i].pid = app->pid;
                tmp_events[i].enabled = -1;
                len = strlen(reply->payload + offset) + 1;
@@ -365,12 +387,14 @@ error:
  *
  * Return LTTNG_OK on success or else a LTTNG_ERR* code.
  */
-static int enable_event(struct agent_app *app, struct agent_event *event)
+static int enable_event(const struct agent_app *app, struct agent_event *event)
 {
        int ret;
+       char *bytes_to_send;
        uint64_t data_size;
+       size_t filter_expression_length;
        uint32_t reply_ret_code;
-       struct lttcomm_agent_enable msg;
+       struct lttcomm_agent_enable_event msg;
        struct lttcomm_agent_generic_reply reply;
 
        assert(app);
@@ -380,18 +404,45 @@ static int enable_event(struct agent_app *app, struct agent_event *event)
        DBG2("Agent enabling event %s for app pid: %d and socket %d", event->name,
                        app->pid, app->sock->fd);
 
-       data_size = sizeof(msg);
+       /*
+        * Calculate the payload's size, which is the fixed-size struct followed
+        * by the variable-length filter expression (+1 for the ending \0).
+        */
+       if (!event->filter_expression) {
+               filter_expression_length = 0;
+       } else {
+               filter_expression_length = strlen(event->filter_expression) + 1;
+       }
+       data_size = sizeof(msg) + filter_expression_length;
+
+       memset(&msg, 0, sizeof(msg));
+       msg.loglevel_value = htobe32(event->loglevel_value);
+       msg.loglevel_type = htobe32(event->loglevel_type);
+       if (lttng_strncpy(msg.name, event->name, sizeof(msg.name))) {
+               ret = LTTNG_ERR_INVALID;
+               goto error;
+       }
+       msg.filter_expression_length = htobe32(filter_expression_length);
 
        ret = send_header(app->sock, data_size, AGENT_CMD_ENABLE, 0);
        if (ret < 0) {
                goto error_io;
        }
 
-       memset(&msg, 0, sizeof(msg));
-       msg.loglevel_value = event->loglevel_value;
-       msg.loglevel_type = event->loglevel_type;
-       strncpy(msg.name, event->name, sizeof(msg.name));
-       ret = send_payload(app->sock, &msg, sizeof(msg));
+       bytes_to_send = zmalloc(data_size);
+       if (!bytes_to_send) {
+               ret = LTTNG_ERR_NOMEM;
+               goto error;
+       }
+
+       memcpy(bytes_to_send, &msg, sizeof(msg));
+       if (filter_expression_length > 0) {
+               memcpy(bytes_to_send + sizeof(msg), event->filter_expression,
+                               filter_expression_length);
+       }
+
+       ret = send_payload(app->sock, bytes_to_send, data_size);
+       free(bytes_to_send);
        if (ret < 0) {
                goto error_io;
        }
@@ -422,6 +473,112 @@ error:
        return ret;
 }
 
+/*
+ * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
+ */
+static
+int send_pstring(struct lttcomm_sock *sock, const char *str, uint32_t len)
+{
+       int ret;
+       uint32_t len_be;
+
+       len_be = htobe32(len);
+       ret = send_payload(sock, &len_be, sizeof(len_be));
+       if (ret) {
+               goto end;
+       }
+
+       ret = send_payload(sock, str, len);
+       if (ret) {
+               goto end;
+       }
+end:
+       return ret;
+}
+
+/*
+ * Internal enable application context on an agent application. This function
+ * communicates with the agent to enable a given application context.
+ *
+ * Return LTTNG_OK on success or else a LTTNG_ERR* code.
+ */
+static int app_context_op(const struct agent_app *app,
+               const struct agent_app_ctx *ctx, enum lttcomm_agent_command cmd)
+{
+       int ret;
+       uint32_t reply_ret_code;
+       struct lttcomm_agent_generic_reply reply;
+       size_t app_ctx_provider_name_len, app_ctx_name_len, data_size;
+
+       assert(app);
+       assert(app->sock);
+       assert(ctx);
+       assert(cmd == AGENT_CMD_APP_CTX_ENABLE ||
+                       cmd == AGENT_CMD_APP_CTX_DISABLE);
+
+       DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
+                       cmd == AGENT_CMD_APP_CTX_ENABLE ? "enabling" : "disabling",
+                       ctx->provider_name, ctx->ctx_name,
+                       app->pid, app->sock->fd);
+
+       /*
+        * Calculate the payload's size, which consists of the size (u32, BE)
+        * of the provider name, the NULL-terminated provider name string, the
+        * size (u32, BE) of the context name, followed by the NULL-terminated
+        * context name string.
+        */
+       app_ctx_provider_name_len = strlen(ctx->provider_name) + 1;
+       app_ctx_name_len = strlen(ctx->ctx_name) + 1;
+       data_size = sizeof(uint32_t) + app_ctx_provider_name_len +
+                       sizeof(uint32_t) + app_ctx_name_len;
+
+       ret = send_header(app->sock, data_size, cmd, 0);
+       if (ret < 0) {
+               goto error_io;
+       }
+
+       if (app_ctx_provider_name_len > UINT32_MAX ||
+                       app_ctx_name_len > UINT32_MAX) {
+               ERR("Application context name > MAX_UINT32");
+               ret = LTTNG_ERR_INVALID;
+               goto error;
+       }
+
+       ret = send_pstring(app->sock, ctx->provider_name,
+                       (uint32_t) app_ctx_provider_name_len);
+       if (ret < 0) {
+               goto error_io;
+       }
+
+       ret = send_pstring(app->sock, ctx->ctx_name,
+                       (uint32_t) app_ctx_name_len);
+       if (ret < 0) {
+               goto error_io;
+       }
+
+       ret = recv_reply(app->sock, &reply, sizeof(reply));
+       if (ret < 0) {
+               goto error_io;
+       }
+
+       reply_ret_code = be32toh(reply.ret_code);
+       log_reply_code(reply_ret_code);
+       switch (reply_ret_code) {
+       case AGENT_RET_CODE_SUCCESS:
+               break;
+       default:
+               ret = LTTNG_ERR_UNK;
+               goto error;
+       }
+
+       return LTTNG_OK;
+
+error_io:
+       ret = LTTNG_ERR_UST_ENABLE_FAIL;
+error:
+       return ret;
+}
+
 /*
  * Internal disable agent event call on a agent application. This function
  * communicates with the agent to disable a given event.
@@ -433,7 +590,7 @@ static int disable_event(struct agent_app *app, struct agent_event *event)
        int ret;
        uint64_t data_size;
        uint32_t reply_ret_code;
-       struct lttcomm_agent_disable msg;
+       struct lttcomm_agent_disable_event msg;
        struct lttcomm_agent_generic_reply reply;
 
        assert(app);
@@ -444,14 +601,17 @@ static int disable_event(struct agent_app *app, struct agent_event *event)
                        app->pid, app->sock->fd);
 
        data_size = sizeof(msg);
+       memset(&msg, 0, sizeof(msg));
+       if (lttng_strncpy(msg.name, event->name, sizeof(msg.name))) {
+               ret = LTTNG_ERR_INVALID;
+               goto error;
+       }
 
        ret = send_header(app->sock, data_size, AGENT_CMD_DISABLE, 0);
        if (ret < 0) {
                goto error_io;
        }
 
-       memset(&msg, 0, sizeof(msg));
-       strncpy(msg.name, event->name, sizeof(msg.name));
        ret = send_payload(app->sock, &msg, sizeof(msg));
        if (ret < 0) {
                goto error_io;
@@ -529,6 +689,7 @@ int agent_enable_event(struct agent_event *event,
        }
 
        event->enabled = 1;
+       event->user_refcount++;
        ret = LTTNG_OK;
 
 error:
@@ -536,8 +697,92 @@ error:
        return ret;
 }
 
+static
+void destroy_app_ctx(struct agent_app_ctx *ctx)
+{
+       free(ctx->provider_name);
+       free(ctx->ctx_name);
+       free(ctx);
+}
+
+static
+struct agent_app_ctx *create_app_ctx(const struct lttng_event_context *ctx)
+{
+       struct agent_app_ctx *agent_ctx = NULL;
+
+       if (!ctx) {
+               goto end;
+       }
+
+       assert(ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
+       agent_ctx = zmalloc(sizeof(*ctx));
+       if (!agent_ctx) {
+               goto end;
+       }
+
+       agent_ctx->provider_name = strdup(ctx->u.app_ctx.provider_name);
+       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;
+       }
+end:
+       return agent_ctx;
+}
+
 /*
- * Disable agent event on every agent applications registered with the session
+ * Enable agent context on every agent applications registered with the session
+ * daemon.
+ *
+ * Return LTTNG_OK on success or else a LTTNG_ERR* code.
+ */
+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;
+
+       assert(ctx);
+       if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
+               ret = LTTNG_ERR_INVALID;
+               goto error;
+       }
+
+       rcu_read_lock();
+
+       cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
+                       node.node) {
+               struct agent_app_ctx *agent_ctx;
+
+               if (app->domain != domain) {
+                       continue;
+               }
+
+               agent_ctx = create_app_ctx(ctx);
+               if (!agent_ctx) {
+                       ret = LTTNG_ERR_NOMEM;
+                       goto error_unlock;
+               }
+
+               /* Enable event on agent application through TCP socket. */
+               ret = app_context_op(app, agent_ctx, AGENT_CMD_APP_CTX_ENABLE);
+               destroy_app_ctx(agent_ctx);
+               if (ret != LTTNG_OK) {
+                       goto error_unlock;
+               }
+       }
+
+       ret = LTTNG_OK;
+
+error_unlock:
+       rcu_read_unlock();
+error:
+       return ret;
+}
+
+/*
+ * Disable agent event on every agent application registered with the session
  * daemon.
  *
  * Return LTTNG_OK on success or else a LTTNG_ERR* code.
@@ -554,6 +799,17 @@ int agent_disable_event(struct agent_event *event,
                goto end;
        }
 
+       if (event->user_refcount - 1 != 0) {
+               /*
+                * Disable the agent event only when all users (trigger etc.)
+                * have disabled it.
+                */
+
+               event->user_refcount--;
+               ret = LTTNG_OK;
+               goto end;
+       }
+
        rcu_read_lock();
 
        cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
@@ -569,6 +825,7 @@ int agent_disable_event(struct agent_event *event,
                }
        }
 
+       event->user_refcount = 0;
        event->enabled = 0;
 
 error:
@@ -577,6 +834,40 @@ end:
        return ret;
 }
 
+/*
+ * Disable agent context on every agent application registered with the session
+ * daemon.
+ *
+ * Return LTTNG_OK on success or else a LTTNG_ERR* code.
+ */
+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;
+
+       assert(ctx);
+
+       rcu_read_lock();
+       DBG2("Disabling agent application context %s:%s",
+                       ctx->provider_name, ctx->ctx_name);
+       cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
+                       node.node) {
+               if (app->domain != domain) {
+                       continue;
+               }
+
+               ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_DISABLE);
+               if (ret != LTTNG_OK) {
+                       goto end;
+               }
+       }
+end:
+       rcu_read_unlock();
+       return ret;
+}
+
 /*
  * Ask every agent for the list of possible event. Events is allocated with the
  * events of every agent application.
@@ -675,7 +966,7 @@ struct agent_app *agent_create_app(pid_t pid, enum lttng_domain_type domain,
 
        app = zmalloc(sizeof(*app));
        if (!app) {
-               PERROR("zmalloc agent create");
+               PERROR("Failed to allocate agent application instance");
                goto error;
        }
 
@@ -783,6 +1074,7 @@ int agent_init(struct agent *agt)
        }
        lttng_ht_node_init_u64(&agt->node, agt->domain);
 
+       CDS_INIT_LIST_HEAD(&agt->app_ctx_list);
        return 0;
 
 error:
@@ -837,12 +1129,14 @@ error:
  */
 struct agent_event *agent_create_event(const char *name,
                enum lttng_loglevel_type loglevel_type, int loglevel_value,
-               struct lttng_filter_bytecode *filter, char *filter_expression)
+               struct lttng_bytecode *filter, char *filter_expression)
 {
        struct agent_event *event = NULL;
 
-       DBG3("Agent create new event with name %s, loglevel type %d and loglevel value %d",
-               name, loglevel_type, loglevel_value);
+       DBG3("Agent create new event with name %s, loglevel type %d, \
+                       loglevel value %d and filter %s",
+                       name, loglevel_type, loglevel_value,
+                       filter_expression ? filter_expression : "NULL");
 
        if (!name) {
                ERR("Failed to create agent event; no name provided.");
@@ -881,51 +1175,147 @@ void agent_add_event(struct agent_event *event, struct agent *agt)
 }
 
 /*
- * Find a agent event in the given agent using name.
+ * Unique add of a agent context to an agent object.
+ */
+int agent_add_context(const struct lttng_event_context *ctx, struct agent *agt)
+{
+       int ret = LTTNG_OK;
+       struct agent_app_ctx *agent_ctx = NULL;
+
+       assert(ctx);
+       assert(agt);
+       assert(agt->events);
+       assert(ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
+
+       agent_ctx = create_app_ctx(ctx);
+       if (!agent_ctx) {
+               ret = LTTNG_ERR_NOMEM;
+               goto end;
+       }
+
+       DBG3("Agent adding context %s:%s", ctx->u.app_ctx.provider_name,
+                       ctx->u.app_ctx.ctx_name);
+       cds_list_add_tail_rcu(&agent_ctx->list_node, &agt->app_ctx_list);
+end:
+       return ret;
+}
+
+/*
+ * Find multiple agent events sharing the given name.
  *
- * RCU read side lock MUST be acquired.
+ * RCU read side lock MUST be acquired. It must be held for the
+ * duration of the iteration.
  *
- * Return object if found else NULL.
+ * Sets the given iterator.
  */
-struct agent_event *agent_find_event_by_name(const char *name,
-               struct agent *agt)
+void agent_find_events_by_name(const char *name, struct agent *agt,
+               struct lttng_ht_iter* iter)
 {
-       struct lttng_ht_node_str *node;
-       struct lttng_ht_iter iter;
        struct lttng_ht *ht;
        struct agent_ht_key key;
 
        assert(name);
        assert(agt);
        assert(agt->events);
+       assert(iter);
 
        ht = agt->events;
        key.name = name;
 
        cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
-                       ht_match_event_by_name, &key, &iter.iter);
-       node = lttng_ht_iter_get_node_str(&iter);
-       if (node == NULL) {
-               goto error;
+                       ht_match_event_by_name, &key, &iter->iter);
+}
+
+/*
+ * Find the agent event matching the trigger.
+ *
+ * RCU read side lock MUST be acquired. It must be kept for as long as
+ * the returned agent_event is used.
+ *
+ * Return object if found else NULL.
+ */
+struct agent_event *agent_find_event_by_trigger(
+               const struct lttng_trigger *trigger, struct agent *agt)
+{
+       enum lttng_condition_status c_status;
+       enum lttng_event_rule_status er_status;
+       enum lttng_domain_type d_type;
+       const struct lttng_condition *condition;
+       const struct lttng_event_rule *rule;
+       const char *name;
+       const char *filter_expression;
+       /* TODO validate if this is the unset value or no */
+       int loglevel_value = 0;
+       enum lttng_loglevel_type loglevel_type;
+
+       assert(agt);
+       assert(agt->events);
+
+       condition = lttng_trigger_get_const_condition(trigger);
+
+       assert(lttng_condition_get_type(condition) ==
+                       LTTNG_CONDITION_TYPE_EVENT_RULE_HIT);
+
+       c_status = lttng_condition_event_rule_get_rule(condition, &rule);
+       assert(c_status == LTTNG_CONDITION_STATUS_OK);
+
+       assert(lttng_event_rule_get_type(rule) ==
+                       LTTNG_EVENT_RULE_TYPE_TRACEPOINT);
+
+       d_type = lttng_event_rule_get_domain_type(rule);
+       assert(d_type == LTTNG_DOMAIN_JUL || d_type == LTTNG_DOMAIN_LOG4J ||
+                       d_type == LTTNG_DOMAIN_PYTHON);
+
+       /* Get the name (aka pattern) */
+       er_status = lttng_event_rule_tracepoint_get_pattern(rule, &name);
+       assert(er_status == LTTNG_EVENT_RULE_STATUS_OK);
+
+       /* Get the internal filter_expression */
+       filter_expression = lttng_event_rule_get_filter(rule);
+
+       er_status = lttng_event_rule_tracepoint_get_log_level_type(
+                       rule, &loglevel_type);
+       assert(er_status == LTTNG_EVENT_RULE_STATUS_OK);
+       if (loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
+               er_status = lttng_event_rule_tracepoint_get_log_level(
+                               rule, &loglevel_value);
+               assert(er_status == LTTNG_EVENT_RULE_STATUS_OK);
        }
 
-       DBG3("Agent event found %s by name.", name);
-       return caa_container_of(node, struct agent_event, node);
+       return agent_find_event(name, loglevel_type, loglevel_value,
+                       filter_expression, agt);
+}
 
-error:
-       DBG3("Agent NOT found by name %s.", name);
-       return NULL;
+/*
+ * Get the next agent event duplicate by name. This should be called
+ * after a call to agent_find_events_by_name() to iterate on events.
+ *
+ * The RCU read lock must be held during the iteration and for as long
+ * as the object the iterator points to remains in use.
+ */
+void agent_event_next_duplicate(const char *name,
+               struct agent *agt, struct lttng_ht_iter* iter)
+{
+       struct agent_ht_key key;
+
+       key.name = name;
+
+       cds_lfht_next_duplicate(agt->events->ht, ht_match_event_by_name,
+               &key, &iter->iter);
 }
 
 /*
- * Find a agent event in the given agent using name and loglevel.
+ * Find a agent event in the given agent using name, loglevel and filter.
  *
- * RCU read side lock MUST be acquired.
+ * RCU read side lock MUST be acquired. It must be kept for as long as
+ * the returned agent_event is used.
  *
  * Return object if found else NULL.
  */
 struct agent_event *agent_find_event(const char *name,
-               enum lttng_loglevel_type loglevel_type, int loglevel_value,
+               enum lttng_loglevel_type loglevel_type,
+               int loglevel_value,
+               const char *filter_expression,
                struct agent *agt)
 {
        struct lttng_ht_node_str *node;
@@ -941,6 +1331,7 @@ struct agent_event *agent_find_event(const char *name,
        key.name = name;
        key.loglevel_value = loglevel_value;
        key.loglevel_type = loglevel_type;
+       key.filter_expression = filter_expression;
 
        cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
                        ht_match_event, &key, &iter.iter);
@@ -972,6 +1363,15 @@ void agent_destroy_event(struct agent_event *event)
        free(event);
 }
 
+static
+void destroy_app_ctx_rcu(struct rcu_head *head)
+{
+       struct agent_app_ctx *ctx =
+                       caa_container_of(head, struct agent_app_ctx, rcu_node);
+
+       destroy_app_ctx(ctx);
+}
+
 /*
  * Destroy an agent completely.
  */
@@ -979,6 +1379,7 @@ void agent_destroy(struct agent *agt)
 {
        struct lttng_ht_node_str *node;
        struct lttng_ht_iter iter;
+       struct agent_app_ctx *ctx;
 
        assert(agt);
 
@@ -990,9 +1391,10 @@ void agent_destroy(struct agent *agt)
                struct agent_event *event;
 
                /*
-                * When destroying an event, we have to try to disable it on the agent
-                * side so the event stops generating data. The return value is not
-                * important since we have to continue anyway destroying the object.
+                * When destroying an event, we have to try to disable it on the
+                * agent side so the event stops generating data. The return
+                * value is not important since we have to continue anyway
+                * destroying the object.
                 */
                event = caa_container_of(node, struct agent_event, node);
                (void) agent_disable_event(event, agt->domain);
@@ -1001,8 +1403,13 @@ void agent_destroy(struct agent *agt)
                assert(!ret);
                call_rcu(&node->head, destroy_event_agent_rcu);
        }
-       rcu_read_unlock();
 
+       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();
        ht_cleanup_push(agt->events);
        free(agt);
 }
@@ -1022,6 +1429,21 @@ int agent_app_ht_alloc(void)
        return ret;
 }
 
+/*
+ * Allocate agent_apps_ht_by_sock.
+ */
+int trigger_agent_ht_alloc(void)
+{
+       int ret = 0;
+
+       trigger_agents_ht_by_domain = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
+       if (!trigger_agents_ht_by_domain) {
+               ret = -1;
+       }
+
+       return ret;
+}
+
 /*
  * Destroy a agent application by socket.
  */
@@ -1071,38 +1493,62 @@ void agent_app_ht_clean(void)
        lttng_ht_destroy(agent_apps_ht_by_sock);
 }
 
+/*
+ * Clean-up the trigger agent hash table and destroy it.
+ */
+void trigger_agent_ht_clean(void)
+{
+       struct lttng_ht_node_u64 *node;
+       struct lttng_ht_iter iter;
+
+       if (!trigger_agents_ht_by_domain) {
+               return;
+       }
+       rcu_read_lock();
+       cds_lfht_for_each_entry (trigger_agents_ht_by_domain->ht, &iter.iter,
+                       node, node) {
+               struct agent *agent;
+
+               (void) lttng_ht_del(trigger_agents_ht_by_domain, &iter);
+
+               agent = caa_container_of(node, struct agent, node);
+               agent_destroy(agent);
+       }
+       rcu_read_unlock();
+
+       lttng_ht_destroy(trigger_agents_ht_by_domain);
+}
+
 /*
  * Update a agent application (given socket) using the given agent.
  *
  * Note that this function is most likely to be used with a tracing session
  * thus the caller should make sure to hold the appropriate lock(s).
  */
-void agent_update(struct agent *agt, int sock)
+void agent_update(const struct agent *agt, const struct agent_app *app)
 {
        int ret;
-       struct agent_app *app;
        struct agent_event *event;
        struct lttng_ht_iter iter;
+       struct agent_app_ctx *ctx;
 
        assert(agt);
-       assert(sock >= 0);
+       assert(app);
 
-       DBG("Agent updating app socket %d", sock);
+       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) {
                /* Skip event if disabled. */
                if (!event->enabled) {
                        continue;
                }
 
-               app = agent_find_app_by_sock(sock);
-               /*
-                * We are in the registration path thus if the application is gone,
-                * there is a serious code flow error.
-                */
-               assert(app);
-
                ret = enable_event(app, event);
                if (ret != LTTNG_OK) {
                        DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
@@ -1111,5 +1557,40 @@ void agent_update(struct agent *agt, int sock)
                        continue;
                }
        }
+
+       cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) {
+               ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_ENABLE);
+               if (ret != LTTNG_OK) {
+                       DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
+                                       ctx->provider_name, ctx->ctx_name,
+                                       app->pid, app->sock->fd);
+                       continue;
+               }
+       }
+
        rcu_read_unlock();
 }
+
+struct agent *trigger_find_agent(enum lttng_domain_type domain_type)
+{
+       struct agent *agt = NULL;
+       struct lttng_ht_node_u64 *node;
+       struct lttng_ht_iter iter;
+       uint64_t key;
+
+       assert(trigger_agents_ht_by_domain);
+
+       DBG3("Trigger agent lookup for domain %d", domain_type);
+
+       key = domain_type;
+
+       lttng_ht_lookup(trigger_agents_ht_by_domain, &key, &iter);
+       node = lttng_ht_iter_get_node_u64(&iter);
+       if (!node) {
+               goto end;
+       }
+       agt = caa_container_of(node, struct agent, node);
+
+end:
+       return agt;
+}
This page took 0.034274 seconds and 5 git commands to generate.