2 * probes/lttng-kretprobes.c
4 * LTTng kretprobes integration module.
6 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <linux/module.h>
24 #include <linux/kprobes.h>
25 #include <linux/slab.h>
26 #include <linux/kref.h>
27 #include "../lttng-events.h"
28 #include "../wrapper/ringbuffer/frontend_types.h"
29 #include "../wrapper/vmalloc.h"
30 #include "../wrapper/irqflags.h"
31 #include "../lttng-tracer.h"
33 enum lttng_kretprobe_type
{
40 struct lttng_event
*event
[2]; /* ENTRY and RETURN */
41 struct kref kref_register
;
42 struct kref kref_alloc
;
46 int _lttng_kretprobes_handler(struct kretprobe_instance
*krpi
,
48 enum lttng_kretprobe_type type
)
50 struct lttng_krp
*lttng_krp
=
51 container_of(krpi
->rp
, struct lttng_krp
, krp
);
52 struct lttng_event
*event
=
53 lttng_krp
->event
[type
];
54 struct lttng_probe_ctx lttng_probe_ctx
= {
56 .interruptible
= !lttng_regs_irqs_disabled(regs
),
58 struct lttng_channel
*chan
= event
->chan
;
59 struct lib_ring_buffer_ctx ctx
;
63 unsigned long parent_ip
;
66 if (unlikely(!ACCESS_ONCE(chan
->session
->active
)))
68 if (unlikely(!ACCESS_ONCE(chan
->enabled
)))
70 if (unlikely(!ACCESS_ONCE(event
->enabled
)))
73 payload
.ip
= (unsigned long) krpi
->rp
->kp
.addr
;
74 payload
.parent_ip
= (unsigned long) krpi
->ret_addr
;
76 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, <tng_probe_ctx
, sizeof(payload
),
77 lttng_alignof(payload
), -1);
78 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
81 lib_ring_buffer_align_ctx(&ctx
, lttng_alignof(payload
));
82 chan
->ops
->event_write(&ctx
, &payload
, sizeof(payload
));
83 chan
->ops
->event_commit(&ctx
);
88 int lttng_kretprobes_handler_entry(struct kretprobe_instance
*krpi
,
91 return _lttng_kretprobes_handler(krpi
, regs
, EVENT_ENTRY
);
95 int lttng_kretprobes_handler_return(struct kretprobe_instance
*krpi
,
98 return _lttng_kretprobes_handler(krpi
, regs
, EVENT_RETURN
);
102 * Create event description
105 int lttng_create_kprobe_event(const char *name
, struct lttng_event
*event
,
106 enum lttng_kretprobe_type type
)
108 struct lttng_event_field
*fields
;
109 struct lttng_event_desc
*desc
;
113 const char *suffix
= NULL
;
115 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
118 name_len
= strlen(name
);
127 name_len
+= strlen(suffix
);
128 alloc_name
= kmalloc(name_len
+ 1, GFP_KERNEL
);
133 strcpy(alloc_name
, name
);
134 strcat(alloc_name
, suffix
);
135 desc
->name
= alloc_name
;
137 desc
->fields
= fields
=
138 kzalloc(2 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
143 fields
[0].name
= "ip";
144 fields
[0].type
.atype
= atype_integer
;
145 fields
[0].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
146 fields
[0].type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
147 fields
[0].type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
148 fields
[0].type
.u
.basic
.integer
.reverse_byte_order
= 0;
149 fields
[0].type
.u
.basic
.integer
.base
= 16;
150 fields
[0].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
152 fields
[1].name
= "parent_ip";
153 fields
[1].type
.atype
= atype_integer
;
154 fields
[1].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
155 fields
[1].type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
156 fields
[1].type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
157 fields
[1].type
.u
.basic
.integer
.reverse_byte_order
= 0;
158 fields
[1].type
.u
.basic
.integer
.base
= 16;
159 fields
[1].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
161 desc
->owner
= THIS_MODULE
;
173 int lttng_kretprobes_register(const char *name
,
174 const char *symbol_name
,
177 struct lttng_event
*event_entry
,
178 struct lttng_event
*event_return
)
181 struct lttng_krp
*lttng_krp
;
183 /* Kprobes expects a NULL symbol name if unused */
184 if (symbol_name
[0] == '\0')
187 ret
= lttng_create_kprobe_event(name
, event_entry
, EVENT_ENTRY
);
190 ret
= lttng_create_kprobe_event(name
, event_return
, EVENT_RETURN
);
192 goto event_return_error
;
193 lttng_krp
= kzalloc(sizeof(*lttng_krp
), GFP_KERNEL
);
196 lttng_krp
->krp
.entry_handler
= lttng_kretprobes_handler_entry
;
197 lttng_krp
->krp
.handler
= lttng_kretprobes_handler_return
;
201 alloc_symbol
= kstrdup(symbol_name
, GFP_KERNEL
);
206 lttng_krp
->krp
.kp
.symbol_name
=
208 event_entry
->u
.kretprobe
.symbol_name
=
210 event_return
->u
.kretprobe
.symbol_name
=
213 lttng_krp
->krp
.kp
.offset
= offset
;
214 lttng_krp
->krp
.kp
.addr
= (void *) (unsigned long) addr
;
216 /* Allow probe handler to find event structures */
217 lttng_krp
->event
[EVENT_ENTRY
] = event_entry
;
218 lttng_krp
->event
[EVENT_RETURN
] = event_return
;
219 event_entry
->u
.kretprobe
.lttng_krp
= lttng_krp
;
220 event_return
->u
.kretprobe
.lttng_krp
= lttng_krp
;
223 * Both events must be unregistered before the kretprobe is
224 * unregistered. Same for memory allocation.
226 kref_init(<tng_krp
->kref_alloc
);
227 kref_get(<tng_krp
->kref_alloc
); /* inc refcount to 2, no overflow. */
228 kref_init(<tng_krp
->kref_register
);
229 kref_get(<tng_krp
->kref_register
); /* inc refcount to 2, no overflow. */
232 * Ensure the memory we just allocated don't trigger page faults.
233 * Well.. kprobes itself puts the page fault handler on the blacklist,
234 * but we can never be too careful.
236 wrapper_vmalloc_sync_all();
238 ret
= register_kretprobe(<tng_krp
->krp
);
244 kfree(lttng_krp
->krp
.kp
.symbol_name
);
248 kfree(event_return
->desc
->fields
);
249 kfree(event_return
->desc
->name
);
250 kfree(event_return
->desc
);
252 kfree(event_entry
->desc
->fields
);
253 kfree(event_entry
->desc
->name
);
254 kfree(event_entry
->desc
);
258 EXPORT_SYMBOL_GPL(lttng_kretprobes_register
);
261 void _lttng_kretprobes_unregister_release(struct kref
*kref
)
263 struct lttng_krp
*lttng_krp
=
264 container_of(kref
, struct lttng_krp
, kref_register
);
265 unregister_kretprobe(<tng_krp
->krp
);
268 void lttng_kretprobes_unregister(struct lttng_event
*event
)
270 kref_put(&event
->u
.kretprobe
.lttng_krp
->kref_register
,
271 _lttng_kretprobes_unregister_release
);
273 EXPORT_SYMBOL_GPL(lttng_kretprobes_unregister
);
276 void _lttng_kretprobes_release(struct kref
*kref
)
278 struct lttng_krp
*lttng_krp
=
279 container_of(kref
, struct lttng_krp
, kref_alloc
);
280 kfree(lttng_krp
->krp
.kp
.symbol_name
);
283 void lttng_kretprobes_destroy_private(struct lttng_event
*event
)
285 kfree(event
->desc
->fields
);
286 kfree(event
->desc
->name
);
288 kref_put(&event
->u
.kretprobe
.lttng_krp
->kref_alloc
,
289 _lttng_kretprobes_release
);
291 EXPORT_SYMBOL_GPL(lttng_kretprobes_destroy_private
);
293 int lttng_kretprobes_event_enable_state(struct lttng_event
*event
,
296 struct lttng_event
*event_return
;
297 struct lttng_krp
*lttng_krp
;
299 if (event
->instrumentation
!= LTTNG_KERNEL_KRETPROBE
) {
302 if (event
->enabled
== enable
) {
305 lttng_krp
= event
->u
.kretprobe
.lttng_krp
;
306 event_return
= lttng_krp
->event
[EVENT_RETURN
];
307 ACCESS_ONCE(event
->enabled
) = enable
;
308 ACCESS_ONCE(event_return
->enabled
) = enable
;
311 EXPORT_SYMBOL_GPL(lttng_kretprobes_event_enable_state
);
313 MODULE_LICENSE("GPL and additional rights");
314 MODULE_AUTHOR("Mathieu Desnoyers");
315 MODULE_DESCRIPTION("Linux Trace Toolkit Kretprobes Support");
316 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
317 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
318 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
319 LTTNG_MODULES_EXTRAVERSION
);