+ ret = channel_ust_create(usess, attr, usess->buffer_type);
+ if (ret != LTTNG_OK) {
+ free(attr);
+ goto error;
+ }
+ free(attr);
+ chan_ust_created = 1;
+ }
+
+ ret = context_ust_add(usess, domain, ctx, channel_name);
+ free(app_ctx_provider_name);
+ free(app_ctx_name);
+ app_ctx_name = NULL;
+ app_ctx_provider_name = NULL;
+ if (ret != LTTNG_OK) {
+ goto error;
+ }
+ break;
+ }
+ default:
+ ret = LTTNG_ERR_UND;
+ goto error;
+ }
+
+ ret = LTTNG_OK;
+ goto end;
+
+error:
+ if (chan_kern_created) {
+ struct ltt_kernel_channel *kchan =
+ trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
+ session->kernel_session);
+ /* Created previously, this should NOT fail. */
+ assert(kchan);
+ kernel_destroy_channel(kchan);
+ }
+
+ if (chan_ust_created) {
+ struct ltt_ust_channel *uchan =
+ trace_ust_find_channel_by_name(
+ session->ust_session->domain_global.channels,
+ DEFAULT_CHANNEL_NAME);
+ /* Created previously, this should NOT fail. */
+ assert(uchan);
+ /* Remove from the channel list of the session. */
+ trace_ust_delete_channel(session->ust_session->domain_global.channels,
+ uchan);
+ trace_ust_destroy_channel(uchan);
+ }
+end:
+ free(app_ctx_provider_name);
+ free(app_ctx_name);
+ return ret;
+}
+
+static int validate_event_name(const char *name)
+{
+ int ret = 0;
+ const char *c = name;
+ const char *event_name_end = c + LTTNG_SYMBOL_NAME_LEN;
+ bool null_terminated = false;
+
+ /*
+ * Make sure that unescaped wildcards are only used as the last
+ * character of the event name.
+ */
+ while (c < event_name_end) {
+ switch (*c) {
+ case '\0':
+ null_terminated = true;
+ goto end;
+ case '\\':
+ c++;
+ break;
+ case '*':
+ if ((c + 1) < event_name_end && *(c + 1)) {
+ /* Wildcard is not the last character */
+ ret = LTTNG_ERR_INVALID_EVENT_NAME;
+ goto end;
+ }
+ default:
+ break;
+ }
+ c++;
+ }
+end:
+ if (!ret && !null_terminated) {
+ ret = LTTNG_ERR_INVALID_EVENT_NAME;
+ }
+ return ret;
+}
+
+static inline bool name_starts_with(const char *name, const char *prefix)
+{
+ const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
+
+ return !strncmp(name, prefix, max_cmp_len);
+}
+
+/* Perform userspace-specific event name validation */
+static int validate_ust_event_name(const char *name)
+{
+ int ret = 0;
+
+ if (!name) {
+ ret = -1;
+ goto end;
+ }
+
+ /*
+ * Check name against all internal UST event component namespaces used
+ * by the agents.
+ */
+ if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
+ name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
+ name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
+ ret = -1;
+ }
+
+end:
+ return ret;
+}
+
+/*
+ * Internal version of cmd_enable_event() with a supplemental
+ * "internal_event" flag which is used to enable internal events which should
+ * be hidden from clients. Such events are used in the agent implementation to
+ * enable the events through which all "agent" events are funeled.
+ */
+static int _cmd_enable_event(struct ltt_session *session,
+ struct lttng_domain *domain,
+ char *channel_name, struct lttng_event *event,
+ char *filter_expression,
+ struct lttng_filter_bytecode *filter,
+ struct lttng_event_exclusion *exclusion,
+ int wpipe, bool internal_event)
+{
+ int ret, channel_created = 0;
+ struct lttng_channel *attr;
+
+ assert(session);
+ assert(event);
+ assert(channel_name);
+
+ /* If we have a filter, we must have its filter expression */
+ assert(!(!!filter_expression ^ !!filter));
+
+ DBG("Enable event command for event \'%s\'", event->name);
+
+ rcu_read_lock();
+
+ ret = validate_event_name(event->name);
+ if (ret) {
+ goto error;
+ }
+
+ switch (domain->type) {
+ case LTTNG_DOMAIN_KERNEL:
+ {
+ struct ltt_kernel_channel *kchan;
+
+ /*
+ * If a non-default channel has been created in the
+ * session, explicitely require that -c chan_name needs
+ * to be provided.
+ */
+ if (session->kernel_session->has_non_default_channel
+ && channel_name[0] == '\0') {
+ ret = LTTNG_ERR_NEED_CHANNEL_NAME;
+ goto error;
+ }
+
+ kchan = trace_kernel_get_channel_by_name(channel_name,
+ session->kernel_session);
+ if (kchan == NULL) {
+ attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
+ LTTNG_BUFFER_GLOBAL);
+ if (attr == NULL) {
+ ret = LTTNG_ERR_FATAL;
+ goto error;
+ }
+ strncpy(attr->name, channel_name, sizeof(attr->name));
+
+ ret = cmd_enable_channel(session, domain, attr, wpipe);
+ if (ret != LTTNG_OK) {
+ free(attr);
+ goto error;
+ }
+ free(attr);
+
+ channel_created = 1;
+ }
+
+ /* Get the newly created kernel channel pointer */
+ kchan = trace_kernel_get_channel_by_name(channel_name,
+ session->kernel_session);
+ if (kchan == NULL) {
+ /* This sould not happen... */
+ ret = LTTNG_ERR_FATAL;
+ goto error;
+ }
+
+ switch (event->type) {
+ case LTTNG_EVENT_ALL:
+ {
+ char *filter_expression_a = NULL;
+ struct lttng_filter_bytecode *filter_a = NULL;
+
+ /*
+ * We need to duplicate filter_expression and filter,
+ * because ownership is passed to first enable
+ * event.
+ */
+ if (filter_expression) {
+ filter_expression_a = strdup(filter_expression);
+ if (!filter_expression_a) {
+ ret = LTTNG_ERR_FATAL;
+ goto error;
+ }
+ }
+ if (filter) {
+ filter_a = zmalloc(sizeof(*filter_a) + filter->len);
+ if (!filter_a) {
+ free(filter_expression_a);
+ ret = LTTNG_ERR_FATAL;
+ goto error;
+ }
+ memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
+ }
+ event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
+ ret = event_kernel_enable_event(kchan, event,
+ filter_expression, filter);
+ /* We have passed ownership */
+ filter_expression = NULL;
+ filter = NULL;
+ if (ret != LTTNG_OK) {
+ if (channel_created) {
+ /* Let's not leak a useless channel. */
+ kernel_destroy_channel(kchan);
+ }
+ free(filter_expression_a);
+ free(filter_a);
+ goto error;
+ }
+ event->type = LTTNG_EVENT_SYSCALL; /* Hack */
+ ret = event_kernel_enable_event(kchan, event,
+ filter_expression_a, filter_a);
+ /* We have passed ownership */
+ filter_expression_a = NULL;
+ filter_a = NULL;
+ if (ret != LTTNG_OK) {
+ goto error;
+ }
+ break;
+ }
+ case LTTNG_EVENT_PROBE:
+ case LTTNG_EVENT_FUNCTION:
+ case LTTNG_EVENT_FUNCTION_ENTRY:
+ case LTTNG_EVENT_TRACEPOINT:
+ ret = event_kernel_enable_event(kchan, event,
+ filter_expression, filter);
+ /* We have passed ownership */
+ filter_expression = NULL;
+ filter = NULL;
+ if (ret != LTTNG_OK) {
+ if (channel_created) {
+ /* Let's not leak a useless channel. */
+ kernel_destroy_channel(kchan);
+ }
+ goto error;
+ }
+ break;
+ case LTTNG_EVENT_SYSCALL:
+ ret = event_kernel_enable_event(kchan, event,
+ filter_expression, filter);
+ /* We have passed ownership */
+ filter_expression = NULL;
+ filter = NULL;
+ if (ret != LTTNG_OK) {
+ goto error;
+ }
+ break;
+ default:
+ ret = LTTNG_ERR_UNK;
+ goto error;
+ }
+
+ kernel_wait_quiescent(kernel_tracer_fd);
+ break;
+ }
+ case LTTNG_DOMAIN_UST:
+ {
+ struct ltt_ust_channel *uchan;
+ struct ltt_ust_session *usess = session->ust_session;
+
+ assert(usess);
+
+ /*
+ * If a non-default channel has been created in the
+ * session, explicitely require that -c chan_name needs
+ * to be provided.
+ */
+ if (usess->has_non_default_channel && channel_name[0] == '\0') {
+ ret = LTTNG_ERR_NEED_CHANNEL_NAME;
+ goto error;
+ }
+
+ /* Get channel from global UST domain */
+ uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
+ channel_name);
+ if (uchan == NULL) {
+ /* Create default channel */
+ attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
+ usess->buffer_type);
+ if (attr == NULL) {
+ ret = LTTNG_ERR_FATAL;
+ goto error;
+ }
+ strncpy(attr->name, channel_name, sizeof(attr->name));
+
+ ret = cmd_enable_channel(session, domain, attr, wpipe);