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/module.h>
26 #include <linux/namei.h>
27 #include <linux/slab.h>
28 #include <lttng-events.h>
29 #include <lttng-tracer.h>
30 #include <wrapper/irqflags.h>
31 #include <wrapper/ringbuffer/frontend_types.h>
32 #include <wrapper/uprobes.h>
33 #include <wrapper/vmalloc.h>
36 int lttng_uprobes_handler_pre(struct uprobe_consumer
*uc
, struct pt_regs
*regs
)
38 struct lttng_event
*event
=
39 container_of(uc
, struct lttng_event
, u
.uprobe
.up_consumer
);
40 struct lttng_probe_ctx lttng_probe_ctx
= {
42 .interruptible
= !lttng_regs_irqs_disabled(regs
),
44 struct lttng_channel
*chan
= event
->chan
;
45 struct lib_ring_buffer_ctx ctx
;
52 if (unlikely(!ACCESS_ONCE(chan
->session
->active
)))
54 if (unlikely(!ACCESS_ONCE(chan
->enabled
)))
56 if (unlikely(!ACCESS_ONCE(event
->enabled
)))
59 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, <tng_probe_ctx
,
60 sizeof(payload
), lttng_alignof(payload
), -1);
62 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
67 payload
.ip
= regs
->ip
;
69 lib_ring_buffer_align_ctx(&ctx
, lttng_alignof(payload
));
70 chan
->ops
->event_write(&ctx
, &payload
, sizeof(payload
));
71 chan
->ops
->event_commit(&ctx
);
76 * Create event description.
79 int lttng_create_uprobe_event(const char *name
, struct lttng_event
*event
)
81 struct lttng_event_desc
*desc
;
82 struct lttng_event_field
*fields
;
85 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
88 desc
->name
= kstrdup(name
, GFP_KERNEL
);
95 desc
->fields
= fields
=
96 kzalloc(1 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
102 fields
[0].name
= "ip";
103 fields
[0].type
.atype
= atype_integer
;
104 fields
[0].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
105 fields
[0].type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
106 fields
[0].type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
107 fields
[0].type
.u
.basic
.integer
.reverse_byte_order
= 0;
108 fields
[0].type
.u
.basic
.integer
.base
= 16;
109 fields
[0].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
111 desc
->owner
= THIS_MODULE
;
124 * Returns the inode struct from the current task and an fd. The inode is
125 * grabbed by this function and must be put once we are done with it using
128 static struct inode
*get_inode_from_fd(int fd
)
135 * Returns the file backing the given fd. Needs to be done inside an RCU
140 printk(KERN_WARNING
"Cannot access file backing the fd(%d)\n", fd
);
145 /* Grab a reference on the inode. */
146 inode
= igrab(file
->f_path
.dentry
->d_inode
);
148 printk(KERN_WARNING
"Cannot grab a reference on the inode.\n");
154 int lttng_uprobes_register(const char *name
,
157 struct lttng_event
*event
)
162 ret
= lttng_create_uprobe_event(name
, event
);
166 inode
= get_inode_from_fd(fd
);
168 printk(KERN_WARNING
"Cannot get inode from fd\n");
173 memset(&event
->u
.uprobe
.up_consumer
, 0,
174 sizeof(event
->u
.uprobe
.up_consumer
));
176 event
->u
.uprobe
.up_consumer
.handler
= lttng_uprobes_handler_pre
;
177 event
->u
.uprobe
.inode
= inode
;
178 event
->u
.uprobe
.offset
= offset
;
180 /* Ensure the memory we just allocated don't trigger page faults. */
181 wrapper_vmalloc_sync_all();
182 ret
= wrapper_uprobe_register(event
->u
.uprobe
.inode
,
183 event
->u
.uprobe
.offset
,
184 &event
->u
.uprobe
.up_consumer
);
186 printk(KERN_WARNING
"Error registering probe on inode %lu "
187 "and offset %llu\n", event
->u
.uprobe
.inode
->i_ino
,
188 event
->u
.uprobe
.offset
);
194 iput(event
->u
.uprobe
.inode
);
196 kfree(event
->desc
->name
);
201 EXPORT_SYMBOL_GPL(lttng_uprobes_register
);
203 void lttng_uprobes_unregister(struct lttng_event
*event
)
205 wrapper_uprobe_unregister(event
->u
.uprobe
.inode
,
206 event
->u
.uprobe
.offset
,
207 &event
->u
.uprobe
.up_consumer
);
209 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister
);
211 void lttng_uprobes_destroy_private(struct lttng_event
*event
)
213 iput(event
->u
.uprobe
.inode
);
214 kfree(event
->desc
->name
);
217 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_private
);
219 MODULE_LICENSE("GPL and additional rights");
220 MODULE_AUTHOR("Yannick Brosseau");
221 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");