/*
- * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
- * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@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 _LGPL_SOURCE
#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/on-event.h>
+#include <lttng/domain-internal.h>
+#include <lttng/log-level-rule-internal.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)
}
/*
- * Match function for the events hash table lookup by name and loglevel.
+ * Match function for the events hash table lookup by name, log level and
+ * filter expression.
*/
static int ht_match_event(struct cds_lfht_node *node,
const void *_key)
}
/* Filter expression */
- if (strncmp(event->filter_expression, key->filter_expression,
- strlen(event->filter_expression)) != 0) {
+ 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:
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;
*
* 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;
}
data_size = sizeof(msg) + 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 = htobe32(event->loglevel_value);
msg.loglevel_type = htobe32(event->loglevel_type);
- strncpy(msg.name, event->name, sizeof(msg.name));
+ 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;
+ }
+
bytes_to_send = zmalloc(data_size);
if (!bytes_to_send) {
ret = LTTNG_ERR_NOMEM;
*
* Return LTTNG_OK on success or else a LTTNG_ERR* code.
*/
-static int app_context_op(struct agent_app *app,
- struct agent_app_ctx *ctx, enum lttcomm_agent_command cmd)
+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;
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;
rcu_read_lock();
- cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
+ cds_lfht_for_each_entry(the_agent_apps_ht_by_sock->ht, &iter.iter, app,
node.node) {
if (app->domain != domain) {
continue;
}
}
- event->enabled = 1;
+ event->enabled_count++;
ret = LTTNG_OK;
error:
}
static
-struct agent_app_ctx *create_app_ctx(struct lttng_event_context *ctx)
+struct agent_app_ctx *create_app_ctx(const struct lttng_event_context *ctx)
{
struct agent_app_ctx *agent_ctx = NULL;
*
* Return LTTNG_OK on success or else a LTTNG_ERR* code.
*/
-int agent_enable_context(struct lttng_event_context *ctx,
+int agent_enable_context(const struct lttng_event_context *ctx,
enum lttng_domain_type domain)
{
int ret;
rcu_read_lock();
- cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
+ cds_lfht_for_each_entry(the_agent_apps_ht_by_sock->ht, &iter.iter, app,
node.node) {
struct agent_app_ctx *agent_ctx;
/* 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) {
- destroy_app_ctx(agent_ctx);
goto error_unlock;
}
}
struct lttng_ht_iter iter;
assert(event);
- if (!event->enabled) {
+ if (!AGENT_EVENT_IS_ENABLED(event)) {
+ goto end;
+ }
+
+ if (--event->enabled_count != 0) {
+ /*
+ * Agent event still enabled. Disable the agent event only when
+ * all "users" have disabled it (event notifiers, event rules,
+ * etc.).
+ */
+ ret = LTTNG_OK;
goto end;
}
rcu_read_lock();
- cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
+ cds_lfht_for_each_entry(the_agent_apps_ht_by_sock->ht, &iter.iter, app,
node.node) {
if (app->domain != domain) {
continue;
}
}
- event->enabled = 0;
+ /* event->enabled_count is now 0. */
+ assert(!AGENT_EVENT_IS_ENABLED(event));
error:
rcu_read_unlock();
*
* Return LTTNG_OK on success or else a LTTNG_ERR* code.
*/
-int disable_context(struct agent_app_ctx *ctx, enum lttng_domain_type domain)
+static int disable_context(struct agent_app_ctx *ctx,
+ enum lttng_domain_type domain)
{
int ret = LTTNG_OK;
struct agent_app *app;
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,
+ cds_lfht_for_each_entry(the_agent_apps_ht_by_sock->ht, &iter.iter, app,
node.node) {
if (app->domain != domain) {
continue;
}
rcu_read_lock();
- cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
+ cds_lfht_for_each_entry(the_agent_apps_ht_by_sock->ht, &iter.iter, app,
node.node) {
ssize_t nb_ev;
struct lttng_event *agent_events;
app = zmalloc(sizeof(*app));
if (!app) {
- PERROR("zmalloc agent create");
+ PERROR("Failed to allocate agent application instance");
goto error;
}
assert(sock >= 0);
- lttng_ht_lookup(agent_apps_ht_by_sock, (void *)((unsigned long) sock), &iter);
+ 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) {
goto error;
assert(app);
DBG3("Agent adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid);
- lttng_ht_add_unique_ulong(agent_apps_ht_by_sock, &app->node);
+ lttng_ht_add_unique_ulong(the_agent_apps_ht_by_sock, &app->node);
}
/*
DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
iter.iter.node = &app->node.node;
- ret = lttng_ht_del(agent_apps_ht_by_sock, &iter);
+ ret = lttng_ht_del(the_agent_apps_ht_by_sock, &iter);
assert(!ret);
}
*/
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;
/*
* Unique add of a agent context to an agent object.
*/
-int agent_add_context(struct lttng_event_context *ctx, 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;
ht_match_event_by_name, &key, &iter->iter);
}
+/*
+ * Find the agent event matching a trigger.
+ *
+ * RCU read side lock MUST be acquired. It must be held 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 domain;
+ const struct lttng_condition *condition;
+ const struct lttng_event_rule *rule;
+ const char *name;
+ const char *filter_expression;
+ const struct lttng_log_level_rule *log_level_rule;
+ /* Unused when loglevel_type is 'ALL'. */
+ 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_ON_EVENT);
+
+ c_status = lttng_condition_on_event_get_rule(condition, &rule);
+ assert(c_status == LTTNG_CONDITION_STATUS_OK);
+
+ assert(lttng_event_rule_get_type(rule) ==
+ LTTNG_EVENT_RULE_TYPE_TRACEPOINT);
+
+ domain = lttng_event_rule_get_domain_type(rule);
+ assert(domain == LTTNG_DOMAIN_JUL || domain == LTTNG_DOMAIN_LOG4J ||
+ domain == LTTNG_DOMAIN_PYTHON);
+
+ /* Get the event's pattern ('name' in the legacy terminology). */
+ 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);
+
+ /* Map log_level_rule to loglevel value. */
+ er_status = lttng_event_rule_tracepoint_get_log_level_rule(
+ rule, &log_level_rule);
+ if (er_status == LTTNG_EVENT_RULE_STATUS_UNSET) {
+ loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
+ loglevel_value = 0;
+ } else if (er_status == LTTNG_EVENT_RULE_STATUS_OK) {
+ lttng_log_level_rule_to_loglevel(log_level_rule, &loglevel_type, &loglevel_value);
+ } else {
+ abort();
+ }
+
+ return agent_find_event(name, loglevel_type, loglevel_value,
+ filter_expression, agt);
+}
+
/*
* 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.
* Return object if found else NULL.
*/
struct agent_event *agent_find_event(const char *name,
- enum lttng_loglevel_type loglevel_type, int loglevel_value,
- char *filter_expression, struct agent *agt)
+ enum lttng_loglevel_type loglevel_type,
+ int loglevel_value,
+ const char *filter_expression,
+ struct agent *agt)
{
struct lttng_ht_node_str *node;
struct lttng_ht_iter iter;
*/
int agent_app_ht_alloc(void)
{
- int ret = 0;
-
- agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
- if (!agent_apps_ht_by_sock) {
- ret = -1;
- }
-
- return ret;
+ the_agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
+ return the_agent_apps_ht_by_sock ? 0 : -1;
}
/*
struct lttng_ht_node_ulong *node;
struct lttng_ht_iter iter;
- if (!agent_apps_ht_by_sock) {
+ if (!the_agent_apps_ht_by_sock) {
return;
}
rcu_read_lock();
- cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
+ cds_lfht_for_each_entry(
+ the_agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
struct agent_app *app;
app = caa_container_of(node, struct agent_app, node);
}
rcu_read_unlock();
- lttng_ht_destroy(agent_apps_ht_by_sock);
+ lttng_ht_destroy(the_agent_apps_ht_by_sock);
}
/*
* 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();
- 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);
+
cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
/* Skip event if disabled. */
- if (!event->enabled) {
+ if (!AGENT_EVENT_IS_ENABLED(event)) {
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)
+{
+ the_trigger_agents_ht_by_domain = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
+ return the_trigger_agents_ht_by_domain ? 0 : -1;
+}
+
+/*
+ * Clean-up the per-event notifier domain agent hash table and destroy it.
+ */
+void agent_by_event_notifier_domain_ht_destroy(void)
+{
+ 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 =
+ caa_container_of(node, struct agent, node);
+ const int ret = lttng_ht_del(
+ the_trigger_agents_ht_by_domain, &iter);
+
+ 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 lttng_ht_node_u64 *node;
+ struct lttng_ht_iter iter;
+ const uint64_t key = (uint64_t) domain_type;
+
+ assert(the_trigger_agents_ht_by_domain);
+
+ DBG3("Per-event notifier domain agent lookup for domain '%s'",
+ 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);
+ if (!node) {
+ goto end;
+ }
+
+ agt = caa_container_of(node, struct agent, node);
+
+end:
+ return agt;
+}