+static void destroy_id_tracker_node_rcu(struct rcu_head *head)
+{
+ struct ust_id_tracker_node *tracker_node = caa_container_of(
+ head, struct ust_id_tracker_node, node.head);
+ free(tracker_node);
+}
+
+static void destroy_id_tracker_node(struct ust_id_tracker_node *tracker_node)
+{
+ call_rcu(&tracker_node->node.head, destroy_id_tracker_node_rcu);
+}
+
+static int init_id_tracker(struct ust_id_tracker *id_tracker)
+{
+ int ret = LTTNG_OK;
+
+ id_tracker->ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
+ if (!id_tracker->ht) {
+ ret = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+
+end:
+ return ret;
+}
+
+/*
+ * Teardown id tracker content, but don't free id_tracker object.
+ */
+static void fini_id_tracker(struct ust_id_tracker *id_tracker)
+{
+ struct ust_id_tracker_node *tracker_node;
+ struct lttng_ht_iter iter;
+
+ if (!id_tracker->ht) {
+ return;
+ }
+ rcu_read_lock();
+ cds_lfht_for_each_entry (id_tracker->ht->ht, &iter.iter, tracker_node,
+ node.node) {
+ int ret = lttng_ht_del(id_tracker->ht, &iter);
+
+ assert(!ret);
+ destroy_id_tracker_node(tracker_node);
+ }
+ rcu_read_unlock();
+ ht_cleanup_push(id_tracker->ht);
+ id_tracker->ht = NULL;
+}
+
+static struct ust_id_tracker_node *id_tracker_lookup(
+ struct ust_id_tracker *id_tracker,
+ int id,
+ struct lttng_ht_iter *iter)
+{
+ unsigned long _id = (unsigned long) id;
+ struct lttng_ht_node_ulong *node;
+
+ lttng_ht_lookup(id_tracker->ht, (void *) _id, iter);
+ node = lttng_ht_iter_get_node_ulong(iter);
+ if (node) {
+ return caa_container_of(node, struct ust_id_tracker_node, node);
+ } else {
+ return NULL;
+ }
+}
+
+static int id_tracker_add_id(struct ust_id_tracker *id_tracker, int id)
+{
+ int retval = LTTNG_OK;
+ struct ust_id_tracker_node *tracker_node;
+ struct lttng_ht_iter iter;
+
+ if (id < 0) {
+ retval = LTTNG_ERR_INVALID;
+ goto end;
+ }
+ tracker_node = id_tracker_lookup(id_tracker, id, &iter);
+ if (tracker_node) {
+ /* Already exists. */
+ retval = LTTNG_ERR_ID_TRACKED;
+ goto end;
+ }
+ tracker_node = zmalloc(sizeof(*tracker_node));
+ if (!tracker_node) {
+ retval = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+ lttng_ht_node_init_ulong(&tracker_node->node, (unsigned long) id);
+ lttng_ht_add_unique_ulong(id_tracker->ht, &tracker_node->node);
+end:
+ return retval;
+}
+
+static int id_tracker_del_id(struct ust_id_tracker *id_tracker, int id)
+{
+ int retval = LTTNG_OK, ret;
+ struct ust_id_tracker_node *tracker_node;
+ struct lttng_ht_iter iter;
+
+ if (id < 0) {
+ retval = LTTNG_ERR_INVALID;
+ goto end;
+ }
+ tracker_node = id_tracker_lookup(id_tracker, id, &iter);
+ if (!tracker_node) {
+ /* Not found */
+ retval = LTTNG_ERR_ID_NOT_TRACKED;
+ goto end;
+ }
+ ret = lttng_ht_del(id_tracker->ht, &iter);
+ assert(!ret);
+
+ destroy_id_tracker_node(tracker_node);
+end:
+ return retval;
+}
+
+static struct ust_id_tracker *get_id_tracker(struct ltt_ust_session *session,
+ enum lttng_tracker_type tracker_type)
+{
+ switch (tracker_type) {
+ case LTTNG_TRACKER_VPID:
+ return &session->vpid_tracker;
+ case LTTNG_TRACKER_VUID:
+ return &session->vuid_tracker;
+ case LTTNG_TRACKER_VGID:
+ return &session->vgid_tracker;
+ default:
+ return NULL;
+ }
+}
+
+static struct lttng_tracker_list *get_id_tracker_list(
+ struct ltt_ust_session *session,
+ enum lttng_tracker_type tracker_type)
+{
+ switch (tracker_type) {
+ case LTTNG_TRACKER_VPID:
+ return session->tracker_list_vpid;
+ case LTTNG_TRACKER_VUID:
+ return session->tracker_list_vuid;
+ case LTTNG_TRACKER_VGID:
+ return session->tracker_list_vgid;
+ default:
+ return NULL;
+ }
+}
+
+/*
+ * The session lock is held when calling this function.
+ */
+int trace_ust_id_tracker_lookup(enum lttng_tracker_type tracker_type,
+ struct ltt_ust_session *session,
+ int id)
+{
+ struct lttng_ht_iter iter;
+ struct ust_id_tracker *id_tracker;
+
+ id_tracker = get_id_tracker(session, tracker_type);
+ if (!id_tracker) {
+ abort();
+ }
+ if (!id_tracker->ht) {
+ return 1;
+ }
+ if (id_tracker_lookup(id_tracker, id, &iter)) {
+ return 1;
+ }
+ return 0;
+}
+
+/*
+ * Called with the session lock held.
+ */
+int trace_ust_track_id(enum lttng_tracker_type tracker_type,
+ struct ltt_ust_session *session,
+ const struct lttng_tracker_id *id)
+{
+ int retval = LTTNG_OK;
+ bool should_update_apps = false;
+ struct ust_id_tracker *id_tracker;
+ struct lttng_tracker_list *tracker_list;
+ int value;
+ struct lttng_tracker_ids *saved_ids;
+
+ if (tracker_type == LTTNG_TRACKER_PID) {
+ DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
+ tracker_type = LTTNG_TRACKER_VPID;
+ }
+
+ retval = lttng_tracker_id_lookup_string(tracker_type, id, &value);
+ if (retval != LTTNG_OK) {
+ return retval;
+ }
+ tracker_list = get_id_tracker_list(session, tracker_type);
+ if (!tracker_list) {
+ return LTTNG_ERR_INVALID;
+ }
+ /* Save list for restore on error. */
+ retval = lttng_tracker_id_get_list(tracker_list, &saved_ids);
+ if (retval != LTTNG_OK) {
+ return LTTNG_ERR_INVALID;
+ }
+ /* Add to list. */
+ retval = lttng_tracker_list_add(tracker_list, id);
+ if (retval != LTTNG_OK) {
+ goto end;
+ }
+
+ id_tracker = get_id_tracker(session, tracker_type);
+ if (!id_tracker) {
+ abort();
+ }
+ if (value == -1) {
+ /* Track all ids: destroy tracker if exists. */
+ if (id_tracker->ht) {
+ fini_id_tracker(id_tracker);
+ /* Ensure all apps have session. */
+ should_update_apps = true;
+ }
+ } else {
+ if (!id_tracker->ht) {
+ /* Create tracker. */
+ retval = init_id_tracker(id_tracker);
+ if (retval != LTTNG_OK) {
+ ERR("Error initializing ID tracker");
+ goto end_restore;
+ }
+ retval = id_tracker_add_id(id_tracker, value);
+ if (retval != LTTNG_OK) {
+ fini_id_tracker(id_tracker);
+ goto end_restore;
+ }
+ /* Remove all apps from session except pid. */
+ should_update_apps = true;
+ } else {
+ struct ust_app *app;
+
+ retval = id_tracker_add_id(id_tracker, value);
+ if (retval != LTTNG_OK) {
+ goto end_restore;
+ }
+ /* Add session to application */
+ switch (tracker_type) {
+ case LTTNG_TRACKER_VPID:
+ app = ust_app_find_by_pid(value);
+ if (app) {
+ should_update_apps = true;
+ }
+ break;
+ default:
+ should_update_apps = true;
+ }
+ }
+ }
+ if (should_update_apps && session->active) {
+ ust_app_global_update_all(session);
+ }
+ goto end;
+
+end_restore:
+ if (lttng_tracker_id_set_list(tracker_list, saved_ids) != LTTNG_OK) {
+ ERR("Error on tracker add error handling.\n");
+ }
+end:
+ lttng_tracker_ids_destroy(saved_ids);
+ return retval;
+}
+
+/*
+ * Called with the session lock held.
+ */
+int trace_ust_untrack_id(enum lttng_tracker_type tracker_type,
+ struct ltt_ust_session *session,
+ const struct lttng_tracker_id *id)
+{
+ int retval = LTTNG_OK;
+ bool should_update_apps = false;
+ struct ust_id_tracker *id_tracker;
+ struct lttng_tracker_list *tracker_list;
+ int value;
+ struct lttng_tracker_ids *saved_ids;
+
+ if (tracker_type == LTTNG_TRACKER_PID) {
+ DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
+ tracker_type = LTTNG_TRACKER_VPID;
+ }
+
+ retval = lttng_tracker_id_lookup_string(tracker_type, id, &value);
+ if (retval != LTTNG_OK) {
+ return retval;
+ }
+
+ tracker_list = get_id_tracker_list(session, tracker_type);
+ if (!tracker_list) {
+ return LTTNG_ERR_INVALID;
+ }
+ /* Save list for restore on error. */
+ retval = lttng_tracker_id_get_list(tracker_list, &saved_ids);
+ if (retval != LTTNG_OK) {
+ return LTTNG_ERR_INVALID;
+ }
+ /* Remove from list. */
+ retval = lttng_tracker_list_remove(tracker_list, id);
+ if (retval != LTTNG_OK) {
+ goto end;
+ }
+
+ id_tracker = get_id_tracker(session, tracker_type);
+ if (!id_tracker) {
+ abort();
+ }
+
+ if (value == -1) {
+ /* Create empty tracker, replace old tracker. */
+ struct ust_id_tracker tmp_tracker;
+
+ tmp_tracker = *id_tracker;
+ retval = init_id_tracker(id_tracker);
+ if (retval != LTTNG_OK) {
+ ERR("Error initializing ID tracker");
+ /* Rollback operation. */
+ *id_tracker = tmp_tracker;
+ goto end_restore;
+ }
+ fini_id_tracker(&tmp_tracker);
+
+ /* Remove session from all applications */
+ should_update_apps = true;
+ } else {
+ struct ust_app *app;
+
+ if (!id_tracker->ht) {
+ /* No ID being tracked. */
+ retval = LTTNG_ERR_ID_NOT_TRACKED;
+ goto end_restore;
+ }
+ /* Remove ID from tracker */
+ retval = id_tracker_del_id(id_tracker, value);
+ if (retval != LTTNG_OK) {
+ goto end_restore;
+ }
+ switch (tracker_type) {
+ case LTTNG_TRACKER_VPID:
+ /* Remove session from application. */
+ app = ust_app_find_by_pid(value);
+ if (app) {
+ should_update_apps = true;
+ }
+ break;
+ default:
+ /* Keep only apps matching ID. */
+ should_update_apps = true;
+ }
+ }
+ if (should_update_apps && session->active) {
+ ust_app_global_update_all(session);
+ }
+ goto end;
+
+end_restore:
+ if (lttng_tracker_id_set_list(tracker_list, saved_ids) != LTTNG_OK) {
+ ERR("Error on tracker remove error handling.\n");
+ }
+end:
+ lttng_tracker_ids_destroy(saved_ids);
+ return retval;
+}
+
+/*
+ * Called with session lock held.
+ */
+int trace_ust_list_tracker_ids(enum lttng_tracker_type tracker_type,
+ struct ltt_ust_session *session,
+ struct lttng_tracker_ids **_ids)
+{
+ int ret = LTTNG_OK;
+ struct lttng_tracker_list *tracker_list;
+
+ if (tracker_type == LTTNG_TRACKER_PID) {
+ DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
+ tracker_type = LTTNG_TRACKER_VPID;
+ }
+
+ tracker_list = get_id_tracker_list(session, tracker_type);
+ if (!tracker_list) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ ret = lttng_tracker_id_get_list(tracker_list, _ids);
+ if (ret != LTTNG_OK) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+end:
+ return ret;
+}
+