/*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
- * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright (C) 2011 EfficiOS Inc.
+ * 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 <errno.h>
#include <urcu/list.h>
#include <string.h>
+#include <common/compat/errno.h>
#include <lttng/lttng.h>
+#include <lttng/condition/condition.h>
+#include <lttng/condition/event-rule-matches.h>
+#include <lttng/event-rule/event-rule.h>
+#include <lttng/event-rule/event-rule-internal.h>
+#include <common/bytecode/bytecode.h>
#include <common/error.h>
#include <common/sessiond-comm/sessiond-comm.h>
#include <common/filter.h>
#include "event.h"
#include "kernel.h"
#include "lttng-sessiond.h"
-#include "ust-ctl.h"
+#include "lttng-ust-ctl.h"
+#include "lttng-ust-error.h"
#include "ust-app.h"
#include "trace-kernel.h"
#include "trace-ust.h"
#include "agent.h"
+#include "utils.h"
/*
* Add unique UST event based on the event name, filter bytecode and loglevel.
assert(event);
key.name = event->attr.name;
- key.filter = (struct lttng_filter_bytecode *) event->filter;
+ key.filter = (struct lttng_bytecode *) event->filter;
key.loglevel_type = event->attr.loglevel_type;
key.loglevel_value = event->attr.loglevel;
key.exclusion = event->exclusion;
* If event_name is NULL all events of the specified type are disabled.
*/
int event_kernel_disable_event(struct ltt_kernel_channel *kchan,
- char *event_name, enum lttng_event_type type)
+ const char *event_name, enum lttng_event_type type)
{
int ret, error = 0, found = 0;
struct ltt_kernel_event *kevent;
*/
int event_kernel_enable_event(struct ltt_kernel_channel *kchan,
struct lttng_event *event, char *filter_expression,
- struct lttng_filter_bytecode *filter)
+ struct lttng_bytecode *filter)
{
int ret;
struct ltt_kernel_event *kevent;
int event_ust_enable_tracepoint(struct ltt_ust_session *usess,
struct ltt_ust_channel *uchan, struct lttng_event *event,
char *filter_expression,
- struct lttng_filter_bytecode *filter,
+ struct lttng_bytecode *filter,
struct lttng_event_exclusion *exclusion,
bool internal_event)
{
rcu_read_lock();
uevent = trace_ust_find_event(uchan->events, event->name, filter,
- (enum lttng_ust_loglevel_type) event->loglevel_type,
+ (enum lttng_ust_abi_loglevel_type) event->loglevel_type,
event->loglevel, exclusion);
if (!uevent) {
ret = trace_ust_create_event(event, filter_expression,
filter = NULL;
exclusion = NULL;
if (ret != LTTNG_OK) {
- goto error;
+ goto end;
}
/* Valid to set it after the goto error since uevent is still NULL */
if (ret < 0) {
if (ret == -LTTNG_UST_ERR_EXIST) {
ret = LTTNG_ERR_UST_EVENT_EXIST;
- goto end;
} else {
ret = LTTNG_ERR_UST_ENABLE_FAIL;
- goto error;
}
+ goto end;
}
DBG("Event UST %s %s in channel %s", uevent->attr.name,
free(filter);
free(exclusion);
return ret;
-
-error:
- /*
- * Only destroy event on creation time (not enabling time) because if the
- * event is found in the channel (to_create == 0), it means that at some
- * point the enable_event worked and it's thus valid to keep it alive.
- * Destroying it also implies that we also destroy it's shadow copy to sync
- * everyone up.
- */
- if (to_create) {
- /* In this code path, the uevent was not added to the hash table */
- trace_ust_destroy_event(uevent);
- }
- rcu_read_unlock();
- free(filter_expression);
- free(filter);
- free(exclusion);
- return ret;
}
/*
* Disable UST tracepoint of a channel from a UST session.
*/
int event_ust_disable_tracepoint(struct ltt_ust_session *usess,
- struct ltt_ust_channel *uchan, char *event_name)
+ struct ltt_ust_channel *uchan, const char *event_name)
{
int ret;
struct ltt_ust_event *uevent;
return ret;
}
+static void agent_enable_all(struct agent *agt)
+{
+ struct agent_event *aevent;
+ struct lttng_ht_iter iter;
+
+ /* Flag every event as enabled. */
+ rcu_read_lock();
+ cds_lfht_for_each_entry (
+ agt->events->ht, &iter.iter, aevent, node.node) {
+ aevent->enabled_count++;
+ }
+ rcu_read_unlock();
+}
+
/*
* Enable all agent event for a given UST session.
*
*/
int event_agent_enable_all(struct ltt_ust_session *usess,
struct agent *agt, struct lttng_event *event,
- struct lttng_filter_bytecode *filter ,char *filter_expression)
+ struct lttng_bytecode *filter ,char *filter_expression)
{
int ret;
- struct agent_event *aevent;
- struct lttng_ht_iter iter;
assert(usess);
goto error;
}
- /* Flag every event that they are now enabled. */
- rcu_read_lock();
- cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
- node.node) {
- aevent->enabled = 1;
- }
- rcu_read_unlock();
+ agent_enable_all(agt);
ret = LTTNG_OK;
* contexts yet. Not an issue for now, since they are not generated by
* the lttng-ctl library.
*/
-static int add_filter_app_ctx(struct lttng_filter_bytecode *bytecode,
+static int add_filter_app_ctx(struct lttng_bytecode *bytecode,
const char *filter_expression, struct agent *agt)
{
int ret = LTTNG_OK;
return ret;
}
-/*
- * Enable a single agent event for a given UST session.
- *
- * Return LTTNG_OK on success or else a LTTNG_ERR* code.
- */
-int event_agent_enable(struct ltt_ust_session *usess,
- struct agent *agt, struct lttng_event *event,
- struct lttng_filter_bytecode *filter,
+static int agent_enable(struct agent *agt,
+ struct lttng_event *event,
+ struct lttng_bytecode *filter,
char *filter_expression)
{
int ret, created = 0;
struct agent_event *aevent;
- assert(usess);
assert(event);
assert(agt);
- DBG("Event agent enabling %s for session %" PRIu64 " with loglevel type %d "
- ", loglevel %d and filter \"%s\"", event->name,
- usess->id, event->loglevel_type, event->loglevel,
- filter_expression ? filter_expression : "NULL");
-
+ rcu_read_lock();
aevent = agent_find_event(event->name, event->loglevel_type,
event->loglevel, filter_expression, agt);
if (!aevent) {
filter = NULL;
filter_expression = NULL;
created = 1;
+ assert(!AGENT_EVENT_IS_ENABLED(aevent));
}
- if (created && filter) {
- ret = add_filter_app_ctx(filter, filter_expression, agt);
+ if (created && aevent->filter) {
+ ret = add_filter_app_ctx(
+ aevent->filter, aevent->filter_expression, agt);
if (ret != LTTNG_OK) {
goto error;
}
}
/* Already enabled? */
- if (aevent->enabled) {
+ if (AGENT_EVENT_IS_ENABLED(aevent)) {
ret = LTTNG_OK;
goto end;
}
agent_destroy_event(aevent);
}
end:
+ rcu_read_unlock();
free(filter);
free(filter_expression);
return ret;
}
+/*
+ * Enable a single agent event for a given UST session.
+ *
+ * Return LTTNG_OK on success or else a LTTNG_ERR* code.
+ */
+int event_agent_enable(struct ltt_ust_session *usess,
+ struct agent *agt,
+ struct lttng_event *event,
+ struct lttng_bytecode *filter,
+ char *filter_expression)
+{
+ assert(usess);
+ assert(event);
+ assert(agt);
+
+ DBG("Enabling agent event: event pattern = '%s', session id = %" PRIu64 ", loglevel type = %d, loglevel = %d, filter expression = '%s'",
+ event->name, usess->id, event->loglevel_type,
+ event->loglevel,
+ filter_expression ? filter_expression : "(none)");
+
+ return agent_enable(agt, event, filter, filter_expression);
+}
+
+/*
+ * Enable a single agent event for a trigger.
+ *
+ * Return LTTNG_OK on success or else a LTTNG_ERR* code.
+ */
+int trigger_agent_enable(const struct lttng_trigger *trigger, struct agent *agt)
+{
+ int ret;
+ enum lttng_condition_status c_status;
+ enum lttng_trigger_status t_status;
+ enum lttng_domain_type d_type;
+ const struct lttng_condition *condition;
+ const struct lttng_event_rule *rule;
+ const char *filter_expression;
+ char *filter_expression_copy = NULL;
+ const struct lttng_bytecode *filter_bytecode;
+ struct lttng_bytecode *filter_bytecode_copy = NULL;
+ struct lttng_event *event = NULL;
+ uid_t trigger_owner_uid = 0;
+ const char *trigger_name;
+
+ assert(trigger);
+ assert(agt);
+
+ t_status = lttng_trigger_get_name(trigger, &trigger_name);
+ if (t_status != LTTNG_TRIGGER_STATUS_OK) {
+ trigger_name = "(anonymous)";
+ }
+
+ t_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner_uid);
+ assert(t_status == LTTNG_TRIGGER_STATUS_OK);
+
+ condition = lttng_trigger_get_const_condition(trigger);
+
+ assert(lttng_condition_get_type(condition) ==
+ LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
+
+ c_status = lttng_condition_event_rule_matches_get_rule(
+ condition, &rule);
+ assert(c_status == LTTNG_CONDITION_STATUS_OK);
+
+ switch (lttng_event_rule_get_type(rule)) {
+ case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
+ case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
+ case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
+ break;
+ default:
+ abort();
+ break;
+ }
+
+ d_type = lttng_event_rule_get_domain_type(rule);
+ assert(d_type == agt->domain);
+
+ event = lttng_event_rule_generate_lttng_event(rule);
+ if (!event) {
+ ret = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+
+ /* Get the internal filter expression and bytecode. */
+ filter_expression = lttng_event_rule_get_filter(rule);
+ if (filter_expression) {
+ filter_expression_copy = strdup(filter_expression);
+ if (!filter_expression_copy) {
+ ret = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+
+ /* Get the filter bytecode */
+ filter_bytecode = lttng_event_rule_get_filter_bytecode(rule);
+ if (filter_bytecode) {
+ filter_bytecode_copy =
+ lttng_bytecode_copy(filter_bytecode);
+ if (!filter_bytecode_copy) {
+ ret = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+ }
+ }
+
+ DBG("Enabling agent event from trigger: trigger name = '%s', trigger owner uid = %d, token = %" PRIu64,
+ trigger_name, trigger_owner_uid,
+ lttng_trigger_get_tracer_token(trigger));
+
+ ret = agent_enable(agt, event, filter_bytecode_copy,
+ filter_expression_copy);
+ /* Ownership was passed even in case of error. */
+ filter_expression_copy = NULL;
+ filter_bytecode_copy = NULL;
+
+end:
+ free(filter_expression_copy);
+ free(filter_bytecode_copy);
+ free(event);
+ return ret;
+}
+
/*
* Return the default event name associated with the provided UST domain. Return
* NULL on error.
return default_event_name;
}
+static int trigger_agent_disable_one(const struct lttng_trigger *trigger,
+ struct agent *agt,
+ struct agent_event *aevent)
+
+{
+ int ret;
+
+ assert(agt);
+ assert(trigger);
+ assert(aevent);
+
+ /*
+ * Actual ust event un-registration happens on the trigger
+ * un-registration at that point.
+ */
+
+ DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for trigger %" PRIu64,
+ aevent->name, aevent->loglevel_type,
+ aevent->loglevel_value, lttng_trigger_get_tracer_token(trigger));
+
+ /* Already disabled? */
+ if (!AGENT_EVENT_IS_ENABLED(aevent)) {
+ goto end;
+ }
+
+ ret = agent_disable_event(aevent, agt->domain);
+ if (ret != LTTNG_OK) {
+ goto error;
+ }
+
+end:
+ return LTTNG_OK;
+
+error:
+ return ret;
+}
+
/*
* Disable a given agent event for a given UST session.
*
usess->id);
/* Already disabled? */
- if (!aevent->enabled) {
+ if (!AGENT_EVENT_IS_ENABLED(aevent)) {
goto end;
}
* ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
*/
uevent = trace_ust_find_event(uchan->events, (char *) ust_event_name,
- aevent->filter, LTTNG_UST_LOGLEVEL_ALL, -1, NULL);
+ aevent->filter, LTTNG_UST_ABI_LOGLEVEL_ALL, -1, NULL);
/* If the agent event exists, it must be available on the UST side. */
assert(uevent);
return ret;
}
+/*
+ * Disable agent event matching a given trigger.
+ *
+ * Return LTTNG_OK on success or else a LTTNG_ERR* code.
+ */
+int trigger_agent_disable(
+ const struct lttng_trigger *trigger, struct agent *agt)
+{
+ int ret = LTTNG_OK;
+ struct agent_event *aevent;
+
+ assert(trigger);
+ assert(agt);
+
+ DBG("Event agent disabling for trigger %" PRIu64,
+ lttng_trigger_get_tracer_token(trigger));
+
+ rcu_read_lock();
+ aevent = agent_find_event_by_trigger(trigger, agt);
+
+ if (aevent == NULL) {
+ DBG2("Event agent NOT found by trigger %" PRIu64,
+ lttng_trigger_get_tracer_token(trigger));
+ ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
+ goto end;
+ }
+
+ ret = trigger_agent_disable_one(trigger, agt, aevent);
+
+ if (ret != LTTNG_OK) {
+ goto end;
+ }
+
+end:
+ rcu_read_unlock();
+ return ret;
+}
+
/*
* Disable all agent events matching a given name for a given UST session.
*
* Return LTTNG_OK on success or else a LTTNG_ERR* code.
*/
int event_agent_disable(struct ltt_ust_session *usess, struct agent *agt,
- char *event_name)
+ const char *event_name)
{
int ret = LTTNG_OK;
struct agent_event *aevent;
rcu_read_lock();
cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
node.node) {
- if (!aevent->enabled) {
+ if (!AGENT_EVENT_IS_ENABLED(aevent)) {
continue;
}