Backport: trackers: update lttng-sessiond
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
index 86c3c765c8dfb2173137894a4fceffb156515ce9..d5ed1dcfc39e4d02a6d0a8996f1d7945c91f2137 100644 (file)
@@ -41,6 +41,7 @@
 #include "syscall.h"
 #include "agent.h"
 #include "buffer-registry.h"
+#include "agent-thread.h"
 
 #include "cmd.h"
 
@@ -235,11 +236,11 @@ end:
 /*
  * Fill lttng_channel array of all channels.
  */
-static void list_lttng_channels(enum lttng_domain_type domain,
+static ssize_t list_lttng_channels(enum lttng_domain_type domain,
                struct ltt_session *session, struct lttng_channel *channels,
                struct lttcomm_channel_extended *chan_exts)
 {
-       int i = 0, ret;
+       int i = 0, ret = 0;
        struct ltt_kernel_channel *kchan;
 
        DBG("Listing channels for session %s", session->name);
@@ -325,7 +326,11 @@ static void list_lttng_channels(enum lttng_domain_type domain,
        }
 
 end:
-       return;
+       if (ret < 0) {
+               return -LTTNG_ERR_FATAL;
+       } else {
+               return LTTNG_OK;
+       }
 }
 
 static void increment_extended_len(const char *filter_expression,
@@ -852,8 +857,11 @@ static int create_connect_relayd(struct lttng_uri *uri,
 
                /* Check relayd version */
                ret = relayd_version_check(rsock);
-               if (ret < 0) {
-                       ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
+               if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) {
+                       goto close_sock;
+               } else if (ret < 0) {
+                       ERR("Unable to reach lttng-relayd");
+                       ret = LTTNG_ERR_RELAYD_CONNECT_FAIL;
                        goto close_sock;
                }
                consumer->relay_major_version = rsock->major;
@@ -882,6 +890,8 @@ error:
 
 /*
  * Connect to the relayd using URI and send the socket to the right consumer.
+ *
+ * The consumer socket lock must be held by the caller.
  */
 static int send_consumer_relayd_socket(enum lttng_domain_type domain,
                unsigned int session_id, struct lttng_uri *relayd_uri,
@@ -895,7 +905,7 @@ static int send_consumer_relayd_socket(enum lttng_domain_type domain,
        /* Connect to relayd and make version check if uri is the control. */
        ret = create_connect_relayd(relayd_uri, &rsock, consumer);
        if (ret != LTTNG_OK) {
-               goto error;
+               goto relayd_comm_error;
        }
        assert(rsock);
 
@@ -935,10 +945,6 @@ static int send_consumer_relayd_socket(enum lttng_domain_type domain,
         */
 
 close_sock:
-       (void) relayd_close(rsock);
-       free(rsock);
-
-error:
        if (ret != LTTNG_OK) {
                /*
                 * The consumer output for this session should not be used anymore
@@ -947,6 +953,10 @@ error:
                 */
                consumer->enabled = 0;
        }
+       (void) relayd_close(rsock);
+       free(rsock);
+
+relayd_comm_error:
        return ret;
 }
 
@@ -954,6 +964,8 @@ error:
  * Send both relayd sockets to a specific consumer and domain.  This is a
  * helper function to facilitate sending the information to the consumer for a
  * session.
+ *
+ * The consumer socket lock must be held by the caller.
  */
 static int send_consumer_relayd_sockets(enum lttng_domain_type domain,
                unsigned int session_id, struct consumer_output *consumer,
@@ -1183,12 +1195,14 @@ error:
 }
 
 /*
- * Command LTTNG_TRACK_PID processed by the client thread.
+ * Command LTTNG_TRACK_ID processed by the client thread.
  *
  * Called with session lock held.
  */
-int cmd_track_pid(struct ltt_session *session, enum lttng_domain_type domain,
-               int pid)
+int cmd_track_id(struct ltt_session *session,
+               enum lttng_tracker_type tracker_type,
+               enum lttng_domain_type domain,
+               struct lttng_tracker_id *id)
 {
        int ret;
 
@@ -1201,7 +1215,7 @@ int cmd_track_pid(struct ltt_session *session, enum lttng_domain_type domain,
 
                ksess = session->kernel_session;
 
-               ret = kernel_track_pid(ksess, pid);
+               ret = kernel_track_id(tracker_type, ksess, id);
                if (ret != LTTNG_OK) {
                        goto error;
                }
@@ -1215,7 +1229,7 @@ int cmd_track_pid(struct ltt_session *session, enum lttng_domain_type domain,
 
                usess = session->ust_session;
 
-               ret = trace_ust_track_pid(usess, pid);
+               ret = trace_ust_track_id(tracker_type, usess, id);
                if (ret != LTTNG_OK) {
                        goto error;
                }
@@ -1234,12 +1248,14 @@ error:
 }
 
 /*
- * Command LTTNG_UNTRACK_PID processed by the client thread.
+ * Command LTTNG_UNTRACK_ID processed by the client thread.
  *
  * Called with session lock held.
  */
-int cmd_untrack_pid(struct ltt_session *session, enum lttng_domain_type domain,
-               int pid)
+int cmd_untrack_id(struct ltt_session *session,
+               enum lttng_tracker_type tracker_type,
+               enum lttng_domain_type domain,
+               struct lttng_tracker_id *id)
 {
        int ret;
 
@@ -1252,7 +1268,7 @@ int cmd_untrack_pid(struct ltt_session *session, enum lttng_domain_type domain,
 
                ksess = session->kernel_session;
 
-               ret = kernel_untrack_pid(ksess, pid);
+               ret = kernel_untrack_id(tracker_type, ksess, id);
                if (ret != LTTNG_OK) {
                        goto error;
                }
@@ -1266,7 +1282,7 @@ int cmd_untrack_pid(struct ltt_session *session, enum lttng_domain_type domain,
 
                usess = session->ust_session;
 
-               ret = trace_ust_untrack_pid(usess, pid);
+               ret = trace_ust_untrack_id(tracker_type, usess, id);
                if (ret != LTTNG_OK) {
                        goto error;
                }
@@ -1333,6 +1349,21 @@ int cmd_enable_channel(struct ltt_session *session,
                attr->attr.switch_timer_interval = 0;
        }
 
+       /* Check for feature support */
+       switch (domain->type) {
+       case LTTNG_DOMAIN_JUL:
+       case LTTNG_DOMAIN_LOG4J:
+       case LTTNG_DOMAIN_PYTHON:
+               if (!agent_tracing_is_enabled()) {
+                       DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
+                       ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
+                       goto error;
+               }
+               break;
+       default:
+               break;
+       }
+
        switch (domain->type) {
        case LTTNG_DOMAIN_KERNEL:
        {
@@ -1605,6 +1636,16 @@ int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
        int ret, chan_kern_created = 0, chan_ust_created = 0;
        char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
 
+       /*
+        * Don't try to add a context if the session has been started at
+        * some point in time before. The tracer does not allow it and would
+        * result in a corrupted trace.
+        */
+       if (session->has_been_started) {
+               ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
+               goto end;
+       }
+
        if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
                app_ctx_provider_name = ctx->u.app_ctx.provider_name;
                app_ctx_name = ctx->u.app_ctx.ctx_name;
@@ -2056,6 +2097,12 @@ static int _cmd_enable_event(struct ltt_session *session,
 
                assert(usess);
 
+               if (!agent_tracing_is_enabled()) {
+                       DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
+                       ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
+                       goto error;
+               }
+
                agt = trace_ust_find_agent(usess, domain->type);
                if (!agt) {
                        agt = agent_create(domain->type);
@@ -2289,12 +2336,14 @@ ssize_t cmd_list_syscalls(struct lttng_event **events)
 }
 
 /*
- * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
+ * Command LTTNG_LIST_TRACKER_IDS processed by the client thread.
  *
  * Called with session lock held.
  */
-ssize_t cmd_list_tracker_pids(struct ltt_session *session,
-               enum lttng_domain_type domain, int32_t **pids)
+ssize_t cmd_list_tracker_ids(enum lttng_tracker_type tracker_type,
+               struct ltt_session *session,
+               enum lttng_domain_type domain,
+               struct lttng_tracker_id **ids)
 {
        int ret;
        ssize_t nr_pids = 0;
@@ -2305,7 +2354,7 @@ ssize_t cmd_list_tracker_pids(struct ltt_session *session,
                struct ltt_kernel_session *ksess;
 
                ksess = session->kernel_session;
-               nr_pids = kernel_list_tracker_pids(ksess, pids);
+               nr_pids = kernel_list_tracker_ids(tracker_type, ksess, ids);
                if (nr_pids < 0) {
                        ret = LTTNG_ERR_KERN_LIST_FAIL;
                        goto error;
@@ -2317,7 +2366,7 @@ ssize_t cmd_list_tracker_pids(struct ltt_session *session,
                struct ltt_ust_session *usess;
 
                usess = session->ust_session;
-               nr_pids = trace_ust_list_tracker_pids(usess, pids);
+               nr_pids = trace_ust_list_tracker_ids(tracker_type, usess, ids);
                if (nr_pids < 0) {
                        ret = LTTNG_ERR_UST_LIST_FAIL;
                        goto error;
@@ -2748,64 +2797,6 @@ int cmd_destroy_session(struct ltt_session *session, int wpipe)
        return ret;
 }
 
-/*
- * Command LTTNG_CALIBRATE processed by the client thread.
- */
-int cmd_calibrate(enum lttng_domain_type domain,
-               struct lttng_calibrate *calibrate)
-{
-       int ret;
-
-       switch (domain) {
-       case LTTNG_DOMAIN_KERNEL:
-       {
-               struct lttng_kernel_calibrate kcalibrate;
-
-               switch (calibrate->type) {
-               case LTTNG_CALIBRATE_FUNCTION:
-               default:
-                       /* Default and only possible calibrate option. */
-                       kcalibrate.type = LTTNG_KERNEL_CALIBRATE_KRETPROBE;
-                       break;
-               }
-
-               ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
-               if (ret < 0) {
-                       ret = LTTNG_ERR_KERN_ENABLE_FAIL;
-                       goto error;
-               }
-               break;
-       }
-       case LTTNG_DOMAIN_UST:
-       {
-               struct lttng_ust_calibrate ucalibrate;
-
-               switch (calibrate->type) {
-               case LTTNG_CALIBRATE_FUNCTION:
-               default:
-                       /* Default and only possible calibrate option. */
-                       ucalibrate.type = LTTNG_UST_CALIBRATE_TRACEPOINT;
-                       break;
-               }
-
-               ret = ust_app_calibrate_glb(&ucalibrate);
-               if (ret < 0) {
-                       ret = LTTNG_ERR_UST_CALIBRATE_FAIL;
-                       goto error;
-               }
-               break;
-       }
-       default:
-               ret = LTTNG_ERR_UND;
-               goto error;
-       }
-
-       ret = LTTNG_OK;
-
-error:
-       return ret;
-}
-
 /*
  * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
  */
@@ -3010,7 +3001,12 @@ ssize_t cmd_list_channels(enum lttng_domain_type domain,
 
                channel_exts = ((void *) *channels) +
                                (nb_chan * sizeof(struct lttng_channel));
-               list_lttng_channels(domain, session, *channels, channel_exts);
+               ret = list_lttng_channels(domain, session, *channels, channel_exts);
+               if (ret != LTTNG_OK) {
+                       free(*channels);
+                       *channels = NULL;
+                       goto end;
+               }
        } else {
                *channels = NULL;
        }
@@ -3207,11 +3203,10 @@ int cmd_snapshot_add_output(struct ltt_session *session,
        DBG("Cmd snapshot add output for session %s", session->name);
 
        /*
-        * Permission denied to create an output if the session is not
-        * set in no output mode.
+        * Can't create an output if the session is not set in no-output mode.
         */
        if (session->output_traces) {
-               ret = LTTNG_ERR_EPERM;
+               ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
                goto error;
        }
 
@@ -3275,7 +3270,7 @@ int cmd_snapshot_del_output(struct ltt_session *session,
         * set in no output mode.
         */
        if (session->output_traces) {
-               ret = LTTNG_ERR_EPERM;
+               ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
                goto error;
        }
 
@@ -3327,19 +3322,19 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
         * set in no output mode.
         */
        if (session->output_traces) {
-               ret = -LTTNG_ERR_EPERM;
-               goto error;
+               ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
+               goto end;
        }
 
        if (session->snapshot.nb_output == 0) {
                ret = 0;
-               goto error;
+               goto end;
        }
 
        list = zmalloc(session->snapshot.nb_output * sizeof(*list));
        if (!list) {
                ret = -LTTNG_ERR_NOMEM;
-               goto error;
+               goto end;
        }
 
        /* Copy list from session to the new list object. */
@@ -3352,14 +3347,14 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
                if (lttng_strncpy(list[idx].name, output->name,
                                sizeof(list[idx].name))) {
                        ret = -LTTNG_ERR_INVALID;
-                       goto error_unlock;
+                       goto error;
                }
                if (output->consumer->type == CONSUMER_DST_LOCAL) {
                        if (lttng_strncpy(list[idx].ctrl_url,
                                        output->consumer->dst.trace_path,
                                        sizeof(list[idx].ctrl_url))) {
                                ret = -LTTNG_ERR_INVALID;
-                               goto error_unlock;
+                               goto error;
                        }
                } else {
                        /* Control URI. */
@@ -3367,7 +3362,7 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
                                        list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
                        if (ret < 0) {
                                ret = -LTTNG_ERR_NOMEM;
-                               goto error_unlock;
+                               goto error;
                        }
 
                        /* Data URI. */
@@ -3375,7 +3370,7 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
                                        list[idx].data_url, sizeof(list[idx].data_url));
                        if (ret < 0) {
                                ret = -LTTNG_ERR_NOMEM;
-                               goto error_unlock;
+                               goto error;
                        }
                }
                idx++;
@@ -3384,10 +3379,10 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
        *outputs = list;
        list = NULL;
        ret = session->snapshot.nb_output;
-error_unlock:
-       rcu_read_unlock();
 error:
+       rcu_read_unlock();
        free(list);
+end:
        return ret;
 }
 
@@ -3436,6 +3431,29 @@ end:
        return ret;
 }
 
+static
+int clear_metadata_file(int fd)
+{
+       int ret;
+       off_t lseek_ret;
+
+       lseek_ret = lseek(fd, 0, SEEK_SET);
+       if (lseek_ret < 0) {
+               PERROR("lseek");
+               ret = -1;
+               goto end;
+       }
+
+       ret = ftruncate(fd, 0);
+       if (ret < 0) {
+               PERROR("ftruncate");
+               goto end;
+       }
+
+end:
+       return ret;
+}
+
 static
 int ust_regenerate_metadata(struct ltt_ust_session *usess)
 {
@@ -3457,6 +3475,15 @@ int ust_regenerate_metadata(struct ltt_ust_session *usess)
                memset(registry->metadata, 0, registry->metadata_alloc_len);
                registry->metadata_len = 0;
                registry->metadata_version++;
+               if (registry->metadata_fd > 0) {
+                       /* Clear the metadata file's content. */
+                       ret = clear_metadata_file(registry->metadata_fd);
+                       if (ret) {
+                               pthread_mutex_unlock(&registry->lock);
+                               goto end;
+                       }
+               }
+
                ret = ust_metadata_session_statedump(registry, NULL,
                                registry->major, registry->minor);
                if (ret) {
@@ -3626,10 +3653,12 @@ static int set_relayd_for_snapshot(struct consumer_output *consumer,
        rcu_read_lock();
        cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter,
                        socket, node.node) {
+               pthread_mutex_lock(socket->lock);
                ret = send_consumer_relayd_sockets(0, session->id,
                                snap_output->consumer, socket,
                                session->name, session->hostname,
                                session->live_timer);
+               pthread_mutex_unlock(socket->lock);
                if (ret != LTTNG_OK) {
                        rcu_read_unlock();
                        goto error;
@@ -3861,7 +3890,7 @@ int cmd_snapshot_record(struct ltt_session *session,
         * set in no output mode.
         */
        if (session->output_traces) {
-               ret = LTTNG_ERR_EPERM;
+               ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
                goto error;
        }
 
This page took 0.03197 seconds and 5 git commands to generate.