LTTng sessiond: fix ust app error handling
[lttng-tools.git] / lttng-sessiond / ust-app.c
index d93dc90f579437d992ddb783edcafef57cffeec3..e22a5929ecf77322a68a48a776b5380e999c4eb4 100644 (file)
@@ -264,6 +264,66 @@ unsigned long ust_app_list_count(void)
        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;
+                               }
+                       }
+
+                       tmp[count].type = LTTNG_UST_TRACEPOINT;
+                       tmp[count].pid = app->key.pid;
+                       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.
  */
@@ -748,7 +808,7 @@ int ust_app_add_channel_all(struct ltt_ust_session *usess,
 
                /* Create session on the tracer side and add it to app session HT */
                ua_sess = create_ust_app_session(usess, app);
-               if (ret < 0) {
+               if (ua_sess == NULL) {
                        goto next;
                }
 
@@ -791,7 +851,7 @@ int ust_app_add_event_all(struct ltt_ust_session *usess,
 
                /* Create session on the tracer side and add it to app session HT */
                ua_sess = create_ust_app_session(usess, app);
-               if (ret < 0) {
+               if (ua_sess == NULL) {
                        goto next;
                }
 
@@ -829,15 +889,17 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
 
        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) {
                /* Only malloc can failed so something is really wrong */
-               goto error;
+               goto error_rcu_unlock;
        }
 
        ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
        if (ret < 0) {
-               goto error;
+               goto error_rcu_unlock;
        }
 
        /* For each channel */
@@ -852,7 +914,7 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
                        ustream = zmalloc(sizeof(*ustream));
                        if (ustream == NULL) {
                                PERROR("zmalloc ust stream");
-                               continue;
+                               goto error_rcu_unlock;
                        }
 
                        ret = ustctl_create_stream(app->key.sock, ua_chan->obj,
@@ -883,22 +945,24 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
        /* Setup UST consumer socket and send fds to it */
        ret = ust_consumer_send_session(usess->consumer_fd, ua_sess);
        if (ret < 0) {
-               goto error;
+               goto error_rcu_unlock;
        }
 
        /* 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);
-               goto error;
+               goto error_rcu_unlock;
        }
+       rcu_read_unlock();
 
        /* Quiescent wait after starting trace */
        ustctl_wait_quiescent(app->key.sock);
 
        return 0;
 
-error:
+error_rcu_unlock:
+       rcu_read_unlock();
        return -1;
 }
 
This page took 0.024527 seconds and 4 git commands to generate.