+ DBG2("UST app ustctl create session handle %d", ret);
+ ua_sess->handle = ret;
+
+ /* Add ust app session to app's HT */
+ hashtable_node_init(&ua_sess->node,
+ (void *)((unsigned long) ua_sess->uid), sizeof(void *));
+ hashtable_add_unique(app->sessions, &ua_sess->node);
+
+ DBG2("UST app session created successfully with handle %d", ret);
+ }
+
+ return ua_sess;
+
+error:
+ return NULL;
+}
+
+/*
+ * Create a context for the channel on the tracer.
+ */
+static
+int create_ust_app_channel_context(struct ust_app_session *ua_sess,
+ struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
+ struct ust_app *app)
+{
+ int ret = 0;
+ struct cds_lfht_iter iter;
+ struct cds_lfht_node *node;
+ struct ust_app_ctx *ua_ctx;
+
+ DBG2("UST app adding context to channel %s", ua_chan->name);
+
+ node = hashtable_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx),
+ sizeof(void *), &iter);
+ if (node != NULL) {
+ ret = -EEXIST;
+ goto error;
+ }
+
+ ua_ctx = alloc_ust_app_ctx(uctx);
+ if (ua_ctx == NULL) {
+ /* malloc failed */
+ ret = -1;
+ goto error;
+ }
+
+ hashtable_node_init(&ua_ctx->node,
+ (void *)((unsigned long) ua_ctx->ctx.ctx), sizeof(void *));
+ hashtable_add_unique(ua_chan->ctx, &ua_ctx->node);
+
+ ret = create_ust_channel_context(ua_chan, ua_ctx, app);
+ if (ret < 0) {
+ goto error;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Create an UST context and enable it for the event on the tracer.
+ */
+static
+int create_ust_app_event_context(struct ust_app_session *ua_sess,
+ struct ust_app_event *ua_event, struct lttng_ust_context *uctx,
+ struct ust_app *app)
+{
+ int ret = 0;
+ struct cds_lfht_iter iter;
+ struct cds_lfht_node *node;
+ struct ust_app_ctx *ua_ctx;
+
+ DBG2("UST app adding context to event %s", ua_event->name);
+
+ node = hashtable_lookup(ua_event->ctx, (void *)((unsigned long)uctx->ctx),
+ sizeof(void *), &iter);
+ if (node != NULL) {
+ ret = -EEXIST;
+ goto error;
+ }
+
+ ua_ctx = alloc_ust_app_ctx(uctx);
+ if (ua_ctx == NULL) {
+ /* malloc failed */
+ ret = -1;
+ goto error;
+ }
+
+ hashtable_node_init(&ua_ctx->node,
+ (void *)((unsigned long) ua_ctx->ctx.ctx), sizeof(void *));
+ hashtable_add_unique(ua_event->ctx, &ua_ctx->node);
+
+ ret = create_ust_event_context(ua_event, ua_ctx, app);
+ if (ret < 0) {
+ goto error;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Enable on the tracer side a ust app event for the session and channel.
+ */
+static
+int enable_ust_app_event(struct ust_app_session *ua_sess,
+ struct ust_app_event *ua_event, struct ust_app *app)
+{
+ int ret;
+
+ ret = enable_ust_event(app, ua_sess, ua_event);
+ if (ret < 0) {
+ goto error;
+ }
+
+ ua_event->enabled = 1;
+
+error:
+ return ret;
+}
+
+/*
+ * Disable on the tracer side a ust app event for the session and channel.
+ */
+static int disable_ust_app_event(struct ust_app_session *ua_sess,
+ struct ust_app_channel *ua_chan, struct ust_app_event *ua_event,
+ struct ust_app *app)
+{
+ int ret;
+
+ ret = disable_ust_event(app, ua_sess, ua_event);
+ if (ret < 0) {
+ goto error;
+ }
+
+ ua_event->enabled = 0;
+
+error:
+ return ret;
+}
+
+/*
+ * Lookup ust app channel for session and disable it on the tracer side.
+ */
+static
+int disable_ust_app_channel(struct ust_app_session *ua_sess,
+ struct ust_app_channel *ua_chan, struct ust_app *app)
+{
+ int ret;
+
+ ret = disable_ust_channel(app, ua_sess, ua_chan);
+ if (ret < 0) {
+ goto error;
+ }
+
+ ua_chan->enabled = 0;
+
+error:
+ return ret;
+}
+
+/*
+ * Lookup ust app channel for session and enable it on the tracer side.
+ */
+static int enable_ust_app_channel(struct ust_app_session *ua_sess,
+ struct ltt_ust_channel *uchan, struct ust_app *app)
+{
+ int ret = 0;
+ struct cds_lfht_iter iter;
+ struct cds_lfht_node *ua_chan_node;
+ struct ust_app_channel *ua_chan;
+
+ ua_chan_node = hashtable_lookup(ua_sess->channels,
+ (void *)uchan->name, strlen(uchan->name), &iter);
+ if (ua_chan_node == NULL) {
+ DBG2("Unable to find channel %s in ust session uid %u",
+ uchan->name, ua_sess->uid);
+ goto error;
+ }
+
+ ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
+
+ ret = enable_ust_channel(app, ua_sess, ua_chan);
+ if (ret < 0) {
+ goto error;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Create UST app channel and create it on the tracer.
+ */
+static struct ust_app_channel *create_ust_app_channel(
+ struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
+ struct ust_app *app)
+{
+ int ret = 0;
+ struct cds_lfht_iter iter;
+ struct cds_lfht_node *ua_chan_node;
+ struct ust_app_channel *ua_chan;
+
+ /* Lookup channel in the ust app session */
+ ua_chan_node = hashtable_lookup(ua_sess->channels,
+ (void *)uchan->name, strlen(uchan->name), &iter);
+ if (ua_chan_node == NULL) {
+ DBG2("Unable to find channel %s in ust session uid %u",
+ uchan->name, ua_sess->uid);
+ ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
+ if (ua_chan == NULL) {
+ goto error;
+ }
+ shadow_copy_channel(ua_chan, uchan);
+
+ hashtable_add_unique(ua_sess->channels, &ua_chan->node);
+ } else {
+ ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
+ }
+
+ ret = create_ust_channel(app, ua_sess, ua_chan);
+ if (ret < 0) {
+ goto error;
+ }
+
+ return ua_chan;
+
+error:
+ return NULL;
+}
+
+/*
+ * Create UST app event and create it on the tracer side.
+ */
+static
+int create_ust_app_event(struct ust_app_session *ua_sess,
+ struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
+ struct ust_app *app)
+{
+ int ret = 0;
+ struct cds_lfht_iter iter;
+ struct cds_lfht_node *ua_event_node;
+ struct ust_app_event *ua_event;
+
+ /* Get event node */
+ ua_event_node = hashtable_lookup(ua_chan->events,
+ (void *)uevent->attr.name, strlen(uevent->attr.name), &iter);
+ if (ua_event_node != NULL) {
+ ERR("UST app event %s already exist. Stopping creation.",
+ uevent->attr.name);
+ goto end;
+ }
+
+ /* Does not exist so create one */
+ ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
+ if (ua_event == NULL) {
+ /* Only malloc can failed so something is really wrong */
+ ret = -ENOMEM;
+ goto error;
+ }
+ shadow_copy_event(ua_event, uevent);
+
+ /* Create it on the tracer side */
+ ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
+ if (ret < 0) {
+ rcu_read_lock();
+ delete_ust_app_event(app->key.sock, ua_event);
+ rcu_read_unlock();
+ goto error;
+ }
+
+ ua_event->enabled = 1;
+
+ hashtable_add_unique(ua_chan->events, &ua_event->node);
+
+end:
+error:
+ return ret;
+}
+
+/*
+ * Create UST metadata and open it on the tracer side.
+ */
+static int create_ust_app_metadata(struct ust_app_session *ua_sess,
+ char *pathname, struct ust_app *app)
+{
+ int ret = 0;
+
+ if (ua_sess->metadata == NULL) {
+ /* Allocate UST metadata */
+ ua_sess->metadata = trace_ust_create_metadata(pathname);
+ if (ua_sess->metadata == NULL) {
+ ERR("UST app session %d creating metadata failed",
+ ua_sess->handle);
+ goto error;
+ }
+
+ ret = open_ust_metadata(app, ua_sess);
+ if (ret < 0) {
+ goto error;
+ }
+
+ DBG2("UST metadata opened for app pid %d", app->key.pid);
+ }
+
+ /* Open UST metadata stream */
+ if (ua_sess->metadata->stream_obj == NULL) {
+ ret = create_ust_stream(app, ua_sess);
+ if (ret < 0) {
+ goto error;
+ }
+
+ ret = mkdir(ua_sess->path, S_IRWXU | S_IRWXG);
+ if (ret < 0) {
+ PERROR("mkdir UST metadata");
+ goto error;
+ }
+
+ ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
+ "%s/metadata", ua_sess->path);
+ if (ret < 0) {
+ PERROR("asprintf UST create stream");
+ goto error;
+ }
+
+ DBG2("UST metadata stream object created for app pid %d",
+ app->key.pid);
+ } else {
+ ERR("Attempting to create stream without metadata opened");
+ goto error;
+ }
+
+ return 0;
+
+error:
+ return -1;
+}
+
+/*
+ * Return pointer to traceable apps list.
+ */
+struct cds_lfht *ust_app_get_ht(void)
+{
+ return ust_app_ht;
+}
+
+/*
+ * Return ust app pointer or NULL if not found.
+ */
+struct ust_app *ust_app_find_by_pid(pid_t pid)
+{
+ struct cds_lfht_node *node;
+ struct cds_lfht_iter iter;
+
+ rcu_read_lock();
+ node = hashtable_lookup(ust_app_ht,
+ (void *)((unsigned long) pid), sizeof(void *), &iter);
+ if (node == NULL) {
+ DBG2("UST app no found with pid %d", pid);
+ goto error;
+ }
+ rcu_read_unlock();
+
+ DBG2("Found UST app by pid %d", pid);
+
+ return caa_container_of(node, struct ust_app, node);
+
+error:
+ rcu_read_unlock();
+ return NULL;
+}
+
+/*
+ * Using pid and uid (of the app), allocate a new ust_app struct and
+ * add it to the global traceable app list.
+ *
+ * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
+ * bitness is not supported.
+ */
+int ust_app_register(struct ust_register_msg *msg, int sock)
+{
+ struct ust_app *lta;
+
+ if ((msg->bits_per_long == 64 && ust_consumerd64_fd == -EINVAL)
+ || (msg->bits_per_long == 32 && ust_consumerd32_fd == -EINVAL)) {
+ ERR("Registration failed: application \"%s\" (pid: %d) has "
+ "%d-bit long, but no consumerd for this long size is available.\n",
+ msg->name, msg->pid, msg->bits_per_long);
+ close(sock);
+ return -EINVAL;
+ }
+ lta = zmalloc(sizeof(struct ust_app));
+ if (lta == NULL) {
+ PERROR("malloc");
+ return -ENOMEM;
+ }
+
+ lta->ppid = msg->ppid;
+ lta->uid = msg->uid;
+ lta->gid = msg->gid;
+ lta->bits_per_long = msg->bits_per_long;
+ lta->v_major = msg->major;
+ lta->v_minor = msg->minor;
+ strncpy(lta->name, msg->name, sizeof(lta->name));
+ lta->name[16] = '\0';
+ lta->sessions = hashtable_new(0);
+
+ /* Set key map */
+ lta->key.pid = msg->pid;
+ hashtable_node_init(<a->node, (void *)((unsigned long)lta->key.pid),
+ sizeof(void *));
+ lta->key.sock = sock;
+ hashtable_node_init(<a->key.node, (void *)((unsigned long)lta->key.sock),
+ sizeof(void *));
+
+ rcu_read_lock();
+ hashtable_add_unique(ust_app_sock_key_map, <a->key.node);
+ hashtable_add_unique(ust_app_ht, <a->node);
+ rcu_read_unlock();
+
+ DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
+ " (version %d.%d)", lta->key.pid, lta->ppid, lta->uid, lta->gid,
+ lta->key.sock, lta->name, lta->v_major, lta->v_minor);
+
+ return 0;
+}
+
+/*
+ * Unregister app by removing it from the global traceable app list and freeing
+ * the data struct.
+ *
+ * The socket is already closed at this point so no close to sock.
+ */
+void ust_app_unregister(int sock)
+{
+ struct ust_app *lta;
+ struct cds_lfht_node *node;
+ struct cds_lfht_iter iter;
+ int ret;
+
+ rcu_read_lock();
+ lta = find_app_by_sock(sock);
+ if (lta == NULL) {
+ ERR("Unregister app sock %d not found!", sock);
+ goto error;
+ }
+
+ DBG("PID %d unregistering with sock %d", lta->key.pid, sock);
+
+ /* Get the node reference for a call_rcu */
+ node = hashtable_lookup(ust_app_ht,
+ (void *)((unsigned long) lta->key.pid), sizeof(void *), &iter);
+ if (node == NULL) {
+ ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid);
+ goto error;
+ }
+
+ ret = hashtable_del(ust_app_ht, &iter);
+ assert(!ret);
+ call_rcu(&node->head, delete_ust_app_rcu);
+error:
+ rcu_read_unlock();
+ return;
+}
+
+/*
+ * Return traceable_app_count
+ */
+unsigned long ust_app_list_count(void)
+{
+ unsigned long count;
+
+ rcu_read_lock();
+ count = hashtable_get_count(ust_app_ht);
+ rcu_read_unlock();
+
+ return count;
+}
+
+/*
+ * Fill events array with all events name of all registered apps.
+ */
+int ust_app_list_events(struct lttng_event **events)
+{
+ int ret, handle;
+ size_t nbmem, count = 0;
+ struct cds_lfht_iter iter;
+ struct ust_app *app;
+ struct lttng_event *tmp;
+
+ nbmem = UST_APP_EVENT_LIST_SIZE;
+ tmp = zmalloc(nbmem * sizeof(struct lttng_event));
+ if (tmp == NULL) {
+ PERROR("zmalloc ust app events");
+ ret = -ENOMEM;
+ goto error;
+ }
+
+ rcu_read_lock();
+
+ cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
+ struct lttng_ust_tracepoint_iter iter;
+
+ handle = ustctl_tracepoint_list(app->key.sock);
+ if (handle < 0) {
+ ERR("UST app list events getting handle failed for app pid %d",
+ app->key.pid);
+ continue;
+ }
+
+ while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle,
+ &iter)) != -ENOENT) {
+ memcpy(tmp[count].name, iter.name, LTTNG_UST_SYM_NAME_LEN);
+ /* TODO : get loglevel too */
+ if (count > nbmem) {
+ DBG2("Reallocating event list from %zu to %zu bytes", nbmem,
+ nbmem + UST_APP_EVENT_LIST_SIZE);
+ nbmem += UST_APP_EVENT_LIST_SIZE;
+ tmp = realloc(tmp, nbmem * sizeof(struct lttng_event));
+ if (tmp == NULL) {
+ PERROR("realloc ust app events");
+ ret = -ENOMEM;
+ goto rcu_error;
+ }
+ }
+
+ tmp[count].type = LTTNG_UST_TRACEPOINT;
+ tmp[count].pid = app->key.pid;
+ tmp[count].enabled = -1;
+ count++;
+ }
+ }
+
+ ret = count;
+ *events = tmp;
+
+ DBG2("UST app list events done (%zu events)", count);
+
+rcu_error:
+ rcu_read_unlock();
+error:
+ return ret;
+}
+
+/*
+ * Free and clean all traceable apps of the global list.
+ */
+void ust_app_clean_list(void)
+{
+ int ret;
+ struct cds_lfht_iter iter;
+ struct ust_app *app;
+
+ DBG2("UST app cleaning registered apps hash table");
+
+ rcu_read_lock();
+
+ cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
+ ret = hashtable_del(ust_app_ht, &iter);
+ assert(!ret);
+ call_rcu(&iter.node->head, delete_ust_app_rcu);
+ }
+
+ hashtable_destroy(ust_app_ht);
+ hashtable_destroy(ust_app_sock_key_map);