SoW-2019-0002: Dynamic Snapshot
[deliverable/lttng-modules.git] / probes / lttng-uprobes.c
index 1f549c2f34bb14660c3513953a5711c9aaddd693..beeaea32986cbef8f7015832d7a81f6653bb33fb 100644 (file)
@@ -1,4 +1,5 @@
-/*
+/* SPDX-License-Identifier: (GPL-2.0 OR LGPL-2.1)
+ *
  * probes/lttng-uprobes.c
  *
  * LTTng uprobes integration module.
@@ -6,25 +7,14 @@
  * Copyright (C) 2013 Yannick Brosseau <yannick.brosseau@gmail.com>
  * Copyright (C) 2009-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
  */
 
 #include <linux/fdtable.h>
+#include <linux/list.h>
 #include <linux/module.h>
 #include <linux/namei.h>
 #include <linux/slab.h>
+#include <linux/uaccess.h>
 #include <lttng-events.h>
 #include <lttng-tracer.h>
 #include <wrapper/irqflags.h>
 #include <wrapper/vmalloc.h>
 
 static
-int lttng_uprobes_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs)
+int lttng_uprobes_event_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs)
 {
-       struct lttng_event *event =
-               container_of(uc, struct lttng_event, u.uprobe.up_consumer);
+       struct lttng_uprobe_handler *uprobe_handler =
+               container_of(uc, struct lttng_uprobe_handler, up_consumer);
+       struct lttng_event *event = uprobe_handler->u.event;
        struct lttng_probe_ctx lttng_probe_ctx = {
                .event = event,
                .interruptible = !lttng_regs_irqs_disabled(regs),
@@ -49,11 +40,11 @@ int lttng_uprobes_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs)
                unsigned long ip;
        } payload;
 
-       if (unlikely(!ACCESS_ONCE(chan->session->active)))
+       if (unlikely(!READ_ONCE(chan->session->active)))
                return 0;
-       if (unlikely(!ACCESS_ONCE(chan->enabled)))
+       if (unlikely(!READ_ONCE(chan->enabled)))
                return 0;
-       if (unlikely(!ACCESS_ONCE(event->enabled)))
+       if (unlikely(!READ_ONCE(event->enabled)))
                return 0;
 
        lib_ring_buffer_ctx_init(&ctx, chan->chan, &lttng_probe_ctx,
@@ -64,7 +55,7 @@ int lttng_uprobes_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs)
                return 0;
 
        /* Event payload. */
-       payload.ip = regs->ip;
+       payload.ip = (unsigned long)instruction_pointer(regs);
 
        lib_ring_buffer_align_ctx(&ctx, lttng_alignof(payload));
        chan->ops->event_write(&ctx, &payload, sizeof(payload));
@@ -72,6 +63,20 @@ int lttng_uprobes_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs)
        return 0;
 }
 
+static
+int lttng_uprobes_trigger_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_trigger *trigger = uprobe_handler->u.trigger;
+
+       if (unlikely(!READ_ONCE(trigger->enabled)))
+               return 0;
+
+       trigger->send_notification(trigger);
+       return 0;
+}
+
 /*
  * Create event description.
  */
@@ -120,6 +125,36 @@ error_str:
        return ret;
 }
 
+/*
+ * Create trigger description.
+ */
+static
+int lttng_create_uprobe_trigger(const char *name, struct lttng_trigger *trigger)
+{
+       struct lttng_event_desc *desc;
+       int ret;
+
+       desc = kzalloc(sizeof(*trigger->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;
+       trigger->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
@@ -151,17 +186,80 @@ error:
        return inode;
 }
 
-int lttng_uprobes_register(const char *name,
-                          int fd,
-                          uint64_t offset,
-                          struct lttng_event *event)
+
+static
+int lttng_uprobes_add_callsite(struct lttng_uprobe *uprobe,
+       struct lttng_kernel_event_callsite __user *callsite,
+       int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs),
+       void *priv_data)
 {
-       int ret;
-       struct inode *inode;
+       int ret = 0;
+       struct lttng_uprobe_handler *uprobe_handler;
 
-       ret = lttng_create_uprobe_event(name, event);
-       if (ret)
-               goto error;
+       if (!priv_data) {
+               ret = -EINVAL;
+               goto end;
+       }
+
+       uprobe_handler = kzalloc(sizeof(struct lttng_uprobe_handler), GFP_KERNEL);
+       if (!uprobe_handler) {
+               printk(KERN_WARNING "Error allocating uprobe_uprobe_handlers");
+               ret = -ENOMEM;
+               goto end;
+       }
+
+       /* Ensure the memory we just allocated don't trigger page faults. */
+       wrapper_vmalloc_sync_all();
+
+       uprobe_handler->u.event = priv_data;
+       uprobe_handler->up_consumer.handler = handler;
+
+       ret = copy_from_user(&uprobe_handler->offset, &callsite->u.uprobe.offset, sizeof(uint64_t));
+       if (ret) {
+               goto register_error;
+       }
+
+       ret = wrapper_uprobe_register(uprobe->inode,
+                     uprobe_handler->offset, &uprobe_handler->up_consumer);
+       if (ret) {
+               printk(KERN_WARNING "Error registering probe on inode %lu "
+                      "and offset 0x%llx\n", uprobe->inode->i_ino,
+                      uprobe_handler->offset);
+               ret = -1;
+               goto register_error;
+       }
+
+       list_add(&uprobe_handler->node, &uprobe->head);
+
+       return ret;
+
+register_error:
+       kfree(uprobe_handler);
+end:
+       return ret;
+}
+
+int lttng_uprobes_event_add_callsite(struct lttng_event *event,
+       struct lttng_kernel_event_callsite __user *callsite)
+{
+       return lttng_uprobes_add_callsite(&event->u.uprobe, callsite,
+               lttng_uprobes_event_handler_pre, event);
+}
+EXPORT_SYMBOL_GPL(lttng_uprobes_event_add_callsite);
+
+int lttng_uprobes_trigger_add_callsite(struct lttng_trigger *trigger,
+       struct lttng_kernel_event_callsite __user *callsite)
+{
+       return lttng_uprobes_add_callsite(&trigger->u.uprobe, callsite,
+               lttng_uprobes_trigger_handler_pre, trigger);
+}
+EXPORT_SYMBOL_GPL(lttng_uprobes_trigger_add_callsite);
+
+static
+int lttng_uprobes_register(struct lttng_uprobe *uprobe, int fd)
+{
+       int ret = 0;
+       struct inode *inode;
 
        inode = get_inode_from_fd(fd);
        if (!inode) {
@@ -169,52 +267,102 @@ int lttng_uprobes_register(const char *name,
                ret = -EBADF;
                goto inode_error;
        }
+       uprobe->inode = inode;
+       INIT_LIST_HEAD(&uprobe->head);
 
-       memset(&event->u.uprobe.up_consumer, 0,
-              sizeof(event->u.uprobe.up_consumer));
+inode_error:
+       return ret;
+}
 
-       event->u.uprobe.up_consumer.handler = lttng_uprobes_handler_pre;
-       event->u.uprobe.inode = inode;
-       event->u.uprobe.offset = offset;
+int lttng_uprobes_register_event(const char *name, int fd, struct lttng_event *event)
+{
+       int ret = 0;
 
-        /* Ensure the memory we just allocated don't trigger page faults. */
-       wrapper_vmalloc_sync_all();
-       ret = wrapper_uprobe_register(event->u.uprobe.inode,
-                       event->u.uprobe.offset,
-                       &event->u.uprobe.up_consumer);
-       if (ret) {
-               printk(KERN_WARNING "Error registering probe on inode %lu "
-                      "and offset %llu\n", event->u.uprobe.inode->i_ino,
-                      event->u.uprobe.offset);
+       ret = lttng_create_uprobe_event(name, event);
+       if (ret)
+               goto error;
+
+       ret = lttng_uprobes_register(&event->u.uprobe, fd);
+       if (ret)
                goto register_error;
-       }
+
        return 0;
 
 register_error:
-       iput(event->u.uprobe.inode);
-inode_error:
        kfree(event->desc->name);
        kfree(event->desc);
 error:
        return ret;
 }
-EXPORT_SYMBOL_GPL(lttng_uprobes_register);
+EXPORT_SYMBOL_GPL(lttng_uprobes_register_event);
 
-void lttng_uprobes_unregister(struct lttng_event *event)
+int lttng_uprobes_register_trigger(const char *name, int fd,
+               struct lttng_trigger *trigger)
 {
-       wrapper_uprobe_unregister(event->u.uprobe.inode,
-                       event->u.uprobe.offset,
-                       &event->u.uprobe.up_consumer);
+       int ret = 0;
+
+       ret = lttng_create_uprobe_trigger(name, trigger);
+       if (ret)
+               goto error;
+
+       ret = lttng_uprobes_register(&trigger->u.uprobe, fd);
+       if (ret)
+               goto register_error;
+
+       return 0;
+
+register_error:
+       kfree(trigger->desc->name);
+       kfree(trigger->desc);
+error:
+       return ret;
+}
+EXPORT_SYMBOL_GPL(lttng_uprobes_register_trigger);
+
+static
+void lttng_uprobes_unregister(struct inode *inode, struct list_head *head)
+{
+       struct lttng_uprobe_handler *iter, *tmp;
+
+       /*
+        * Iterate over the list of handler, remove each handler from the list
+        * and free the struct.
+        */
+       list_for_each_entry_safe(iter, tmp, head, node) {
+               wrapper_uprobe_unregister(inode, iter->offset, &iter->up_consumer);
+               list_del(&iter->node);
+               kfree(iter);
+       }
+
 }
-EXPORT_SYMBOL_GPL(lttng_uprobes_unregister);
 
-void lttng_uprobes_destroy_private(struct lttng_event *event)
+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_trigger(struct lttng_trigger *trigger)
+{
+       lttng_uprobes_unregister(trigger->u.uprobe.inode, &trigger->u.uprobe.head);
+}
+EXPORT_SYMBOL_GPL(lttng_uprobes_unregister_trigger);
+
+void lttng_uprobes_destroy_event_private(struct lttng_event *event)
 {
        iput(event->u.uprobe.inode);
        kfree(event->desc->name);
        kfree(event->desc);
 }
-EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_private);
+EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private);
+
+void lttng_uprobes_destroy_trigger_private(struct lttng_trigger *trigger)
+{
+       iput(trigger->u.uprobe.inode);
+       kfree(trigger->desc->name);
+       kfree(trigger->desc);
+}
+EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_trigger_private);
 
 MODULE_LICENSE("GPL and additional rights");
 MODULE_AUTHOR("Yannick Brosseau");
This page took 0.027459 seconds and 5 git commands to generate.