SoW-2020-0002: Trace Hit Counters
[deliverable/lttng-ust.git] / liblttng-ust / lttng-ust-abi.c
index 9e4e5cd56713eb77659b72aa074140c6c51bfa4b..6887a0c39cfd9023504621ad375c9aaf8cd689f8 100644 (file)
@@ -1,24 +1,9 @@
 /*
- * lttng-ust-abi.c
- *
- * LTTng UST ABI
+ * SPDX-License-Identifier: LGPL-2.1-only
  *
  * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; only
- * version 2.1 of the License.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
+ * LTTng UST ABI
  *
  * Mimic system calls for:
  * - session creation, returns an object descriptor or failure.
  *     - Takes instrumentation source specific arguments.
  */
 
-#include <lttng/ust-abi.h>
-#include <lttng/ust-error.h>
+#define _LGPL_SOURCE
+#include <fcntl.h>
+#include <stdint.h>
+#include <unistd.h>
+
 #include <urcu/compiler.h>
 #include <urcu/list.h>
+
+#include <helper.h>
+#include <lttng/tracepoint.h>
+#include <lttng/ust-abi.h>
+#include <lttng/ust-error.h>
 #include <lttng/ust-events.h>
 #include <lttng/ust-version.h>
-#include <lttng/tracepoint.h>
-#include "tracepoint-internal.h"
+#include <ust-fd.h>
 #include <usterr-signal-safe.h>
-#include <helper.h>
-#include "lttng-tracer.h"
-#include "../libringbuffer/shm.h"
+
 #include "../libringbuffer/frontend_types.h"
+#include "../libringbuffer/shm.h"
+#include "../libcounter/counter.h"
+#include "tracepoint-internal.h"
+#include "lttng-tracer.h"
+#include "string-utils.h"
+#include "ust-events-internal.h"
 
 #define OBJ_NAME_LEN   16
 
@@ -201,14 +197,14 @@ int lttng_ust_objd_unref(int id, int is_owner)
        }
        if (is_owner) {
                if (!obj->u.s.owner_ref) {
-                       ERR("Error decrementing owner reference");
+                       ERR("Error decrementing owner reference\n");
                        return -EINVAL;
                }
                obj->u.s.owner_ref--;
        }
        if ((--obj->u.s.f_count) == 1) {
                const struct lttng_ust_objd_ops *ops = objd_ops(id);
-               
+
                if (ops->release)
                        ops->release(id);
                objd_free(id);
@@ -272,9 +268,12 @@ void lttng_ust_objd_table_owner_cleanup(void *owner)
  */
 
 static const struct lttng_ust_objd_ops lttng_ops;
+static const struct lttng_ust_objd_ops lttng_event_notifier_group_ops;
 static const struct lttng_ust_objd_ops lttng_session_ops;
 static const struct lttng_ust_objd_ops lttng_channel_ops;
-static const struct lttng_ust_objd_ops lttng_enabler_ops;
+static const struct lttng_ust_objd_ops lttng_counter_ops;
+static const struct lttng_ust_objd_ops lttng_event_enabler_ops;
+static const struct lttng_ust_objd_ops lttng_event_notifier_enabler_ops;
 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops;
 
@@ -332,6 +331,50 @@ long lttng_abi_tracer_version(int objd,
        return 0;
 }
 
+static
+int lttng_abi_event_notifier_send_fd(void *owner, int *event_notifier_notif_fd)
+{
+       struct lttng_event_notifier_group *event_notifier_group;
+       int event_notifier_group_objd, ret, fd_flag;
+
+       event_notifier_group = lttng_event_notifier_group_create();
+       if (!event_notifier_group)
+               return -ENOMEM;
+
+       /*
+        * Set this file descriptor as NON-BLOCKING.
+        */
+       fd_flag = fcntl(*event_notifier_notif_fd, F_GETFL);
+
+       fd_flag |= O_NONBLOCK;
+
+       ret = fcntl(*event_notifier_notif_fd, F_SETFL, fd_flag);
+       if (ret) {
+               ret = -errno;
+               goto fd_error;
+       }
+
+       event_notifier_group_objd = objd_alloc(event_notifier_group,
+               &lttng_event_notifier_group_ops, owner, "event_notifier_group");
+       if (event_notifier_group_objd < 0) {
+               ret = event_notifier_group_objd;
+               goto objd_error;
+       }
+
+       event_notifier_group->objd = event_notifier_group_objd;
+       event_notifier_group->owner = owner;
+       event_notifier_group->notification_fd = *event_notifier_notif_fd;
+       /* Object descriptor takes ownership of notification fd. */
+       *event_notifier_notif_fd = -1;
+
+       return event_notifier_group_objd;
+
+objd_error:
+       lttng_event_notifier_group_destroy(event_notifier_group);
+fd_error:
+       return ret;
+}
+
 static
 long lttng_abi_add_context(int objd,
        struct lttng_ust_context *context_param,
@@ -379,8 +422,11 @@ long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
        case LTTNG_UST_TRACEPOINT_FIELD_LIST:
                return lttng_abi_tracepoint_field_list(owner);
        case LTTNG_UST_WAIT_QUIESCENT:
-               synchronize_trace();
+               lttng_ust_synchronize_trace();
                return 0;
+       case LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE:
+               return lttng_abi_event_notifier_send_fd(owner,
+                       &uargs->event_notifier_handle.event_notifier_notif_fd);
        default:
                return -EINVAL;
        }
@@ -390,6 +436,7 @@ static const struct lttng_ust_objd_ops lttng_ops = {
        .cmd = lttng_cmd,
 };
 
+static
 int lttng_abi_map_channel(int session_objd,
                struct lttng_ust_channel *ust_chan,
                union ust_args *uargs,
@@ -434,6 +481,10 @@ int lttng_abi_map_channel(int session_objd,
                goto handle_error;
        }
 
+       /* Ownership of chan_data and wakeup_fd taken by channel handle. */
+       uargs->channel.chan_data = NULL;
+       uargs->channel.wakeup_fd = -1;
+
        chan = shmp(channel_handle, channel_handle->chan);
        assert(chan);
        chan->handle = channel_handle;
@@ -487,10 +538,9 @@ int lttng_abi_map_channel(int session_objd,
 
        /* Initialize our lttng chan */
        lttng_chan->chan = chan;
-       lttng_chan->tstate = 1;
-       lttng_chan->enabled = 1;
+       lttng_chan->enabled = 1;        /* Backward compatibility */
        lttng_chan->ctx = NULL;
-       lttng_chan->session = session;
+       lttng_chan->session = session;  /* Backward compatibility */
        lttng_chan->ops = &transport->ops;
        memcpy(&lttng_chan->chan->backend.config,
                transport->client_config,
@@ -500,12 +550,24 @@ int lttng_abi_map_channel(int session_objd,
        lttng_chan->handle = channel_handle;
        lttng_chan->type = type;
 
+       /* Set container properties */
+       lttng_chan->parent.type = LTTNG_EVENT_CONTAINER_CHANNEL;
+       lttng_chan->parent.session = session;
+       lttng_chan->parent.enabled = 1;
+       lttng_chan->parent.tstate = 1;
+       /*
+        * The ring buffer always coalesces hits from various event
+        * enablers matching a given event to a single event record within the
+        * ring buffer.
+        */
+       lttng_chan->parent.coalesce_hits = true;
+
        /*
         * We tolerate no failure path after channel creation. It will stay
         * invariant for the rest of the session.
         */
        objd_set_private(chan_objd, lttng_chan);
-       lttng_chan->objd = chan_objd;
+       lttng_chan->parent.objd = chan_objd;
        /* The channel created holds a reference on the session */
        objd_ref(session_objd);
        return chan_objd;
@@ -517,25 +579,100 @@ alloc_error:
        channel_destroy(chan, channel_handle, 0);
        return ret;
 
-       /*
-        * error path before channel creation (owning chan_data and
-        * wakeup_fd).
-        */
 handle_error:
 active:
 invalid:
+       return ret;
+}
+
+static
+long lttng_abi_session_create_counter(
+               int session_objd,
+               struct lttng_ust_counter *ust_counter,
+               union ust_args *uargs,
+               void *owner)
+{
+       struct lttng_session *session = objd_private(session_objd);
+       int counter_objd, ret, i;
+       char *counter_transport_name;
+       struct lttng_counter *counter = NULL;
+       struct lttng_event_container *container;
+       size_t dimension_sizes[LTTNG_UST_COUNTER_DIMENSION_MAX] = { 0 };
+       size_t number_dimensions;
+       const struct lttng_ust_counter_conf *counter_conf;
+
+       if (ust_counter->len != sizeof(*counter_conf)) {
+               ERR("LTTng: Map: Error counter configuration of wrong size.\n");
+               return -EINVAL;
+       }
+       counter_conf = uargs->counter.counter_data;
+
+       if (counter_conf->arithmetic != LTTNG_UST_COUNTER_ARITHMETIC_MODULAR) {
+               ERR("LTTng: Map: Error counter of the wrong type.\n");
+               return -EINVAL;
+       }
+
+       if (counter_conf->global_sum_step) {
+               /* Unsupported. */
+               return -EINVAL;
+       }
+
+       switch (counter_conf->bitness) {
+       case LTTNG_UST_COUNTER_BITNESS_64:
+               counter_transport_name = "counter-per-cpu-64-modular";
+               break;
+       case LTTNG_UST_COUNTER_BITNESS_32:
+               counter_transport_name = "counter-per-cpu-32-modular";
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       number_dimensions = (size_t) counter_conf->number_dimensions;
+
+       for (i = 0; i < counter_conf->number_dimensions; i++) {
+               if (counter_conf->dimensions[i].has_underflow)
+                       return -EINVAL;
+               if (counter_conf->dimensions[i].has_overflow)
+                       return -EINVAL;
+               dimension_sizes[i] = counter_conf->dimensions[i].size;
+       }
+
+       counter_objd = objd_alloc(NULL, &lttng_counter_ops, owner, "counter");
+       if (counter_objd < 0) {
+               ret = counter_objd;
+               goto objd_error;
+       }
+
+       counter = lttng_session_create_counter(session,
+                       counter_transport_name,
+                       number_dimensions, dimension_sizes,
+                       0, counter_conf->coalesce_hits);
+       if (!counter) {
+               ret = -EINVAL;
+               goto counter_error;
+       }
+       container = lttng_counter_get_event_container(counter);
+       objd_set_private(counter_objd, counter);
+       container->objd = counter_objd;
+       container->enabled = 1;
+       container->tstate = 1;
+       /* The channel created holds a reference on the session */
+       objd_ref(session_objd);
+       return counter_objd;
+
+counter_error:
        {
-               int close_ret;
+               int err;
 
-               close_ret = close(wakeup_fd);
-               if (close_ret) {
-                       PERROR("close");
-               }
+               err = lttng_ust_objd_unref(counter_objd, 1);
+               assert(!err);
        }
-       free(chan_data);
+objd_error:
        return ret;
 }
 
+
 /**
  *     lttng_session_cmd - lttng session object command
  *
@@ -574,6 +711,14 @@ long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
                return lttng_session_disable(session);
        case LTTNG_UST_SESSION_STATEDUMP:
                return lttng_session_statedump(session);
+       case LTTNG_UST_COUNTER:
+               return lttng_abi_session_create_counter(objd,
+                       (struct lttng_ust_counter *) arg,
+                       uargs, owner);
+       case LTTNG_UST_COUNTER_GLOBAL:
+       case LTTNG_UST_COUNTER_CPU:
+               /* Not implemented yet. */
+               return -EINVAL;
        default:
                return -EINVAL;
        }
@@ -605,6 +750,289 @@ static const struct lttng_ust_objd_ops lttng_session_ops = {
        .cmd = lttng_session_cmd,
 };
 
+static int lttng_ust_event_notifier_enabler_create(int event_notifier_group_obj,
+               void *owner, struct lttng_ust_event_notifier *event_notifier_param,
+               enum lttng_enabler_format_type type)
+{
+       struct lttng_event_notifier_group *event_notifier_group =
+               objd_private(event_notifier_group_obj);
+       struct lttng_event_notifier_enabler *event_notifier_enabler;
+       int event_notifier_objd, ret;
+
+       event_notifier_param->event.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
+       event_notifier_objd = objd_alloc(NULL, &lttng_event_notifier_enabler_ops, owner,
+               "event_notifier enabler");
+       if (event_notifier_objd < 0) {
+               ret = event_notifier_objd;
+               goto objd_error;
+       }
+
+       event_notifier_enabler = lttng_event_notifier_enabler_create(
+               event_notifier_group, type, event_notifier_param);
+       if (!event_notifier_enabler) {
+               ret = -ENOMEM;
+               goto event_notifier_error;
+       }
+
+       objd_set_private(event_notifier_objd, event_notifier_enabler);
+       /* The event_notifier holds a reference on the event_notifier group. */
+       objd_ref(event_notifier_enabler->group->objd);
+
+       return event_notifier_objd;
+
+event_notifier_error:
+       {
+               int err;
+
+               err = lttng_ust_objd_unref(event_notifier_objd, 1);
+               assert(!err);
+       }
+objd_error:
+       return ret;
+}
+
+static
+long lttng_event_notifier_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
+               union ust_args *uargs, void *owner)
+{
+       struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd);
+       switch (cmd) {
+       case LTTNG_UST_FILTER:
+               return lttng_event_notifier_enabler_attach_filter_bytecode(
+                       event_notifier_enabler,
+                       (struct lttng_ust_bytecode_node **) arg);
+       case LTTNG_UST_EXCLUSION:
+               return lttng_event_notifier_enabler_attach_exclusion(event_notifier_enabler,
+                       (struct lttng_ust_excluder_node **) arg);
+       case LTTNG_UST_CAPTURE:
+               return lttng_event_notifier_enabler_attach_capture_bytecode(
+                       event_notifier_enabler,
+                       (struct lttng_ust_bytecode_node **) arg);
+       case LTTNG_UST_ENABLE:
+               return lttng_event_notifier_enabler_enable(event_notifier_enabler);
+       case LTTNG_UST_DISABLE:
+               return lttng_event_notifier_enabler_disable(event_notifier_enabler);
+       default:
+               return -EINVAL;
+       }
+}
+
+/**
+ *     lttng_event_notifier_group_error_counter_cmd - lttng event_notifier group error counter object command
+ *
+ *     @obj: the object
+ *     @cmd: the command
+ *     @arg: command arg
+ *     @uargs: UST arguments (internal)
+ *     @owner: objd owner
+ *
+ *     This descriptor implements lttng commands:
+ *      LTTNG_UST_COUNTER_GLOBAL
+ *        Return negative error code on error, 0 on success.
+ *      LTTNG_UST_COUNTER_CPU
+ *        Return negative error code on error, 0 on success.
+ */
+static
+long lttng_event_notifier_group_error_counter_cmd(int objd, unsigned int cmd, unsigned long arg,
+       union ust_args *uargs, void *owner)
+{
+       int ret;
+       struct lttng_counter *counter = objd_private(objd);
+
+       switch (cmd) {
+       case LTTNG_UST_COUNTER_GLOBAL:
+               ret = -EINVAL;  /* Unimplemented. */
+               break;
+       case LTTNG_UST_COUNTER_CPU:
+       {
+               struct lttng_ust_counter_cpu *counter_cpu =
+                       (struct lttng_ust_counter_cpu *)arg;
+
+               ret = lttng_counter_set_cpu_shm(counter->counter,
+                       counter_cpu->cpu_nr, uargs->counter_shm.shm_fd);
+               if (!ret) {
+                       /* Take ownership of the shm_fd. */
+                       uargs->counter_shm.shm_fd = -1;
+               }
+               break;
+       }
+       default:
+               ret = -EINVAL;
+               break;
+       }
+
+       return ret;
+}
+
+LTTNG_HIDDEN
+int lttng_release_event_notifier_group_error_counter(int objd)
+{
+       struct lttng_counter *counter = objd_private(objd);
+
+       if (counter) {
+               return lttng_ust_objd_unref(counter->owner.event_notifier_group->objd, 0);
+       } else {
+               return -EINVAL;
+       }
+}
+
+static const struct lttng_ust_objd_ops lttng_event_notifier_group_error_counter_ops = {
+       .release = lttng_release_event_notifier_group_error_counter,
+       .cmd = lttng_event_notifier_group_error_counter_cmd,
+};
+
+static
+int lttng_ust_event_notifier_group_create_error_counter(int event_notifier_group_objd, void *owner,
+               struct lttng_ust_counter_conf *error_counter_conf)
+{
+       const char *counter_transport_name;
+       struct lttng_event_notifier_group *event_notifier_group =
+               objd_private(event_notifier_group_objd);
+       struct lttng_counter *counter;
+       struct lttng_event_container *container;
+       int counter_objd, ret;
+       size_t counter_len;
+
+       if (event_notifier_group->error_counter)
+               return -EBUSY;
+
+       if (error_counter_conf->arithmetic != LTTNG_UST_COUNTER_ARITHMETIC_MODULAR)
+               return -EINVAL;
+
+       if (error_counter_conf->number_dimensions != 1)
+               return -EINVAL;
+
+       if (error_counter_conf->dimensions[0].has_underflow)
+               return -EINVAL;
+
+       if (error_counter_conf->dimensions[0].has_overflow)
+               return -EINVAL;
+
+       switch (error_counter_conf->bitness) {
+       case LTTNG_UST_COUNTER_BITNESS_64:
+               counter_transport_name = "counter-per-cpu-64-modular";
+               break;
+       case LTTNG_UST_COUNTER_BITNESS_32:
+               counter_transport_name = "counter-per-cpu-32-modular";
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       counter_objd = objd_alloc(NULL, &lttng_event_notifier_group_error_counter_ops, owner,
+               "event_notifier group error counter");
+       if (counter_objd < 0) {
+               ret = counter_objd;
+               goto objd_error;
+       }
+
+       counter_len = error_counter_conf->dimensions[0].size;
+       counter = lttng_ust_counter_create(counter_transport_name, 1, &counter_len, 0, false);
+       if (!counter) {
+               ret = -EINVAL;
+               goto create_error;
+       }
+       container = lttng_counter_get_event_container(counter);
+
+       event_notifier_group->error_counter_len = counter_len;
+       /*
+        * store-release to publish error counter matches load-acquire
+        * in record_error. Ensures the counter is created and the
+        * error_counter_len is set before they are used.
+        * Currently a full memory barrier is used, which could be
+        * turned into acquire-release barriers.
+        */
+       cmm_smp_mb();
+       CMM_STORE_SHARED(event_notifier_group->error_counter, counter);
+
+       container->objd = counter_objd;
+       counter->owner.event_notifier_group = event_notifier_group;     /* owner */
+
+       objd_set_private(counter_objd, counter);
+       /* The error counter holds a reference on the event_notifier group. */
+       objd_ref(event_notifier_group->objd);
+
+       return counter_objd;
+
+create_error:
+       {
+               int err;
+
+               err = lttng_ust_objd_unref(counter_objd, 1);
+               assert(!err);
+       }
+objd_error:
+       return ret;
+}
+
+static
+long lttng_event_notifier_group_cmd(int objd, unsigned int cmd, unsigned long arg,
+               union ust_args *uargs, void *owner)
+{
+       switch (cmd) {
+       case LTTNG_UST_EVENT_NOTIFIER_CREATE:
+       {
+               struct lttng_ust_event_notifier *event_notifier_param =
+                       (struct lttng_ust_event_notifier *) arg;
+               if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
+                       /*
+                        * If the event name is a star globbing pattern,
+                        * we create the special star globbing enabler.
+                        */
+                       return lttng_ust_event_notifier_enabler_create(objd,
+                                       owner, event_notifier_param,
+                                       LTTNG_ENABLER_FORMAT_STAR_GLOB);
+               } else {
+                       return lttng_ust_event_notifier_enabler_create(objd,
+                                       owner, event_notifier_param,
+                                       LTTNG_ENABLER_FORMAT_EVENT);
+               }
+       }
+       case LTTNG_UST_COUNTER:
+       {
+               struct lttng_ust_counter_conf *counter_conf =
+                       (struct lttng_ust_counter_conf *) uargs->counter.counter_data;
+               return lttng_ust_event_notifier_group_create_error_counter(
+                               objd, owner, counter_conf);
+       }
+       default:
+               return -EINVAL;
+       }
+}
+
+static
+int lttng_event_notifier_enabler_release(int objd)
+{
+       struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd);
+
+       if (event_notifier_enabler)
+               return lttng_ust_objd_unref(event_notifier_enabler->group->objd, 0);
+       return 0;
+}
+
+static const struct lttng_ust_objd_ops lttng_event_notifier_enabler_ops = {
+       .release = lttng_event_notifier_enabler_release,
+       .cmd = lttng_event_notifier_enabler_cmd,
+};
+
+static
+int lttng_release_event_notifier_group(int objd)
+{
+       struct lttng_event_notifier_group *event_notifier_group = objd_private(objd);
+
+       if (event_notifier_group) {
+               lttng_event_notifier_group_destroy(event_notifier_group);
+               return 0;
+       } else {
+               return -EINVAL;
+       }
+}
+
+static const struct lttng_ust_objd_ops lttng_event_notifier_group_ops = {
+       .release = lttng_release_event_notifier_group,
+       .cmd = lttng_event_notifier_group_cmd,
+};
+
 static
 long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
        union ust_args *uargs, void *owner)
@@ -777,6 +1205,9 @@ int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
                info->stream_nr, info->len);
        if (ret)
                goto error_add_stream;
+       /* Take ownership of shm_fd and wakeup_fd. */
+       uargs->stream.shm_fd = -1;
+       uargs->stream.wakeup_fd = -1;
 
        return 0;
 
@@ -785,17 +1216,19 @@ error_add_stream:
 }
 
 static
-int lttng_abi_create_enabler(int channel_objd,
+int lttng_abi_create_event_enabler(int objd,   /* channel or counter objd */
+                          struct lttng_event_container *container,
                           struct lttng_ust_event *event_param,
+                          struct lttng_ust_counter_key *key_param,
                           void *owner,
-                          enum lttng_enabler_type type)
+                          enum lttng_enabler_format_type format_type)
 {
-       struct lttng_channel *channel = objd_private(channel_objd);
-       struct lttng_enabler *enabler;
+       struct lttng_event_enabler *enabler;
        int event_objd, ret;
 
        event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
-       event_objd = objd_alloc(NULL, &lttng_enabler_ops, owner, "enabler");
+       event_objd = objd_alloc(NULL, &lttng_event_enabler_ops, owner,
+               "event enabler");
        if (event_objd < 0) {
                ret = event_objd;
                goto objd_error;
@@ -804,14 +1237,15 @@ int lttng_abi_create_enabler(int channel_objd,
         * We tolerate no failure path after event creation. It will stay
         * invariant for the rest of the session.
         */
-       enabler = lttng_enabler_create(type, event_param, channel);
+       enabler = lttng_event_enabler_create(format_type, event_param,
+                       key_param, container);
        if (!enabler) {
                ret = -ENOMEM;
                goto event_error;
        }
        objd_set_private(event_objd, enabler);
-       /* The event holds a reference on the channel */
-       objd_ref(channel_objd);
+       /* The event holds a reference on the channel or counter */
+       objd_ref(objd);
        return event_objd;
 
 event_error:
@@ -854,6 +1288,7 @@ long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
        union ust_args *uargs, void *owner)
 {
        struct lttng_channel *channel = objd_private(objd);
+       struct lttng_event_container *container = lttng_channel_get_event_container(channel);
 
        if (cmd != LTTNG_UST_STREAM) {
                /*
@@ -876,13 +1311,17 @@ long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
        {
                struct lttng_ust_event *event_param =
                        (struct lttng_ust_event *) arg;
-               if (event_param->name[strlen(event_param->name) - 1] == '*') {
-                       /* If ends with wildcard, create wildcard. */
-                       return lttng_abi_create_enabler(objd, event_param,
-                                       owner, LTTNG_ENABLER_WILDCARD);
+
+               if (strutils_is_star_glob_pattern(event_param->name)) {
+                       /*
+                        * If the event name is a star globbing pattern,
+                        * we create the special star globbing enabler.
+                        */
+                       return lttng_abi_create_event_enabler(objd, container, event_param, NULL,
+                                       owner, LTTNG_ENABLER_FORMAT_STAR_GLOB);
                } else {
-                       return lttng_abi_create_enabler(objd, event_param,
-                                       owner, LTTNG_ENABLER_EVENT);
+                       return lttng_abi_create_event_enabler(objd, container, event_param, NULL,
+                                       owner, LTTNG_ENABLER_FORMAT_EVENT);
                }
        }
        case LTTNG_UST_CONTEXT:
@@ -890,9 +1329,9 @@ long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
                                (struct lttng_ust_context *) arg, uargs,
                                &channel->ctx, channel->session);
        case LTTNG_UST_ENABLE:
-               return lttng_channel_enable(channel);
+               return lttng_event_container_enable(&channel->parent);
        case LTTNG_UST_DISABLE:
-               return lttng_channel_disable(channel);
+               return lttng_event_container_disable(&channel->parent);
        case LTTNG_UST_FLUSH_BUFFER:
                return channel->ops->flush_buffer(channel->chan, channel->handle);
        default:
@@ -915,6 +1354,121 @@ static const struct lttng_ust_objd_ops lttng_channel_ops = {
        .cmd = lttng_channel_cmd,
 };
 
+/**
+ *     lttng_counter_cmd - lttng control through object descriptors
+ *
+ *     @objd: the object descriptor
+ *     @cmd: the command
+ *     @arg: command arg
+ *     @uargs: UST arguments (internal)
+ *     @owner: objd owner
+ *
+ *     This object descriptor implements lttng commands:
+ *      LTTNG_UST_COUNTER_GLOBAL:
+ *              Returns a global counter object descriptor or failure.
+ *      LTTNG_UST_COUNTER_CPU:
+ *              Returns a per-cpu counter object descriptor or failure.
+ *     LTTNG_UST_COUNTER_EVENT
+ *             Returns an event object descriptor or failure.
+ *     LTTNG_UST_ENABLE
+ *             Enable recording for events in this channel (weak enable)
+ *     LTTNG_UST_DISABLE
+ *             Disable recording for events in this channel (strong disable)
+ *
+ * Counter and event object descriptors also hold a reference on the session.
+ */
+static
+long lttng_counter_cmd(int objd, unsigned int cmd, unsigned long arg,
+       union ust_args *uargs, void *owner)
+{
+       struct lttng_counter *counter = objd_private(objd);
+       struct lttng_event_container *container = lttng_counter_get_event_container(counter);
+
+       if (cmd != LTTNG_UST_COUNTER_GLOBAL && cmd != LTTNG_UST_COUNTER_CPU) {
+               /*
+                * Check if counter received all global/per-cpu objects.
+                */
+               if (!lttng_counter_ready(counter->counter))
+                       return -EPERM;
+       }
+
+       switch (cmd) {
+       case LTTNG_UST_COUNTER_GLOBAL:
+       {
+               long ret;
+               int shm_fd;
+
+               shm_fd = uargs->counter_shm.shm_fd;
+               ret = lttng_counter_set_global_shm(counter->counter, shm_fd);
+               if (!ret) {
+                       /* Take ownership of shm_fd. */
+                       uargs->counter_shm.shm_fd = -1;
+               }
+               return ret;
+       }
+       case LTTNG_UST_COUNTER_CPU:
+       {
+               struct lttng_ust_counter_cpu *counter_cpu =
+                       (struct lttng_ust_counter_cpu *) arg;
+               long ret;
+               int shm_fd;
+
+               shm_fd = uargs->counter_shm.shm_fd;
+               ret = lttng_counter_set_cpu_shm(counter->counter,
+                               counter_cpu->cpu_nr, shm_fd);
+               if (!ret) {
+                       /* Take ownership of shm_fd. */
+                       uargs->counter_shm.shm_fd = -1;
+               }
+               return ret;
+       }
+       case LTTNG_UST_COUNTER_EVENT:
+       {
+               struct lttng_ust_counter_event *counter_event_param =
+                       (struct lttng_ust_counter_event *) arg;
+               struct lttng_ust_event *event_param = &counter_event_param->event;
+               struct lttng_ust_counter_key *key_param = &counter_event_param->key;
+
+               if (strutils_is_star_glob_pattern(event_param->name)) {
+                       /*
+                        * If the event name is a star globbing pattern,
+                        * we create the special star globbing enabler.
+                        */
+                       return lttng_abi_create_event_enabler(objd, container, event_param, key_param,
+                                       owner, LTTNG_ENABLER_FORMAT_STAR_GLOB);
+               } else {
+                       return lttng_abi_create_event_enabler(objd, container, event_param, key_param,
+                                       owner, LTTNG_ENABLER_FORMAT_EVENT);
+               }
+       }
+       case LTTNG_UST_ENABLE:
+               return lttng_event_container_enable(container);
+       case LTTNG_UST_DISABLE:
+               return lttng_event_container_disable(container);
+       default:
+               return -EINVAL;
+       }
+}
+
+
+static
+int lttng_counter_release(int objd)
+{
+       struct lttng_counter *counter = objd_private(objd);
+
+       if (counter) {
+               struct lttng_event_container *container = lttng_counter_get_event_container(counter);
+
+               return lttng_ust_objd_unref(container->session->objd, 0);
+       }
+       return 0;
+}
+
+static const struct lttng_ust_objd_ops lttng_counter_ops = {
+       .release = lttng_counter_release,
+       .cmd = lttng_counter_cmd,
+};
+
 /**
  *     lttng_enabler_cmd - lttng control through object descriptors
  *
@@ -938,33 +1492,33 @@ static const struct lttng_ust_objd_ops lttng_channel_ops = {
  *             Attach exclusions to an enabler.
  */
 static
-long lttng_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
+long lttng_event_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
        union ust_args *uargs, void *owner)
 {
-       struct lttng_enabler *enabler = objd_private(objd);
+       struct lttng_event_enabler *enabler = objd_private(objd);
 
        switch (cmd) {
        case LTTNG_UST_CONTEXT:
-               return lttng_enabler_attach_context(enabler,
+               return lttng_event_enabler_attach_context(enabler,
                                (struct lttng_ust_context *) arg);
        case LTTNG_UST_ENABLE:
-               return lttng_enabler_enable(enabler);
+               return lttng_event_enabler_enable(enabler);
        case LTTNG_UST_DISABLE:
-               return lttng_enabler_disable(enabler);
+               return lttng_event_enabler_disable(enabler);
        case LTTNG_UST_FILTER:
        {
                int ret;
 
-               ret = lttng_enabler_attach_bytecode(enabler,
-                               (struct lttng_ust_filter_bytecode_node *) arg);
+               ret = lttng_event_enabler_attach_filter_bytecode(enabler,
+                               (struct lttng_ust_bytecode_node **) arg);
                if (ret)
                        return ret;
                return 0;
        }
        case LTTNG_UST_EXCLUSION:
        {
-               return lttng_enabler_attach_exclusion(enabler,
-                               (struct lttng_ust_excluder_node *) arg);
+               return lttng_event_enabler_attach_exclusion(enabler,
+                               (struct lttng_ust_excluder_node **) arg);
        }
        default:
                return -EINVAL;
@@ -972,18 +1526,19 @@ long lttng_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
 }
 
 static
-int lttng_enabler_release(int objd)
+int lttng_event_enabler_release(int objd)
 {
-       struct lttng_enabler *enabler = objd_private(objd);
+       struct lttng_event_enabler *event_enabler = objd_private(objd);
+
+       if (event_enabler)
+               return lttng_ust_objd_unref(event_enabler->container->objd, 0);
 
-       if (enabler)
-               return lttng_ust_objd_unref(enabler->chan->objd, 0);
        return 0;
 }
 
-static const struct lttng_ust_objd_ops lttng_enabler_ops = {
-       .release = lttng_enabler_release,
-       .cmd = lttng_enabler_cmd,
+static const struct lttng_ust_objd_ops lttng_event_enabler_ops = {
+       .release = lttng_event_enabler_release,
+       .cmd = lttng_event_enabler_cmd,
 };
 
 void lttng_ust_abi_exit(void)
This page took 0.034214 seconds and 5 git commands to generate.