2 * (C) Copyright 2009-2011 -
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * LTTng kretprobes integration module.
7 * Dual LGPL v2.1/GPL v2 license.
10 #include <linux/module.h>
11 #include <linux/kprobes.h>
12 #include <linux/slab.h>
13 #include <linux/kref.h>
14 #include "../ltt-events.h"
15 #include "../wrapper/ringbuffer/frontend_types.h"
16 #include "../wrapper/vmalloc.h"
17 #include "../ltt-tracer.h"
19 enum lttng_kretprobe_type
{
26 struct ltt_event
*event
[2]; /* ENTRY and RETURN */
27 struct kref kref_register
;
28 struct kref kref_alloc
;
32 int _lttng_kretprobes_handler(struct kretprobe_instance
*krpi
,
34 enum lttng_kretprobe_type type
)
36 struct lttng_krp
*lttng_krp
=
37 container_of(krpi
->rp
, struct lttng_krp
, krp
);
38 struct ltt_event
*event
=
39 lttng_krp
->event
[type
];
40 struct ltt_channel
*chan
= event
->chan
;
41 struct lib_ring_buffer_ctx ctx
;
45 unsigned long parent_ip
;
48 if (unlikely(!ACCESS_ONCE(chan
->session
->active
)))
50 if (unlikely(!ACCESS_ONCE(chan
->enabled
)))
52 if (unlikely(!ACCESS_ONCE(event
->enabled
)))
55 payload
.ip
= (unsigned long) krpi
->rp
->kp
.addr
;
56 payload
.parent_ip
= (unsigned long) krpi
->ret_addr
;
58 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, event
, sizeof(payload
),
59 ltt_alignof(payload
), -1);
60 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
63 lib_ring_buffer_align_ctx(&ctx
, ltt_alignof(payload
));
64 chan
->ops
->event_write(&ctx
, &payload
, sizeof(payload
));
65 chan
->ops
->event_commit(&ctx
);
70 int lttng_kretprobes_handler_entry(struct kretprobe_instance
*krpi
,
73 return _lttng_kretprobes_handler(krpi
, regs
, EVENT_ENTRY
);
77 int lttng_kretprobes_handler_return(struct kretprobe_instance
*krpi
,
80 return _lttng_kretprobes_handler(krpi
, regs
, EVENT_RETURN
);
84 * Create event description
87 int lttng_create_kprobe_event(const char *name
, struct ltt_event
*event
,
88 enum lttng_kretprobe_type type
)
90 struct lttng_event_field
*fields
;
91 struct lttng_event_desc
*desc
;
95 const char *suffix
= NULL
;
97 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
100 name_len
= strlen(name
);
109 name_len
+= strlen(suffix
);
110 alloc_name
= kmalloc(name_len
+ 1, GFP_KERNEL
);
115 strcpy(alloc_name
, name
);
116 strcat(alloc_name
, suffix
);
117 desc
->name
= alloc_name
;
119 desc
->fields
= fields
=
120 kzalloc(2 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
125 fields
[0].name
= "ip";
126 fields
[0].type
.atype
= atype_integer
;
127 fields
[0].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
128 fields
[0].type
.u
.basic
.integer
.alignment
= ltt_alignof(unsigned long) * CHAR_BIT
;
129 fields
[0].type
.u
.basic
.integer
.signedness
= is_signed_type(unsigned long);
130 fields
[0].type
.u
.basic
.integer
.reverse_byte_order
= 0;
131 fields
[0].type
.u
.basic
.integer
.base
= 16;
132 fields
[0].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
134 fields
[1].name
= "parent_ip";
135 fields
[1].type
.atype
= atype_integer
;
136 fields
[1].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
137 fields
[1].type
.u
.basic
.integer
.alignment
= ltt_alignof(unsigned long) * CHAR_BIT
;
138 fields
[1].type
.u
.basic
.integer
.signedness
= is_signed_type(unsigned long);
139 fields
[1].type
.u
.basic
.integer
.reverse_byte_order
= 0;
140 fields
[1].type
.u
.basic
.integer
.base
= 16;
141 fields
[1].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
143 desc
->owner
= THIS_MODULE
;
155 int lttng_kretprobes_register(const char *name
,
156 const char *symbol_name
,
159 struct ltt_event
*event_entry
,
160 struct ltt_event
*event_return
)
163 struct lttng_krp
*lttng_krp
;
165 /* Kprobes expects a NULL symbol name if unused */
166 if (symbol_name
[0] == '\0')
169 ret
= lttng_create_kprobe_event(name
, event_entry
, EVENT_ENTRY
);
172 ret
= lttng_create_kprobe_event(name
, event_return
, EVENT_RETURN
);
174 goto event_return_error
;
175 lttng_krp
= kzalloc(sizeof(*lttng_krp
), GFP_KERNEL
);
178 lttng_krp
->krp
.entry_handler
= lttng_kretprobes_handler_entry
;
179 lttng_krp
->krp
.handler
= lttng_kretprobes_handler_return
;
183 alloc_symbol
= kstrdup(symbol_name
, GFP_KERNEL
);
188 lttng_krp
->krp
.kp
.symbol_name
=
190 event_entry
->u
.kretprobe
.symbol_name
=
192 event_return
->u
.kretprobe
.symbol_name
=
195 lttng_krp
->krp
.kp
.offset
= offset
;
196 lttng_krp
->krp
.kp
.addr
= (void *) (unsigned long) addr
;
198 /* Allow probe handler to find event structures */
199 lttng_krp
->event
[EVENT_ENTRY
] = event_entry
;
200 lttng_krp
->event
[EVENT_RETURN
] = event_return
;
201 event_entry
->u
.kretprobe
.lttng_krp
= lttng_krp
;
202 event_return
->u
.kretprobe
.lttng_krp
= lttng_krp
;
205 * Both events must be unregistered before the kretprobe is
206 * unregistered. Same for memory allocation.
208 kref_init(<tng_krp
->kref_alloc
);
209 kref_get(<tng_krp
->kref_alloc
); /* inc refcount to 2 */
210 kref_init(<tng_krp
->kref_register
);
211 kref_get(<tng_krp
->kref_register
); /* inc refcount to 2 */
214 * Ensure the memory we just allocated don't trigger page faults.
215 * Well.. kprobes itself puts the page fault handler on the blacklist,
216 * but we can never be too careful.
218 wrapper_vmalloc_sync_all();
220 ret
= register_kretprobe(<tng_krp
->krp
);
226 kfree(lttng_krp
->krp
.kp
.symbol_name
);
230 kfree(event_return
->desc
->fields
);
231 kfree(event_return
->desc
->name
);
232 kfree(event_return
->desc
);
234 kfree(event_entry
->desc
->fields
);
235 kfree(event_entry
->desc
->name
);
236 kfree(event_entry
->desc
);
240 EXPORT_SYMBOL_GPL(lttng_kretprobes_register
);
243 void _lttng_kretprobes_unregister_release(struct kref
*kref
)
245 struct lttng_krp
*lttng_krp
=
246 container_of(kref
, struct lttng_krp
, kref_register
);
247 unregister_kretprobe(<tng_krp
->krp
);
250 void lttng_kretprobes_unregister(struct ltt_event
*event
)
252 kref_put(&event
->u
.kretprobe
.lttng_krp
->kref_register
,
253 _lttng_kretprobes_unregister_release
);
255 EXPORT_SYMBOL_GPL(lttng_kretprobes_unregister
);
258 void _lttng_kretprobes_release(struct kref
*kref
)
260 struct lttng_krp
*lttng_krp
=
261 container_of(kref
, struct lttng_krp
, kref_alloc
);
262 kfree(lttng_krp
->krp
.kp
.symbol_name
);
265 void lttng_kretprobes_destroy_private(struct ltt_event
*event
)
267 kfree(event
->desc
->fields
);
268 kfree(event
->desc
->name
);
270 kref_put(&event
->u
.kretprobe
.lttng_krp
->kref_alloc
,
271 _lttng_kretprobes_release
);
273 EXPORT_SYMBOL_GPL(lttng_kretprobes_destroy_private
);
275 MODULE_LICENSE("GPL and additional rights");
276 MODULE_AUTHOR("Mathieu Desnoyers");
277 MODULE_DESCRIPTION("Linux Trace Toolkit Kretprobes Support");