+ int ret;
+
+ ret = ustctl_add_context(app->key.sock, &ua_ctx->ctx,
+ ua_chan->obj, &ua_ctx->obj);
+ if (ret < 0) {
+ goto error;
+ }
+
+ ua_ctx->handle = ua_ctx->obj->handle;
+
+ DBG2("UST app context added to channel %s successfully", ua_chan->name);
+
+error:
+ return ret;
+}
+
+/*
+ * Create the event context on the tracer.
+ */
+static
+int create_ust_event_context(struct ust_app_event *ua_event,
+ struct ust_app_ctx *ua_ctx, struct ust_app *app)
+{
+ int ret;
+
+ ret = ustctl_add_context(app->key.sock, &ua_ctx->ctx,
+ ua_event->obj, &ua_ctx->obj);
+ if (ret < 0) {
+ goto error;
+ }
+
+ ua_ctx->handle = ua_ctx->obj->handle;
+
+ DBG2("UST app context added to event %s successfully", ua_event->name);
+
+error:
+ return ret;
+}
+
+/*
+ * Disable the specified event on to UST tracer for the UST session.
+ */
+static int disable_ust_event(struct ust_app *app,
+ struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
+{
+ int ret;
+
+ ret = ustctl_disable(app->key.sock, ua_event->obj);
+ if (ret < 0) {
+ ERR("UST app event %s disable failed for app (pid: %d) "
+ "and session handle %d with ret %d",
+ ua_event->attr.name, app->key.pid, ua_sess->handle, ret);
+ goto error;
+ }
+
+ DBG2("UST app event %s disabled successfully for app (pid: %d)",
+ ua_event->attr.name, app->key.pid);
+
+error:
+ return ret;
+}
+
+/*
+ * Disable the specified channel on to UST tracer for the UST session.
+ */
+static int disable_ust_channel(struct ust_app *app,
+ struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
+{
+ int ret;
+
+ ret = ustctl_disable(app->key.sock, ua_chan->obj);
+ if (ret < 0) {
+ ERR("UST app channel %s disable failed for app (pid: %d) "
+ "and session handle %d with ret %d",
+ ua_chan->name, app->key.pid, ua_sess->handle, ret);
+ goto error;
+ }
+
+ DBG2("UST app channel %s disabled successfully for app (pid: %d)",
+ ua_chan->name, app->key.pid);
+
+error:
+ return ret;
+}
+
+/*
+ * Enable the specified channel on to UST tracer for the UST session.
+ */
+static int enable_ust_channel(struct ust_app *app,
+ struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
+{
+ int ret;
+
+ ret = ustctl_enable(app->key.sock, ua_chan->obj);
+ if (ret < 0) {
+ ERR("UST app channel %s enable failed for app (pid: %d) "
+ "and session handle %d with ret %d",
+ ua_chan->name, app->key.pid, ua_sess->handle, ret);
+ goto error;
+ }
+
+ ua_chan->enabled = 1;
+
+ DBG2("UST app channel %s enabled successfully for app (pid: %d)",
+ ua_chan->name, app->key.pid);
+
+error:
+ return ret;
+}
+
+/*
+ * Enable the specified event on to UST tracer for the UST session.
+ */
+static int enable_ust_event(struct ust_app *app,
+ struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
+{
+ int ret;
+
+ ret = ustctl_enable(app->key.sock, ua_event->obj);
+ if (ret < 0) {
+ ERR("UST app event %s enable failed for app (pid: %d) "
+ "and session handle %d with ret %d",
+ ua_event->attr.name, app->key.pid, ua_sess->handle, ret);
+ goto error;
+ }
+
+ DBG2("UST app event %s enabled successfully for app (pid: %d)",
+ ua_event->attr.name, app->key.pid);
+
+error:
+ return ret;
+}
+
+/*
+ * Open metadata onto the UST tracer for a UST session.
+ */
+static int open_ust_metadata(struct ust_app *app,
+ struct ust_app_session *ua_sess)
+{
+ int ret;
+ struct lttng_ust_channel_attr uattr;
+
+ uattr.overwrite = ua_sess->metadata->attr.overwrite;
+ uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size;
+ uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf;
+ uattr.switch_timer_interval =
+ ua_sess->metadata->attr.switch_timer_interval;
+ uattr.read_timer_interval =
+ ua_sess->metadata->attr.read_timer_interval;
+ uattr.output = ua_sess->metadata->attr.output;
+
+ /* UST tracer metadata creation */
+ ret = ustctl_open_metadata(app->key.sock, ua_sess->handle, &uattr,
+ &ua_sess->metadata->obj);
+ if (ret < 0) {
+ ERR("UST app open metadata failed for app pid:%d",
+ app->key.pid);
+ goto error;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Create stream onto the UST tracer for a UST session.
+ */
+static int create_ust_stream(struct ust_app *app,
+ struct ust_app_session *ua_sess)
+{
+ int ret;
+
+ ret = ustctl_create_stream(app->key.sock, ua_sess->metadata->obj,
+ &ua_sess->metadata->stream_obj);
+ if (ret < 0) {
+ ERR("UST create metadata stream failed");
+ goto error;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Create the specified channel onto the UST tracer for a UST session.
+ */
+static int create_ust_channel(struct ust_app *app,
+ struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
+{
+ int ret;
+
+ /* TODO: remove cast and use lttng-ust-abi.h */
+ ret = ustctl_create_channel(app->key.sock, ua_sess->handle,
+ (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
+ if (ret < 0) {
+ DBG("Error creating channel %s for app (pid: %d, sock: %d) "
+ "and session handle %d with ret %d",
+ ua_chan->name, app->key.pid, app->key.sock,
+ ua_sess->handle, ret);
+ goto error;
+ }
+
+ ua_chan->handle = ua_chan->obj->handle;
+ ua_chan->attr.shm_fd = ua_chan->obj->shm_fd;
+ ua_chan->attr.wait_fd = ua_chan->obj->wait_fd;
+ ua_chan->attr.memory_map_size = ua_chan->obj->memory_map_size;
+
+ DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
+ ua_chan->name, app->key.pid, app->key.sock);
+
+ /* If channel is not enabled, disable it on the tracer */
+ if (!ua_chan->enabled) {
+ ret = disable_ust_channel(app, ua_sess, ua_chan);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Create the specified event onto the UST tracer for a UST session.
+ */
+static
+int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
+ struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
+{
+ int ret = 0;
+
+ /* Create UST event on tracer */
+ ret = ustctl_create_event(app->key.sock, &ua_event->attr, ua_chan->obj,
+ &ua_event->obj);
+ if (ret < 0) {
+ ERR("Error ustctl create event %s for app pid: %d with ret %d",
+ ua_event->attr.name, app->key.pid, ret);
+ goto error;
+ }
+
+ ua_event->handle = ua_event->obj->handle;
+
+ DBG2("UST app event %s created successfully for pid:%d",
+ ua_event->attr.name, app->key.pid);
+
+ /* If event not enabled, disable it on the tracer */
+ if (!ua_event->enabled) {
+ ret = disable_ust_event(app, ua_sess, ua_event);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Copy data between an UST app event and a LTT event.
+ */
+static void shadow_copy_event(struct ust_app_event *ua_event,
+ struct ltt_ust_event *uevent)
+{
+ struct cds_lfht_iter iter;
+ struct ltt_ust_context *uctx;
+ struct ust_app_ctx *ua_ctx;
+
+ strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
+ ua_event->name[sizeof(ua_event->name) - 1] = '\0';
+
+ /* Copy event attributes */
+ memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
+
+ cds_lfht_for_each_entry(uevent->ctx, &iter, uctx, node) {
+ ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
+ if (ua_ctx == NULL) {
+ continue;
+ }
+ hashtable_node_init(&ua_ctx->node,
+ (void *)((unsigned long) ua_ctx->ctx.ctx), sizeof(void *));
+ hashtable_add_unique(ua_event->ctx, &ua_ctx->node);
+ }
+}
+
+/*
+ * Copy data between an UST app channel and a LTT channel.
+ */
+static void shadow_copy_channel(struct ust_app_channel *ua_chan,
+ struct ltt_ust_channel *uchan)
+{
+ struct cds_lfht_iter iter;
+ struct cds_lfht_node *ua_event_node;
+ struct ltt_ust_event *uevent;
+ struct ltt_ust_context *uctx;
+ struct ust_app_event *ua_event;
+ struct ust_app_ctx *ua_ctx;
+
+ DBG2("Shadow copy of UST app channel %s", ua_chan->name);
+
+ strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
+ ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
+ /* Copy event attributes */
+ memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr));
+
+ cds_lfht_for_each_entry(uchan->ctx, &iter, uctx, node) {
+ ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
+ if (ua_ctx == NULL) {
+ continue;
+ }
+ hashtable_node_init(&ua_ctx->node,
+ (void *)((unsigned long) ua_ctx->ctx.ctx), sizeof(void *));
+ hashtable_add_unique(ua_chan->ctx, &ua_ctx->node);
+ }
+
+ /* Copy all events from ltt ust channel to ust app channel */
+ cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) {
+ struct cds_lfht_iter uiter;
+
+ ua_event_node = hashtable_lookup(ua_chan->events,
+ (void *) uevent->attr.name, strlen(uevent->attr.name),
+ &uiter);
+ if (ua_event_node == NULL) {
+ DBG2("UST event %s not found on shadow copy channel",
+ uevent->attr.name);
+ ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
+ if (ua_event == NULL) {
+ continue;
+ }
+ shadow_copy_event(ua_event, uevent);
+ hashtable_add_unique(ua_chan->events, &ua_event->node);
+ }
+ }
+
+ DBG3("Shadow copy channel done");
+}
+
+/*
+ * Copy data between a UST app session and a regular LTT session.
+ */
+static void shadow_copy_session(struct ust_app_session *ua_sess,
+ struct ltt_ust_session *usess,
+ struct ust_app *app)
+{
+ struct cds_lfht_node *ua_chan_node;
+ struct cds_lfht_iter iter;
+ struct ltt_ust_channel *uchan;
+ struct ust_app_channel *ua_chan;