Implement PID tracking
[lttng-modules.git] / lttng-events.c
index eefee69b31a5cc59583fb57d7f074c5715aec654..8a48c6b5dc6f094132b24ed7640415a6cbd74ae1 100644 (file)
@@ -98,6 +98,8 @@ struct lttng_session *lttng_session_create(void)
        kref_init(&metadata_cache->refcount);
        session->metadata_cache = metadata_cache;
        INIT_LIST_HEAD(&metadata_cache->metadata_stream);
+       memcpy(&metadata_cache->uuid, &session->uuid,
+               sizeof(metadata_cache->uuid));
        list_add(&session->list, &sessions);
        mutex_unlock(&sessions_mutex);
        return session;
@@ -145,6 +147,8 @@ void lttng_session_destroy(struct lttng_session *session)
        }
        list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list)
                _lttng_metadata_channel_hangup(metadata_stream);
+       if (session->pid_tracker)
+               lttng_pid_tracker_destroy(session->pid_tracker);
        kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
        list_del(&session->list);
        mutex_unlock(&sessions_mutex);
@@ -377,8 +381,14 @@ struct lttng_event *lttng_event_create(struct lttng_channel *chan,
         */
        list_for_each_entry(event, &chan->session->events, list) {
                if (!strcmp(event->desc->name, event_param->name)) {
-                       ret = -EEXIST;
-                       goto exist;
+                       /*
+                        * Allow events with the same name to appear in
+                        * different channels.
+                        */
+                       if (event->chan == chan) {
+                               ret = -EEXIST;
+                               goto exist;
+                       }
                }
        }
        event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
@@ -578,6 +588,78 @@ void _lttng_event_destroy(struct lttng_event *event)
        kmem_cache_free(event_cache, event);
 }
 
+int lttng_session_track_pid(struct lttng_session *session, int pid)
+{
+       int ret;
+
+       if (pid < -1)
+               return -EINVAL;
+       mutex_lock(&sessions_mutex);
+       if (pid == -1) {
+               /* track all pids: destroy tracker. */
+               if (session->pid_tracker) {
+                       struct lttng_pid_tracker *lpf;
+
+                       lpf = session->pid_tracker;
+                       rcu_assign_pointer(session->pid_tracker, NULL);
+                       synchronize_trace();
+                       lttng_pid_tracker_destroy(lpf);
+               }
+               ret = 0;
+       } else {
+               if (!session->pid_tracker) {
+                       struct lttng_pid_tracker *lpf;
+
+                       lpf = lttng_pid_tracker_create();
+                       if (!lpf) {
+                               ret = -ENOMEM;
+                               goto unlock;
+                       }
+                       ret = lttng_pid_tracker_add(lpf, pid);
+                       rcu_assign_pointer(session->pid_tracker, lpf);
+               } else {
+                       ret = lttng_pid_tracker_add(session->pid_tracker, pid);
+               }
+       }
+unlock:
+       mutex_unlock(&sessions_mutex);
+       return ret;
+}
+
+int lttng_session_untrack_pid(struct lttng_session *session, int pid)
+{
+       int ret;
+
+       if (pid < -1)
+               return -EINVAL;
+       mutex_lock(&sessions_mutex);
+       if (pid == -1) {
+               /* untrack all pids: replace by empty tracker. */
+               struct lttng_pid_tracker *old_lpf = session->pid_tracker;
+               struct lttng_pid_tracker *lpf;
+
+               lpf = lttng_pid_tracker_create();
+               if (!lpf) {
+                       ret = -ENOMEM;
+                       goto unlock;
+               }
+               rcu_assign_pointer(session->pid_tracker, lpf);
+               synchronize_trace();
+               if (old_lpf)
+                       lttng_pid_tracker_destroy(old_lpf);
+               ret = 0;
+       } else {
+               if (!session->pid_tracker) {
+                       ret = -ENOENT;
+                       goto unlock;
+               }
+               ret = lttng_pid_tracker_del(session->pid_tracker, pid);
+       }
+unlock:
+       mutex_unlock(&sessions_mutex);
+       return ret;
+}
+
 /*
  * Serialize at most one packet worth of metadata into a metadata
  * channel.
@@ -597,16 +679,20 @@ int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
 
        /*
         * Ensure we support mutiple get_next / put sequences followed
-        * by put_next.
+        * by put_next. The metadata stream lock internally protects
+        * reading the metadata cache. It can indeed be read
+        * concurrently by "get_next_subbuf" and "flush" operations on
+        * the buffer invoked by different processes.
         */
+       mutex_lock(&stream->lock);
        WARN_ON(stream->metadata_in < stream->metadata_out);
        if (stream->metadata_in != stream->metadata_out)
-               return 0;
+               goto end;
 
        len = stream->metadata_cache->metadata_written -
                stream->metadata_in;
        if (!len)
-               return 0;
+               goto end;
        reserve_len = min_t(size_t,
                        stream->transport->ops.packet_avail_size(chan),
                        len);
@@ -628,6 +714,7 @@ int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
        ret = reserve_len;
 
 end:
+       mutex_unlock(&stream->lock);
        return ret;
 }
 
@@ -1255,6 +1342,10 @@ static int __init lttng_events_init(void)
 {
        int ret;
 
+       ret = wrapper_lttng_fixup_sig(THIS_MODULE);
+       if (ret)
+               return ret;
+
        ret = lttng_tracepoint_init();
        if (ret)
                return ret;
This page took 0.025506 seconds and 4 git commands to generate.