Basic hashtable support for UST
[lttng-tools.git] / ltt-sessiond / main.c
index 4da4cfbc1fa324d68fcb97dfac09ecfde249c9f9..081b0c855efa2172f0a7a0dfa5c9eb3bd1dd10f1 100644 (file)
 #include <lttng-consumerd.h>
 #include <lttng-sessiond-comm.h>
 #include <lttng/lttng-consumer.h>
+
+#ifdef CONFIG_LTTNG_TOOLS_HAVE_UST
+#include <ust/lttng-ust-ctl.h>
+#else
+#include "lttng-ust-ctl.h"
+#endif
+
+
 #include <lttngerr.h>
 
 #include "channel.h"
@@ -48,6 +56,7 @@
 #include "context.h"
 #include "event.h"
 #include "futex.h"
+#include "hashtable.h"
 #include "kernel-ctl.h"
 #include "ltt-sessiond.h"
 #include "shm.h"
@@ -296,6 +305,17 @@ static void teardown_kernel_session(struct ltt_session *session)
        }
 }
 
+/*
+ * Complete teardown of all UST sessions. This will free everything on his path
+ * and destroy the core essence of all ust sessions :)
+ */
+static void teardown_ust_session(struct ltt_session *session)
+{
+       DBG("Tearing down UST session(s)");
+
+       trace_ust_destroy_session(session->ust_session);
+}
+
 /*
  * Stop all threads by closing the thread quit pipe.
  */
@@ -356,7 +376,8 @@ static void cleanup(void)
                cds_list_for_each_entry_safe(sess, stmp,
                                &session_list_ptr->head, list) {
                        teardown_kernel_session(sess);
-                       // TODO complete session cleanup (including UST)
+                       teardown_ust_session(sess);
+                       free(sess);
                }
        }
 
@@ -413,24 +434,22 @@ static void clean_command_ctx(struct command_ctx **cmd_ctx)
 /*
  * Send all stream fds of kernel channel to the consumer.
  */
-static int send_consumer_channel_streams(struct consumer_data *consumer_data,
+static int send_kconsumer_channel_streams(struct consumer_data *consumer_data,
                int sock, struct ltt_kernel_channel *channel)
 {
        int ret;
-       size_t nb_fd;
        struct ltt_kernel_stream *stream;
        struct lttcomm_consumer_msg lkm;
 
        DBG("Sending streams of channel %s to kernel consumer",
                        channel->channel->name);
-       nb_fd = channel->stream_count;
 
        /* Send channel */
        lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
        lkm.u.channel.channel_key = channel->fd;
        lkm.u.channel.max_sb_size = channel->channel->attr.subbuf_size;
        lkm.u.channel.mmap_len = 0;     /* for kernel */
-       DBG("Sending channel %d to consumer", lkm.u.stream.stream_key);
+       DBG("Sending channel %d to consumer", lkm.u.channel.channel_key);
        ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
        if (ret < 0) {
                perror("send consumer channel");
@@ -471,10 +490,84 @@ error:
        return ret;
 }
 
+/*
+ * Send all stream fds of UST channel to the consumer.
+ */
+static int send_ustconsumer_channel_streams(struct consumer_data *consumer_data,
+               int sock, struct ltt_ust_channel *channel)
+{
+       int ret, fds[2];
+       struct ltt_ust_stream *stream;
+       struct lttcomm_consumer_msg lum;
+
+       DBG("Sending streams of channel %s to UST consumer",
+                       channel->name);
+
+       /* Send channel */
+       lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
+       /*
+        * We need to keep shm_fd open to make sure this key stays
+        * unique within the session daemon.
+        */
+       lum.u.channel.channel_key = channel->obj->shm_fd;
+       lum.u.channel.max_sb_size = channel->attr.subbuf_size;
+       lum.u.channel.mmap_len = channel->obj->memory_map_size;
+       DBG("Sending channel %d to consumer", lum.u.channel.channel_key);
+       ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
+       if (ret < 0) {
+               perror("send consumer channel");
+               goto error;
+       }
+       fds[0] = channel->obj->shm_fd;
+       fds[1] = channel->obj->wait_fd;
+       ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
+       if (ret < 0) {
+               perror("send consumer channel ancillary data");
+               goto error;
+       }
+
+       /* Send streams */
+       cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
+               int fds[2];
+
+               if (!stream->obj->shm_fd) {
+                       continue;
+               }
+               lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
+               lum.u.stream.channel_key = channel->obj->shm_fd;
+               lum.u.stream.stream_key = stream->obj->shm_fd;
+               lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
+               lum.u.stream.output = channel->attr.output;
+               lum.u.stream.mmap_len = stream->obj->memory_map_size;
+               strncpy(lum.u.stream.path_name, stream->pathname, PATH_MAX - 1);
+               lum.u.stream.path_name[PATH_MAX - 1] = '\0';
+               DBG("Sending stream %d to consumer", lum.u.stream.stream_key);
+               ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
+               if (ret < 0) {
+                       perror("send consumer stream");
+                       goto error;
+               }
+               fds[0] = stream->obj->shm_fd;
+               fds[1] = stream->obj->wait_fd;
+               ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
+               if (ret < 0) {
+                       perror("send consumer stream ancillary data");
+                       goto error;
+               }
+       }
+
+       DBG("consumer channel streams sent");
+
+       return 0;
+
+error:
+       return ret;
+}
+
 /*
  * Send all stream fds of the kernel session to the consumer.
  */
-static int send_consumer_session_streams(struct consumer_data *consumer_data,
+static int send_kconsumer_session_streams(struct consumer_data *consumer_data,
                struct ltt_kernel_session *session)
 {
        int ret;
@@ -484,7 +577,7 @@ static int send_consumer_session_streams(struct consumer_data *consumer_data,
 
        DBG("Sending metadata stream fd");
 
-       /* Extra protection. It's NOT suppose to be set to 0 at this point */
+       /* Extra protection. It's NOT supposed to be set to 0 at this point */
        if (session->consumer_fd == 0) {
                session->consumer_fd = consumer_data->cmd_sock;
        }
@@ -525,7 +618,86 @@ static int send_consumer_session_streams(struct consumer_data *consumer_data,
        }
 
        cds_list_for_each_entry(chan, &session->channel_list.head, list) {
-               ret = send_consumer_channel_streams(consumer_data, sock, chan);
+               ret = send_kconsumer_channel_streams(consumer_data, sock, chan);
+               if (ret < 0) {
+                       goto error;
+               }
+       }
+
+       DBG("consumer fds (metadata and channel streams) sent");
+
+       return 0;
+
+error:
+       return ret;
+}
+
+/*
+ * Send all stream fds of the UST session to the consumer.
+ */
+static int send_ustconsumer_session_streams(struct consumer_data *consumer_data,
+               struct ltt_ust_session *session)
+{
+       int ret;
+       struct ltt_ust_channel *chan;
+       struct lttcomm_consumer_msg lum;
+       int sock = session->consumer_fd;
+
+       DBG("Sending metadata stream fd");
+
+       /* Extra protection. It's NOT supposed to be set to 0 at this point */
+       if (session->consumer_fd == 0) {
+               session->consumer_fd = consumer_data->cmd_sock;
+       }
+
+       if (session->metadata->obj->shm_fd != 0) {
+               int fds[2];
+
+               /* Send metadata channel fd */
+               lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
+               lum.u.channel.channel_key = session->metadata->obj->shm_fd;
+               lum.u.channel.max_sb_size = session->metadata->attr.subbuf_size;
+               lum.u.channel.mmap_len = 0;     /* for kernel */
+               DBG("Sending metadata channel %d to consumer", lum.u.stream.stream_key);
+               ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
+               if (ret < 0) {
+                       perror("send consumer channel");
+                       goto error;
+               }
+               fds[0] = session->metadata->obj->shm_fd;
+               fds[1] = session->metadata->obj->wait_fd;
+               ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
+               if (ret < 0) {
+                       perror("send consumer metadata channel");
+                       goto error;
+               }
+
+               /* Send metadata stream fd */
+               lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
+               lum.u.stream.channel_key = session->metadata->obj->shm_fd;
+               lum.u.stream.stream_key = session->metadata->stream_obj->shm_fd;
+               lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
+               lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT;
+               lum.u.stream.mmap_len = session->metadata->stream_obj->memory_map_size;
+               strncpy(lum.u.stream.path_name, session->metadata->pathname, PATH_MAX - 1);
+               lum.u.stream.path_name[PATH_MAX - 1] = '\0';
+               DBG("Sending metadata stream %d to consumer", lum.u.stream.stream_key);
+               ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
+               if (ret < 0) {
+                       perror("send consumer metadata stream");
+                       goto error;
+               }
+               fds[0] = session->metadata->stream_obj->shm_fd;
+               fds[1] = session->metadata->stream_obj->wait_fd;
+               ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
+               if (ret < 0) {
+                       perror("send consumer stream");
+                       goto error;
+               }
+       }
+
+       cds_list_for_each_entry(chan, &session->channels.head, list) {
+               ret = send_ustconsumer_channel_streams(consumer_data, sock, chan);
                if (ret < 0) {
                        goto error;
                }
@@ -644,7 +816,7 @@ error:
  *
  * Useful for CPU hotplug feature.
  */
-static int update_stream(struct consumer_data *consumer_data, int fd)
+static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
 {
        int ret = 0;
        struct ltt_session *session;
@@ -680,7 +852,7 @@ static int update_stream(struct consumer_data *consumer_data, int fd)
                                 * stream fds.
                                 */
                                if (session->kernel_session->consumer_fds_sent == 1) {
-                                       ret = send_consumer_channel_streams(consumer_data,
+                                       ret = send_kconsumer_channel_streams(consumer_data,
                                                        session->kernel_session->consumer_fd, channel);
                                        if (ret < 0) {
                                                goto error;
@@ -780,7 +952,7 @@ static void *thread_manage_kernel(void *data)
                                 * kernel session and updating the kernel consumer
                                 */
                                if (revents & LPOLLIN) {
-                                       ret = update_stream(&kconsumer_data, pollfd);
+                                       ret = update_kernel_stream(&kconsumer_data, pollfd);
                                        if (ret < 0) {
                                                continue;
                                        }
@@ -972,6 +1144,9 @@ static void *thread_manage_apps(void *data)
 
        DBG("[thread] Manage application started");
 
+       rcu_register_thread();
+       rcu_thread_online();
+
        ret = create_thread_poll_set(&events, 2);
        if (ret < 0) {
                goto error;
@@ -1077,6 +1252,8 @@ error:
 
        lttng_poll_clean(&events);
 
+       rcu_thread_offline();
+       rcu_unregister_thread();
        return NULL;
 }
 
@@ -1624,7 +1801,7 @@ static int init_kernel_tracing(struct ltt_kernel_session *session)
                        session->consumer_fd = kconsumer_data.cmd_sock;
                }
 
-               ret = send_consumer_session_streams(&kconsumer_data, session);
+               ret = send_kconsumer_session_streams(&kconsumer_data, session);
                if (ret < 0) {
                        ret = LTTCOMM_KERN_CONSUMER_FAIL;
                        goto error;
@@ -1637,6 +1814,36 @@ error:
        return ret;
 }
 
+/*
+ * Init tracing by creating trace directory and sending fds ust consumer.
+ */
+static int init_ust_tracing(struct ltt_ust_session *session)
+{
+       int ret = 0;
+
+       if (session->consumer_fds_sent == 0) {
+               /*
+                * Assign default ust consumer socket if no consumer assigned to the
+                * ust session. At this point, it's NOT suppose to be 0 but this is
+                * an extra security check.
+                */
+               if (session->consumer_fd == 0) {
+                       session->consumer_fd = ustconsumer_data.cmd_sock;
+               }
+
+               ret = send_ustconsumer_session_streams(&ustconsumer_data, session);
+               if (ret < 0) {
+                       ret = LTTCOMM_UST_CONSUMER_FAIL;
+                       goto error;
+               }
+
+               session->consumer_fds_sent = 1;
+       }
+
+error:
+       return ret;
+}
+
 /*
  * Create an UST session and add it to the session ust list.
  */
@@ -1649,7 +1856,7 @@ static int create_ust_session(struct ltt_session *session,
 
        switch (domain->type) {
        case LTTNG_DOMAIN_UST_PID:
-               app = ust_app_get_by_pid(domain->attr.pid);
+               app = ust_app_find_by_pid(domain->attr.pid);
                if (app == NULL) {
                        ret = LTTCOMM_APP_NOT_FOUND;
                        goto error;
@@ -1678,15 +1885,41 @@ static int create_ust_session(struct ltt_session *session,
                }
        }
 
-       /* Create session on the UST tracer */
-       ret = ustctl_create_session(app->sock, lus);
-       if (ret < 0) {
-               ret = LTTCOMM_UST_SESS_FAIL;
-               goto error;
+       /* The domain type dictate different actions on session creation */
+       switch (domain->type) {
+               case LTTNG_DOMAIN_UST_PID:
+                       app = ust_app_find_by_pid(domain->attr.pid);
+                       if (app == NULL) {
+                               ret = LTTCOMM_APP_NOT_FOUND;
+                               goto error;
+                       }
+                       /* Create session on the UST tracer */
+                       ret = ustctl_create_session(app->key.sock, lus);
+                       if (ret < 0) {
+                               ret = LTTCOMM_UST_SESS_FAIL;
+                               goto error;
+                       }
+
+                       lus->handle = ret;
+                       break;
+               case LTTNG_DOMAIN_UST:
+                       /* Create session on the UST tracer */
+                       ret = ustctl_create_session(app->key.sock, lus);
+                       if (ret < 0) {
+                               ret = LTTCOMM_UST_SESS_FAIL;
+                               goto error;
+                       }
+                       break;
+               case LTTNG_DOMAIN_UST_EXEC_NAME:
+                       break;
+               default:
+                       goto error;
        }
+       lus->handle = ret;
+       lus->sock = app->sock;
 
-       cds_list_add(&lus->list, &session->ust_session_list.head);
-       session->ust_session_list.count++;
+       session->ust_session = lus;
+       printf("%p\n", session->ust_session);
 
        return LTTCOMM_OK;
 
@@ -1861,13 +2094,14 @@ error:
 /*
  * Copy channel from attributes and set it in the application channel list.
  */
+/*
 static int copy_ust_channel_to_app(struct ltt_ust_session *usess,
                struct lttng_channel *attr, struct ust_app *app)
 {
        int ret;
        struct ltt_ust_channel *uchan, *new_chan;
 
-       uchan = trace_ust_get_channel_by_name(attr->name, usess);
+       uchan = trace_ust_get_channel_by_key(usess->channels, attr->name);
        if (uchan == NULL) {
                ret = LTTCOMM_FATAL;
                goto error;
@@ -1886,13 +2120,10 @@ static int copy_ust_channel_to_app(struct ltt_ust_session *usess,
                goto error;
        }
 
-       /* Add channel to the ust app channel list */
-       cds_list_add(&new_chan->list, &app->channels.head);
-       app->channels.count++;
-
 error:
        return ret;
 }
+*/
 
 /*
  * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
@@ -1901,6 +2132,9 @@ static int cmd_enable_channel(struct ltt_session *session,
                struct lttng_domain *domain, struct lttng_channel *attr)
 {
        int ret;
+       struct ltt_ust_session *usess = session->ust_session;
+
+       DBG("Enabling channel %s for session %s", session->name, attr->name);
 
        switch (domain->type) {
        case LTTNG_DOMAIN_KERNEL:
@@ -1923,8 +2157,35 @@ static int cmd_enable_channel(struct ltt_session *session,
                kernel_wait_quiescent(kernel_tracer_fd);
                break;
        }
+       case LTTNG_DOMAIN_UST:
+       {
+               struct ltt_ust_channel *uchan;
+
+               DBG2("Enabling channel for LTTNG_DOMAIN_UST domain");
+
+               uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
+                               attr->name);
+               if (uchan == NULL) {
+                       uchan = trace_ust_create_channel(attr, usess->path);
+                       if (uchan == NULL) {
+                               ret = LTTCOMM_UST_CHAN_FAIL;
+                               goto error;
+                       }
+                       rcu_read_lock();
+                       hashtable_add_unique(usess->domain_global.channels, &uchan->node);
+                       rcu_read_unlock();
+               } else {
+                       ret = LTTCOMM_UST_CHAN_EXIST;
+                       goto error;
+               }
+
+               /* TODO: Iterate over trace apps to enable that channel */
+
+               break;
+       }
        case LTTNG_DOMAIN_UST_PID:
        {
+               /*
                int sock;
                struct ltt_ust_channel *uchan;
                struct ltt_ust_session *usess;
@@ -1962,7 +2223,9 @@ static int cmd_enable_channel(struct ltt_session *session,
 
                DBG("UST channel %s created for app sock %d with pid %d",
                                attr->name, app->sock, domain->attr.pid);
-               break;
+               */
+               ret = LTTCOMM_NOT_IMPLEMENTED;
+               goto error;
        }
        default:
                ret = LTTCOMM_UNKNOWN_DOMAIN;
@@ -1982,10 +2245,12 @@ static int cmd_disable_event(struct ltt_session *session, int domain,
                char *channel_name, char *event_name)
 {
        int ret;
-       struct ltt_kernel_channel *kchan;
 
        switch (domain) {
        case LTTNG_DOMAIN_KERNEL:
+       {
+               struct ltt_kernel_channel *kchan;
+
                kchan = trace_kernel_get_channel_by_name(channel_name,
                                session->kernel_session);
                if (kchan == NULL) {
@@ -2000,8 +2265,34 @@ static int cmd_disable_event(struct ltt_session *session, int domain,
 
                kernel_wait_quiescent(kernel_tracer_fd);
                break;
+       }
+       case LTTNG_DOMAIN_UST:
+       {
+               struct ltt_ust_session *ustsession;
+
+               cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) {
+                       struct ltt_ust_channel *ustchan;
+
+                       ustchan = trace_ust_get_channel_by_name(channel_name,
+                                       ustsession);
+                       if (ustchan == NULL) {
+                               ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
+                               goto error;
+                       }
+                       ret = event_ust_disable_tracepoint(ustsession, ustchan, event_name);
+                       if (ret != LTTCOMM_OK) {
+                               goto error;
+                       }
+
+                       ustctl_wait_quiescent(ustsession->sock);
+               }
+               break;
+       }
+       case LTTNG_DOMAIN_UST_EXEC_NAME:
+       case LTTNG_DOMAIN_UST_PID:
+       case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
        default:
-               /* TODO: Userspace tracing */
+               /* TODO: Other UST domains */
                ret = LTTCOMM_NOT_IMPLEMENTED;
                goto error;
        }
@@ -2065,10 +2356,23 @@ static int cmd_add_context(struct ltt_session *session, int domain,
                if (ret != LTTCOMM_OK) {
                        goto error;
                }
+               break;
+       case LTTNG_DOMAIN_UST:
+       {
+               struct ltt_ust_session *ustsession;
 
+               cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) {
+                       /* Add UST context to UST tracer */
+                       ret = context_ust_add(ustsession, ctx,
+                                       event_name, channel_name);
+                       if (ret != LTTCOMM_OK) {
+                               goto error;
+                       }
+               }
                break;
+       }
        default:
-               /* TODO: Userspace tracing */
+               /* TODO: UST other domains */
                ret = LTTCOMM_NOT_IMPLEMENTED;
                goto error;
        }
@@ -2086,11 +2390,13 @@ static int cmd_enable_event(struct ltt_session *session, int domain,
                char *channel_name, struct lttng_event *event)
 {
        int ret;
-       struct ltt_kernel_channel *kchan;
        struct lttng_channel *attr;
 
        switch (domain) {
        case LTTNG_DOMAIN_KERNEL:
+       {
+               struct ltt_kernel_channel *kchan;
+
                kchan = trace_kernel_get_channel_by_name(channel_name,
                                session->kernel_session);
                if (kchan == NULL) {
@@ -2125,8 +2431,54 @@ static int cmd_enable_event(struct ltt_session *session, int domain,
 
                kernel_wait_quiescent(kernel_tracer_fd);
                break;
+       }
+       case LTTNG_DOMAIN_UST:
+       {
+               struct ltt_ust_session *ustsession;
+
+               cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) {
+                       struct ltt_ust_channel *ustchan;
+
+                       ustchan = trace_ust_get_channel_by_name(channel_name,
+                                       ustsession);
+                       if (ustchan == NULL) {
+                               attr = channel_new_default_attr(domain);
+                               if (attr == NULL) {
+                                       ret = LTTCOMM_FATAL;
+                                       goto error;
+                               }
+                               snprintf(attr->name, NAME_MAX, "%s", channel_name);
+
+                               ret = channel_ust_create(ustsession,
+                                               attr, ustsession->sock);
+                               if (ret != LTTCOMM_OK) {
+                                       goto error;
+                               }
+                       }
+
+                       /* Get the newly created ust channel pointer */
+                       ustchan = trace_ust_get_channel_by_name(channel_name,
+                                       ustsession);
+                       if (ustchan == NULL) {
+                               /* This sould not happen... */
+                               ret = LTTCOMM_FATAL;
+                               goto error;
+                       }
+
+                       ret = event_ust_enable_tracepoint(ustsession, ustchan, event);
+                       if (ret != LTTCOMM_OK) {
+                               goto error;
+                       }
+
+                       ustctl_wait_quiescent(ustsession->sock);
+               }
+               break;
+       }
+       case LTTNG_DOMAIN_UST_EXEC_NAME:
+       case LTTNG_DOMAIN_UST_PID:
+       case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
        default:
-               /* TODO: Userspace tracing */
+               /* TODO: UST other domains */
                ret = LTTCOMM_NOT_IMPLEMENTED;
                goto error;
        }
@@ -2243,14 +2595,16 @@ error:
 static int cmd_start_trace(struct ltt_session *session)
 {
        int ret;
-       struct ltt_kernel_channel *kchan;
        struct ltt_kernel_session *ksession;
+       struct ltt_ust_session *ustsession;
 
        /* Short cut */
        ksession = session->kernel_session;
 
        /* Kernel tracing */
        if (ksession != NULL) {
+               struct ltt_kernel_channel *kchan;
+
                /* Open kernel metadata */
                if (ksession->metadata == NULL) {
                        ret = kernel_open_metadata(ksession, ksession->trace_path);
@@ -2301,7 +2655,101 @@ static int cmd_start_trace(struct ltt_session *session)
                kernel_wait_quiescent(kernel_tracer_fd);
        }
 
-       /* TODO: Start all UST traces */
+       /* Start all UST traces */
+       cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) {
+               struct ltt_ust_channel *ustchan;
+
+               /* Open kernel metadata */
+               if (ustsession->metadata == NULL) {
+                       struct lttng_ust_channel_attr ustattr;
+
+                       /* Allocate UST metadata */
+                       ustsession->metadata = trace_ust_create_metadata(ustsession->path);
+                       if (ustsession->metadata == NULL) {
+                               ret = LTTCOMM_UST_META_FAIL;
+                               goto error;
+                       }
+
+                       ustattr.overwrite = ustsession->metadata->attr.overwrite;
+                       ustattr.subbuf_size = ustsession->metadata->attr.subbuf_size;
+                       ustattr.num_subbuf = ustsession->metadata->attr.num_subbuf;
+                       ustattr.switch_timer_interval = ustsession->metadata->attr.switch_timer_interval;
+                       ustattr.read_timer_interval = ustsession->metadata->attr.read_timer_interval;
+                       ustattr.output = ustsession->metadata->attr.output;
+                       
+                       /* UST tracer metadata creation */
+                       ret = ustctl_open_metadata(ustsession->sock,
+                               ustsession->handle, &ustattr,
+                               &ustsession->metadata->obj);
+                       if (ret < 0) {
+                               ret = LTTCOMM_UST_META_FAIL;
+                               goto error;
+                       }
+               }
+
+               /* Open UST metadata stream */
+               if (ustsession->metadata->stream_obj == NULL) {
+                       ret = ustctl_create_stream(ustsession->sock,
+                               ustsession->metadata->obj,
+                               &ustsession->metadata->stream_obj);
+                       if (ret < 0) {
+                               ERR("UST create metadata stream failed");
+                               ret = LTTCOMM_UST_STREAM_FAIL;
+                               goto error;
+                       }
+                       ret = asprintf(&ustsession->metadata->pathname, "%s/%s",
+                                       ustsession->path, "metadata");
+                       if (ret < 0) {
+                               perror("asprintf UST create stream");
+                               goto error;
+                       }
+               }
+
+               /* For each channel */
+               cds_list_for_each_entry(ustchan, &ustsession->channels.head, list) {
+                       if (ustchan->stream_count == 0) {
+                               struct ltt_ust_stream *ustream;
+
+                               ustream = zmalloc(sizeof(*ustream));
+                               if (!ustream) {
+                                       ret = LTTCOMM_UST_STREAM_FAIL;
+                                       goto error;
+                               }
+                               ret = ustctl_create_stream(ustsession->sock,
+                                       ustchan->obj, &ustream->obj);
+                               if (ret < 0) {
+                                       ret = LTTCOMM_UST_STREAM_FAIL;
+                                       goto error;
+                               }
+                               ret = asprintf(&ustream->pathname, "%s/%s_%d",
+                                               ustchan->trace_path, ustchan->name,
+                                               ustchan->stream_count);
+                               if (ret < 0) {
+                                       perror("asprintf UST create stream");
+                                       goto error;
+                               }
+                               cds_list_add(&ustream->list, &ustchan->stream_list.head);
+                               ustchan->stream_count++;
+                       }
+               }
+
+               /* Setup UST consumer socket and send fds to it */
+               ret = init_ust_tracing(ustsession);
+               if (ret < 0) {
+                       ret = LTTCOMM_UST_START_FAIL;
+                       goto error;
+               }
+
+               /* This start the UST tracing */
+               ret = ustctl_start_session(ustsession->sock, ustsession->handle);
+               if (ret < 0) {
+                       ret = LTTCOMM_UST_START_FAIL;
+                       goto error;
+               }
+
+               /* Quiescent wait after starting trace */
+               ustctl_wait_quiescent(ustsession->sock);
+       }
 
        ret = LTTCOMM_OK;
 
@@ -2317,6 +2765,8 @@ static int cmd_stop_trace(struct ltt_session *session)
        int ret;
        struct ltt_kernel_channel *kchan;
        struct ltt_kernel_session *ksession;
+       struct ltt_ust_session *ustsession;
+       struct ltt_ust_channel *ustchan;
 
        /* Short cut */
        ksession = session->kernel_session;
@@ -2347,7 +2797,30 @@ static int cmd_stop_trace(struct ltt_session *session)
                kernel_wait_quiescent(kernel_tracer_fd);
        }
 
-       /* TODO : User-space tracer */
+       /* Stop each UST session */
+       DBG("Stop UST tracing");
+       cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) {
+               /* Flush all buffers before stopping */
+               ret = ustctl_flush_buffer(ustsession->sock, ustsession->metadata->obj);
+               if (ret < 0) {
+                       ERR("UST metadata flush failed");
+               }
+
+               cds_list_for_each_entry(ustchan, &ustsession->channels.head, list) {
+                       ret = ustctl_flush_buffer(ustsession->sock, ustchan->obj);
+                       if (ret < 0) {
+                               ERR("UST flush buffer error");
+                       }
+               }
+
+               ret = ustctl_stop_session(ustsession->sock, ustsession->handle);
+               if (ret < 0) {
+                       ret = LTTCOMM_KERN_STOP_FAIL;
+                       goto error;
+               }
+
+               ustctl_wait_quiescent(ustsession->sock);
+       }
 
        ret = LTTCOMM_OK;
 
@@ -2478,7 +2951,7 @@ static ssize_t cmd_list_domains(struct ltt_session *session,
                nb_dom++;
        }
 
-       nb_dom += session->ust_session_list.count;
+       /* TODO: User-space tracer domain support */
 
        *domains = malloc(nb_dom * sizeof(struct lttng_domain));
        if (*domains == NULL) {
@@ -2488,8 +2961,6 @@ static ssize_t cmd_list_domains(struct ltt_session *session,
 
        (*domains)[0].type = LTTNG_DOMAIN_KERNEL;
 
-       /* TODO: User-space tracer domain support */
-
        return nb_dom;
 
 error:
@@ -2517,6 +2988,8 @@ static ssize_t cmd_list_channels(struct ltt_session *session,
 
        list_lttng_channels(session, *channels);
 
+       /* TODO UST support */
+
        return nb_chan;
 
 error:
@@ -2661,26 +3134,35 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
                        pthread_mutex_unlock(&kconsumer_data.pid_mutex);
                }
                break;
+       case LTTNG_DOMAIN_UST:
+       case LTTNG_DOMAIN_UST_EXEC_NAME:
        case LTTNG_DOMAIN_UST_PID:
+       case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
        {
                struct ltt_ust_session *usess;
-
                if (need_tracing_session) {
-                       usess = trace_ust_get_session_by_pid(
-                                       &cmd_ctx->session->ust_session_list,
-                                       cmd_ctx->lsm->domain.attr.pid);
-                       if (usess == NULL) {
+                       if (cmd_ctx->session->ust_session == NULL) {
                                ret = create_ust_session(cmd_ctx->session,
                                                &cmd_ctx->lsm->domain);
                                if (ret != LTTCOMM_OK) {
                                        goto error;
                                }
                        }
+                       /* Start the kernel consumer daemon */
+                       pthread_mutex_lock(&ustconsumer_data.pid_mutex);
+                       if (ustconsumer_data.pid == 0 &&
+                                       cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
+                               pthread_mutex_unlock(&ustconsumer_data.pid_mutex);
+                               ret = start_consumerd(&ustconsumer_data);
+                               if (ret < 0) {
+                                       ret = LTTCOMM_KERN_CONSUMER_FAIL;
+                                       goto error;
+                               }
+                       }
+                       pthread_mutex_unlock(&ustconsumer_data.pid_mutex);
                }
                break;
-       }
        default:
-               /* TODO Userspace tracer */
                break;
        }
 
@@ -2710,7 +3192,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
        }
        case LTTNG_DISABLE_ALL_EVENT:
        {
-               DBG("Disabling all kernel event");
+               DBG("Disabling all events");
 
                ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
                                cmd_ctx->lsm->u.disable.channel_name);
@@ -2731,7 +3213,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
        }
        case LTTNG_ENABLE_ALL_EVENT:
        {
-               DBG("Enabling all kernel event");
+               DBG("Enabling all events");
 
                ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
                                cmd_ctx->lsm->u.enable.channel_name,
@@ -2938,6 +3420,8 @@ static void *thread_manage_clients(void *data)
 
        DBG("[thread] Manage client started");
 
+       rcu_register_thread();
+
        ret = lttcomm_listen_unix_sock(client_sock);
        if (ret < 0) {
                goto error;
@@ -3038,6 +3522,7 @@ static void *thread_manage_clients(void *data)
                // TODO: Validate cmd_ctx including sanity check for
                // security purpose.
 
+               rcu_thread_online();
                /*
                 * This function dispatch the work to the kernel or userspace tracer
                 * libs and fill the lttcomm_lttng_msg data structure of all the needed
@@ -3045,6 +3530,7 @@ static void *thread_manage_clients(void *data)
                 * everything this function may needs.
                 */
                ret = process_client_msg(cmd_ctx);
+               rcu_thread_offline();
                if (ret < 0) {
                        /*
                         * TODO: Inform client somehow of the fatal error. At
@@ -3057,7 +3543,7 @@ static void *thread_manage_clients(void *data)
 
                DBG("Sending response (size: %d, retcode: %s)",
                                cmd_ctx->lttng_msg_size,
-                               lttng_get_readable_code(-cmd_ctx->llm->ret_code));
+                               lttng_strerror(-cmd_ctx->llm->ret_code));
                ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
                if (ret < 0) {
                        ERR("Failed to send data back to client");
@@ -3077,6 +3563,8 @@ error:
 
        lttng_poll_clean(&events);
        clean_command_ctx(&cmd_ctx);
+
+       rcu_unregister_thread();
        return NULL;
 }
 
@@ -3505,6 +3993,8 @@ int main(int argc, char **argv)
        void *status;
        const char *home_path;
 
+       rcu_register_thread();
+
        /* Create thread quit pipe */
        if ((ret = init_thread_quit_pipe()) < 0) {
                goto error;
@@ -3647,6 +4137,9 @@ int main(int argc, char **argv)
        /* Init UST command queue. */
        cds_wfq_init(&ust_cmd_queue.queue);
 
+       /* Init UST app hash table */
+       ust_app_ht_alloc();
+
        /*
         * Get session list pointer. This pointer MUST NOT be free(). This list is
         * statically declared in session.c
@@ -3741,7 +4234,10 @@ exit:
        /*
         * cleanup() is called when no other thread is running.
         */
+       rcu_thread_online();
        cleanup();
+       rcu_thread_offline();
+       rcu_unregister_thread();
        if (!ret)
                exit(EXIT_SUCCESS);
 error:
This page took 0.034743 seconds and 4 git commands to generate.