2 * probes/lttng-uprobes.c
4 * LTTng uprobes integration module.
6 * Copyright (C) 2013 Yannick Brosseau <yannick.brosseau@gmail.com>
7 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <linux/fdtable.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/namei.h>
28 #include <linux/slab.h>
29 #include <linux/uaccess.h>
30 #include <lttng-events.h>
31 #include <lttng-tracer.h>
32 #include <wrapper/irqflags.h>
33 #include <wrapper/ringbuffer/frontend_types.h>
34 #include <wrapper/uprobes.h>
35 #include <wrapper/vmalloc.h>
38 int lttng_uprobes_handler_pre(struct uprobe_consumer
*uc
, struct pt_regs
*regs
)
40 struct lttng_uprobe_handler
*uprobe_handler
=
41 container_of(uc
, struct lttng_uprobe_handler
, up_consumer
);
42 struct lttng_event
*event
= uprobe_handler
->event
;
43 struct lttng_probe_ctx lttng_probe_ctx
= {
45 .interruptible
= !lttng_regs_irqs_disabled(regs
),
47 struct lttng_channel
*chan
= event
->chan
;
48 struct lib_ring_buffer_ctx ctx
;
55 if (unlikely(!READ_ONCE(chan
->session
->active
)))
57 if (unlikely(!READ_ONCE(chan
->enabled
)))
59 if (unlikely(!READ_ONCE(event
->enabled
)))
62 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, <tng_probe_ctx
,
63 sizeof(payload
), lttng_alignof(payload
), -1);
65 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
70 payload
.ip
= (unsigned long)instruction_pointer(regs
);
72 lib_ring_buffer_align_ctx(&ctx
, lttng_alignof(payload
));
73 chan
->ops
->event_write(&ctx
, &payload
, sizeof(payload
));
74 chan
->ops
->event_commit(&ctx
);
79 * Create event description.
82 int lttng_create_uprobe_event(const char *name
, struct lttng_event
*event
)
84 struct lttng_event_desc
*desc
;
85 struct lttng_event_field
*fields
;
88 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
91 desc
->name
= kstrdup(name
, GFP_KERNEL
);
98 desc
->fields
= fields
=
99 kzalloc(1 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
105 fields
[0].name
= "ip";
106 fields
[0].type
.atype
= atype_integer
;
107 fields
[0].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
108 fields
[0].type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
109 fields
[0].type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
110 fields
[0].type
.u
.basic
.integer
.reverse_byte_order
= 0;
111 fields
[0].type
.u
.basic
.integer
.base
= 16;
112 fields
[0].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
114 desc
->owner
= THIS_MODULE
;
127 * Returns the inode struct from the current task and an fd. The inode is
128 * grabbed by this function and must be put once we are done with it using
131 static struct inode
*get_inode_from_fd(int fd
)
138 * Returns the file backing the given fd. Needs to be done inside an RCU
143 printk(KERN_WARNING
"Cannot access file backing the fd(%d)\n", fd
);
148 /* Grab a reference on the inode. */
149 inode
= igrab(file
->f_path
.dentry
->d_inode
);
151 printk(KERN_WARNING
"Cannot grab a reference on the inode.\n");
157 int lttng_uprobes_add_callsite(struct lttng_event
*event
,
158 struct lttng_kernel_event_callsite __user
*callsite
)
161 struct lttng_uprobe_handler
*uprobe_handler
;
168 uprobe_handler
= kzalloc(sizeof(struct lttng_uprobe_handler
), GFP_KERNEL
);
169 if (!uprobe_handler
) {
170 printk(KERN_WARNING
"Error allocating uprobe_uprobe_handlers");
175 /* Ensure the memory we just allocated don't trigger page faults. */
176 wrapper_vmalloc_sync_all();
178 uprobe_handler
->event
= event
;
179 uprobe_handler
->up_consumer
.handler
= lttng_uprobes_handler_pre
;
181 ret
= copy_from_user(&uprobe_handler
->offset
, &callsite
->u
.uprobe
.offset
, sizeof(uint64_t));
186 ret
= wrapper_uprobe_register(event
->u
.uprobe
.inode
,
187 uprobe_handler
->offset
, &uprobe_handler
->up_consumer
);
189 printk(KERN_WARNING
"Error registering probe on inode %lu "
190 "and offset 0x%llx\n", event
->u
.uprobe
.inode
->i_ino
,
191 uprobe_handler
->offset
);
196 list_add(&uprobe_handler
->node
, &event
->u
.uprobe
.head
);
201 kfree(uprobe_handler
);
205 EXPORT_SYMBOL_GPL(lttng_uprobes_add_callsite
);
207 int lttng_uprobes_register(const char *name
, int fd
, struct lttng_event
*event
)
212 ret
= lttng_create_uprobe_event(name
, event
);
216 inode
= get_inode_from_fd(fd
);
218 printk(KERN_WARNING
"Cannot get inode from fd\n");
222 event
->u
.uprobe
.inode
= inode
;
223 INIT_LIST_HEAD(&event
->u
.uprobe
.head
);
228 kfree(event
->desc
->name
);
233 EXPORT_SYMBOL_GPL(lttng_uprobes_register
);
235 void lttng_uprobes_unregister(struct lttng_event
*event
)
237 struct lttng_uprobe_handler
*iter
, *tmp
;
240 * Iterate over the list of handler, remove each handler from the list
241 * and free the struct.
243 list_for_each_entry_safe(iter
, tmp
, &event
->u
.uprobe
.head
, node
) {
244 wrapper_uprobe_unregister(event
->u
.uprobe
.inode
, iter
->offset
,
246 list_del(&iter
->node
);
250 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister
);
252 void lttng_uprobes_destroy_private(struct lttng_event
*event
)
254 iput(event
->u
.uprobe
.inode
);
255 kfree(event
->desc
->name
);
258 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_private
);
260 MODULE_LICENSE("GPL and additional rights");
261 MODULE_AUTHOR("Yannick Brosseau");
262 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");