/*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
+ * 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 as published by the Free
- * Software Foundation; only version 2 of the License.
+ * 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., 59 Temple
- * Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-#define _GNU_SOURCE
-#include <errno.h>
+#define _LGPL_SOURCE
#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 <common/context.h>
#include "channel.h"
#include "event.h"
#include "kernel.h"
-#include "ust-ctl.h"
+#include "lttng-sessiond.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"
/*
- * Setup a lttng_event used to enable *all* syscall tracing.
+ * Add unique UST event based on the event name, filter bytecode and loglevel.
*/
-static void init_syscalls_kernel_event(struct lttng_event *event)
+static void add_unique_ust_event(struct lttng_ht *ht,
+ struct ltt_ust_event *event)
{
- event->name[0] = '\0';
- /*
- * We use LTTNG_EVENT* here since the trace kernel creation will make the
- * right changes for the kernel.
- */
- event->type = LTTNG_EVENT_SYSCALL;
+ struct cds_lfht_node *node_ptr;
+ struct ltt_ust_ht_key key;
+
+ assert(ht);
+ assert(ht->ht);
+ assert(event);
+
+ key.name = event->attr.name;
+ key.filter = (struct lttng_bytecode *) event->filter;
+ key.loglevel_type = event->attr.loglevel_type;
+ key.loglevel_value = event->attr.loglevel;
+ key.exclusion = event->exclusion;
+
+ node_ptr = cds_lfht_add_unique(ht->ht,
+ ht->hash_fct(event->node.key, lttng_ht_seed),
+ trace_ust_ht_match_event, &key, &event->node.node);
+ assert(node_ptr == &event->node.node);
}
/*
- * Disable kernel tracepoint event for a channel from the kernel session.
+ * Disable kernel tracepoint events for a channel from the kernel session of
+ * a specified event_name and event type.
+ * On type LTTNG_EVENT_ALL all events with event_name are disabled.
+ * If event_name is NULL all events of the specified type are disabled.
*/
-int event_kernel_disable_tracepoint(struct ltt_kernel_session *ksession,
- struct ltt_kernel_channel *kchan, char *event_name)
+int event_kernel_disable_event(struct ltt_kernel_channel *kchan,
+ const char *event_name, enum lttng_event_type type)
{
- int ret;
+ int ret, error = 0, found = 0;
struct ltt_kernel_event *kevent;
- kevent = trace_kernel_get_event_by_name(event_name, kchan);
- if (kevent == NULL) {
- ret = LTTCOMM_NO_EVENT;
- goto error;
- }
+ assert(kchan);
- ret = kernel_disable_event(kevent);
- if (ret < 0) {
- ret = LTTCOMM_KERN_DISABLE_FAIL;
- goto error;
+ /* For each event in the kernel session */
+ cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
+ if (type != LTTNG_EVENT_ALL && kevent->type != type)
+ continue;
+ if (event_name != NULL && strcmp(event_name, kevent->event->name)) {
+ continue;
+ }
+ found++;
+ ret = kernel_disable_event(kevent);
+ if (ret < 0) {
+ error = 1;
+ continue;
+ }
}
+ DBG("Disable kernel event: found %d events with name: %s and type: %d",
+ found, event_name ? event_name : "NULL", type);
- DBG("Kernel event %s disable for channel %s.",
- kevent->event->name, kchan->channel->name);
-
- ret = LTTCOMM_OK;
+ if (event_name != NULL && !found) {
+ ret = LTTNG_ERR_NO_EVENT;
+ } else {
+ ret = error ? LTTNG_ERR_KERN_DISABLE_FAIL : LTTNG_OK;
+ }
-error:
return ret;
}
/*
- * Disable kernel tracepoint events for a channel from the kernel session.
+ * Enable kernel tracepoint event for a channel from the kernel session.
+ * We own filter_expression and filter.
*/
-int event_kernel_disable_all_tracepoints(struct ltt_kernel_session *ksession,
- struct ltt_kernel_channel *kchan)
+int event_kernel_enable_event(struct ltt_kernel_channel *kchan,
+ struct lttng_event *event, char *filter_expression,
+ struct lttng_bytecode *filter)
{
int ret;
struct ltt_kernel_event *kevent;
- /* For each event in the kernel session */
- cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
- ret = kernel_disable_event(kevent);
+ assert(kchan);
+ assert(event);
+
+ kevent = trace_kernel_find_event(event->name, kchan,
+ event->type, filter);
+ if (kevent == NULL) {
+ ret = kernel_create_event(event, kchan, filter_expression, filter);
+ /* We have passed ownership */
+ filter_expression = NULL;
+ filter = NULL;
+ if (ret) {
+ goto end;
+ }
+ } else if (kevent->enabled == 0) {
+ ret = kernel_enable_event(kevent);
if (ret < 0) {
- /* We continue disabling the rest */
- continue;
+ ret = LTTNG_ERR_KERN_ENABLE_FAIL;
+ goto end;
}
+ } else {
+ /* At this point, the event is considered enabled */
+ ret = LTTNG_ERR_KERN_EVENT_EXIST;
+ goto end;
}
- ret = LTTCOMM_OK;
+
+ ret = LTTNG_OK;
+end:
+ free(filter_expression);
+ free(filter);
return ret;
}
/*
- * Disable kernel syscall events for a channel from the kernel session.
+ * ============================
+ * UST : The Ultimate Frontier!
+ * ============================
*/
-int event_kernel_disable_all_syscalls(struct ltt_kernel_session *ksession,
- struct ltt_kernel_channel *kchan)
-{
- ERR("Cannot disable syscall tracing for existing session. Please destroy session instead.");
- return LTTCOMM_OK; /* Return OK so disable all succeeds */
-}
/*
- * Disable all kernel event for a channel from the kernel session.
+ * Enable UST tracepoint event for a channel from a UST session.
+ * We own filter_expression, filter, and exclusion.
*/
-int event_kernel_disable_all(struct ltt_kernel_session *ksession,
- struct ltt_kernel_channel *kchan)
+int event_ust_enable_tracepoint(struct ltt_ust_session *usess,
+ struct ltt_ust_channel *uchan, struct lttng_event *event,
+ char *filter_expression,
+ struct lttng_bytecode *filter,
+ struct lttng_event_exclusion *exclusion,
+ bool internal_event)
{
- int ret;
+ int ret = LTTNG_OK, to_create = 0;
+ struct ltt_ust_event *uevent;
- ret = event_kernel_disable_all_tracepoints(ksession, kchan);
- if (ret != LTTCOMM_OK)
- return ret;
- ret = event_kernel_disable_all_syscalls(ksession, kchan);
+ assert(usess);
+ assert(uchan);
+ assert(event);
+
+ rcu_read_lock();
+
+ uevent = trace_ust_find_event(uchan->events, event->name, filter,
+ (enum lttng_ust_abi_loglevel_type) event->loglevel_type,
+ event->loglevel, exclusion);
+ if (!uevent) {
+ ret = trace_ust_create_event(event, filter_expression,
+ filter, exclusion, internal_event, &uevent);
+ /* We have passed ownership */
+ filter_expression = NULL;
+ filter = NULL;
+ exclusion = NULL;
+ if (ret != LTTNG_OK) {
+ goto end;
+ }
+
+ /* Valid to set it after the goto error since uevent is still NULL */
+ to_create = 1;
+ }
+
+ if (uevent->enabled) {
+ /* It's already enabled so everything is OK */
+ assert(!to_create);
+ ret = LTTNG_ERR_UST_EVENT_ENABLED;
+ goto end;
+ }
+
+ uevent->enabled = 1;
+ if (to_create) {
+ /* Add ltt ust event to channel */
+ add_unique_ust_event(uchan->events, uevent);
+ }
+
+ if (!usess->active) {
+ goto end;
+ }
+
+ if (to_create) {
+ /* Create event on all UST registered apps for session */
+ ret = ust_app_create_event_glb(usess, uchan, uevent);
+ } else {
+ /* Enable event on all UST registered apps for session */
+ ret = ust_app_enable_event_glb(usess, uchan, uevent);
+ }
+
+ if (ret < 0) {
+ if (ret == -LTTNG_UST_ERR_EXIST) {
+ ret = LTTNG_ERR_UST_EVENT_EXIST;
+ } else {
+ ret = LTTNG_ERR_UST_ENABLE_FAIL;
+ }
+ goto end;
+ }
+
+ DBG("Event UST %s %s in channel %s", uevent->attr.name,
+ to_create ? "created" : "enabled", uchan->name);
+
+ ret = LTTNG_OK;
+
+end:
+ rcu_read_unlock();
+ free(filter_expression);
+ free(filter);
+ free(exclusion);
return ret;
}
/*
- * Enable kernel tracepoint event for a channel from the kernel session.
+ * Disable UST tracepoint of a channel from a UST session.
*/
-int event_kernel_enable_tracepoint(struct ltt_kernel_session *ksession,
- struct ltt_kernel_channel *kchan, struct lttng_event *event)
+int event_ust_disable_tracepoint(struct ltt_ust_session *usess,
+ struct ltt_ust_channel *uchan, const char *event_name)
{
int ret;
- struct ltt_kernel_event *kevent;
+ struct ltt_ust_event *uevent;
+ struct lttng_ht_node_str *node;
+ struct lttng_ht_iter iter;
+ struct lttng_ht *ht;
- kevent = trace_kernel_get_event_by_name(event->name, kchan);
- if (kevent == NULL) {
- ret = kernel_create_event(event, kchan);
- if (ret < 0) {
- if (ret == -EEXIST) {
- ret = LTTCOMM_KERN_EVENT_EXIST;
- } else {
- ret = LTTCOMM_KERN_ENABLE_FAIL;
- }
- goto end;
+ assert(usess);
+ assert(uchan);
+ assert(event_name);
+
+ ht = uchan->events;
+
+ rcu_read_lock();
+
+ /*
+ * We use a custom lookup since we need the iterator for the next_duplicate
+ * call in the do while loop below.
+ */
+ cds_lfht_lookup(ht->ht, ht->hash_fct((void *) event_name, lttng_ht_seed),
+ trace_ust_ht_match_event_by_name, event_name, &iter.iter);
+ node = lttng_ht_iter_get_node_str(&iter);
+ if (node == NULL) {
+ DBG2("Trace UST event NOT found by name %s", event_name);
+ ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
+ goto error;
+ }
+
+ do {
+ uevent = caa_container_of(node, struct ltt_ust_event, node);
+ assert(uevent);
+
+ if (uevent->enabled == 0) {
+ /* It's already disabled so everything is OK */
+ goto next;
}
- } else if (kevent->enabled == 0) {
- ret = kernel_enable_event(kevent);
- if (ret < 0) {
- ret = LTTCOMM_KERN_ENABLE_FAIL;
- goto end;
+ uevent->enabled = 0;
+ DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
+ uchan->name);
+
+ if (!usess->active) {
+ goto next;
}
- }
- ret = LTTCOMM_OK;
-end:
+ ret = ust_app_disable_event_glb(usess, uchan, uevent);
+ if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
+ ret = LTTNG_ERR_UST_DISABLE_FAIL;
+ goto error;
+ }
+next:
+ /* Get next duplicate event by name. */
+ cds_lfht_next_duplicate(ht->ht, trace_ust_ht_match_event_by_name,
+ event_name, &iter.iter);
+ node = lttng_ht_iter_get_node_str(&iter);
+ } while (node);
+
+ ret = LTTNG_OK;
+
+error:
+ rcu_read_unlock();
return ret;
}
/*
- * Enable all kernel tracepoint events of a channel of the kernel session.
+ * Disable all UST tracepoints for a channel from a UST session.
*/
-int event_kernel_enable_all_tracepoints(struct ltt_kernel_session *ksession,
- struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
+int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess,
+ struct ltt_ust_channel *uchan)
{
- int size, i, ret;
- struct ltt_kernel_event *kevent;
- struct lttng_event *event_list = NULL;
+ int ret, i, size, error = 0;
+ struct lttng_ht_iter iter;
+ struct ltt_ust_event *uevent = NULL;
+ struct lttng_event *events = NULL;
- /* For each event in the kernel session */
- cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
- if (kevent->enabled == 0) {
- ret = kernel_enable_event(kevent);
+ assert(usess);
+ assert(uchan);
+
+ rcu_read_lock();
+
+ /* Disabling existing events */
+ cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
+ node.node) {
+ if (uevent->enabled == 1) {
+ ret = event_ust_disable_tracepoint(usess, uchan,
+ uevent->attr.name);
if (ret < 0) {
- /* Enable failed but still continue */
+ error = LTTNG_ERR_UST_DISABLE_FAIL;
continue;
}
}
}
- size = kernel_list_events(kernel_tracer_fd, &event_list);
+ /* Get all UST available events */
+ size = ust_app_list_events(&events);
if (size < 0) {
- ret = LTTCOMM_KERN_LIST_FAIL;
- goto end;
+ ret = LTTNG_ERR_UST_LIST_FAIL;
+ goto error;
}
for (i = 0; i < size; i++) {
- kevent = trace_kernel_get_event_by_name(event_list[i].name, kchan);
- if (kevent == NULL) {
- /* Default event type for enable all */
- event_list[i].type = LTTNG_EVENT_TRACEPOINT;
- /* Enable each single tracepoint event */
- ret = kernel_create_event(&event_list[i], kchan);
- if (ret < 0) {
- /* Ignore error here and continue */
- }
+ ret = event_ust_disable_tracepoint(usess, uchan,
+ events[i].name);
+ if (ret < 0) {
+ /* Continue to disable the rest... */
+ error = LTTNG_ERR_UST_DISABLE_FAIL;
+ continue;
}
}
- free(event_list);
- ret = LTTCOMM_OK;
-end:
+ ret = error ? error : LTTNG_OK;
+error:
+ rcu_read_unlock();
+ free(events);
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 kernel tracepoint events of a channel of the kernel session.
+ * Enable all agent event for a given UST session.
+ *
+ * Return LTTNG_OK on success or else a LTTNG_ERR* code.
*/
-int event_kernel_enable_all_syscalls(struct ltt_kernel_session *ksession,
- struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
+int event_agent_enable_all(struct ltt_ust_session *usess,
+ struct agent *agt, struct lttng_event *event,
+ struct lttng_bytecode *filter ,char *filter_expression)
{
int ret;
- struct lttng_event event;
- init_syscalls_kernel_event(&event);
+ assert(usess);
- DBG("Enabling all syscall tracing");
+ DBG("Event agent enabling ALL events for session %" PRIu64, usess->id);
- ret = kernel_create_event(&event, kchan);
- if (ret < 0) {
- if (ret == -EEXIST) {
- ret = LTTCOMM_KERN_EVENT_EXIST;
- } else {
- ret = LTTCOMM_KERN_ENABLE_FAIL;
- }
- goto end;
+ /* Enable event on agent application through TCP socket. */
+ ret = event_agent_enable(usess, agt, event, filter, filter_expression);
+ if (ret != LTTNG_OK) {
+ goto error;
}
- ret = LTTCOMM_OK;
-end:
+ agent_enable_all(agt);
+
+ ret = LTTNG_OK;
+
+error:
return ret;
}
/*
- * Enable all kernel events of a channel of the kernel session.
+ * Check if this event's filter requires the activation of application contexts
+ * and enable them in the agent.
+ * TODO: bytecode iterator does not support non-legacy application
+ * contexts yet. Not an issue for now, since they are not generated by
+ * the lttng-ctl library.
*/
-int event_kernel_enable_all(struct ltt_kernel_session *ksession,
- struct ltt_kernel_channel *kchan, int kernel_tracer_fd)
+static int add_filter_app_ctx(struct lttng_bytecode *bytecode,
+ const char *filter_expression, struct agent *agt)
{
- int ret;
+ int ret = LTTNG_OK;
+ char *provider_name = NULL, *ctx_name = NULL;
+ struct bytecode_symbol_iterator *it =
+ bytecode_symbol_iterator_create(bytecode);
- ret = event_kernel_enable_all_tracepoints(ksession, kchan, kernel_tracer_fd);
- if (ret != LTTCOMM_OK) {
+ if (!it) {
+ ret = LTTNG_ERR_NOMEM;
goto end;
}
- ret = event_kernel_enable_all_syscalls(ksession, kchan, kernel_tracer_fd);
-end:
- return ret;
-}
-/*
- * ============================
- * UST : The Ultimate Frontier!
- * ============================
- */
-
-/*
- * Enable all UST tracepoints for a channel from a UST session.
- */
-int event_ust_enable_all_tracepoints(struct ltt_ust_session *usess, int domain,
- struct ltt_ust_channel *uchan)
-{
- int ret, i;
- size_t size;
- struct lttng_ht_iter iter;
- struct ltt_ust_event *uevent = NULL;
- struct lttng_event *events = NULL;
+ do {
+ struct lttng_event_context ctx;
+ const char *symbol_name =
+ bytecode_symbol_iterator_get_name(it);
- switch (domain) {
- case LTTNG_DOMAIN_UST:
- {
- /* Enable existing events */
- cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
- node.node) {
- if (uevent->enabled == 0) {
- ret = ust_app_enable_event_glb(usess, uchan, uevent);
- if (ret < 0) {
- continue;
- }
- uevent->enabled = 1;
- }
+ if (parse_application_context(symbol_name, &provider_name,
+ &ctx_name)) {
+ /* Not an application context. */
+ continue;
}
- /* Get all UST available events */
- size = ust_app_list_events(&events);
- if (size < 0) {
- ret = LTTCOMM_UST_LIST_FAIL;
- goto error;
+ ctx.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT;
+ ctx.u.app_ctx.provider_name = provider_name;
+ ctx.u.app_ctx.ctx_name = ctx_name;
+
+ /* Recognized an application context. */
+ DBG("Enabling event with filter expression \"%s\" requires enabling the %s:%s application context.",
+ filter_expression, provider_name, ctx_name);
+
+ ret = agent_add_context(&ctx, agt);
+ if (ret != LTTNG_OK) {
+ ERR("Failed to add application context %s:%s.",
+ provider_name, ctx_name);
+ goto end;
}
- for (i = 0; i < size; i++) {
- /*
- * Check if event exist and if so, continue since it was enable
- * previously.
- */
- uevent = trace_ust_find_event_by_name(uchan->events,
- events[i].name);
- if (uevent != NULL) {
- ret = ust_app_enable_event_pid(usess, uchan, uevent,
- events[i].pid);
- if (ret < 0) {
- if (ret != -EEXIST) {
- ret = LTTCOMM_UST_ENABLE_FAIL;
- goto error;
- }
- }
- continue;
- }
+ ret = agent_enable_context(&ctx, agt->domain);
+ if (ret != LTTNG_OK) {
+ ERR("Failed to enable application context %s:%s.",
+ provider_name, ctx_name);
+ goto end;
+ }
- /* Create ust event */
- uevent = trace_ust_create_event(&events[i]);
- if (uevent == NULL) {
- ret = LTTCOMM_FATAL;
- goto error_destroy;
- }
+ free(provider_name);
+ free(ctx_name);
+ provider_name = ctx_name = NULL;
+ } while (bytecode_symbol_iterator_next(it) == 0);
+end:
+ free(provider_name);
+ free(ctx_name);
+ bytecode_symbol_iterator_destroy(it);
+ return ret;
+}
- /* Create event for the specific PID */
- ret = ust_app_enable_event_pid(usess, uchan, uevent,
- events[i].pid);
- if (ret < 0) {
- if (ret == -EEXIST) {
- ret = LTTCOMM_UST_EVENT_EXIST;
- goto error;
- } else {
- ret = LTTCOMM_UST_ENABLE_FAIL;
- goto error_destroy;
- }
- }
+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(event);
+ assert(agt);
+
+ rcu_read_lock();
+ aevent = agent_find_event(event->name, event->loglevel_type,
+ event->loglevel, filter_expression, agt);
+ if (!aevent) {
+ aevent = agent_create_event(event->name, event->loglevel_type,
+ event->loglevel, filter,
+ filter_expression);
+ if (!aevent) {
+ ret = LTTNG_ERR_NOMEM;
+ goto error;
+ }
+ filter = NULL;
+ filter_expression = NULL;
+ created = 1;
+ assert(!AGENT_EVENT_IS_ENABLED(aevent));
+ }
- uevent->enabled = 1;
- /* Add ltt ust event to channel */
- rcu_read_lock();
- lttng_ht_add_unique_str(uchan->events, &uevent->node);
- rcu_read_unlock();
+ if (created && aevent->filter) {
+ ret = add_filter_app_ctx(
+ aevent->filter, aevent->filter_expression, agt);
+ if (ret != LTTNG_OK) {
+ goto error;
}
+ }
- free(events);
- break;
+ /* Already enabled? */
+ if (AGENT_EVENT_IS_ENABLED(aevent)) {
+ ret = LTTNG_OK;
+ goto end;
}
-#if 0
- case LTTNG_DOMAIN_UST_EXEC_NAME:
- case LTTNG_DOMAIN_UST_PID:
- case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
-#endif
- default:
- ret = LTTCOMM_UND;
+
+ ret = agent_enable_event(aevent, agt->domain);
+ if (ret != LTTNG_OK) {
goto error;
}
- return LTTCOMM_OK;
+ /* If the event was created prior to the enable, add it to the domain. */
+ if (created) {
+ agent_add_event(aevent, agt);
+ }
-error_destroy:
- trace_ust_destroy_event(uevent);
+ ret = LTTNG_OK;
+ goto end;
error:
- free(events);
+ if (created) {
+ agent_destroy_event(aevent);
+ }
+end:
+ rcu_read_unlock();
+ free(filter);
+ free(filter_expression);
return ret;
}
/*
- * Enable UST tracepoint event for a channel from a UST session.
+ * Enable a single agent event for a given UST session.
+ *
+ * Return LTTNG_OK on success or else a LTTNG_ERR* code.
*/
-int event_ust_enable_tracepoint(struct ltt_ust_session *usess, int domain,
- struct ltt_ust_channel *uchan, struct lttng_event *event)
+int event_agent_enable(struct ltt_ust_session *usess,
+ struct agent *agt,
+ struct lttng_event *event,
+ struct lttng_bytecode *filter,
+ char *filter_expression)
{
- int ret = LTTCOMM_OK, to_create = 0;
- struct ltt_ust_event *uevent;
+ assert(usess);
+ assert(event);
+ assert(agt);
- uevent = trace_ust_find_event_by_name(uchan->events, event->name);
- if (uevent == NULL) {
- uevent = trace_ust_create_event(event);
- if (uevent == NULL) {
- ret = LTTCOMM_FATAL;
- goto error;
- }
- /* Valid to set it after the goto error since uevent is still NULL */
- to_create = 1;
+ 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)";
}
- if (uevent->enabled) {
- /* It's already enabled so everything is OK */
- goto end;
+ 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;
}
- uevent->enabled = 1;
+ d_type = lttng_event_rule_get_domain_type(rule);
+ assert(d_type == agt->domain);
- switch (domain) {
- case LTTNG_DOMAIN_UST:
- {
- if (to_create) {
- /* Create event on all UST registered apps for session */
- ret = ust_app_create_event_glb(usess, uchan, uevent);
- } else {
- /* Enable event on all UST registered apps for session */
- ret = ust_app_enable_event_glb(usess, uchan, uevent);
+ 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;
}
- if (ret < 0) {
- if (ret == -EEXIST) {
- ret = LTTCOMM_UST_EVENT_EXIST;
+ /* 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;
- } else {
- ret = LTTCOMM_UST_ENABLE_FAIL;
- goto error;
}
}
- break;
}
-#if 0
- case LTTNG_DOMAIN_UST_EXEC_NAME:
- case LTTNG_DOMAIN_UST_PID:
- case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
-#endif
+
+ 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.
+ */
+const char *event_get_default_agent_ust_name(enum lttng_domain_type domain)
+{
+ const char *default_event_name = NULL;
+
+ switch (domain) {
+ case LTTNG_DOMAIN_LOG4J:
+ default_event_name = DEFAULT_LOG4J_EVENT_NAME;
+ break;
+ case LTTNG_DOMAIN_JUL:
+ default_event_name = DEFAULT_JUL_EVENT_NAME;
+ break;
+ case LTTNG_DOMAIN_PYTHON:
+ default_event_name = DEFAULT_PYTHON_EVENT_NAME;
+ break;
default:
- ret = LTTCOMM_UND;
- goto end;
+ assert(0);
}
- if (to_create) {
- rcu_read_lock();
- /* Add ltt ust event to channel */
- lttng_ht_add_unique_str(uchan->events, &uevent->node);
- rcu_read_unlock();
- }
+ return default_event_name;
+}
- DBG("Event UST %s %s in channel %s", uevent->attr.name,
- to_create ? "created" : "enabled", uchan->name);
+static int trigger_agent_disable_one(const struct lttng_trigger *trigger,
+ struct agent *agt,
+ struct agent_event *aevent)
- ret = LTTCOMM_OK;
+{
+ int ret;
-end:
- return ret;
+ assert(agt);
+ assert(trigger);
+ assert(aevent);
-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.
+ * Actual ust event un-registration happens on the trigger
+ * un-registration at that point.
*/
- if (to_create) {
- /* In this code path, the uevent was not added to the hash table */
- trace_ust_destroy_event(uevent);
+
+ 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 UST tracepoint of a channel from a UST session.
+ * Disable a given agent event for a given UST session.
+ *
+ * Must be called with the RCU read lock held.
+ * Return LTTNG_OK on success or else a LTTNG_ERR* code.
*/
-int event_ust_disable_tracepoint(struct ltt_ust_session *usess, int domain,
- struct ltt_ust_channel *uchan, char *event_name)
+static int event_agent_disable_one(struct ltt_ust_session *usess,
+ struct agent *agt, struct agent_event *aevent)
{
int ret;
- struct ltt_ust_event *uevent;
+ struct ltt_ust_event *uevent = NULL;
+ struct ltt_ust_channel *uchan = NULL;
+ const char *ust_event_name, *ust_channel_name;
- uevent = trace_ust_find_event_by_name(uchan->events, event_name);
- if (uevent == NULL) {
- ret = LTTCOMM_UST_EVENT_NOT_FOUND;
+ assert(agt);
+ assert(usess);
+ assert(aevent);
+
+ DBG("Event agent disabling %s (loglevel type %d, loglevel value %d) for session %" PRIu64,
+ aevent->name, aevent->loglevel_type, aevent->loglevel_value,
+ usess->id);
+
+ /* Already disabled? */
+ if (!AGENT_EVENT_IS_ENABLED(aevent)) {
+ goto end;
+ }
+
+ if (agt->domain == LTTNG_DOMAIN_JUL) {
+ ust_channel_name = DEFAULT_JUL_CHANNEL_NAME;
+ } else if (agt->domain == LTTNG_DOMAIN_LOG4J) {
+ ust_channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
+ } else if (agt->domain == LTTNG_DOMAIN_PYTHON) {
+ ust_channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
+ } else {
+ ret = LTTNG_ERR_INVALID;
goto error;
}
- if (uevent->enabled == 0) {
- /* It's already enabled so everything is OK */
- ret = LTTCOMM_OK;
- goto end;
+ /*
+ * Disable it on the UST side. First get the channel reference then find
+ * the event and finally disable it.
+ */
+ uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
+ (char *) ust_channel_name);
+ if (!uchan) {
+ ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
+ goto error;
}
- switch (domain) {
- case LTTNG_DOMAIN_UST:
+ ust_event_name = event_get_default_agent_ust_name(agt->domain);
+ if (!ust_event_name) {
+ ret = LTTNG_ERR_FATAL;
+ goto error;
+ }
+
+ /*
+ * Agent UST event has its loglevel type forced to
+ * LTTNG_UST_LOGLEVEL_ALL. The actual loglevel type/value filtering
+ * happens thanks to an UST filter. The following -1 is actually
+ * ignored since the type is LTTNG_UST_LOGLEVEL_ALL.
+ */
+ uevent = trace_ust_find_event(uchan->events, (char *) ust_event_name,
+ aevent->filter, LTTNG_UST_ABI_LOGLEVEL_ALL, -1, NULL);
+ /* If the agent event exists, it must be available on the UST side. */
+ assert(uevent);
+
+ if (usess->active) {
ret = ust_app_disable_event_glb(usess, uchan, uevent);
- if (ret < 0 && ret != -EEXIST) {
- ret = LTTCOMM_UST_DISABLE_FAIL;
+ if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
+ ret = LTTNG_ERR_UST_DISABLE_FAIL;
goto error;
}
- break;
-#if 0
- case LTTNG_DOMAIN_UST_EXEC_NAME:
- case LTTNG_DOMAIN_UST_PID:
- case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
-#endif
- default:
- ret = LTTCOMM_UND;
- goto error;
}
+ /*
+ * Flag event that it's disabled so the shadow copy on the ust app side
+ * will disable it if an application shows up.
+ */
uevent->enabled = 0;
- ret = LTTCOMM_OK;
+
+ ret = agent_disable_event(aevent, agt->domain);
+ if (ret != LTTNG_OK) {
+ goto error;
+ }
end:
- DBG2("Event UST %s disabled in channel %s", uevent->attr.name,
- uchan->name);
+ return LTTNG_OK;
error:
return ret;
}
/*
- * Disable all UST tracepoints for a channel from a UST session.
+ * Disable agent event matching a given trigger.
+ *
+ * Return LTTNG_OK on success or else a LTTNG_ERR* code.
*/
-int event_ust_disable_all_tracepoints(struct ltt_ust_session *usess, int domain,
- struct ltt_ust_channel *uchan)
+int trigger_agent_disable(
+ const struct lttng_trigger *trigger, struct agent *agt)
{
- int ret, i;
- size_t size;
+ 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,
+ const char *event_name)
+{
+ int ret = LTTNG_OK;
+ struct agent_event *aevent;
struct lttng_ht_iter iter;
- struct ltt_ust_event *uevent = NULL;
- struct lttng_event *events = NULL;
+ struct lttng_ht_node_str *node;
- switch (domain) {
- case LTTNG_DOMAIN_UST:
- {
- /* Disabling existing events */
- cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent,
- node.node) {
- if (uevent->enabled == 1) {
- ret = ust_app_disable_event_glb(usess, uchan, uevent);
- if (ret < 0) {
- continue;
- }
- uevent->enabled = 0;
- }
- }
+ assert(agt);
+ assert(usess);
+ assert(event_name);
- /* Get all UST available events */
- size = ust_app_list_events(&events);
- if (size < 0) {
- ret = LTTCOMM_UST_LIST_FAIL;
- goto error;
- }
+ DBG("Event agent disabling %s (all loglevels) for session %" PRIu64, event_name, usess->id);
- for (i = 0; i < size; i++) {
- uevent = trace_ust_find_event_by_name(uchan->events,
- events[i].name);
- if (uevent != NULL && uevent->enabled == 1) {
- ret = ust_app_disable_event_pid(usess, uchan, uevent,
- events[i].pid);
- if (ret < 0 && ret != -EEXIST) {
- ret = LTTCOMM_UST_DISABLE_FAIL;
- goto error;
- }
- uevent->enabled = 0;
- continue;
- }
- }
+ rcu_read_lock();
+ agent_find_events_by_name(event_name, agt, &iter);
+ node = lttng_ht_iter_get_node_str(&iter);
- free(events);
- break;
+ if (node == NULL) {
+ DBG2("Event agent NOT found by name %s", event_name);
+ ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
+ goto end;
}
-#if 0
- case LTTNG_DOMAIN_UST_EXEC_NAME:
- case LTTNG_DOMAIN_UST_PID:
- case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
-#endif
- default:
- ret = LTTCOMM_UND;
+
+ do {
+ aevent = caa_container_of(node, struct agent_event, node);
+ ret = event_agent_disable_one(usess, agt, aevent);
+
+ if (ret != LTTNG_OK) {
+ goto end;
+ }
+
+ /* Get next duplicate agent event by name. */
+ agent_event_next_duplicate(event_name, agt, &iter);
+ node = lttng_ht_iter_get_node_str(&iter);
+ } while (node);
+end:
+ rcu_read_unlock();
+ return ret;
+}
+/*
+ * Disable all agent event for a given UST session.
+ *
+ * Return LTTNG_OK on success or else a LTTNG_ERR* code.
+ */
+int event_agent_disable_all(struct ltt_ust_session *usess,
+ struct agent *agt)
+{
+ int ret;
+ struct agent_event *aevent;
+ struct lttng_ht_iter iter;
+
+ assert(agt);
+ assert(usess);
+
+ /*
+ * Disable event on agent application. Continue to disable all other events
+ * if the * event is not found.
+ */
+ ret = event_agent_disable(usess, agt, "*");
+ if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_NOT_FOUND) {
goto error;
}
- return LTTCOMM_OK;
+ /* Disable every event. */
+ rcu_read_lock();
+ cds_lfht_for_each_entry(agt->events->ht, &iter.iter, aevent,
+ node.node) {
+ if (!AGENT_EVENT_IS_ENABLED(aevent)) {
+ continue;
+ }
+
+ ret = event_agent_disable(usess, agt, aevent->name);
+ if (ret != LTTNG_OK) {
+ goto error_unlock;
+ }
+ }
+ ret = LTTNG_OK;
+error_unlock:
+ rcu_read_unlock();
error:
- free(events);
return ret;
}