- /* Create UST event on tracer */
- ret = ustctl_create_event(app->key.sock, <t_uevent, ua_chan->obj,
- &obj_event);
- if (ret < 0) {
- ERR("Error ustctl create event %s for app pid: %d with ret %d",
- uevent->attr.name, app->key.pid, ret);
- /* TODO: free() ua_event and obj_event */
- goto next;
- }
- ua_event->obj = obj_event;
- ua_event->handle = obj_event->handle;
- ua_event->enabled = 1;
- } else {
- ua_event = caa_container_of(ua_event_node,
- struct ust_app_event, node);
-
- if (ua_event->enabled == 0) {
- ret = ustctl_enable(app->key.sock, ua_event->obj);
- if (ret < 0) {
- ERR("Error ustctl enable event %s for app "
- "pid: %d with ret %d", uevent->attr.name,
- app->key.pid, ret);
- goto next;
+/*
+ * 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.
+ */
+int ust_app_register(struct ust_register_msg *msg, int sock)
+{
+ struct ust_app *lta;
+
+ 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->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;
+
+ 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;
+ }
+
+ /* We got called because the socket was closed on the remote end. */
+ close(sock);
+ /* Using a flag because we still need "sock" as a key. */
+ lta->sock_closed = 1;
+ hashtable_del(ust_app_ht, &iter);
+ 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) {
+ 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,
+ tmp[count].name)) != -ENOENT) {
+ 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);
+ if (tmp == NULL) {
+ PERROR("realloc ust app events");
+ ret = -ENOMEM;
+ goto rcu_error;