Fix cast warning
[lttng-modules.git] / probes / lttng-kretprobes.c
1 /*
2 * (C) Copyright 2009-2011 -
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * LTTng kretprobes integration module.
6 *
7 * Dual LGPL v2.1/GPL v2 license.
8 */
9
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"
18
19 enum lttng_kretprobe_type {
20 EVENT_ENTRY = 0,
21 EVENT_RETURN = 1,
22 };
23
24 struct lttng_krp {
25 struct kretprobe krp;
26 struct ltt_event *event[2]; /* ENTRY and RETURN */
27 struct kref kref_register;
28 struct kref kref_alloc;
29 };
30
31 static
32 int _lttng_kretprobes_handler(struct kretprobe_instance *krpi,
33 struct pt_regs *regs,
34 enum lttng_kretprobe_type type)
35 {
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;
42 int ret;
43 struct {
44 unsigned long ip;
45 unsigned long parent_ip;
46 } payload;
47
48 if (unlikely(!ACCESS_ONCE(chan->session->active)))
49 return 0;
50 if (unlikely(!ACCESS_ONCE(chan->enabled)))
51 return 0;
52 if (unlikely(!ACCESS_ONCE(event->enabled)))
53 return 0;
54
55 payload.ip = (unsigned long) krpi->rp->kp.addr;
56 payload.parent_ip = (unsigned long) krpi->ret_addr;
57
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);
61 if (ret < 0)
62 return 0;
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);
66 return 0;
67 }
68
69 static
70 int lttng_kretprobes_handler_entry(struct kretprobe_instance *krpi,
71 struct pt_regs *regs)
72 {
73 return _lttng_kretprobes_handler(krpi, regs, EVENT_ENTRY);
74 }
75
76 static
77 int lttng_kretprobes_handler_return(struct kretprobe_instance *krpi,
78 struct pt_regs *regs)
79 {
80 return _lttng_kretprobes_handler(krpi, regs, EVENT_RETURN);
81 }
82
83 /*
84 * Create event description
85 */
86 static
87 int lttng_create_kprobe_event(const char *name, struct ltt_event *event,
88 enum lttng_kretprobe_type type)
89 {
90 struct lttng_event_field *fields;
91 struct lttng_event_desc *desc;
92 int ret;
93 char *alloc_name;
94 size_t name_len;
95 const char *suffix = NULL;
96
97 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
98 if (!desc)
99 return -ENOMEM;
100 name_len = strlen(name);
101 switch (type) {
102 case EVENT_ENTRY:
103 suffix = "_entry";
104 break;
105 case EVENT_RETURN:
106 suffix = "_return";
107 break;
108 }
109 name_len += strlen(suffix);
110 alloc_name = kmalloc(name_len + 1, GFP_KERNEL);
111 if (!alloc_name) {
112 ret = -ENOMEM;
113 goto error_str;
114 }
115 strcpy(alloc_name, name);
116 strcat(alloc_name, suffix);
117 desc->name = alloc_name;
118 desc->nr_fields = 2;
119 desc->fields = fields =
120 kzalloc(2 * sizeof(struct lttng_event_field), GFP_KERNEL);
121 if (!desc->fields) {
122 ret = -ENOMEM;
123 goto error_fields;
124 }
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;
133
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;
142
143 desc->owner = THIS_MODULE;
144 event->desc = desc;
145
146 return 0;
147
148 error_fields:
149 kfree(desc->name);
150 error_str:
151 kfree(desc);
152 return ret;
153 }
154
155 int lttng_kretprobes_register(const char *name,
156 const char *symbol_name,
157 uint64_t offset,
158 uint64_t addr,
159 struct ltt_event *event_entry,
160 struct ltt_event *event_return)
161 {
162 int ret;
163 struct lttng_krp *lttng_krp;
164
165 /* Kprobes expects a NULL symbol name if unused */
166 if (symbol_name[0] == '\0')
167 symbol_name = NULL;
168
169 ret = lttng_create_kprobe_event(name, event_entry, EVENT_ENTRY);
170 if (ret)
171 goto error;
172 ret = lttng_create_kprobe_event(name, event_return, EVENT_RETURN);
173 if (ret)
174 goto event_return_error;
175 lttng_krp = kzalloc(sizeof(*lttng_krp), GFP_KERNEL);
176 if (!lttng_krp)
177 goto krp_error;
178 lttng_krp->krp.entry_handler = lttng_kretprobes_handler_entry;
179 lttng_krp->krp.handler = lttng_kretprobes_handler_return;
180 if (symbol_name) {
181 char *alloc_symbol;
182
183 alloc_symbol = kstrdup(symbol_name, GFP_KERNEL);
184 if (!alloc_symbol) {
185 ret = -ENOMEM;
186 goto name_error;
187 }
188 lttng_krp->krp.kp.symbol_name =
189 alloc_symbol;
190 event_entry->u.kretprobe.symbol_name =
191 alloc_symbol;
192 event_return->u.kretprobe.symbol_name =
193 alloc_symbol;
194 }
195 lttng_krp->krp.kp.offset = offset;
196 lttng_krp->krp.kp.addr = (void *) (unsigned long) addr;
197
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;
203
204 /*
205 * Both events must be unregistered before the kretprobe is
206 * unregistered. Same for memory allocation.
207 */
208 kref_init(&lttng_krp->kref_alloc);
209 kref_get(&lttng_krp->kref_alloc); /* inc refcount to 2 */
210 kref_init(&lttng_krp->kref_register);
211 kref_get(&lttng_krp->kref_register); /* inc refcount to 2 */
212
213 /*
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.
217 */
218 wrapper_vmalloc_sync_all();
219
220 ret = register_kretprobe(&lttng_krp->krp);
221 if (ret)
222 goto register_error;
223 return 0;
224
225 register_error:
226 kfree(lttng_krp->krp.kp.symbol_name);
227 name_error:
228 kfree(lttng_krp);
229 krp_error:
230 kfree(event_return->desc->fields);
231 kfree(event_return->desc->name);
232 kfree(event_return->desc);
233 event_return_error:
234 kfree(event_entry->desc->fields);
235 kfree(event_entry->desc->name);
236 kfree(event_entry->desc);
237 error:
238 return ret;
239 }
240 EXPORT_SYMBOL_GPL(lttng_kretprobes_register);
241
242 static
243 void _lttng_kretprobes_unregister_release(struct kref *kref)
244 {
245 struct lttng_krp *lttng_krp =
246 container_of(kref, struct lttng_krp, kref_register);
247 unregister_kretprobe(&lttng_krp->krp);
248 }
249
250 void lttng_kretprobes_unregister(struct ltt_event *event)
251 {
252 kref_put(&event->u.kretprobe.lttng_krp->kref_register,
253 _lttng_kretprobes_unregister_release);
254 }
255 EXPORT_SYMBOL_GPL(lttng_kretprobes_unregister);
256
257 static
258 void _lttng_kretprobes_release(struct kref *kref)
259 {
260 struct lttng_krp *lttng_krp =
261 container_of(kref, struct lttng_krp, kref_alloc);
262 kfree(lttng_krp->krp.kp.symbol_name);
263 }
264
265 void lttng_kretprobes_destroy_private(struct ltt_event *event)
266 {
267 kfree(event->desc->fields);
268 kfree(event->desc->name);
269 kfree(event->desc);
270 kref_put(&event->u.kretprobe.lttng_krp->kref_alloc,
271 _lttng_kretprobes_release);
272 }
273 EXPORT_SYMBOL_GPL(lttng_kretprobes_destroy_private);
274
275 MODULE_LICENSE("GPL and additional rights");
276 MODULE_AUTHOR("Mathieu Desnoyers");
277 MODULE_DESCRIPTION("Linux Trace Toolkit Kretprobes Support");
This page took 0.036401 seconds and 4 git commands to generate.