1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
3 * probes/lttng-kretprobes.c
5 * LTTng kretprobes integration module.
7 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <linux/module.h>
11 #include <linux/kprobes.h>
12 #include <linux/slab.h>
13 #include <linux/kref.h>
14 #include <lttng-events.h>
15 #include <wrapper/ringbuffer/frontend_types.h>
16 #include <wrapper/vmalloc.h>
17 #include <wrapper/irqflags.h>
18 #include <lttng-tracer.h>
19 #include <blacklist/kprobes.h>
21 enum lttng_kretprobe_type
{
28 struct lttng_event
*event
[2]; /* ENTRY and RETURN */
29 struct kref kref_register
;
30 struct kref kref_alloc
;
34 int _lttng_kretprobes_handler(struct kretprobe_instance
*krpi
,
36 enum lttng_kretprobe_type type
)
38 struct lttng_krp
*lttng_krp
=
39 container_of(krpi
->rp
, struct lttng_krp
, krp
);
40 struct lttng_event
*event
=
41 lttng_krp
->event
[type
];
42 struct lttng_probe_ctx lttng_probe_ctx
= {
44 .interruptible
= !lttng_regs_irqs_disabled(regs
),
46 struct lttng_channel
*chan
= event
->chan
;
47 struct lib_ring_buffer_ctx ctx
;
51 unsigned long parent_ip
;
54 if (unlikely(!READ_ONCE(chan
->session
->active
)))
56 if (unlikely(!READ_ONCE(chan
->enabled
)))
58 if (unlikely(!READ_ONCE(event
->enabled
)))
61 payload
.ip
= (unsigned long) krpi
->rp
->kp
.addr
;
62 payload
.parent_ip
= (unsigned long) krpi
->ret_addr
;
64 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, <tng_probe_ctx
, sizeof(payload
),
65 lttng_alignof(payload
), -1);
66 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
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 int lttng_kretprobes_handler_entry(struct kretprobe_instance
*krpi
,
79 return _lttng_kretprobes_handler(krpi
, regs
, EVENT_ENTRY
);
83 int lttng_kretprobes_handler_return(struct kretprobe_instance
*krpi
,
86 return _lttng_kretprobes_handler(krpi
, regs
, EVENT_RETURN
);
90 * Create event description
93 int lttng_create_kprobe_event(const char *name
, struct lttng_event
*event
,
94 enum lttng_kretprobe_type type
)
96 struct lttng_event_field
*fields
;
97 struct lttng_event_desc
*desc
;
101 const char *suffix
= NULL
;
103 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
106 name_len
= strlen(name
);
115 name_len
+= strlen(suffix
);
116 alloc_name
= kmalloc(name_len
+ 1, GFP_KERNEL
);
121 strcpy(alloc_name
, name
);
122 strcat(alloc_name
, suffix
);
123 desc
->name
= alloc_name
;
125 desc
->fields
= fields
=
126 kzalloc(2 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
131 fields
[0].name
= "ip";
132 fields
[0].type
.atype
= atype_integer
;
133 fields
[0].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
134 fields
[0].type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
135 fields
[0].type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
136 fields
[0].type
.u
.basic
.integer
.reverse_byte_order
= 0;
137 fields
[0].type
.u
.basic
.integer
.base
= 16;
138 fields
[0].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
140 fields
[1].name
= "parent_ip";
141 fields
[1].type
.atype
= atype_integer
;
142 fields
[1].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
143 fields
[1].type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
144 fields
[1].type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
145 fields
[1].type
.u
.basic
.integer
.reverse_byte_order
= 0;
146 fields
[1].type
.u
.basic
.integer
.base
= 16;
147 fields
[1].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
149 desc
->owner
= THIS_MODULE
;
161 int lttng_kretprobes_register(const char *name
,
162 const char *symbol_name
,
165 struct lttng_event
*event_entry
,
166 struct lttng_event
*event_return
)
169 struct lttng_krp
*lttng_krp
;
171 /* Kprobes expects a NULL symbol name if unused */
172 if (symbol_name
[0] == '\0')
175 ret
= lttng_create_kprobe_event(name
, event_entry
, EVENT_ENTRY
);
178 ret
= lttng_create_kprobe_event(name
, event_return
, EVENT_RETURN
);
180 goto event_return_error
;
181 lttng_krp
= kzalloc(sizeof(*lttng_krp
), GFP_KERNEL
);
184 lttng_krp
->krp
.entry_handler
= lttng_kretprobes_handler_entry
;
185 lttng_krp
->krp
.handler
= lttng_kretprobes_handler_return
;
189 alloc_symbol
= kstrdup(symbol_name
, GFP_KERNEL
);
194 lttng_krp
->krp
.kp
.symbol_name
=
196 event_entry
->u
.kretprobe
.symbol_name
=
198 event_return
->u
.kretprobe
.symbol_name
=
201 lttng_krp
->krp
.kp
.offset
= offset
;
202 lttng_krp
->krp
.kp
.addr
= (void *) (unsigned long) addr
;
204 /* Allow probe handler to find event structures */
205 lttng_krp
->event
[EVENT_ENTRY
] = event_entry
;
206 lttng_krp
->event
[EVENT_RETURN
] = event_return
;
207 event_entry
->u
.kretprobe
.lttng_krp
= lttng_krp
;
208 event_return
->u
.kretprobe
.lttng_krp
= lttng_krp
;
211 * Both events must be unregistered before the kretprobe is
212 * unregistered. Same for memory allocation.
214 kref_init(<tng_krp
->kref_alloc
);
215 kref_get(<tng_krp
->kref_alloc
); /* inc refcount to 2, no overflow. */
216 kref_init(<tng_krp
->kref_register
);
217 kref_get(<tng_krp
->kref_register
); /* inc refcount to 2, no overflow. */
220 * Ensure the memory we just allocated don't trigger page faults.
221 * Well.. kprobes itself puts the page fault handler on the blacklist,
222 * but we can never be too careful.
224 wrapper_vmalloc_sync_mappings();
226 ret
= register_kretprobe(<tng_krp
->krp
);
232 kfree(lttng_krp
->krp
.kp
.symbol_name
);
236 kfree(event_return
->desc
->fields
);
237 kfree(event_return
->desc
->name
);
238 kfree(event_return
->desc
);
240 kfree(event_entry
->desc
->fields
);
241 kfree(event_entry
->desc
->name
);
242 kfree(event_entry
->desc
);
246 EXPORT_SYMBOL_GPL(lttng_kretprobes_register
);
249 void _lttng_kretprobes_unregister_release(struct kref
*kref
)
251 struct lttng_krp
*lttng_krp
=
252 container_of(kref
, struct lttng_krp
, kref_register
);
253 unregister_kretprobe(<tng_krp
->krp
);
256 void lttng_kretprobes_unregister(struct lttng_event
*event
)
258 kref_put(&event
->u
.kretprobe
.lttng_krp
->kref_register
,
259 _lttng_kretprobes_unregister_release
);
261 EXPORT_SYMBOL_GPL(lttng_kretprobes_unregister
);
264 void _lttng_kretprobes_release(struct kref
*kref
)
266 struct lttng_krp
*lttng_krp
=
267 container_of(kref
, struct lttng_krp
, kref_alloc
);
268 kfree(lttng_krp
->krp
.kp
.symbol_name
);
271 void lttng_kretprobes_destroy_private(struct lttng_event
*event
)
273 kfree(event
->desc
->fields
);
274 kfree(event
->desc
->name
);
276 kref_put(&event
->u
.kretprobe
.lttng_krp
->kref_alloc
,
277 _lttng_kretprobes_release
);
279 EXPORT_SYMBOL_GPL(lttng_kretprobes_destroy_private
);
281 int lttng_kretprobes_event_enable_state(struct lttng_event
*event
,
284 struct lttng_event
*event_return
;
285 struct lttng_krp
*lttng_krp
;
287 if (event
->instrumentation
!= LTTNG_KERNEL_KRETPROBE
) {
290 if (event
->enabled
== enable
) {
293 lttng_krp
= event
->u
.kretprobe
.lttng_krp
;
294 event_return
= lttng_krp
->event
[EVENT_RETURN
];
295 WRITE_ONCE(event
->enabled
, enable
);
296 WRITE_ONCE(event_return
->enabled
, enable
);
299 EXPORT_SYMBOL_GPL(lttng_kretprobes_event_enable_state
);
301 MODULE_LICENSE("GPL and additional rights");
302 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
303 MODULE_DESCRIPTION("LTTng kretprobes probes");
304 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
305 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
306 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
307 LTTNG_MODULES_EXTRAVERSION
);