+static int init_ust_event_notifier_from_event_rule(
+ const struct lttng_event_rule *rule,
+ struct lttng_ust_event_notifier *event_notifier)
+{
+ enum lttng_event_rule_status status;
+ enum lttng_loglevel_type loglevel_type;
+ enum lttng_ust_loglevel_type ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
+ int loglevel = -1, ret = 0;
+ const char *pattern;
+
+ /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */
+ assert(lttng_event_rule_get_type(rule) ==
+ LTTNG_EVENT_RULE_TYPE_TRACEPOINT);
+
+ memset(event_notifier, 0, sizeof(*event_notifier));
+
+ status = lttng_event_rule_tracepoint_get_pattern(rule, &pattern);
+ if (status != LTTNG_EVENT_RULE_STATUS_OK) {
+ /* At this point, this is a fatal error. */
+ abort();
+ }
+
+ status = lttng_event_rule_tracepoint_get_log_level_type(
+ rule, &loglevel_type);
+ if (status != LTTNG_EVENT_RULE_STATUS_OK) {
+ /* At this point, this is a fatal error. */
+ abort();
+ }
+
+ switch (loglevel_type) {
+ case LTTNG_EVENT_LOGLEVEL_ALL:
+ ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
+ break;
+ case LTTNG_EVENT_LOGLEVEL_RANGE:
+ ust_loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
+ break;
+ case LTTNG_EVENT_LOGLEVEL_SINGLE:
+ ust_loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
+ break;
+ default:
+ /* Unknown log level specification type. */
+ abort();
+ }
+
+ if (loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
+ status = lttng_event_rule_tracepoint_get_log_level(
+ rule, &loglevel);
+ assert(status == LTTNG_EVENT_RULE_STATUS_OK);
+ }
+
+ event_notifier->event.instrumentation = LTTNG_UST_TRACEPOINT;
+ ret = lttng_strncpy(event_notifier->event.name, pattern,
+ LTTNG_UST_SYM_NAME_LEN - 1);
+ if (ret) {
+ ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
+ pattern);
+ goto end;
+ }
+
+ event_notifier->event.loglevel_type = ust_loglevel_type;
+ event_notifier->event.loglevel = loglevel;
+end:
+ return ret;
+}
+
+/*
+ * Create the specified event notifier against the user space tracer of a
+ * given application.
+ */
+static int create_ust_event_notifier(struct ust_app *app,
+ struct ust_app_event_notifier_rule *ua_event_notifier_rule)
+{
+ int ret = 0;
+ struct lttng_ust_event_notifier event_notifier;
+
+ health_code_update();
+ assert(app->event_notifier_group.object);
+
+ ret = init_ust_event_notifier_from_event_rule(
+ ua_event_notifier_rule->event_rule, &event_notifier);
+ if (ret) {
+ ERR("Failed to initialize UST event notifier from event rule: app = '%s' (ppid: %d)",
+ app->name, app->ppid);
+ goto error;
+ }
+
+ event_notifier.event.token = ua_event_notifier_rule->token;
+
+ /* Create UST event notifier against the tracer. */
+ pthread_mutex_lock(&app->sock_lock);
+ ret = ustctl_create_event_notifier(app->sock, &event_notifier,
+ app->event_notifier_group.object,
+ &ua_event_notifier_rule->obj);
+ pthread_mutex_unlock(&app->sock_lock);
+ if (ret < 0) {
+ if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
+ ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d",
+ event_notifier.event.name, app->name,
+ app->ppid, ret);
+ } else {
+ /*
+ * This is normal behavior, an application can die
+ * during the creation process. Don't report an error so
+ * the execution can continue normally.
+ */
+ ret = 0;
+ DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)",
+ app->name, app->ppid);
+ }
+
+ goto error;
+ }
+
+ ua_event_notifier_rule->handle = ua_event_notifier_rule->obj->handle;
+
+ DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p",
+ event_notifier.event.name, app->name, app->ppid,
+ ua_event_notifier_rule->obj);
+
+ health_code_update();
+
+ /* Set filter if one is present. */
+ if (ua_event_notifier_rule->filter) {
+ ret = set_ust_object_filter(app, ua_event_notifier_rule->filter,
+ ua_event_notifier_rule->obj);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+
+ /* Set exclusions for the event. */
+ if (ua_event_notifier_rule->exclusion) {
+ ret = set_ust_object_exclusions(app,
+ ua_event_notifier_rule->exclusion,
+ ua_event_notifier_rule->obj);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+
+ /*
+ * We now need to explicitly enable the event, since it
+ * is disabled at creation.
+ */
+ ret = enable_ust_object(app, ua_event_notifier_rule->obj);
+ if (ret < 0) {
+ /*
+ * If we hit an EPERM, something is wrong with our enable call.
+ * If we get an EEXIST, there is a problem on the tracer side
+ * since we just created it.
+ */
+ switch (ret) {
+ case -LTTNG_UST_ERR_PERM:
+ /* Code flow problem. */
+ abort();
+ case -LTTNG_UST_ERR_EXIST:
+ /* It's OK for our use case. */
+ ret = 0;
+ break;
+ default:
+ break;
+ }
+
+ goto error;
+ }
+
+ ua_event_notifier_rule->enabled = true;
+
+error:
+ health_code_update();
+ return ret;
+}
+