+ /*
+ * Enabled state is based on union of enablers, with
+ * intesection of session and channel transient enable
+ * states.
+ */
+ enabled = enabled && session->tstate && event->chan->tstate;
+
+ CMM_STORE_SHARED(event->enabled, enabled);
+ /*
+ * Sync tracepoint registration with event enabled
+ * state.
+ */
+ if (enabled) {
+ if (!event->registered)
+ register_event(event);
+ } else {
+ if (event->registered)
+ unregister_event(event);
+ }
+
+ /* Check if has enablers without bytecode enabled */
+ cds_list_for_each_entry(enabler_ref,
+ &event->enablers_ref_head, node) {
+ if (enabler_ref->ref->enabled
+ && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) {
+ has_enablers_without_bytecode = 1;
+ break;
+ }
+ }
+ event->has_enablers_without_bytecode =
+ has_enablers_without_bytecode;
+
+ /* Enable filters */
+ cds_list_for_each_entry(runtime,
+ &event->filter_bytecode_runtime_head, node) {
+ lttng_bytecode_filter_sync_state(runtime);
+ }
+ }
+ __tracepoint_probe_prune_release_queue();
+}
+
+static
+void lttng_create_event_notifier_if_missing(
+ struct lttng_event_notifier_enabler *event_notifier_enabler)
+{
+ struct lttng_event_notifier_group *event_notifier_group = event_notifier_enabler->group;
+ struct lttng_probe_desc *probe_desc;
+ struct cds_list_head *probe_list;
+ int i;
+
+ probe_list = lttng_get_probe_list_head();
+
+ cds_list_for_each_entry(probe_desc, probe_list, head) {
+ for (i = 0; i < probe_desc->nr_events; i++) {
+ int ret;
+ bool found = false;
+ const struct lttng_event_desc *desc;
+ struct lttng_event_notifier *event_notifier;
+ struct cds_hlist_head *head;
+ struct cds_hlist_node *node;
+
+ desc = probe_desc->event_desc[i];
+
+ if (!lttng_desc_match_enabler(desc,
+ lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)))
+ continue;
+
+ /*
+ * Given the current event_notifier group, get the bucket that
+ * the target event_notifier would be if it was already
+ * created.
+ */
+ head = borrow_hash_table_bucket(
+ event_notifier_group->event_notifiers_ht.table,
+ LTTNG_UST_EVENT_NOTIFIER_HT_SIZE, desc);
+
+ cds_hlist_for_each_entry(event_notifier, node, head, hlist) {
+ /*
+ * Check if event_notifier already exists by checking
+ * if the event_notifier and enabler share the same
+ * description and id.
+ */
+ if (event_notifier->desc == desc &&
+ event_notifier->user_token == event_notifier_enabler->user_token) {
+ found = true;
+ break;
+ }
+ }
+
+ if (found)
+ continue;
+
+ /*
+ * We need to create a event_notifier for this event probe.
+ */
+ ret = lttng_event_notifier_create(desc,
+ event_notifier_enabler->user_token,
+ event_notifier_enabler->error_counter_index,
+ event_notifier_group);
+ if (ret) {
+ DBG("Unable to create event_notifier %s, error %d\n",
+ probe_desc->event_desc[i]->name, ret);
+ }
+ }
+ }
+}
+
+/*
+ * Create event_notifiers associated with a event_notifier enabler (if not already present).
+ */
+static
+int lttng_event_notifier_enabler_ref_event_notifiers(
+ struct lttng_event_notifier_enabler *event_notifier_enabler)
+{
+ struct lttng_event_notifier_group *event_notifier_group = event_notifier_enabler->group;
+ struct lttng_event_notifier *event_notifier;
+
+ /*
+ * Only try to create event_notifiers for enablers that are enabled, the user
+ * might still be attaching filter or exclusion to the
+ * event_notifier_enabler.
+ */
+ if (!lttng_event_notifier_enabler_as_enabler(event_notifier_enabler)->enabled)
+ goto end;
+
+ /* First, ensure that probe event_notifiers are created for this enabler. */
+ lttng_create_event_notifier_if_missing(event_notifier_enabler);
+
+ /* Link the created event_notifier with its associated enabler. */
+ cds_list_for_each_entry(event_notifier, &event_notifier_group->event_notifiers_head, node) {
+ struct lttng_enabler_ref *enabler_ref;
+
+ if (!lttng_event_notifier_enabler_match_event_notifier(event_notifier_enabler, event_notifier))
+ continue;
+
+ enabler_ref = lttng_enabler_ref(&event_notifier->enablers_ref_head,
+ lttng_event_notifier_enabler_as_enabler(event_notifier_enabler));
+ if (!enabler_ref) {
+ /*
+ * If no backward ref, create it.
+ * Add backward ref from event_notifier to enabler.
+ */
+ enabler_ref = zmalloc(sizeof(*enabler_ref));
+ if (!enabler_ref)
+ return -ENOMEM;
+
+ enabler_ref->ref = lttng_event_notifier_enabler_as_enabler(
+ event_notifier_enabler);
+ cds_list_add(&enabler_ref->node,
+ &event_notifier->enablers_ref_head);
+ }
+
+ /*
+ * Link filter bytecodes if not linked yet.
+ */
+ lttng_enabler_link_bytecode(event_notifier->desc,
+ &event_notifier_group->ctx,
+ &event_notifier->filter_bytecode_runtime_head,
+ <tng_event_notifier_enabler_as_enabler(event_notifier_enabler)->filter_bytecode_head);
+
+ /*
+ * Link capture bytecodes if not linked yet.
+ */
+ lttng_enabler_link_bytecode(event_notifier->desc,
+ &event_notifier_group->ctx, &event_notifier->capture_bytecode_runtime_head,
+ &event_notifier_enabler->capture_bytecode_head);
+
+ event_notifier->num_captures = event_notifier_enabler->num_captures;
+ }
+end:
+ return 0;
+}
+
+static
+void lttng_event_notifier_group_sync_enablers(struct lttng_event_notifier_group *event_notifier_group)
+{
+ struct lttng_event_notifier_enabler *event_notifier_enabler;
+ struct lttng_event_notifier *event_notifier;
+
+ cds_list_for_each_entry(event_notifier_enabler, &event_notifier_group->enablers_head, node)
+ lttng_event_notifier_enabler_ref_event_notifiers(event_notifier_enabler);
+
+ /*
+ * For each event_notifier, if at least one of its enablers is enabled,
+ * we enable the event_notifier, else we disable it.
+ */
+ cds_list_for_each_entry(event_notifier, &event_notifier_group->event_notifiers_head, node) {
+ struct lttng_enabler_ref *enabler_ref;
+ struct lttng_bytecode_runtime *runtime;
+ int enabled = 0, has_enablers_without_bytecode = 0;
+
+ /* Enable event_notifiers */
+ cds_list_for_each_entry(enabler_ref,
+ &event_notifier->enablers_ref_head, node) {
+ if (enabler_ref->ref->enabled) {
+ enabled = 1;
+ break;
+ }
+ }
+
+ CMM_STORE_SHARED(event_notifier->enabled, enabled);
+ /*
+ * Sync tracepoint registration with event_notifier enabled
+ * state.
+ */
+ if (enabled) {
+ if (!event_notifier->registered)
+ register_event_notifier(event_notifier);
+ } else {
+ if (event_notifier->registered)
+ unregister_event_notifier(event_notifier);
+ }
+
+ /* Check if has enablers without bytecode enabled */
+ cds_list_for_each_entry(enabler_ref,
+ &event_notifier->enablers_ref_head, node) {
+ if (enabler_ref->ref->enabled
+ && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) {
+ has_enablers_without_bytecode = 1;
+ break;
+ }
+ }
+ event_notifier->has_enablers_without_bytecode =
+ has_enablers_without_bytecode;