SoW-2020-0002: Trace Hit Counters
[deliverable/lttng-modules.git] / src / probes / lttng-uprobes.c
index d23653202782318473508ec02e0f088861414d63..fbed39eb0befa2d0c2c8f4d1e2315263f5492503 100644 (file)
@@ -9,7 +9,7 @@
  *
  */
 
-#include <linux/fdtable.h>
+#include <wrapper/fdtable.h>
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/namei.h>
@@ -32,34 +32,66 @@ int lttng_uprobes_event_handler_pre(struct uprobe_consumer *uc, struct pt_regs *
                .event = event,
                .interruptible = !lttng_regs_irqs_disabled(regs),
        };
-       struct lttng_channel *chan = event->chan;
-       struct lib_ring_buffer_ctx ctx;
+       struct lttng_event_container *container = event->container;
        int ret;
 
        struct {
                unsigned long ip;
        } payload;
 
-       if (unlikely(!LTTNG_READ_ONCE(chan->session->active)))
+       if (unlikely(!LTTNG_READ_ONCE(container->session->active)))
                return 0;
-       if (unlikely(!LTTNG_READ_ONCE(chan->enabled)))
+       if (unlikely(!LTTNG_READ_ONCE(container->enabled)))
                return 0;
        if (unlikely(!LTTNG_READ_ONCE(event->enabled)))
                return 0;
 
-       lib_ring_buffer_ctx_init(&ctx, chan->chan, &lttng_probe_ctx,
-               sizeof(payload), lttng_alignof(payload), -1);
+       switch (container->type) {
+       case LTTNG_EVENT_CONTAINER_CHANNEL:
+       {
+               struct lttng_channel *chan = lttng_event_container_get_channel(container);
+               struct lib_ring_buffer_ctx ctx;
 
-       ret = chan->ops->event_reserve(&ctx, event->id);
-       if (ret < 0)
-               return 0;
+               lib_ring_buffer_ctx_init(&ctx, chan->chan, &lttng_probe_ctx,
+                       sizeof(payload), lttng_alignof(payload), -1);
+
+               ret = chan->ops->event_reserve(&ctx, event->id);
+               if (ret < 0)
+                       return 0;
 
-       /* Event payload. */
-       payload.ip = (unsigned long)instruction_pointer(regs);
+               /* Event payload. */
+               payload.ip = (unsigned long)instruction_pointer(regs);
 
-       lib_ring_buffer_align_ctx(&ctx, lttng_alignof(payload));
-       chan->ops->event_write(&ctx, &payload, sizeof(payload));
-       chan->ops->event_commit(&ctx);
+               lib_ring_buffer_align_ctx(&ctx, lttng_alignof(payload));
+               chan->ops->event_write(&ctx, &payload, sizeof(payload));
+               chan->ops->event_commit(&ctx);
+               break;
+       }
+       case LTTNG_EVENT_CONTAINER_COUNTER:
+       {
+               struct lttng_counter *counter = lttng_event_container_get_counter(container);
+               size_t index = event->id;
+
+               (void) counter->ops->counter_add(counter->counter, &index, 1);
+               break;
+       }
+       }
+       return 0;
+}
+
+static
+int lttng_uprobes_event_notifier_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs)
+{
+       struct lttng_uprobe_handler *uprobe_handler =
+               container_of(uc, struct lttng_uprobe_handler, up_consumer);
+       struct lttng_event_notifier *event_notifier = uprobe_handler->u.event_notifier;
+       struct lttng_kernel_notifier_ctx notif_ctx;
+
+       if (unlikely(!READ_ONCE(event_notifier->enabled)))
+               return 0;
+
+       notif_ctx.eval_capture = LTTNG_READ_ONCE(event_notifier->eval_capture);
+       event_notifier->send_notification(event_notifier, NULL, NULL, &notif_ctx);
        return 0;
 }
 
@@ -111,6 +143,36 @@ error_str:
        return ret;
 }
 
+/*
+ * Create event_notifier description.
+ */
+static
+int lttng_create_uprobe_event_notifier(const char *name, struct lttng_event_notifier *event_notifier)
+{
+       struct lttng_event_desc *desc;
+       int ret;
+
+       desc = kzalloc(sizeof(*event_notifier->desc), GFP_KERNEL);
+       if (!desc)
+               return -ENOMEM;
+       desc->name = kstrdup(name, GFP_KERNEL);
+       if (!desc->name) {
+               ret = -ENOMEM;
+               goto error_str;
+       }
+
+       desc->nr_fields = 0;
+
+       desc->owner = THIS_MODULE;
+       event_notifier->desc = desc;
+
+       return 0;
+
+error_str:
+       kfree(desc);
+       return ret;
+}
+
 /*
  * Returns the inode struct from the current task and an fd. The inode is
  * grabbed by this function and must be put once we are done with it using
@@ -126,7 +188,7 @@ static struct inode *get_inode_from_fd(int fd)
         * Returns the file backing the given fd. Needs to be done inside an RCU
         * critical section.
         */
-       file = fcheck(fd);
+       file = lttng_lookup_fd_rcu(fd);
        if (file == NULL) {
                printk(KERN_WARNING "LTTng: Cannot access file backing the fd(%d)\n", fd);
                inode = NULL;
@@ -164,7 +226,7 @@ int lttng_uprobes_add_callsite(struct lttng_uprobe *uprobe,
                goto end;
        }
 
-       /* Ensure the memory we just allocated don't trigger page faults. */
+       /* Ensure the memory we just allocated don't event_notifier page faults. */
        wrapper_vmalloc_sync_mappings();
 
        uprobe_handler->u.event = priv_data;
@@ -203,6 +265,14 @@ int lttng_uprobes_event_add_callsite(struct lttng_event *event,
 }
 EXPORT_SYMBOL_GPL(lttng_uprobes_event_add_callsite);
 
+int lttng_uprobes_event_notifier_add_callsite(struct lttng_event_notifier *event_notifier,
+       struct lttng_kernel_event_callsite __user *callsite)
+{
+       return lttng_uprobes_add_callsite(&event_notifier->u.uprobe, callsite,
+               lttng_uprobes_event_notifier_handler_pre, event_notifier);
+}
+EXPORT_SYMBOL_GPL(lttng_uprobes_event_notifier_add_callsite);
+
 static
 int lttng_uprobes_register(struct lttng_uprobe *uprobe, int fd)
 {
@@ -244,7 +314,31 @@ error:
 }
 EXPORT_SYMBOL_GPL(lttng_uprobes_register_event);
 
-void lttng_uprobes_unregister_event(struct lttng_event *event)
+int lttng_uprobes_register_event_notifier(const char *name, int fd,
+               struct lttng_event_notifier *event_notifier)
+{
+       int ret = 0;
+
+       ret = lttng_create_uprobe_event_notifier(name, event_notifier);
+       if (ret)
+               goto error;
+
+       ret = lttng_uprobes_register(&event_notifier->u.uprobe, fd);
+       if (ret)
+               goto register_error;
+
+       return 0;
+
+register_error:
+       kfree(event_notifier->desc->name);
+       kfree(event_notifier->desc);
+error:
+       return ret;
+}
+EXPORT_SYMBOL_GPL(lttng_uprobes_register_event_notifier);
+
+static
+void lttng_uprobes_unregister(struct inode *inode, struct list_head *head)
 {
        struct lttng_uprobe_handler *iter, *tmp;
 
@@ -252,15 +346,26 @@ void lttng_uprobes_unregister_event(struct lttng_event *event)
         * Iterate over the list of handler, remove each handler from the list
         * and free the struct.
         */
-       list_for_each_entry_safe(iter, tmp, &event->u.uprobe.head, node) {
-               wrapper_uprobe_unregister(event->u.uprobe.inode, iter->offset,
-                       &iter->up_consumer);
+       list_for_each_entry_safe(iter, tmp, head, node) {
+               wrapper_uprobe_unregister(inode, iter->offset, &iter->up_consumer);
                list_del(&iter->node);
                kfree(iter);
        }
+
+}
+
+void lttng_uprobes_unregister_event(struct lttng_event *event)
+{
+       lttng_uprobes_unregister(event->u.uprobe.inode, &event->u.uprobe.head);
 }
 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event);
 
+void lttng_uprobes_unregister_event_notifier(struct lttng_event_notifier *event_notifier)
+{
+       lttng_uprobes_unregister(event_notifier->u.uprobe.inode, &event_notifier->u.uprobe.head);
+}
+EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_event_notifier);
+
 void lttng_uprobes_destroy_event_private(struct lttng_event *event)
 {
        iput(event->u.uprobe.inode);
@@ -269,6 +374,14 @@ void lttng_uprobes_destroy_event_private(struct lttng_event *event)
 }
 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private);
 
+void lttng_uprobes_destroy_event_notifier_private(struct lttng_event_notifier *event_notifier)
+{
+       iput(event_notifier->u.uprobe.inode);
+       kfree(event_notifier->desc->name);
+       kfree(event_notifier->desc);
+}
+EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_notifier_private);
+
 MODULE_LICENSE("GPL and additional rights");
 MODULE_AUTHOR("Yannick Brosseau");
 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");
This page took 0.028011 seconds and 5 git commands to generate.