sessiond: clean-up: enhance logging on event allocation failure
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
index d2edfd5ce4248062457432ff123498b582a9dfba..47cda24517da22ad53a899892562f660e71e8e32 100644 (file)
@@ -1100,7 +1100,7 @@ struct ust_app_event *alloc_ust_app_event(char *name,
        /* Init most of the default value by allocating and zeroing */
        ua_event = zmalloc(sizeof(struct ust_app_event));
        if (ua_event == NULL) {
-               PERROR("malloc");
+               PERROR("Failed to allocate ust_app_event structure");
                goto error;
        }
 
@@ -1832,15 +1832,12 @@ static void shadow_copy_channel(struct ust_app_channel *ua_chan,
 static void shadow_copy_session(struct ust_app_session *ua_sess,
                struct ltt_ust_session *usess, struct ust_app *app)
 {
-       time_t rawtime;
        struct tm *timeinfo;
        char datetime[16];
        int ret;
        char tmp_shm_path[PATH_MAX];
 
-       /* Get date and time for unique app path */
-       time(&rawtime);
-       timeinfo = localtime(&rawtime);
+       timeinfo = localtime(&app->registration_time);
        strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
 
        DBG2("Shadow copy of session handle %d", ua_sess->handle);
@@ -1996,7 +1993,8 @@ static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
                        app->byte_order, app->version.major, app->version.minor,
                        reg_pid->root_shm_path, reg_pid->shm_path,
                        ua_sess->effective_credentials.uid,
-                       ua_sess->effective_credentials.gid);
+                       ua_sess->effective_credentials.gid, ua_sess->tracing_id,
+                       app->uid);
        if (ret < 0) {
                /*
                 * reg_pid->registry->reg.ust is NULL upon error, so we need to
@@ -2063,7 +2061,8 @@ static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
                        app->uint64_t_alignment, app->long_alignment,
                        app->byte_order, app->version.major,
                        app->version.minor, reg_uid->root_shm_path,
-                       reg_uid->shm_path, usess->uid, usess->gid);
+                       reg_uid->shm_path, usess->uid, usess->gid,
+                       ua_sess->tracing_id, app->uid);
        if (ret < 0) {
                /*
                 * reg_uid->registry->reg.ust is NULL upon error, so we need to
@@ -3134,7 +3133,7 @@ int create_ust_app_event(struct ust_app_session *ua_sess,
 
        ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
        if (ua_event == NULL) {
-               /* Only malloc can failed so something is really wrong */
+               /* Only failure mode of alloc_ust_app_event(). */
                ret = -ENOMEM;
                goto end;
        }
@@ -3143,8 +3142,19 @@ int create_ust_app_event(struct ust_app_session *ua_sess,
        /* Create it on the tracer side */
        ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
        if (ret < 0) {
-               /* Not found previously means that it does not exist on the tracer */
-               assert(ret != -LTTNG_UST_ERR_EXIST);
+               /*
+                * Not found previously means that it does not exist on the
+                * tracer. If the application reports that the event existed,
+                * it means there is a bug in the sessiond or lttng-ust
+                * (or corruption, etc.)
+                */
+               if (ret == -LTTNG_UST_ERR_EXIST) {
+                       ERR("Tracer for application reported that an event being created already existed: "
+                                       "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
+                                       uevent->attr.name,
+                                       app->pid, app->ppid, app->uid,
+                                       app->gid);
+               }
                goto error;
        }
 
@@ -3377,6 +3387,8 @@ void ust_app_add(struct ust_app *app)
        assert(app);
        assert(app->notify_sock >= 0);
 
+       app->registration_time = time(NULL);
+
        rcu_read_lock();
 
        /*
@@ -4412,7 +4424,7 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
        health_code_update();
 
 skip_setup:
-       /* This start the UST tracing */
+       /* This starts the UST tracing */
        pthread_mutex_lock(&app->sock_lock);
        ret = ustctl_start_session(app->sock, ua_sess->handle);
        pthread_mutex_unlock(&app->sock_lock);
@@ -5877,14 +5889,14 @@ void ust_app_destroy(struct ust_app *app)
  */
 enum lttng_error_code ust_app_snapshot_record(
                const struct ltt_ust_session *usess,
-               const struct snapshot_output *output, int wait,
+               const struct consumer_output *output, int wait,
                uint64_t nb_packets_per_stream)
 {
        int ret = 0;
        enum lttng_error_code status = LTTNG_OK;
        struct lttng_ht_iter iter;
        struct ust_app *app;
-       char pathname[PATH_MAX];
+       char *trace_path = NULL;
 
        assert(usess);
        assert(output);
@@ -5899,6 +5911,7 @@ enum lttng_error_code ust_app_snapshot_record(
                cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
                        struct buffer_reg_channel *reg_chan;
                        struct consumer_socket *socket;
+                       char pathname[PATH_MAX];
 
                        if (!reg->registry->reg.ust->metadata_key) {
                                /* Skip since no metadata is present */
@@ -5926,14 +5939,20 @@ enum lttng_error_code ust_app_snapshot_record(
                                status = LTTNG_ERR_INVALID;
                                goto error;
                        }
-
+                       /* Free path allowed on previous iteration. */
+                       free(trace_path);
+                       trace_path = setup_channel_trace_path(usess->consumer, pathname);
+                       if (!trace_path) {
+                               status = LTTNG_ERR_INVALID;
+                               goto error;
+                       }
                         /* Add the UST default trace dir to path. */
                        cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
                                        reg_chan, node.node) {
                                status = consumer_snapshot_channel(socket,
                                                reg_chan->consumer_key,
                                                output, 0, usess->uid,
-                                               usess->gid, pathname, wait,
+                                               usess->gid, trace_path, wait,
                                                nb_packets_per_stream);
                                if (status != LTTNG_OK) {
                                        goto error;
@@ -5941,7 +5960,7 @@ enum lttng_error_code ust_app_snapshot_record(
                        }
                        status = consumer_snapshot_channel(socket,
                                        reg->registry->reg.ust->metadata_key, output, 1,
-                                       usess->uid, usess->gid, pathname, wait, 0);
+                                       usess->uid, usess->gid, trace_path, wait, 0);
                        if (status != LTTNG_OK) {
                                goto error;
                        }
@@ -5956,6 +5975,7 @@ enum lttng_error_code ust_app_snapshot_record(
                        struct ust_app_channel *ua_chan;
                        struct ust_app_session *ua_sess;
                        struct ust_registry_session *registry;
+                       char pathname[PATH_MAX];
 
                        ua_sess = lookup_session_by_app(usess, app);
                        if (!ua_sess) {
@@ -5965,7 +5985,7 @@ enum lttng_error_code ust_app_snapshot_record(
 
                        /* Get the right consumer socket for the application. */
                        socket = consumer_find_socket_by_bitness(app->bits_per_long,
-                                       output->consumer);
+                                       output);
                        if (!socket) {
                                status = LTTNG_ERR_INVALID;
                                goto error;
@@ -5980,7 +6000,13 @@ enum lttng_error_code ust_app_snapshot_record(
                                PERROR("snprintf snapshot path");
                                goto error;
                        }
-
+                       /* Free path allowed on previous iteration. */
+                       free(trace_path);
+                       trace_path = setup_channel_trace_path(usess->consumer, pathname);
+                       if (!trace_path) {
+                               status = LTTNG_ERR_INVALID;
+                               goto error;
+                       }
                         cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
                                        ua_chan, node.node) {
                                status = consumer_snapshot_channel(socket,
@@ -5989,7 +6015,7 @@ enum lttng_error_code ust_app_snapshot_record(
                                                                .uid,
                                                ua_sess->effective_credentials
                                                                .gid,
-                                               pathname, wait,
+                                               trace_path, wait,
                                                nb_packets_per_stream);
                                switch (status) {
                                case LTTNG_OK:
@@ -6010,7 +6036,7 @@ enum lttng_error_code ust_app_snapshot_record(
                                        registry->metadata_key, output, 1,
                                        ua_sess->effective_credentials.uid,
                                        ua_sess->effective_credentials.gid,
-                                       pathname, wait, 0);
+                                       trace_path, wait, 0);
                        switch (status) {
                        case LTTNG_OK:
                                break;
@@ -6028,6 +6054,7 @@ enum lttng_error_code ust_app_snapshot_record(
        }
 
 error:
+       free(trace_path);
        rcu_read_unlock();
        return status;
 }
This page took 0.026992 seconds and 4 git commands to generate.