Implement filter bytecode support in lttng-session, and parse filter string
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
index 10ffe403bc44448a9d9511fb60e3983345988829..6fb328eb9ca0cfd991328d227445d1aa6fbdadca 100644 (file)
@@ -31,6 +31,8 @@
 #include "ust-app.h"
 #include "ust-consumer.h"
 #include "ust-ctl.h"
+#include "fd-limit.h"
+#include "../../common/sessiond-comm/sessiond-comm.h"
 
 /*
  * Delete ust context safely. RCU read lock must be held before calling
@@ -64,6 +66,7 @@ void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
                assert(!ret);
                delete_ust_app_ctx(sock, ua_ctx);
        }
+       free(ua_event->filter);
        lttng_ht_destroy(ua_event->ctx);
 
        if (ua_event->obj != NULL) {
@@ -82,6 +85,7 @@ void delete_ust_app_stream(int sock, struct ltt_ust_stream *stream)
 {
        if (stream->obj) {
                ustctl_release_object(sock, stream->obj);
+               lttng_fd_put(LTTNG_FD_APPS, 2);
                free(stream->obj);
        }
        free(stream);
@@ -125,6 +129,7 @@ void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan)
 
        if (ua_chan->obj != NULL) {
                ustctl_release_object(sock, ua_chan->obj);
+               lttng_fd_put(LTTNG_FD_APPS, 2);
                free(ua_chan->obj);
        }
        free(ua_chan);
@@ -144,12 +149,15 @@ void delete_ust_app_session(int sock, struct ust_app_session *ua_sess)
        if (ua_sess->metadata) {
                if (ua_sess->metadata->stream_obj) {
                        ustctl_release_object(sock, ua_sess->metadata->stream_obj);
+                       lttng_fd_put(LTTNG_FD_APPS, 2);
                        free(ua_sess->metadata->stream_obj);
                }
                if (ua_sess->metadata->obj) {
                        ustctl_release_object(sock, ua_sess->metadata->obj);
+                       lttng_fd_put(LTTNG_FD_APPS, 2);
                        free(ua_sess->metadata->obj);
                }
+               trace_ust_destroy_metadata(ua_sess->metadata);
        }
 
        cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
@@ -209,6 +217,7 @@ void delete_ust_app(struct ust_app *app)
        if (ret) {
                PERROR("close");
        }
+       lttng_fd_put(LTTNG_FD_APPS, 1);
 
        DBG2("UST app pid %d deleted", app->pid);
        free(app);
@@ -423,6 +432,31 @@ error:
        return ret;
 }
 
+/*
+ * Set the filter on the tracer.
+ */
+static
+int set_ust_event_filter(struct ust_app_event *ua_event,
+               struct ust_app *app)
+{
+       int ret;
+
+       if (!ua_event->filter) {
+               return 0;
+       }
+
+       ret = ustctl_set_filter(app->sock, ua_event->filter,
+                       ua_event->obj);
+       if (ret < 0) {
+               goto error;
+       }
+
+       DBG2("UST filter set successfully for event %s", ua_event->name);
+
+error:
+       return ret;
+}
+
 /*
  * Disable the specified event on to UST tracer for the UST session.
  */
@@ -535,6 +569,12 @@ static int open_ust_metadata(struct ust_app *app,
                ua_sess->metadata->attr.read_timer_interval;
        uattr.output = ua_sess->metadata->attr.output;
 
+       /* We are going to receive 2 fds, we need to reserve them. */
+       ret = lttng_fd_get(LTTNG_FD_APPS, 2);
+       if (ret < 0) {
+               ERR("Exhausted number of available FD upon metadata open");
+               goto error;
+       }
        /* UST tracer metadata creation */
        ret = ustctl_open_metadata(app->sock, ua_sess->handle, &uattr,
                        &ua_sess->metadata->obj);
@@ -558,6 +598,12 @@ static int create_ust_stream(struct ust_app *app,
 {
        int ret;
 
+       /* We are going to receive 2 fds, we need to reserve them. */
+       ret = lttng_fd_get(LTTNG_FD_APPS, 2);
+       if (ret < 0) {
+               ERR("Exhausted number of available FD upon metadata stream create");
+               goto error;
+       }
        ret = ustctl_create_stream(app->sock, ua_sess->metadata->obj,
                        &ua_sess->metadata->stream_obj);
        if (ret < 0) {
@@ -578,6 +624,13 @@ static int create_ust_channel(struct ust_app *app,
        int ret;
 
        /* TODO: remove cast and use lttng-ust-abi.h */
+
+       /* We are going to receive 2 fds, we need to reserve them. */
+       ret = lttng_fd_get(LTTNG_FD_APPS, 2);
+       if (ret < 0) {
+               ERR("Exhausted number of available FD upon create channel");
+               goto error;
+       }
        ret = ustctl_create_channel(app->sock, ua_sess->handle,
                        (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
        if (ret < 0) {
@@ -585,6 +638,7 @@ static int create_ust_channel(struct ust_app *app,
                                "and session handle %d with ret %d",
                                ua_chan->name, app->pid, app->sock,
                                ua_sess->handle, ret);
+               lttng_fd_put(LTTNG_FD_APPS, 2);
                goto error;
        }
 
@@ -618,7 +672,7 @@ int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
        ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
                        &ua_event->obj);
        if (ret < 0) {
-               if (ret == -EEXIST) {
+               if (ret == -EEXIST || ret == -EPERM) {
                        ret = 0;
                        goto error;
                }
@@ -678,6 +732,16 @@ static void shadow_copy_event(struct ust_app_event *ua_event,
        /* Copy event attributes */
        memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
 
+       /* Copy filter bytecode */
+       if (uevent->filter) {
+               ua_event->filter = zmalloc(sizeof(*ua_event->filter) +
+                       uevent->filter->len);
+               if (!ua_event->filter) {
+                       return;
+               }
+               memcpy(ua_event->filter, uevent->filter,
+                       sizeof(*ua_event->filter) + uevent->filter->len);
+       }
        cds_lfht_for_each_entry(uevent->ctx->ht, &iter.iter, uctx, node.node) {
                ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
                if (ua_ctx == NULL) {
@@ -770,8 +834,8 @@ static void shadow_copy_session(struct ust_app_session *ua_sess,
        ua_sess->uid = usess->uid;
        ua_sess->gid = usess->gid;
 
-       ret = snprintf(ua_sess->path, PATH_MAX, "%s/%s-%d-%s", usess->pathname,
-                       app->name, app->pid, datetime);
+       ret = snprintf(ua_sess->path, PATH_MAX, "%s-%d-%s/", app->name, app->pid,
+                       datetime);
        if (ret < 0) {
                PERROR("asprintf UST shadow copy session");
                /* TODO: We cannot return an error from here.. */
@@ -970,6 +1034,35 @@ error:
        return ret;
 }
 
+/*
+ * Set UST filter for the event on the tracer.
+ */
+static
+int set_ust_app_event_filter(struct ust_app_session *ua_sess,
+               struct ust_app_event *ua_event,
+               struct lttng_filter_bytecode *bytecode,
+               struct ust_app *app)
+{
+       int ret = 0;
+
+       DBG2("UST app adding context to event %s", ua_event->name);
+
+       /* Copy filter bytecode */
+       ua_event->filter = zmalloc(sizeof(*ua_event->filter) + bytecode->len);
+       if (!ua_event->filter) {
+               return -ENOMEM;
+       }
+       memcpy(ua_event->filter, bytecode,
+               sizeof(*ua_event->filter) + bytecode->len);
+       ret = set_ust_event_filter(ua_event, app);
+       if (ret < 0) {
+               goto error;
+       }
+
+error:
+       return ret;
+}
+
 /*
  * Enable on the tracer side a ust app event for the session and channel.
  */
@@ -1198,13 +1291,6 @@ static int create_ust_app_metadata(struct ust_app_session *ua_sess,
                        goto error;
                }
 
-               ret = run_as_mkdir(ua_sess->path, S_IRWXU | S_IRWXG,
-                               ua_sess->uid, ua_sess->gid);
-               if (ret < 0) {
-                       PERROR("mkdir UST metadata");
-                       goto error;
-               }
-
                ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
                                "%s/metadata", ua_sess->path);
                if (ret < 0) {
@@ -1280,6 +1366,7 @@ int ust_app_register(struct ust_register_msg *msg, int sock)
                if (ret) {
                        PERROR("close");
                }
+               lttng_fd_put(LTTNG_FD_APPS, 1);
                return -EINVAL;
        }
        if (msg->major != LTTNG_UST_COMM_MAJOR) {
@@ -1290,6 +1377,7 @@ int ust_app_register(struct ust_register_msg *msg, int sock)
                if (ret) {
                        PERROR("close");
                }
+               lttng_fd_put(LTTNG_FD_APPS, 1);
                return -EINVAL;
        }
        lta = zmalloc(sizeof(struct ust_app));
@@ -1450,7 +1538,7 @@ int ust_app_list_events(struct lttng_event **events)
                        }
                        memcpy(tmp[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
                        tmp[count].loglevel = uiter.loglevel;
-                       tmp[count].type = LTTNG_UST_TRACEPOINT;
+                       tmp[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
                        tmp[count].pid = app->pid;
                        tmp[count].enabled = -1;
                        count++;
@@ -1468,6 +1556,81 @@ error:
        return ret;
 }
 
+/*
+ * Fill events array with all events name of all registered apps.
+ */
+int ust_app_list_event_fields(struct lttng_event_field **fields)
+{
+       int ret, handle;
+       size_t nbmem, count = 0;
+       struct lttng_ht_iter iter;
+       struct ust_app *app;
+       struct lttng_event_field *tmp;
+
+       nbmem = UST_APP_EVENT_LIST_SIZE;
+       tmp = zmalloc(nbmem * sizeof(struct lttng_event_field));
+       if (tmp == NULL) {
+               PERROR("zmalloc ust app event fields");
+               ret = -ENOMEM;
+               goto error;
+       }
+
+       rcu_read_lock();
+
+       cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
+               struct lttng_ust_field_iter uiter;
+
+               if (!app->compatible) {
+                       /*
+                        * TODO: In time, we should notice the caller of this error by
+                        * telling him that this is a version error.
+                        */
+                       continue;
+               }
+               handle = ustctl_tracepoint_field_list(app->sock);
+               if (handle < 0) {
+                       ERR("UST app list event fields getting handle failed for app pid %d",
+                                       app->pid);
+                       continue;
+               }
+
+               while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
+                                               &uiter)) != -ENOENT) {
+                       if (count >= nbmem) {
+                               DBG2("Reallocating event field list from %zu to %zu entries", nbmem,
+                                               2 * nbmem);
+                               nbmem *= 2;
+                               tmp = realloc(tmp, nbmem * sizeof(struct lttng_event_field));
+                               if (tmp == NULL) {
+                                       PERROR("realloc ust app event fields");
+                                       ret = -ENOMEM;
+                                       goto rcu_error;
+                               }
+                       }
+
+                       memcpy(tmp[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
+                       tmp[count].type = uiter.type;
+
+                       memcpy(tmp[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
+                       tmp[count].event.loglevel = uiter.loglevel;
+                       tmp[count].event.type = LTTNG_UST_TRACEPOINT;
+                       tmp[count].event.pid = app->pid;
+                       tmp[count].event.enabled = -1;
+                       count++;
+               }
+       }
+
+       ret = count;
+       *fields = tmp;
+
+       DBG2("UST app list event fields done (%zu events)", count);
+
+rcu_error:
+       rcu_read_unlock();
+error:
+       return ret;
+}
+
 /*
  * Free and clean all traceable apps of the global list.
  */
@@ -1976,25 +2139,37 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
                                goto error_rcu_unlock;
                        }
 
+                       /* We are going to receive 2 fds, we need to reserve them. */
+                       ret = lttng_fd_get(LTTNG_FD_APPS, 2);
+                       if (ret < 0) {
+                               ERR("Exhausted number of available FD upon stream create");
+                               free(ustream);
+                               goto error_rcu_unlock;
+                       }
                        ret = ustctl_create_stream(app->sock, ua_chan->obj,
                                        &ustream->obj);
                        if (ret < 0) {
                                /* Got all streams */
+                               lttng_fd_put(LTTNG_FD_APPS, 2);
+                               free(ustream);
                                break;
                        }
                        ustream->handle = ustream->obj->handle;
 
                        /* Order is important */
                        cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
-                       ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s_%u",
-                                       ua_sess->path, ua_chan->name,
-                                       ua_chan->streams.count++);
+                       ret = snprintf(ustream->name, sizeof(ustream->name), "%s_%u",
+                                       ua_chan->name, ua_chan->streams.count++);
                        if (ret < 0) {
                                PERROR("asprintf UST create stream");
+                               /*
+                                * XXX what should we do here with the
+                                * stream ?
+                                */
                                continue;
                        }
-                       DBG2("UST stream %d ready at %s", ua_chan->streams.count,
-                                       ustream->pathname);
+                       DBG2("UST stream %d ready (handle: %d)", ua_chan->streams.count,
+                                       ustream->handle);
                }
        }
 
@@ -2011,7 +2186,7 @@ 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(consumerd_fd, ua_sess);
+       ret = ust_consumer_send_session(consumerd_fd, ua_sess, usess->consumer);
        if (ret < 0) {
                goto error_rcu_unlock;
        }
@@ -2310,6 +2485,11 @@ void ust_app_global_update(struct ltt_ust_session *usess, int sock)
                                        continue;
                                }
                        }
+                       ret = set_ust_event_filter(ua_event, app);
+                       if (ret < 0) {
+                               /* FIXME: Should we quit here or continue... */
+                               continue;
+                       }
                }
        }
 
@@ -2431,6 +2611,63 @@ int ust_app_add_ctx_event_glb(struct ltt_ust_session *usess,
        return ret;
 }
 
+/*
+ * Add context to a specific event in a channel for global UST domain.
+ */
+int ust_app_set_filter_event_glb(struct ltt_ust_session *usess,
+               struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent,
+               struct lttng_filter_bytecode *bytecode)
+{
+       int ret = 0;
+       struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
+       struct lttng_ht_iter iter, uiter;
+       struct ust_app_session *ua_sess;
+       struct ust_app_event *ua_event;
+       struct ust_app_channel *ua_chan = NULL;
+       struct ust_app *app;
+
+       rcu_read_lock();
+
+       cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
+               if (!app->compatible) {
+                       /*
+                        * TODO: In time, we should notice the caller of this error by
+                        * telling him that this is a version error.
+                        */
+                       continue;
+               }
+               ua_sess = lookup_session_by_app(usess, app);
+               if (ua_sess == NULL) {
+                       continue;
+               }
+
+               /* Lookup channel in the ust app session */
+               lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
+               ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
+               if (ua_chan_node == NULL) {
+                       continue;
+               }
+               ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
+                               node);
+
+               lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
+               ua_event_node = lttng_ht_iter_get_node_str(&uiter);
+               if (ua_event_node == NULL) {
+                       continue;
+               }
+               ua_event = caa_container_of(ua_event_node, struct ust_app_event,
+                               node);
+
+               ret = set_ust_app_event_filter(ua_sess, ua_event, bytecode, app);
+               if (ret < 0) {
+                       continue;
+               }
+       }
+
+       rcu_read_unlock();
+       return ret;
+}
+
 /*
  * Enable event for a channel from a UST session for a specific PID.
  */
This page took 0.029021 seconds and 4 git commands to generate.