Refactoring: uprobes: combine common code between recorder and notifier
[lttng-modules.git] / src / probes / lttng-uprobes.c
index b9007146d39fbb3504f287f845890de38f867834..b6531a71e16df3938e364023c9d767319f71f2c6 100644 (file)
@@ -9,13 +9,14 @@
  *
  */
 
-#include <linux/fdtable.h>
+#include <wrapper/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/events-internal.h>
 #include <lttng/tracer.h>
 #include <wrapper/irqflags.h>
 #include <ringbuffer/frontend_types.h>
@@ -27,126 +28,107 @@ int lttng_uprobes_event_handler_pre(struct uprobe_consumer *uc, struct pt_regs *
 {
        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 = {
+       struct lttng_kernel_event_common *event = uprobe_handler->event;
+       struct lttng_kernel_probe_ctx lttng_probe_ctx = {
                .event = event,
                .interruptible = !lttng_regs_irqs_disabled(regs),
        };
-       struct lttng_channel *chan = event->chan;
-       struct lib_ring_buffer_ctx ctx;
-       int ret;
-
        struct {
                unsigned long ip;
        } payload;
 
-       if (unlikely(!LTTNG_READ_ONCE(chan->session->active)))
-               return 0;
-       if (unlikely(!LTTNG_READ_ONCE(chan->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 (event->type) {
+       case LTTNG_KERNEL_EVENT_TYPE_RECORDER:
+       {
+               struct lttng_kernel_event_recorder *event_recorder =
+                       container_of(event, struct lttng_kernel_event_recorder, parent);
+               struct lttng_kernel_channel_buffer *chan = event_recorder->chan;
+
+               if (unlikely(!LTTNG_READ_ONCE(chan->parent.session->active)))
+                       return 0;
+               if (unlikely(!LTTNG_READ_ONCE(chan->parent.enabled)))
+                       return 0;
+               break;
+       }
+       case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER:
+               break;
+       default:
+               WARN_ON_ONCE(1);
+       }
 
-       ret = chan->ops->event_reserve(&ctx, event->id);
-       if (ret < 0)
+       if (unlikely(!LTTNG_READ_ONCE(event->enabled)))
                return 0;
 
-       /* Event payload. */
-       payload.ip = (unsigned long)instruction_pointer(regs);
+       switch (event->type) {
+       case LTTNG_KERNEL_EVENT_TYPE_RECORDER:
+       {
+               struct lttng_kernel_event_recorder *event_recorder =
+                       container_of(event, struct lttng_kernel_event_recorder, parent);
+               struct lttng_kernel_channel_buffer *chan = event_recorder->chan;
+               struct lttng_kernel_ring_buffer_ctx ctx;
+               int ret;
 
-       lib_ring_buffer_align_ctx(&ctx, lttng_alignof(payload));
-       chan->ops->event_write(&ctx, &payload, sizeof(payload));
-       chan->ops->event_commit(&ctx);
-       return 0;
-}
+               lib_ring_buffer_ctx_init(&ctx, event_recorder,
+                       sizeof(payload), lttng_alignof(payload), &lttng_probe_ctx);
 
-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;
+               ret = chan->ops->event_reserve(&ctx);
+               if (ret < 0)
+                       return 0;
 
-       if (unlikely(!READ_ONCE(event_notifier->enabled)))
-               return 0;
+               /* Event payload. */
+               payload.ip = (unsigned long)instruction_pointer(regs);
 
-       event_notifier->send_notification(event_notifier, NULL, NULL);
-       return 0;
-}
-
-/*
- * Create event description.
- */
-static
-int lttng_create_uprobe_event(const char *name, struct lttng_event *event)
-{
-       struct lttng_event_desc *desc;
-       struct lttng_event_field *fields;
-       int ret;
-
-       desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
-       if (!desc)
-               return -ENOMEM;
-       desc->name = kstrdup(name, GFP_KERNEL);
-       if (!desc->name) {
-               ret = -ENOMEM;
-               goto error_str;
+               chan->ops->event_write(&ctx, &payload, sizeof(payload), lttng_alignof(payload));
+               chan->ops->event_commit(&ctx);
+               break;
        }
-
-       desc->nr_fields = 1;
-       desc->fields = fields =
-               kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL);
-
-       if (!desc->fields) {
-               ret = -ENOMEM;
-               goto error_fields;
+       case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER:
+       {
+               struct lttng_kernel_event_notifier *event_notifier =
+                       container_of(event, struct lttng_kernel_event_notifier, parent);
+               struct lttng_kernel_notification_ctx notif_ctx;
+
+               notif_ctx.eval_capture = LTTNG_READ_ONCE(event_notifier->eval_capture);
+               event_notifier->notification_send(event_notifier, NULL, NULL, &notif_ctx);
+               break;
+       }
+       default:
+               WARN_ON_ONCE(1);
        }
-       fields[0].name = "ip";
-       fields[0].type.atype = atype_integer;
-       fields[0].type.u.integer.size = sizeof(unsigned long) * CHAR_BIT;
-       fields[0].type.u.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT;
-       fields[0].type.u.integer.signedness = lttng_is_signed_type(unsigned long);
-       fields[0].type.u.integer.reverse_byte_order = 0;
-       fields[0].type.u.integer.base = 16;
-       fields[0].type.u.integer.encoding = lttng_encode_none;
-
-       desc->owner = THIS_MODULE;
-       event->desc = desc;
-
        return 0;
-
-error_fields:
-       kfree(desc->name);
-error_str:
-       kfree(desc);
-       return ret;
 }
 
+static const struct lttng_kernel_event_field *event_fields[] = {
+       lttng_kernel_static_event_field("ip",
+               lttng_kernel_static_type_integer_from_type(unsigned long, __BYTE_ORDER, 16),
+               false, false, false),
+};
+
+static const struct lttng_kernel_tracepoint_class tp_class = {
+       .nr_fields = ARRAY_SIZE(event_fields),
+       .fields = event_fields,
+};
+
 /*
- * Create event_notifier description.
+ * Create event description.
  */
 static
-int lttng_create_uprobe_event_notifier(const char *name, struct lttng_event_notifier *event_notifier)
+int lttng_create_uprobe_event(const char *name, struct lttng_kernel_event_common *event)
 {
-       struct lttng_event_desc *desc;
+       struct lttng_kernel_event_desc *desc;
        int ret;
 
-       desc = kzalloc(sizeof(*event_notifier->desc), GFP_KERNEL);
+       desc = kzalloc(sizeof(*desc), GFP_KERNEL);
        if (!desc)
                return -ENOMEM;
-       desc->name = kstrdup(name, GFP_KERNEL);
-       if (!desc->name) {
+       desc->tp_class = &tp_class;
+       desc->event_name = kstrdup(name, GFP_KERNEL);
+       if (!desc->event_name) {
                ret = -ENOMEM;
                goto error_str;
        }
-
-       desc->nr_fields = 0;
-
        desc->owner = THIS_MODULE;
-       event_notifier->desc = desc;
+       event->priv->desc = desc;
 
        return 0;
 
@@ -170,7 +152,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;
@@ -189,7 +171,7 @@ error:
 
 static
 int lttng_uprobes_add_callsite(struct lttng_uprobe *uprobe,
-       struct lttng_kernel_event_callsite __user *callsite,
+       struct lttng_kernel_abi_event_callsite __user *callsite,
        int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs),
        void *priv_data)
 {
@@ -208,10 +190,10 @@ int lttng_uprobes_add_callsite(struct lttng_uprobe *uprobe,
                goto end;
        }
 
-       /* Ensure the memory we just allocated don't event_notifier page faults. */
+       /* Ensure the memory we just allocated don't notify page faults. */
        wrapper_vmalloc_sync_mappings();
 
-       uprobe_handler->u.event = priv_data;
+       uprobe_handler->event = priv_data;
        uprobe_handler->up_consumer.handler = handler;
 
        ret = copy_from_user(&uprobe_handler->offset, &callsite->u.uprobe.offset, sizeof(uint64_t));
@@ -239,22 +221,14 @@ end:
        return ret;
 }
 
-int lttng_uprobes_event_add_callsite(struct lttng_event *event,
-       struct lttng_kernel_event_callsite __user *callsite)
+int lttng_uprobes_event_add_callsite(struct lttng_kernel_event_common *event,
+       struct lttng_kernel_abi_event_callsite __user *callsite)
 {
-       return lttng_uprobes_add_callsite(&event->u.uprobe, callsite,
+       return lttng_uprobes_add_callsite(&event->priv->u.uprobe, callsite,
                lttng_uprobes_event_handler_pre, 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)
 {
@@ -274,7 +248,7 @@ inode_error:
        return ret;
 }
 
-int lttng_uprobes_register_event(const char *name, int fd, struct lttng_event *event)
+int lttng_uprobes_register_event(const char *name, int fd, struct lttng_kernel_event_common *event)
 {
        int ret = 0;
 
@@ -282,43 +256,20 @@ int lttng_uprobes_register_event(const char *name, int fd, struct lttng_event *e
        if (ret)
                goto error;
 
-       ret = lttng_uprobes_register(&event->u.uprobe, fd);
+       ret = lttng_uprobes_register(&event->priv->u.uprobe, fd);
        if (ret)
                goto register_error;
 
        return 0;
 
 register_error:
-       kfree(event->desc->name);
-       kfree(event->desc);
+       kfree(event->priv->desc->event_name);
+       kfree(event->priv->desc);
 error:
        return ret;
 }
 EXPORT_SYMBOL_GPL(lttng_uprobes_register_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)
 {
@@ -333,34 +284,27 @@ void lttng_uprobes_unregister(struct inode *inode, struct list_head *head)
                list_del(&iter->node);
                kfree(iter);
        }
-
 }
 
-void lttng_uprobes_unregister_event(struct lttng_event *event)
+void lttng_uprobes_unregister_event(struct lttng_kernel_event_common *event)
 {
-       lttng_uprobes_unregister(event->u.uprobe.inode, &event->u.uprobe.head);
+       lttng_uprobes_unregister(event->priv->u.uprobe.inode, &event->priv->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)
+void lttng_uprobes_destroy_event_private(struct lttng_kernel_event_common *event)
 {
-       iput(event->u.uprobe.inode);
-       kfree(event->desc->name);
-       kfree(event->desc);
+       iput(event->priv->u.uprobe.inode);
+       kfree(event->priv->desc->event_name);
+       kfree(event->priv->desc);
 }
 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_private);
 
-void lttng_uprobes_destroy_event_notifier_private(struct lttng_event_notifier *event_notifier)
+void lttng_uprobes_destroy_event_notifier_private(struct lttng_kernel_event_notifier *event_notifier)
 {
-       iput(event_notifier->u.uprobe.inode);
-       kfree(event_notifier->desc->name);
-       kfree(event_notifier->desc);
+       iput(event_notifier->priv->parent.u.uprobe.inode);
+       kfree(event_notifier->priv->parent.desc->event_name);
+       kfree(event_notifier->priv->parent.desc);
 }
 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_event_notifier_private);
 
This page took 0.027696 seconds and 4 git commands to generate.