+ return ret;
+}
+
+/*
+ * For a specific UST session, create the channel for all registered apps.
+ */
+int ust_app_create_channel_glb(struct ltt_ust_session *usess,
+ struct ltt_ust_channel *uchan)
+{
+ struct lttng_ht_iter iter;
+ struct ust_app *app;
+ struct ust_app_session *ua_sess;
+ struct ust_app_channel *ua_chan;
+
+ /* Very wrong code flow */
+ assert(usess);
+ assert(uchan);
+
+ DBG2("UST app adding channel %s to global domain for session id %d",
+ uchan->name, usess->id);
+
+ rcu_read_lock();
+
+ /* For every registered applications */
+ cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
+ /*
+ * Create session on the tracer side and add it to app session HT. Note
+ * that if session exist, it will simply return a pointer to the ust
+ * app session.
+ */
+ ua_sess = create_ust_app_session(usess, app);
+ if (ua_sess == NULL) {
+ /* Major problem here and it's maybe the tracer or malloc() */
+ goto error;
+ }
+
+ /* Create channel onto application */
+ ua_chan = create_ust_app_channel(ua_sess, uchan, app);
+ if (ua_chan == NULL) {
+ /* Major problem here and it's maybe the tracer or malloc() */
+ goto error;
+ }
+ }
+
+ rcu_read_unlock();
+
+ return 0;
+
+error:
+ return -1;
+}
+
+/*
+ * Enable event for a specific session and channel on the tracer.
+ */
+int ust_app_enable_event_glb(struct ltt_ust_session *usess,
+ struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
+{
+ int ret = 0;
+ struct lttng_ht_iter iter, uiter;
+ struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
+ struct ust_app *app;
+ struct ust_app_session *ua_sess;
+ struct ust_app_channel *ua_chan;
+ struct ust_app_event *ua_event;
+
+ DBG("UST app enabling event %s for all apps for session id %d",
+ uevent->attr.name, usess->id);
+
+ /*
+ * NOTE: At this point, this function is called only if the session and
+ * channel passed are already created for all apps. and enabled on the
+ * tracer also.
+ */
+
+ rcu_read_lock();
+
+ /* For all registered applications */
+ cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
+ ua_sess = lookup_session_by_app(usess, app);
+ /* If ua_sess is NULL, there is a code flow error */
+ assert(ua_sess);
+
+ /* Lookup channel in the ust app session */
+ lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
+ ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
+ /* If the channel is not found, there is a code flow error */
+ assert(ua_chan_node);
+
+ ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
+
+ lttng_ht_lookup(ua_chan->events, (void*)uevent->attr.name, &uiter);
+ ua_event_node = lttng_ht_iter_get_node_str(&uiter);
+ if (ua_event_node == NULL) {
+ DBG3("UST app enable event %s not found for app PID %d."
+ "Skipping app", uevent->attr.name, app->key.pid);
+ continue;
+ }
+ ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
+
+ ret = enable_ust_app_event(ua_sess, ua_event, app);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+
+error:
+ rcu_read_unlock();
+ return ret;
+}
+
+/*
+ * For a specific existing UST session and UST channel, creates the event for
+ * all registered apps.
+ */
+int ust_app_create_event_glb(struct ltt_ust_session *usess,
+ struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
+{
+ int ret = 0;
+ struct lttng_ht_iter iter, uiter;
+ struct lttng_ht_node_str *ua_chan_node;
+ struct ust_app *app;
+ struct ust_app_session *ua_sess;
+ struct ust_app_channel *ua_chan;
+
+ DBG("UST app creating event %s for all apps for session id %d",
+ uevent->attr.name, usess->id);
+
+ rcu_read_lock();
+
+ /* For all registered applications */
+ cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
+ ua_sess = lookup_session_by_app(usess, app);
+ /* If ua_sess is NULL, there is a code flow error */
+ assert(ua_sess);
+
+ /* Lookup channel in the ust app session */
+ lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
+ ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
+ /* If the channel is not found, there is a code flow error */
+ assert(ua_chan_node);
+
+ ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
+
+ ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
+ if (ret < 0) {
+ if (ret != -EEXIST) {
+ /* Possible value at this point: -ENOMEM. If so, we stop! */
+ break;
+ }
+ DBG2("UST app event %s already exist on app PID %d",
+ uevent->attr.name, app->key.pid);
+ continue;
+ }
+ }
+
+ rcu_read_unlock();
+
+ return ret;
+}
+
+/*
+ * Start tracing for a specific UST session and app.
+ */
+int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
+{
+ int ret = 0;
+ struct lttng_ht_iter iter;
+ struct ust_app_session *ua_sess;
+ struct ust_app_channel *ua_chan;
+ struct ltt_ust_stream *ustream;
+ int consumerd_fd;
+
+ DBG("Starting tracing for ust app pid %d", app->key.pid);
+
+ rcu_read_lock();
+
+ ua_sess = lookup_session_by_app(usess, app);
+ if (ua_sess == NULL) {
+ goto error_rcu_unlock;
+ }
+
+ /* Upon restart, we skip the setup, already done */
+ if (ua_sess->started) {
+ goto skip_setup;
+ }
+
+ ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
+ if (ret < 0) {
+ goto error_rcu_unlock;
+ }
+
+ /* For each channel */
+ cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
+ node.node) {
+ /* Create all streams */
+ while (1) {
+ /* Create UST stream */
+ ustream = zmalloc(sizeof(*ustream));
+ if (ustream == NULL) {
+ PERROR("zmalloc ust stream");
+ goto error_rcu_unlock;
+ }
+
+ ret = ustctl_create_stream(app->key.sock, ua_chan->obj,
+ &ustream->obj);
+ if (ret < 0) {
+ /* Got all streams */
+ break;
+ }
+ ustream->handle = ustream->obj->handle;
+
+ /* Order is important */
+ cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
+ ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s_%u",
+ ua_sess->path, ua_chan->name,
+ ua_chan->streams.count++);
+ if (ret < 0) {
+ PERROR("asprintf UST create stream");
+ continue;
+ }
+ DBG2("UST stream %d ready at %s", ua_chan->streams.count,
+ ustream->pathname);
+ }
+ }
+
+ switch (app->bits_per_long) {
+ case 64:
+ consumerd_fd = ust_consumerd64_fd;
+ break;
+ case 32:
+ consumerd_fd = ust_consumerd32_fd;
+ break;
+ default:
+ ret = -EINVAL;
+ goto error_rcu_unlock;
+ }
+
+ /* Setup UST consumer socket and send fds to it */
+ ret = ust_consumer_send_session(consumerd_fd, ua_sess);
+ if (ret < 0) {
+ goto error_rcu_unlock;
+ }
+ ua_sess->started = 1;
+
+skip_setup:
+ /* This start the UST tracing */
+ ret = ustctl_start_session(app->key.sock, ua_sess->handle);
+ if (ret < 0) {
+ ERR("Error starting tracing for app pid: %d", app->key.pid);