1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
3 * probes/lttng-uprobes.c
5 * LTTng uprobes integration module.
7 * Copyright (C) 2013 Yannick Brosseau <yannick.brosseau@gmail.com>
8 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 #include <linux/fdtable.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/namei.h>
16 #include <linux/slab.h>
17 #include <linux/uaccess.h>
18 #include <linux/uprobes.h>
19 #include <lttng-events.h>
20 #include <lttng-tracer.h>
21 #include <wrapper/irqflags.h>
22 #include <wrapper/ringbuffer/frontend_types.h>
25 int lttng_uprobes_handler_pre(struct uprobe_consumer
*uc
, struct pt_regs
*regs
)
27 struct lttng_uprobe_handler
*uprobe_handler
=
28 container_of(uc
, struct lttng_uprobe_handler
, up_consumer
);
29 struct lttng_event
*event
= uprobe_handler
->event
;
30 struct lttng_probe_ctx lttng_probe_ctx
= {
32 .interruptible
= !lttng_regs_irqs_disabled(regs
),
34 struct lttng_channel
*chan
= event
->chan
;
35 struct lib_ring_buffer_ctx ctx
;
42 if (unlikely(!READ_ONCE(chan
->session
->active
)))
44 if (unlikely(!READ_ONCE(chan
->enabled
)))
46 if (unlikely(!READ_ONCE(event
->enabled
)))
49 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, <tng_probe_ctx
,
50 sizeof(payload
), lttng_alignof(payload
), -1);
52 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
57 payload
.ip
= (unsigned long)instruction_pointer(regs
);
59 lib_ring_buffer_align_ctx(&ctx
, lttng_alignof(payload
));
60 chan
->ops
->event_write(&ctx
, &payload
, sizeof(payload
));
61 chan
->ops
->event_commit(&ctx
);
66 * Create event description.
69 int lttng_create_uprobe_event(const char *name
, struct lttng_event
*event
)
71 struct lttng_event_desc
*desc
;
72 struct lttng_event_field
*fields
;
75 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
78 desc
->name
= kstrdup(name
, GFP_KERNEL
);
85 desc
->fields
= fields
=
86 kzalloc(1 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
92 fields
[0].name
= "ip";
93 fields
[0].type
.atype
= atype_integer
;
94 fields
[0].type
.u
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
95 fields
[0].type
.u
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
96 fields
[0].type
.u
.integer
.signedness
= lttng_is_signed_type(unsigned long);
97 fields
[0].type
.u
.integer
.reverse_byte_order
= 0;
98 fields
[0].type
.u
.integer
.base
= 16;
99 fields
[0].type
.u
.integer
.encoding
= lttng_encode_none
;
101 desc
->owner
= THIS_MODULE
;
114 * Returns the inode struct from the current task and an fd. The inode is
115 * grabbed by this function and must be put once we are done with it using
118 static struct inode
*get_inode_from_fd(int fd
)
125 * Returns the file backing the given fd. Needs to be done inside an RCU
130 printk(KERN_WARNING
"Cannot access file backing the fd(%d)\n", fd
);
135 /* Grab a reference on the inode. */
136 inode
= igrab(file
->f_path
.dentry
->d_inode
);
138 printk(KERN_WARNING
"Cannot grab a reference on the inode.\n");
144 int lttng_uprobes_add_callsite(struct lttng_event
*event
,
145 struct lttng_kernel_event_callsite __user
*callsite
)
148 struct lttng_uprobe_handler
*uprobe_handler
;
155 uprobe_handler
= kzalloc(sizeof(struct lttng_uprobe_handler
), GFP_KERNEL
);
156 if (!uprobe_handler
) {
157 printk(KERN_WARNING
"Error allocating uprobe_uprobe_handlers");
162 uprobe_handler
->event
= event
;
163 uprobe_handler
->up_consumer
.handler
= lttng_uprobes_handler_pre
;
165 ret
= copy_from_user(&uprobe_handler
->offset
, &callsite
->u
.uprobe
.offset
, sizeof(uint64_t));
170 ret
= uprobe_register(event
->u
.uprobe
.inode
,
171 uprobe_handler
->offset
, &uprobe_handler
->up_consumer
);
173 printk(KERN_WARNING
"Error registering probe on inode %lu "
174 "and offset 0x%llx\n", event
->u
.uprobe
.inode
->i_ino
,
175 uprobe_handler
->offset
);
180 list_add(&uprobe_handler
->node
, &event
->u
.uprobe
.head
);
185 kfree(uprobe_handler
);
189 EXPORT_SYMBOL_GPL(lttng_uprobes_add_callsite
);
191 int lttng_uprobes_register(const char *name
, int fd
, struct lttng_event
*event
)
196 ret
= lttng_create_uprobe_event(name
, event
);
200 inode
= get_inode_from_fd(fd
);
202 printk(KERN_WARNING
"Cannot get inode from fd\n");
206 event
->u
.uprobe
.inode
= inode
;
207 INIT_LIST_HEAD(&event
->u
.uprobe
.head
);
212 kfree(event
->desc
->name
);
217 EXPORT_SYMBOL_GPL(lttng_uprobes_register
);
219 void lttng_uprobes_unregister(struct lttng_event
*event
)
221 struct lttng_uprobe_handler
*iter
, *tmp
;
224 * Iterate over the list of handler, remove each handler from the list
225 * and free the struct.
227 list_for_each_entry_safe(iter
, tmp
, &event
->u
.uprobe
.head
, node
) {
228 uprobe_unregister(event
->u
.uprobe
.inode
, iter
->offset
,
230 list_del(&iter
->node
);
234 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister
);
236 void lttng_uprobes_destroy_private(struct lttng_event
*event
)
238 iput(event
->u
.uprobe
.inode
);
239 kfree(event
->desc
->name
);
242 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_private
);
244 MODULE_LICENSE("GPL and additional rights");
245 MODULE_AUTHOR("Yannick Brosseau");
246 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");