Commit | Line | Data |
---|---|---|
413d37d1 | 1 | /* |
77b44d1b | 2 | * Kprobes-based tracing events |
413d37d1 MH |
3 | * |
4 | * Created by Masami Hiramatsu <mhiramat@redhat.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | ||
20 | #include <linux/module.h> | |
21 | #include <linux/uaccess.h> | |
22 | #include <linux/kprobes.h> | |
23 | #include <linux/seq_file.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/smp.h> | |
26 | #include <linux/debugfs.h> | |
27 | #include <linux/types.h> | |
28 | #include <linux/string.h> | |
29 | #include <linux/ctype.h> | |
30 | #include <linux/ptrace.h> | |
d7a4b414 | 31 | #include <linux/perf_event.h> |
93ccae7a MH |
32 | #include <linux/stringify.h> |
33 | #include <asm/bitsperlong.h> | |
413d37d1 MH |
34 | |
35 | #include "trace.h" | |
36 | #include "trace_output.h" | |
37 | ||
a82378d8 | 38 | #define MAX_TRACE_ARGS 128 |
413d37d1 | 39 | #define MAX_ARGSTR_LEN 63 |
4263565d | 40 | #define MAX_EVENT_NAME_LEN 64 |
f52487e9 | 41 | #define KPROBE_EVENT_SYSTEM "kprobes" |
413d37d1 | 42 | |
a703d946 | 43 | /* Reserved field names */ |
e93f4d85 | 44 | #define FIELD_STRING_IP "__probe_ip" |
e93f4d85 MH |
45 | #define FIELD_STRING_RETIP "__probe_ret_ip" |
46 | #define FIELD_STRING_FUNC "__probe_func" | |
a703d946 MH |
47 | |
48 | const char *reserved_field_names[] = { | |
49 | "common_type", | |
50 | "common_flags", | |
51 | "common_preempt_count", | |
52 | "common_pid", | |
53 | "common_tgid", | |
54 | "common_lock_depth", | |
55 | FIELD_STRING_IP, | |
a703d946 MH |
56 | FIELD_STRING_RETIP, |
57 | FIELD_STRING_FUNC, | |
58 | }; | |
59 | ||
93ccae7a MH |
60 | /* Printing function type */ |
61 | typedef int (*print_type_func_t)(struct trace_seq *, const char *, void *); | |
62 | #define PRINT_TYPE_FUNC_NAME(type) print_type_##type | |
63 | #define PRINT_TYPE_FMT_NAME(type) print_type_format_##type | |
64 | ||
65 | /* Printing in basic type function template */ | |
66 | #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt, cast) \ | |
67 | static __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \ | |
68 | const char *name, void *data)\ | |
69 | { \ | |
70 | return trace_seq_printf(s, " %s=" fmt, name, (cast)*(type *)data);\ | |
71 | } \ | |
72 | static const char PRINT_TYPE_FMT_NAME(type)[] = fmt; | |
73 | ||
74 | DEFINE_BASIC_PRINT_TYPE_FUNC(u8, "%x", unsigned int) | |
75 | DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "%x", unsigned int) | |
76 | DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "%lx", unsigned long) | |
77 | DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "%llx", unsigned long long) | |
78 | DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d", int) | |
79 | DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d", int) | |
80 | DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%ld", long) | |
81 | DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%lld", long long) | |
82 | ||
83 | /* Data fetch function type */ | |
84 | typedef void (*fetch_func_t)(struct pt_regs *, void *, void *); | |
85 | ||
86 | struct fetch_param { | |
87 | fetch_func_t fn; | |
413d37d1 MH |
88 | void *data; |
89 | }; | |
90 | ||
93ccae7a MH |
91 | static __kprobes void call_fetch(struct fetch_param *fprm, |
92 | struct pt_regs *regs, void *dest) | |
413d37d1 | 93 | { |
93ccae7a | 94 | return fprm->fn(regs, fprm->data, dest); |
413d37d1 MH |
95 | } |
96 | ||
93ccae7a MH |
97 | #define FETCH_FUNC_NAME(kind, type) fetch_##kind##_##type |
98 | /* | |
99 | * Define macro for basic types - we don't need to define s* types, because | |
100 | * we have to care only about bitwidth at recording time. | |
101 | */ | |
102 | #define DEFINE_BASIC_FETCH_FUNCS(kind) \ | |
103 | DEFINE_FETCH_##kind(u8) \ | |
104 | DEFINE_FETCH_##kind(u16) \ | |
105 | DEFINE_FETCH_##kind(u32) \ | |
106 | DEFINE_FETCH_##kind(u64) | |
107 | ||
108 | #define CHECK_BASIC_FETCH_FUNCS(kind, fn) \ | |
109 | ((FETCH_FUNC_NAME(kind, u8) == fn) || \ | |
110 | (FETCH_FUNC_NAME(kind, u16) == fn) || \ | |
111 | (FETCH_FUNC_NAME(kind, u32) == fn) || \ | |
112 | (FETCH_FUNC_NAME(kind, u64) == fn)) | |
113 | ||
114 | /* Data fetch function templates */ | |
115 | #define DEFINE_FETCH_reg(type) \ | |
116 | static __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \ | |
117 | void *offset, void *dest) \ | |
118 | { \ | |
119 | *(type *)dest = (type)regs_get_register(regs, \ | |
120 | (unsigned int)((unsigned long)offset)); \ | |
413d37d1 | 121 | } |
93ccae7a MH |
122 | DEFINE_BASIC_FETCH_FUNCS(reg) |
123 | ||
124 | #define DEFINE_FETCH_stack(type) \ | |
125 | static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\ | |
126 | void *offset, void *dest) \ | |
127 | { \ | |
128 | *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \ | |
129 | (unsigned int)((unsigned long)offset)); \ | |
413d37d1 | 130 | } |
93ccae7a | 131 | DEFINE_BASIC_FETCH_FUNCS(stack) |
413d37d1 | 132 | |
93ccae7a MH |
133 | #define DEFINE_FETCH_retval(type) \ |
134 | static __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,\ | |
135 | void *dummy, void *dest) \ | |
136 | { \ | |
137 | *(type *)dest = (type)regs_return_value(regs); \ | |
413d37d1 | 138 | } |
93ccae7a MH |
139 | DEFINE_BASIC_FETCH_FUNCS(retval) |
140 | ||
141 | #define DEFINE_FETCH_memory(type) \ | |
142 | static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\ | |
143 | void *addr, void *dest) \ | |
144 | { \ | |
145 | type retval; \ | |
146 | if (probe_kernel_address(addr, retval)) \ | |
147 | *(type *)dest = 0; \ | |
148 | else \ | |
149 | *(type *)dest = retval; \ | |
413d37d1 | 150 | } |
93ccae7a | 151 | DEFINE_BASIC_FETCH_FUNCS(memory) |
413d37d1 MH |
152 | |
153 | /* Memory fetching by symbol */ | |
154 | struct symbol_cache { | |
155 | char *symbol; | |
156 | long offset; | |
157 | unsigned long addr; | |
158 | }; | |
159 | ||
160 | static unsigned long update_symbol_cache(struct symbol_cache *sc) | |
161 | { | |
162 | sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol); | |
163 | if (sc->addr) | |
164 | sc->addr += sc->offset; | |
165 | return sc->addr; | |
166 | } | |
167 | ||
168 | static void free_symbol_cache(struct symbol_cache *sc) | |
169 | { | |
170 | kfree(sc->symbol); | |
171 | kfree(sc); | |
172 | } | |
173 | ||
174 | static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset) | |
175 | { | |
176 | struct symbol_cache *sc; | |
177 | ||
178 | if (!sym || strlen(sym) == 0) | |
179 | return NULL; | |
180 | sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL); | |
181 | if (!sc) | |
182 | return NULL; | |
183 | ||
184 | sc->symbol = kstrdup(sym, GFP_KERNEL); | |
185 | if (!sc->symbol) { | |
186 | kfree(sc); | |
187 | return NULL; | |
188 | } | |
189 | sc->offset = offset; | |
190 | ||
191 | update_symbol_cache(sc); | |
192 | return sc; | |
193 | } | |
194 | ||
93ccae7a MH |
195 | #define DEFINE_FETCH_symbol(type) \ |
196 | static __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,\ | |
197 | void *data, void *dest) \ | |
198 | { \ | |
199 | struct symbol_cache *sc = data; \ | |
200 | if (sc->addr) \ | |
201 | fetch_memory_##type(regs, (void *)sc->addr, dest); \ | |
202 | else \ | |
203 | *(type *)dest = 0; \ | |
413d37d1 | 204 | } |
93ccae7a | 205 | DEFINE_BASIC_FETCH_FUNCS(symbol) |
413d37d1 | 206 | |
93ccae7a MH |
207 | /* Dereference memory access function */ |
208 | struct deref_fetch_param { | |
209 | struct fetch_param orig; | |
413d37d1 MH |
210 | long offset; |
211 | }; | |
212 | ||
93ccae7a MH |
213 | #define DEFINE_FETCH_deref(type) \ |
214 | static __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,\ | |
215 | void *data, void *dest) \ | |
216 | { \ | |
217 | struct deref_fetch_param *dprm = data; \ | |
218 | unsigned long addr; \ | |
219 | call_fetch(&dprm->orig, regs, &addr); \ | |
220 | if (addr) { \ | |
221 | addr += dprm->offset; \ | |
222 | fetch_memory_##type(regs, (void *)addr, dest); \ | |
223 | } else \ | |
224 | *(type *)dest = 0; \ | |
413d37d1 | 225 | } |
93ccae7a | 226 | DEFINE_BASIC_FETCH_FUNCS(deref) |
413d37d1 | 227 | |
93ccae7a | 228 | static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data) |
413d37d1 | 229 | { |
93ccae7a MH |
230 | if (CHECK_BASIC_FETCH_FUNCS(deref, data->orig.fn)) |
231 | free_deref_fetch_param(data->orig.data); | |
232 | else if (CHECK_BASIC_FETCH_FUNCS(symbol, data->orig.fn)) | |
413d37d1 MH |
233 | free_symbol_cache(data->orig.data); |
234 | kfree(data); | |
235 | } | |
236 | ||
93ccae7a MH |
237 | /* Default (unsigned long) fetch type */ |
238 | #define __DEFAULT_FETCH_TYPE(t) u##t | |
239 | #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t) | |
240 | #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG) | |
241 | #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE) | |
242 | ||
243 | #define ASSIGN_FETCH_FUNC(kind, type) \ | |
244 | .kind = FETCH_FUNC_NAME(kind, type) | |
245 | ||
246 | #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \ | |
247 | {.name = #ptype, \ | |
248 | .size = sizeof(ftype), \ | |
249 | .is_signed = sign, \ | |
250 | .print = PRINT_TYPE_FUNC_NAME(ptype), \ | |
251 | .fmt = PRINT_TYPE_FMT_NAME(ptype), \ | |
252 | ASSIGN_FETCH_FUNC(reg, ftype), \ | |
253 | ASSIGN_FETCH_FUNC(stack, ftype), \ | |
254 | ASSIGN_FETCH_FUNC(retval, ftype), \ | |
255 | ASSIGN_FETCH_FUNC(memory, ftype), \ | |
256 | ASSIGN_FETCH_FUNC(symbol, ftype), \ | |
257 | ASSIGN_FETCH_FUNC(deref, ftype), \ | |
258 | } | |
259 | ||
260 | /* Fetch type information table */ | |
261 | static const struct fetch_type { | |
262 | const char *name; /* Name of type */ | |
263 | size_t size; /* Byte size of type */ | |
264 | int is_signed; /* Signed flag */ | |
265 | print_type_func_t print; /* Print functions */ | |
266 | const char *fmt; /* Fromat string */ | |
267 | /* Fetch functions */ | |
268 | fetch_func_t reg; | |
269 | fetch_func_t stack; | |
270 | fetch_func_t retval; | |
271 | fetch_func_t memory; | |
272 | fetch_func_t symbol; | |
273 | fetch_func_t deref; | |
274 | } fetch_type_table[] = { | |
275 | ASSIGN_FETCH_TYPE(u8, u8, 0), | |
276 | ASSIGN_FETCH_TYPE(u16, u16, 0), | |
277 | ASSIGN_FETCH_TYPE(u32, u32, 0), | |
278 | ASSIGN_FETCH_TYPE(u64, u64, 0), | |
279 | ASSIGN_FETCH_TYPE(s8, u8, 1), | |
280 | ASSIGN_FETCH_TYPE(s16, u16, 1), | |
281 | ASSIGN_FETCH_TYPE(s32, u32, 1), | |
282 | ASSIGN_FETCH_TYPE(s64, u64, 1), | |
283 | }; | |
284 | ||
285 | static const struct fetch_type *find_fetch_type(const char *type) | |
286 | { | |
287 | int i; | |
288 | ||
289 | if (!type) | |
290 | type = DEFAULT_FETCH_TYPE_STR; | |
291 | ||
292 | for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++) | |
293 | if (strcmp(type, fetch_type_table[i].name) == 0) | |
294 | return &fetch_type_table[i]; | |
295 | return NULL; | |
296 | } | |
297 | ||
298 | /* Special function : only accept unsigned long */ | |
299 | static __kprobes void fetch_stack_address(struct pt_regs *regs, | |
300 | void *dummy, void *dest) | |
301 | { | |
302 | *(unsigned long *)dest = kernel_stack_pointer(regs); | |
303 | } | |
304 | ||
413d37d1 | 305 | /** |
77b44d1b | 306 | * Kprobe event core functions |
413d37d1 MH |
307 | */ |
308 | ||
eca0d916 | 309 | struct probe_arg { |
93ccae7a MH |
310 | struct fetch_param fetch; |
311 | unsigned int offset; /* Offset from argument entry */ | |
312 | const char *name; /* Name of this argument */ | |
313 | const char *comm; /* Command of this argument */ | |
314 | const struct fetch_type *type; /* Type of this argument */ | |
eca0d916 MH |
315 | }; |
316 | ||
50d78056 MH |
317 | /* Flags for trace_probe */ |
318 | #define TP_FLAG_TRACE 1 | |
319 | #define TP_FLAG_PROFILE 2 | |
320 | ||
413d37d1 MH |
321 | struct trace_probe { |
322 | struct list_head list; | |
4a846b44 | 323 | struct kretprobe rp; /* Use rp.kp for kprobe use */ |
cd7e7bd5 | 324 | unsigned long nhit; |
50d78056 | 325 | unsigned int flags; /* For TP_FLAG_* */ |
413d37d1 | 326 | const char *symbol; /* symbol name */ |
2239291a | 327 | struct ftrace_event_class class; |
413d37d1 | 328 | struct ftrace_event_call call; |
93ccae7a | 329 | ssize_t size; /* trace entry size */ |
a82378d8 | 330 | unsigned int nr_args; |
eca0d916 | 331 | struct probe_arg args[]; |
413d37d1 MH |
332 | }; |
333 | ||
a82378d8 MH |
334 | #define SIZEOF_TRACE_PROBE(n) \ |
335 | (offsetof(struct trace_probe, args) + \ | |
eca0d916 | 336 | (sizeof(struct probe_arg) * (n))) |
a82378d8 | 337 | |
93ccae7a | 338 | |
413d37d1 MH |
339 | static __kprobes int probe_is_return(struct trace_probe *tp) |
340 | { | |
4a846b44 | 341 | return tp->rp.handler != NULL; |
413d37d1 MH |
342 | } |
343 | ||
344 | static __kprobes const char *probe_symbol(struct trace_probe *tp) | |
345 | { | |
346 | return tp->symbol ? tp->symbol : "unknown"; | |
347 | } | |
348 | ||
413d37d1 MH |
349 | static int register_probe_event(struct trace_probe *tp); |
350 | static void unregister_probe_event(struct trace_probe *tp); | |
351 | ||
352 | static DEFINE_MUTEX(probe_lock); | |
353 | static LIST_HEAD(probe_list); | |
354 | ||
50d78056 MH |
355 | static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs); |
356 | static int kretprobe_dispatcher(struct kretprobe_instance *ri, | |
357 | struct pt_regs *regs); | |
358 | ||
6f3cf440 MH |
359 | /* Check the name is good for event/group */ |
360 | static int check_event_name(const char *name) | |
361 | { | |
362 | if (!isalpha(*name) && *name != '_') | |
363 | return 0; | |
364 | while (*++name != '\0') { | |
365 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') | |
366 | return 0; | |
367 | } | |
368 | return 1; | |
369 | } | |
370 | ||
4a846b44 MH |
371 | /* |
372 | * Allocate new trace_probe and initialize it (including kprobes). | |
373 | */ | |
f52487e9 MH |
374 | static struct trace_probe *alloc_trace_probe(const char *group, |
375 | const char *event, | |
4a846b44 MH |
376 | void *addr, |
377 | const char *symbol, | |
378 | unsigned long offs, | |
379 | int nargs, int is_return) | |
413d37d1 MH |
380 | { |
381 | struct trace_probe *tp; | |
6f3cf440 | 382 | int ret = -ENOMEM; |
413d37d1 | 383 | |
a82378d8 | 384 | tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL); |
413d37d1 | 385 | if (!tp) |
6f3cf440 | 386 | return ERR_PTR(ret); |
413d37d1 MH |
387 | |
388 | if (symbol) { | |
389 | tp->symbol = kstrdup(symbol, GFP_KERNEL); | |
390 | if (!tp->symbol) | |
391 | goto error; | |
4a846b44 MH |
392 | tp->rp.kp.symbol_name = tp->symbol; |
393 | tp->rp.kp.offset = offs; | |
394 | } else | |
395 | tp->rp.kp.addr = addr; | |
396 | ||
397 | if (is_return) | |
50d78056 | 398 | tp->rp.handler = kretprobe_dispatcher; |
4a846b44 | 399 | else |
50d78056 | 400 | tp->rp.kp.pre_handler = kprobe_dispatcher; |
4a846b44 | 401 | |
6f3cf440 MH |
402 | if (!event || !check_event_name(event)) { |
403 | ret = -EINVAL; | |
4263565d | 404 | goto error; |
6f3cf440 MH |
405 | } |
406 | ||
2239291a | 407 | tp->call.class = &tp->class; |
4263565d MH |
408 | tp->call.name = kstrdup(event, GFP_KERNEL); |
409 | if (!tp->call.name) | |
410 | goto error; | |
413d37d1 | 411 | |
6f3cf440 MH |
412 | if (!group || !check_event_name(group)) { |
413 | ret = -EINVAL; | |
f52487e9 | 414 | goto error; |
6f3cf440 MH |
415 | } |
416 | ||
2239291a SR |
417 | tp->class.system = kstrdup(group, GFP_KERNEL); |
418 | if (!tp->class.system) | |
f52487e9 MH |
419 | goto error; |
420 | ||
413d37d1 MH |
421 | INIT_LIST_HEAD(&tp->list); |
422 | return tp; | |
423 | error: | |
f52487e9 | 424 | kfree(tp->call.name); |
413d37d1 MH |
425 | kfree(tp->symbol); |
426 | kfree(tp); | |
6f3cf440 | 427 | return ERR_PTR(ret); |
413d37d1 MH |
428 | } |
429 | ||
eca0d916 MH |
430 | static void free_probe_arg(struct probe_arg *arg) |
431 | { | |
93ccae7a MH |
432 | if (CHECK_BASIC_FETCH_FUNCS(deref, arg->fetch.fn)) |
433 | free_deref_fetch_param(arg->fetch.data); | |
434 | else if (CHECK_BASIC_FETCH_FUNCS(symbol, arg->fetch.fn)) | |
eca0d916 | 435 | free_symbol_cache(arg->fetch.data); |
eca0d916 | 436 | kfree(arg->name); |
93ccae7a | 437 | kfree(arg->comm); |
eca0d916 MH |
438 | } |
439 | ||
413d37d1 MH |
440 | static void free_trace_probe(struct trace_probe *tp) |
441 | { | |
442 | int i; | |
443 | ||
444 | for (i = 0; i < tp->nr_args; i++) | |
eca0d916 | 445 | free_probe_arg(&tp->args[i]); |
413d37d1 | 446 | |
8f082018 | 447 | kfree(tp->call.class->system); |
413d37d1 MH |
448 | kfree(tp->call.name); |
449 | kfree(tp->symbol); | |
450 | kfree(tp); | |
451 | } | |
452 | ||
dd004c47 MH |
453 | static struct trace_probe *find_probe_event(const char *event, |
454 | const char *group) | |
413d37d1 MH |
455 | { |
456 | struct trace_probe *tp; | |
457 | ||
458 | list_for_each_entry(tp, &probe_list, list) | |
dd004c47 | 459 | if (strcmp(tp->call.name, event) == 0 && |
8f082018 | 460 | strcmp(tp->call.class->system, group) == 0) |
413d37d1 MH |
461 | return tp; |
462 | return NULL; | |
463 | } | |
464 | ||
2d5e067e MH |
465 | /* Unregister a trace_probe and probe_event: call with locking probe_lock */ |
466 | static void unregister_trace_probe(struct trace_probe *tp) | |
413d37d1 MH |
467 | { |
468 | if (probe_is_return(tp)) | |
469 | unregister_kretprobe(&tp->rp); | |
470 | else | |
4a846b44 | 471 | unregister_kprobe(&tp->rp.kp); |
413d37d1 | 472 | list_del(&tp->list); |
2d5e067e | 473 | unregister_probe_event(tp); |
413d37d1 MH |
474 | } |
475 | ||
476 | /* Register a trace_probe and probe_event */ | |
477 | static int register_trace_probe(struct trace_probe *tp) | |
478 | { | |
479 | struct trace_probe *old_tp; | |
480 | int ret; | |
481 | ||
482 | mutex_lock(&probe_lock); | |
483 | ||
2d5e067e | 484 | /* register as an event */ |
8f082018 | 485 | old_tp = find_probe_event(tp->call.name, tp->call.class->system); |
2d5e067e MH |
486 | if (old_tp) { |
487 | /* delete old event */ | |
488 | unregister_trace_probe(old_tp); | |
489 | free_trace_probe(old_tp); | |
490 | } | |
491 | ret = register_probe_event(tp); | |
492 | if (ret) { | |
493 | pr_warning("Faild to register probe event(%d)\n", ret); | |
494 | goto end; | |
495 | } | |
496 | ||
5a0d9050 | 497 | tp->rp.kp.flags |= KPROBE_FLAG_DISABLED; |
413d37d1 MH |
498 | if (probe_is_return(tp)) |
499 | ret = register_kretprobe(&tp->rp); | |
500 | else | |
4a846b44 | 501 | ret = register_kprobe(&tp->rp.kp); |
413d37d1 MH |
502 | |
503 | if (ret) { | |
504 | pr_warning("Could not insert probe(%d)\n", ret); | |
505 | if (ret == -EILSEQ) { | |
506 | pr_warning("Probing address(0x%p) is not an " | |
507 | "instruction boundary.\n", | |
4a846b44 | 508 | tp->rp.kp.addr); |
413d37d1 MH |
509 | ret = -EINVAL; |
510 | } | |
2d5e067e MH |
511 | unregister_probe_event(tp); |
512 | } else | |
513 | list_add_tail(&tp->list, &probe_list); | |
413d37d1 MH |
514 | end: |
515 | mutex_unlock(&probe_lock); | |
516 | return ret; | |
517 | } | |
518 | ||
519 | /* Split symbol and offset. */ | |
2fba0c88 | 520 | static int split_symbol_offset(char *symbol, unsigned long *offset) |
413d37d1 MH |
521 | { |
522 | char *tmp; | |
523 | int ret; | |
524 | ||
525 | if (!offset) | |
526 | return -EINVAL; | |
527 | ||
528 | tmp = strchr(symbol, '+'); | |
413d37d1 MH |
529 | if (tmp) { |
530 | /* skip sign because strict_strtol doesn't accept '+' */ | |
2fba0c88 | 531 | ret = strict_strtoul(tmp + 1, 0, offset); |
413d37d1 MH |
532 | if (ret) |
533 | return ret; | |
413d37d1 MH |
534 | *tmp = '\0'; |
535 | } else | |
536 | *offset = 0; | |
537 | return 0; | |
538 | } | |
539 | ||
540 | #define PARAM_MAX_ARGS 16 | |
541 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) | |
542 | ||
93ccae7a MH |
543 | static int parse_probe_vars(char *arg, const struct fetch_type *t, |
544 | struct fetch_param *f, int is_return) | |
413d37d1 MH |
545 | { |
546 | int ret = 0; | |
547 | unsigned long param; | |
413d37d1 | 548 | |
2e06ff63 | 549 | if (strcmp(arg, "retval") == 0) { |
93ccae7a MH |
550 | if (is_return) |
551 | f->fn = t->retval; | |
552 | else | |
413d37d1 | 553 | ret = -EINVAL; |
2e06ff63 MH |
554 | } else if (strncmp(arg, "stack", 5) == 0) { |
555 | if (arg[5] == '\0') { | |
93ccae7a MH |
556 | if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0) |
557 | f->fn = fetch_stack_address; | |
558 | else | |
559 | ret = -EINVAL; | |
2e06ff63 MH |
560 | } else if (isdigit(arg[5])) { |
561 | ret = strict_strtoul(arg + 5, 10, ¶m); | |
413d37d1 MH |
562 | if (ret || param > PARAM_MAX_STACK) |
563 | ret = -EINVAL; | |
564 | else { | |
93ccae7a MH |
565 | f->fn = t->stack; |
566 | f->data = (void *)param; | |
413d37d1 | 567 | } |
2e06ff63 MH |
568 | } else |
569 | ret = -EINVAL; | |
2e06ff63 | 570 | } else |
405b2651 | 571 | ret = -EINVAL; |
405b2651 MH |
572 | return ret; |
573 | } | |
574 | ||
ba8665d7 | 575 | /* Recursive argument parser */ |
93ccae7a MH |
576 | static int __parse_probe_arg(char *arg, const struct fetch_type *t, |
577 | struct fetch_param *f, int is_return) | |
405b2651 MH |
578 | { |
579 | int ret = 0; | |
580 | unsigned long param; | |
581 | long offset; | |
582 | char *tmp; | |
583 | ||
584 | switch (arg[0]) { | |
585 | case '$': | |
93ccae7a | 586 | ret = parse_probe_vars(arg + 1, t, f, is_return); |
405b2651 MH |
587 | break; |
588 | case '%': /* named register */ | |
589 | ret = regs_query_register_offset(arg + 1); | |
590 | if (ret >= 0) { | |
93ccae7a MH |
591 | f->fn = t->reg; |
592 | f->data = (void *)(unsigned long)ret; | |
405b2651 MH |
593 | ret = 0; |
594 | } | |
595 | break; | |
413d37d1 MH |
596 | case '@': /* memory or symbol */ |
597 | if (isdigit(arg[1])) { | |
598 | ret = strict_strtoul(arg + 1, 0, ¶m); | |
599 | if (ret) | |
600 | break; | |
93ccae7a MH |
601 | f->fn = t->memory; |
602 | f->data = (void *)param; | |
413d37d1 MH |
603 | } else { |
604 | ret = split_symbol_offset(arg + 1, &offset); | |
605 | if (ret) | |
606 | break; | |
93ccae7a MH |
607 | f->data = alloc_symbol_cache(arg + 1, offset); |
608 | if (f->data) | |
609 | f->fn = t->symbol; | |
413d37d1 MH |
610 | } |
611 | break; | |
93ccae7a | 612 | case '+': /* deref memory */ |
413d37d1 MH |
613 | case '-': |
614 | tmp = strchr(arg, '('); | |
93ccae7a | 615 | if (!tmp) |
413d37d1 | 616 | break; |
413d37d1 MH |
617 | *tmp = '\0'; |
618 | ret = strict_strtol(arg + 1, 0, &offset); | |
619 | if (ret) | |
620 | break; | |
621 | if (arg[0] == '-') | |
622 | offset = -offset; | |
623 | arg = tmp + 1; | |
624 | tmp = strrchr(arg, ')'); | |
625 | if (tmp) { | |
93ccae7a MH |
626 | struct deref_fetch_param *dprm; |
627 | const struct fetch_type *t2 = find_fetch_type(NULL); | |
413d37d1 | 628 | *tmp = '\0'; |
93ccae7a MH |
629 | dprm = kzalloc(sizeof(struct deref_fetch_param), |
630 | GFP_KERNEL); | |
631 | if (!dprm) | |
413d37d1 | 632 | return -ENOMEM; |
93ccae7a MH |
633 | dprm->offset = offset; |
634 | ret = __parse_probe_arg(arg, t2, &dprm->orig, | |
635 | is_return); | |
413d37d1 | 636 | if (ret) |
93ccae7a | 637 | kfree(dprm); |
413d37d1 | 638 | else { |
93ccae7a MH |
639 | f->fn = t->deref; |
640 | f->data = (void *)dprm; | |
413d37d1 | 641 | } |
93ccae7a | 642 | } |
413d37d1 | 643 | break; |
413d37d1 | 644 | } |
93ccae7a MH |
645 | if (!ret && !f->fn) |
646 | ret = -EINVAL; | |
413d37d1 MH |
647 | return ret; |
648 | } | |
649 | ||
ba8665d7 | 650 | /* String length checking wrapper */ |
93ccae7a MH |
651 | static int parse_probe_arg(char *arg, struct trace_probe *tp, |
652 | struct probe_arg *parg, int is_return) | |
ba8665d7 | 653 | { |
93ccae7a MH |
654 | const char *t; |
655 | ||
ba8665d7 MH |
656 | if (strlen(arg) > MAX_ARGSTR_LEN) { |
657 | pr_info("Argument is too long.: %s\n", arg); | |
658 | return -ENOSPC; | |
659 | } | |
93ccae7a MH |
660 | parg->comm = kstrdup(arg, GFP_KERNEL); |
661 | if (!parg->comm) { | |
662 | pr_info("Failed to allocate memory for command '%s'.\n", arg); | |
663 | return -ENOMEM; | |
664 | } | |
665 | t = strchr(parg->comm, ':'); | |
666 | if (t) { | |
667 | arg[t - parg->comm] = '\0'; | |
668 | t++; | |
669 | } | |
670 | parg->type = find_fetch_type(t); | |
671 | if (!parg->type) { | |
672 | pr_info("Unsupported type: %s\n", t); | |
673 | return -EINVAL; | |
674 | } | |
675 | parg->offset = tp->size; | |
676 | tp->size += parg->type->size; | |
677 | return __parse_probe_arg(arg, parg->type, &parg->fetch, is_return); | |
ba8665d7 MH |
678 | } |
679 | ||
a703d946 MH |
680 | /* Return 1 if name is reserved or already used by another argument */ |
681 | static int conflict_field_name(const char *name, | |
682 | struct probe_arg *args, int narg) | |
683 | { | |
684 | int i; | |
685 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) | |
686 | if (strcmp(reserved_field_names[i], name) == 0) | |
687 | return 1; | |
688 | for (i = 0; i < narg; i++) | |
689 | if (strcmp(args[i].name, name) == 0) | |
690 | return 1; | |
691 | return 0; | |
692 | } | |
693 | ||
413d37d1 MH |
694 | static int create_trace_probe(int argc, char **argv) |
695 | { | |
696 | /* | |
697 | * Argument syntax: | |
f52487e9 MH |
698 | * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS] |
699 | * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS] | |
413d37d1 | 700 | * Fetch args: |
2e06ff63 MH |
701 | * $retval : fetch return value |
702 | * $stack : fetch stack address | |
703 | * $stackN : fetch Nth of stack (N:0-) | |
413d37d1 MH |
704 | * @ADDR : fetch memory at ADDR (ADDR should be in kernel) |
705 | * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol) | |
706 | * %REG : fetch register REG | |
93ccae7a | 707 | * Dereferencing memory fetch: |
413d37d1 | 708 | * +|-offs(ARG) : fetch memory at ARG +|- offs address. |
eca0d916 MH |
709 | * Alias name of args: |
710 | * NAME=FETCHARG : set NAME as alias of FETCHARG. | |
93ccae7a MH |
711 | * Type of args: |
712 | * FETCHARG:TYPE : use TYPE instead of unsigned long. | |
413d37d1 MH |
713 | */ |
714 | struct trace_probe *tp; | |
413d37d1 | 715 | int i, ret = 0; |
a7c312be | 716 | int is_return = 0, is_delete = 0; |
93ccae7a MH |
717 | char *symbol = NULL, *event = NULL, *group = NULL; |
718 | char *arg, *tmp; | |
2fba0c88 | 719 | unsigned long offset = 0; |
413d37d1 | 720 | void *addr = NULL; |
4a846b44 | 721 | char buf[MAX_EVENT_NAME_LEN]; |
413d37d1 | 722 | |
a7c312be | 723 | /* argc must be >= 1 */ |
413d37d1 MH |
724 | if (argv[0][0] == 'p') |
725 | is_return = 0; | |
726 | else if (argv[0][0] == 'r') | |
727 | is_return = 1; | |
a7c312be MH |
728 | else if (argv[0][0] == '-') |
729 | is_delete = 1; | |
e63cc239 | 730 | else { |
a7c312be MH |
731 | pr_info("Probe definition must be started with 'p', 'r' or" |
732 | " '-'.\n"); | |
413d37d1 | 733 | return -EINVAL; |
e63cc239 | 734 | } |
413d37d1 MH |
735 | |
736 | if (argv[0][1] == ':') { | |
737 | event = &argv[0][2]; | |
f52487e9 MH |
738 | if (strchr(event, '/')) { |
739 | group = event; | |
740 | event = strchr(group, '/') + 1; | |
741 | event[-1] = '\0'; | |
742 | if (strlen(group) == 0) { | |
a5efd925 | 743 | pr_info("Group name is not specified\n"); |
f52487e9 MH |
744 | return -EINVAL; |
745 | } | |
746 | } | |
413d37d1 | 747 | if (strlen(event) == 0) { |
a5efd925 | 748 | pr_info("Event name is not specified\n"); |
413d37d1 MH |
749 | return -EINVAL; |
750 | } | |
751 | } | |
a7c312be MH |
752 | if (!group) |
753 | group = KPROBE_EVENT_SYSTEM; | |
413d37d1 | 754 | |
a7c312be MH |
755 | if (is_delete) { |
756 | if (!event) { | |
757 | pr_info("Delete command needs an event name.\n"); | |
758 | return -EINVAL; | |
759 | } | |
760 | tp = find_probe_event(event, group); | |
761 | if (!tp) { | |
762 | pr_info("Event %s/%s doesn't exist.\n", group, event); | |
763 | return -ENOENT; | |
764 | } | |
765 | /* delete an event */ | |
766 | unregister_trace_probe(tp); | |
767 | free_trace_probe(tp); | |
768 | return 0; | |
769 | } | |
770 | ||
771 | if (argc < 2) { | |
772 | pr_info("Probe point is not specified.\n"); | |
773 | return -EINVAL; | |
774 | } | |
413d37d1 | 775 | if (isdigit(argv[1][0])) { |
e63cc239 MH |
776 | if (is_return) { |
777 | pr_info("Return probe point must be a symbol.\n"); | |
413d37d1 | 778 | return -EINVAL; |
e63cc239 | 779 | } |
413d37d1 | 780 | /* an address specified */ |
a9bb18f3 | 781 | ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr); |
e63cc239 MH |
782 | if (ret) { |
783 | pr_info("Failed to parse address.\n"); | |
413d37d1 | 784 | return ret; |
e63cc239 | 785 | } |
413d37d1 MH |
786 | } else { |
787 | /* a symbol specified */ | |
788 | symbol = argv[1]; | |
789 | /* TODO: support .init module functions */ | |
790 | ret = split_symbol_offset(symbol, &offset); | |
e63cc239 MH |
791 | if (ret) { |
792 | pr_info("Failed to parse symbol.\n"); | |
413d37d1 | 793 | return ret; |
e63cc239 MH |
794 | } |
795 | if (offset && is_return) { | |
796 | pr_info("Return probe must be used without offset.\n"); | |
413d37d1 | 797 | return -EINVAL; |
e63cc239 | 798 | } |
413d37d1 | 799 | } |
a82378d8 | 800 | argc -= 2; argv += 2; |
413d37d1 MH |
801 | |
802 | /* setup a probe */ | |
4263565d MH |
803 | if (!event) { |
804 | /* Make a new event name */ | |
4263565d | 805 | if (symbol) |
6f3cf440 | 806 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld", |
4263565d MH |
807 | is_return ? 'r' : 'p', symbol, offset); |
808 | else | |
6f3cf440 | 809 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p", |
4263565d | 810 | is_return ? 'r' : 'p', addr); |
4a846b44 MH |
811 | event = buf; |
812 | } | |
f52487e9 MH |
813 | tp = alloc_trace_probe(group, event, addr, symbol, offset, argc, |
814 | is_return); | |
e63cc239 MH |
815 | if (IS_ERR(tp)) { |
816 | pr_info("Failed to allocate trace_probe.(%d)\n", | |
817 | (int)PTR_ERR(tp)); | |
413d37d1 | 818 | return PTR_ERR(tp); |
e63cc239 | 819 | } |
413d37d1 | 820 | |
413d37d1 | 821 | /* parse arguments */ |
a82378d8 MH |
822 | ret = 0; |
823 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { | |
eca0d916 MH |
824 | /* Parse argument name */ |
825 | arg = strchr(argv[i], '='); | |
826 | if (arg) | |
827 | *arg++ = '\0'; | |
828 | else | |
829 | arg = argv[i]; | |
a703d946 | 830 | |
eca0d916 | 831 | tp->args[i].name = kstrdup(argv[i], GFP_KERNEL); |
ba8665d7 MH |
832 | if (!tp->args[i].name) { |
833 | pr_info("Failed to allocate argument%d name '%s'.\n", | |
834 | i, argv[i]); | |
835 | ret = -ENOMEM; | |
413d37d1 MH |
836 | goto error; |
837 | } | |
93ccae7a MH |
838 | tmp = strchr(tp->args[i].name, ':'); |
839 | if (tmp) | |
840 | *tmp = '_'; /* convert : to _ */ | |
841 | ||
842 | if (conflict_field_name(tp->args[i].name, tp->args, i)) { | |
843 | pr_info("Argument%d name '%s' conflicts with " | |
844 | "another field.\n", i, argv[i]); | |
845 | ret = -EINVAL; | |
846 | goto error; | |
847 | } | |
ba8665d7 MH |
848 | |
849 | /* Parse fetch argument */ | |
93ccae7a | 850 | ret = parse_probe_arg(arg, tp, &tp->args[i], is_return); |
e63cc239 MH |
851 | if (ret) { |
852 | pr_info("Parse error at argument%d. (%d)\n", i, ret); | |
abab9d37 | 853 | kfree(tp->args[i].name); |
413d37d1 | 854 | goto error; |
e63cc239 | 855 | } |
abab9d37 LJ |
856 | |
857 | tp->nr_args++; | |
413d37d1 | 858 | } |
413d37d1 MH |
859 | |
860 | ret = register_trace_probe(tp); | |
861 | if (ret) | |
862 | goto error; | |
863 | return 0; | |
864 | ||
865 | error: | |
866 | free_trace_probe(tp); | |
867 | return ret; | |
868 | } | |
869 | ||
870 | static void cleanup_all_probes(void) | |
871 | { | |
872 | struct trace_probe *tp; | |
873 | ||
874 | mutex_lock(&probe_lock); | |
875 | /* TODO: Use batch unregistration */ | |
876 | while (!list_empty(&probe_list)) { | |
877 | tp = list_entry(probe_list.next, struct trace_probe, list); | |
878 | unregister_trace_probe(tp); | |
879 | free_trace_probe(tp); | |
880 | } | |
881 | mutex_unlock(&probe_lock); | |
882 | } | |
883 | ||
884 | ||
885 | /* Probes listing interfaces */ | |
886 | static void *probes_seq_start(struct seq_file *m, loff_t *pos) | |
887 | { | |
888 | mutex_lock(&probe_lock); | |
889 | return seq_list_start(&probe_list, *pos); | |
890 | } | |
891 | ||
892 | static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos) | |
893 | { | |
894 | return seq_list_next(v, &probe_list, pos); | |
895 | } | |
896 | ||
897 | static void probes_seq_stop(struct seq_file *m, void *v) | |
898 | { | |
899 | mutex_unlock(&probe_lock); | |
900 | } | |
901 | ||
902 | static int probes_seq_show(struct seq_file *m, void *v) | |
903 | { | |
904 | struct trace_probe *tp = v; | |
93ccae7a | 905 | int i; |
413d37d1 MH |
906 | |
907 | seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p'); | |
8f082018 | 908 | seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name); |
413d37d1 | 909 | |
52a11f35 LJ |
910 | if (!tp->symbol) |
911 | seq_printf(m, " 0x%p", tp->rp.kp.addr); | |
912 | else if (tp->rp.kp.offset) | |
4a846b44 | 913 | seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset); |
413d37d1 | 914 | else |
52a11f35 | 915 | seq_printf(m, " %s", probe_symbol(tp)); |
413d37d1 | 916 | |
93ccae7a MH |
917 | for (i = 0; i < tp->nr_args; i++) |
918 | seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm); | |
413d37d1 | 919 | seq_printf(m, "\n"); |
93ccae7a | 920 | |
413d37d1 MH |
921 | return 0; |
922 | } | |
923 | ||
924 | static const struct seq_operations probes_seq_op = { | |
925 | .start = probes_seq_start, | |
926 | .next = probes_seq_next, | |
927 | .stop = probes_seq_stop, | |
928 | .show = probes_seq_show | |
929 | }; | |
930 | ||
931 | static int probes_open(struct inode *inode, struct file *file) | |
932 | { | |
933 | if ((file->f_mode & FMODE_WRITE) && | |
934 | (file->f_flags & O_TRUNC)) | |
935 | cleanup_all_probes(); | |
936 | ||
937 | return seq_open(file, &probes_seq_op); | |
938 | } | |
939 | ||
940 | static int command_trace_probe(const char *buf) | |
941 | { | |
942 | char **argv; | |
943 | int argc = 0, ret = 0; | |
944 | ||
945 | argv = argv_split(GFP_KERNEL, buf, &argc); | |
946 | if (!argv) | |
947 | return -ENOMEM; | |
948 | ||
949 | if (argc) | |
950 | ret = create_trace_probe(argc, argv); | |
951 | ||
952 | argv_free(argv); | |
953 | return ret; | |
954 | } | |
955 | ||
956 | #define WRITE_BUFSIZE 128 | |
957 | ||
958 | static ssize_t probes_write(struct file *file, const char __user *buffer, | |
959 | size_t count, loff_t *ppos) | |
960 | { | |
961 | char *kbuf, *tmp; | |
962 | int ret; | |
963 | size_t done; | |
964 | size_t size; | |
965 | ||
966 | kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL); | |
967 | if (!kbuf) | |
968 | return -ENOMEM; | |
969 | ||
970 | ret = done = 0; | |
971 | while (done < count) { | |
972 | size = count - done; | |
973 | if (size >= WRITE_BUFSIZE) | |
974 | size = WRITE_BUFSIZE - 1; | |
975 | if (copy_from_user(kbuf, buffer + done, size)) { | |
976 | ret = -EFAULT; | |
977 | goto out; | |
978 | } | |
979 | kbuf[size] = '\0'; | |
980 | tmp = strchr(kbuf, '\n'); | |
981 | if (tmp) { | |
982 | *tmp = '\0'; | |
983 | size = tmp - kbuf + 1; | |
984 | } else if (done + size < count) { | |
985 | pr_warning("Line length is too long: " | |
986 | "Should be less than %d.", WRITE_BUFSIZE); | |
987 | ret = -EINVAL; | |
988 | goto out; | |
989 | } | |
990 | done += size; | |
991 | /* Remove comments */ | |
992 | tmp = strchr(kbuf, '#'); | |
993 | if (tmp) | |
994 | *tmp = '\0'; | |
995 | ||
996 | ret = command_trace_probe(kbuf); | |
997 | if (ret) | |
998 | goto out; | |
999 | } | |
1000 | ret = done; | |
1001 | out: | |
1002 | kfree(kbuf); | |
1003 | return ret; | |
1004 | } | |
1005 | ||
1006 | static const struct file_operations kprobe_events_ops = { | |
1007 | .owner = THIS_MODULE, | |
1008 | .open = probes_open, | |
1009 | .read = seq_read, | |
1010 | .llseek = seq_lseek, | |
1011 | .release = seq_release, | |
1012 | .write = probes_write, | |
1013 | }; | |
1014 | ||
cd7e7bd5 MH |
1015 | /* Probes profiling interfaces */ |
1016 | static int probes_profile_seq_show(struct seq_file *m, void *v) | |
1017 | { | |
1018 | struct trace_probe *tp = v; | |
1019 | ||
1020 | seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit, | |
4a846b44 | 1021 | tp->rp.kp.nmissed); |
cd7e7bd5 MH |
1022 | |
1023 | return 0; | |
1024 | } | |
1025 | ||
1026 | static const struct seq_operations profile_seq_op = { | |
1027 | .start = probes_seq_start, | |
1028 | .next = probes_seq_next, | |
1029 | .stop = probes_seq_stop, | |
1030 | .show = probes_profile_seq_show | |
1031 | }; | |
1032 | ||
1033 | static int profile_open(struct inode *inode, struct file *file) | |
1034 | { | |
1035 | return seq_open(file, &profile_seq_op); | |
1036 | } | |
1037 | ||
1038 | static const struct file_operations kprobe_profile_ops = { | |
1039 | .owner = THIS_MODULE, | |
1040 | .open = profile_open, | |
1041 | .read = seq_read, | |
1042 | .llseek = seq_lseek, | |
1043 | .release = seq_release, | |
1044 | }; | |
1045 | ||
413d37d1 | 1046 | /* Kprobe handler */ |
1e12a4a7 | 1047 | static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs) |
413d37d1 | 1048 | { |
4a846b44 | 1049 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); |
93ccae7a | 1050 | struct kprobe_trace_entry_head *entry; |
413d37d1 | 1051 | struct ring_buffer_event *event; |
8f8ffe24 | 1052 | struct ring_buffer *buffer; |
93ccae7a | 1053 | u8 *data; |
413d37d1 MH |
1054 | int size, i, pc; |
1055 | unsigned long irq_flags; | |
4263565d | 1056 | struct ftrace_event_call *call = &tp->call; |
413d37d1 | 1057 | |
cd7e7bd5 MH |
1058 | tp->nhit++; |
1059 | ||
413d37d1 MH |
1060 | local_save_flags(irq_flags); |
1061 | pc = preempt_count(); | |
1062 | ||
93ccae7a | 1063 | size = sizeof(*entry) + tp->size; |
413d37d1 | 1064 | |
32c0edae SR |
1065 | event = trace_current_buffer_lock_reserve(&buffer, call->event.type, |
1066 | size, irq_flags, pc); | |
413d37d1 | 1067 | if (!event) |
1e12a4a7 | 1068 | return; |
413d37d1 MH |
1069 | |
1070 | entry = ring_buffer_event_data(event); | |
413d37d1 | 1071 | entry->ip = (unsigned long)kp->addr; |
93ccae7a | 1072 | data = (u8 *)&entry[1]; |
413d37d1 | 1073 | for (i = 0; i < tp->nr_args; i++) |
93ccae7a | 1074 | call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset); |
413d37d1 | 1075 | |
8f8ffe24 FW |
1076 | if (!filter_current_check_discard(buffer, call, entry, event)) |
1077 | trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc); | |
413d37d1 MH |
1078 | } |
1079 | ||
1080 | /* Kretprobe handler */ | |
1e12a4a7 | 1081 | static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri, |
413d37d1 MH |
1082 | struct pt_regs *regs) |
1083 | { | |
1084 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); | |
93ccae7a | 1085 | struct kretprobe_trace_entry_head *entry; |
413d37d1 | 1086 | struct ring_buffer_event *event; |
8f8ffe24 | 1087 | struct ring_buffer *buffer; |
93ccae7a | 1088 | u8 *data; |
413d37d1 MH |
1089 | int size, i, pc; |
1090 | unsigned long irq_flags; | |
4263565d | 1091 | struct ftrace_event_call *call = &tp->call; |
413d37d1 MH |
1092 | |
1093 | local_save_flags(irq_flags); | |
1094 | pc = preempt_count(); | |
1095 | ||
93ccae7a | 1096 | size = sizeof(*entry) + tp->size; |
413d37d1 | 1097 | |
32c0edae SR |
1098 | event = trace_current_buffer_lock_reserve(&buffer, call->event.type, |
1099 | size, irq_flags, pc); | |
413d37d1 | 1100 | if (!event) |
1e12a4a7 | 1101 | return; |
413d37d1 MH |
1102 | |
1103 | entry = ring_buffer_event_data(event); | |
4a846b44 | 1104 | entry->func = (unsigned long)tp->rp.kp.addr; |
413d37d1 | 1105 | entry->ret_ip = (unsigned long)ri->ret_addr; |
93ccae7a | 1106 | data = (u8 *)&entry[1]; |
413d37d1 | 1107 | for (i = 0; i < tp->nr_args; i++) |
93ccae7a | 1108 | call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset); |
413d37d1 | 1109 | |
8f8ffe24 FW |
1110 | if (!filter_current_check_discard(buffer, call, entry, event)) |
1111 | trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc); | |
413d37d1 MH |
1112 | } |
1113 | ||
1114 | /* Event entry printers */ | |
1115 | enum print_line_t | |
a9a57763 SR |
1116 | print_kprobe_event(struct trace_iterator *iter, int flags, |
1117 | struct trace_event *event) | |
413d37d1 | 1118 | { |
93ccae7a | 1119 | struct kprobe_trace_entry_head *field; |
413d37d1 | 1120 | struct trace_seq *s = &iter->seq; |
eca0d916 | 1121 | struct trace_probe *tp; |
93ccae7a | 1122 | u8 *data; |
413d37d1 MH |
1123 | int i; |
1124 | ||
93ccae7a | 1125 | field = (struct kprobe_trace_entry_head *)iter->ent; |
80decc70 | 1126 | tp = container_of(event, struct trace_probe, call.event); |
413d37d1 | 1127 | |
6e9f23d1 MH |
1128 | if (!trace_seq_printf(s, "%s: (", tp->call.name)) |
1129 | goto partial; | |
1130 | ||
413d37d1 MH |
1131 | if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET)) |
1132 | goto partial; | |
1133 | ||
6e9f23d1 | 1134 | if (!trace_seq_puts(s, ")")) |
413d37d1 MH |
1135 | goto partial; |
1136 | ||
93ccae7a MH |
1137 | data = (u8 *)&field[1]; |
1138 | for (i = 0; i < tp->nr_args; i++) | |
1139 | if (!tp->args[i].type->print(s, tp->args[i].name, | |
1140 | data + tp->args[i].offset)) | |
413d37d1 MH |
1141 | goto partial; |
1142 | ||
1143 | if (!trace_seq_puts(s, "\n")) | |
1144 | goto partial; | |
1145 | ||
1146 | return TRACE_TYPE_HANDLED; | |
1147 | partial: | |
1148 | return TRACE_TYPE_PARTIAL_LINE; | |
1149 | } | |
1150 | ||
1151 | enum print_line_t | |
a9a57763 SR |
1152 | print_kretprobe_event(struct trace_iterator *iter, int flags, |
1153 | struct trace_event *event) | |
413d37d1 | 1154 | { |
93ccae7a | 1155 | struct kretprobe_trace_entry_head *field; |
413d37d1 | 1156 | struct trace_seq *s = &iter->seq; |
eca0d916 | 1157 | struct trace_probe *tp; |
93ccae7a | 1158 | u8 *data; |
413d37d1 MH |
1159 | int i; |
1160 | ||
93ccae7a | 1161 | field = (struct kretprobe_trace_entry_head *)iter->ent; |
80decc70 | 1162 | tp = container_of(event, struct trace_probe, call.event); |
413d37d1 | 1163 | |
6e9f23d1 MH |
1164 | if (!trace_seq_printf(s, "%s: (", tp->call.name)) |
1165 | goto partial; | |
1166 | ||
413d37d1 MH |
1167 | if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET)) |
1168 | goto partial; | |
1169 | ||
1170 | if (!trace_seq_puts(s, " <- ")) | |
1171 | goto partial; | |
1172 | ||
1173 | if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET)) | |
1174 | goto partial; | |
1175 | ||
6e9f23d1 | 1176 | if (!trace_seq_puts(s, ")")) |
413d37d1 MH |
1177 | goto partial; |
1178 | ||
93ccae7a MH |
1179 | data = (u8 *)&field[1]; |
1180 | for (i = 0; i < tp->nr_args; i++) | |
1181 | if (!tp->args[i].type->print(s, tp->args[i].name, | |
1182 | data + tp->args[i].offset)) | |
413d37d1 MH |
1183 | goto partial; |
1184 | ||
1185 | if (!trace_seq_puts(s, "\n")) | |
1186 | goto partial; | |
1187 | ||
1188 | return TRACE_TYPE_HANDLED; | |
1189 | partial: | |
1190 | return TRACE_TYPE_PARTIAL_LINE; | |
1191 | } | |
1192 | ||
413d37d1 MH |
1193 | static int probe_event_enable(struct ftrace_event_call *call) |
1194 | { | |
1195 | struct trace_probe *tp = (struct trace_probe *)call->data; | |
1196 | ||
50d78056 MH |
1197 | tp->flags |= TP_FLAG_TRACE; |
1198 | if (probe_is_return(tp)) | |
413d37d1 | 1199 | return enable_kretprobe(&tp->rp); |
50d78056 | 1200 | else |
4a846b44 | 1201 | return enable_kprobe(&tp->rp.kp); |
413d37d1 MH |
1202 | } |
1203 | ||
1204 | static void probe_event_disable(struct ftrace_event_call *call) | |
1205 | { | |
1206 | struct trace_probe *tp = (struct trace_probe *)call->data; | |
1207 | ||
50d78056 MH |
1208 | tp->flags &= ~TP_FLAG_TRACE; |
1209 | if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) { | |
1210 | if (probe_is_return(tp)) | |
1211 | disable_kretprobe(&tp->rp); | |
1212 | else | |
1213 | disable_kprobe(&tp->rp.kp); | |
1214 | } | |
413d37d1 MH |
1215 | } |
1216 | ||
413d37d1 MH |
1217 | #undef DEFINE_FIELD |
1218 | #define DEFINE_FIELD(type, item, name, is_signed) \ | |
1219 | do { \ | |
1220 | ret = trace_define_field(event_call, #type, name, \ | |
1221 | offsetof(typeof(field), item), \ | |
1222 | sizeof(field.item), is_signed, \ | |
1223 | FILTER_OTHER); \ | |
1224 | if (ret) \ | |
1225 | return ret; \ | |
1226 | } while (0) | |
1227 | ||
1228 | static int kprobe_event_define_fields(struct ftrace_event_call *event_call) | |
1229 | { | |
1230 | int ret, i; | |
93ccae7a | 1231 | struct kprobe_trace_entry_head field; |
413d37d1 MH |
1232 | struct trace_probe *tp = (struct trace_probe *)event_call->data; |
1233 | ||
a703d946 | 1234 | DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0); |
eca0d916 | 1235 | /* Set argument names as fields */ |
93ccae7a MH |
1236 | for (i = 0; i < tp->nr_args; i++) { |
1237 | ret = trace_define_field(event_call, tp->args[i].type->name, | |
1238 | tp->args[i].name, | |
1239 | sizeof(field) + tp->args[i].offset, | |
1240 | tp->args[i].type->size, | |
1241 | tp->args[i].type->is_signed, | |
1242 | FILTER_OTHER); | |
1243 | if (ret) | |
1244 | return ret; | |
1245 | } | |
413d37d1 MH |
1246 | return 0; |
1247 | } | |
1248 | ||
1249 | static int kretprobe_event_define_fields(struct ftrace_event_call *event_call) | |
1250 | { | |
1251 | int ret, i; | |
93ccae7a | 1252 | struct kretprobe_trace_entry_head field; |
413d37d1 MH |
1253 | struct trace_probe *tp = (struct trace_probe *)event_call->data; |
1254 | ||
a703d946 MH |
1255 | DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0); |
1256 | DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0); | |
eca0d916 | 1257 | /* Set argument names as fields */ |
93ccae7a MH |
1258 | for (i = 0; i < tp->nr_args; i++) { |
1259 | ret = trace_define_field(event_call, tp->args[i].type->name, | |
1260 | tp->args[i].name, | |
1261 | sizeof(field) + tp->args[i].offset, | |
1262 | tp->args[i].type->size, | |
1263 | tp->args[i].type->is_signed, | |
1264 | FILTER_OTHER); | |
1265 | if (ret) | |
1266 | return ret; | |
1267 | } | |
413d37d1 MH |
1268 | return 0; |
1269 | } | |
1270 | ||
a342a028 LJ |
1271 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len) |
1272 | { | |
1273 | int i; | |
1274 | int pos = 0; | |
1275 | ||
1276 | const char *fmt, *arg; | |
1277 | ||
1278 | if (!probe_is_return(tp)) { | |
1279 | fmt = "(%lx)"; | |
1280 | arg = "REC->" FIELD_STRING_IP; | |
1281 | } else { | |
1282 | fmt = "(%lx <- %lx)"; | |
1283 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; | |
1284 | } | |
1285 | ||
1286 | /* When len=0, we just calculate the needed length */ | |
1287 | #define LEN_OR_ZERO (len ? len - pos : 0) | |
1288 | ||
1289 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); | |
1290 | ||
1291 | for (i = 0; i < tp->nr_args; i++) { | |
93ccae7a MH |
1292 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s", |
1293 | tp->args[i].name, tp->args[i].type->fmt); | |
a342a028 LJ |
1294 | } |
1295 | ||
1296 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); | |
1297 | ||
1298 | for (i = 0; i < tp->nr_args; i++) { | |
1299 | pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s", | |
1300 | tp->args[i].name); | |
1301 | } | |
1302 | ||
1303 | #undef LEN_OR_ZERO | |
1304 | ||
1305 | /* return the length of print_fmt */ | |
1306 | return pos; | |
1307 | } | |
1308 | ||
1309 | static int set_print_fmt(struct trace_probe *tp) | |
1310 | { | |
1311 | int len; | |
1312 | char *print_fmt; | |
1313 | ||
1314 | /* First: called with 0 length to calculate the needed length */ | |
1315 | len = __set_print_fmt(tp, NULL, 0); | |
1316 | print_fmt = kmalloc(len + 1, GFP_KERNEL); | |
1317 | if (!print_fmt) | |
1318 | return -ENOMEM; | |
1319 | ||
1320 | /* Second: actually write the @print_fmt */ | |
1321 | __set_print_fmt(tp, print_fmt, len + 1); | |
1322 | tp->call.print_fmt = print_fmt; | |
1323 | ||
1324 | return 0; | |
1325 | } | |
1326 | ||
07b139c8 | 1327 | #ifdef CONFIG_PERF_EVENTS |
e08d1c65 MH |
1328 | |
1329 | /* Kprobe profile handler */ | |
97d5a220 | 1330 | static __kprobes void kprobe_perf_func(struct kprobe *kp, |
e08d1c65 MH |
1331 | struct pt_regs *regs) |
1332 | { | |
1333 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); | |
1334 | struct ftrace_event_call *call = &tp->call; | |
93ccae7a | 1335 | struct kprobe_trace_entry_head *entry; |
1c024eca | 1336 | struct hlist_head *head; |
93ccae7a | 1337 | u8 *data; |
430ad5a6 | 1338 | int size, __size, i; |
4ed7c92d | 1339 | int rctx; |
e08d1c65 | 1340 | |
93ccae7a | 1341 | __size = sizeof(*entry) + tp->size; |
74ebb63e MH |
1342 | size = ALIGN(__size + sizeof(u32), sizeof(u64)); |
1343 | size -= sizeof(u32); | |
97d5a220 | 1344 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, |
a1a138d0 | 1345 | "profile buffer not large enough")) |
1e12a4a7 | 1346 | return; |
ce71b9df | 1347 | |
ff5f149b | 1348 | entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx); |
430ad5a6 | 1349 | if (!entry) |
1e12a4a7 | 1350 | return; |
a1a138d0 | 1351 | |
a1a138d0 | 1352 | entry->ip = (unsigned long)kp->addr; |
93ccae7a | 1353 | data = (u8 *)&entry[1]; |
a1a138d0 | 1354 | for (i = 0; i < tp->nr_args; i++) |
93ccae7a | 1355 | call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset); |
444a2a3b | 1356 | |
3771f077 | 1357 | head = this_cpu_ptr(call->perf_events); |
1c024eca | 1358 | perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head); |
e08d1c65 MH |
1359 | } |
1360 | ||
1361 | /* Kretprobe profile handler */ | |
97d5a220 | 1362 | static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri, |
e08d1c65 MH |
1363 | struct pt_regs *regs) |
1364 | { | |
1365 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); | |
1366 | struct ftrace_event_call *call = &tp->call; | |
93ccae7a | 1367 | struct kretprobe_trace_entry_head *entry; |
1c024eca | 1368 | struct hlist_head *head; |
93ccae7a | 1369 | u8 *data; |
430ad5a6 | 1370 | int size, __size, i; |
4ed7c92d | 1371 | int rctx; |
e08d1c65 | 1372 | |
93ccae7a | 1373 | __size = sizeof(*entry) + tp->size; |
74ebb63e MH |
1374 | size = ALIGN(__size + sizeof(u32), sizeof(u64)); |
1375 | size -= sizeof(u32); | |
97d5a220 | 1376 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, |
a1a138d0 | 1377 | "profile buffer not large enough")) |
1e12a4a7 | 1378 | return; |
444a2a3b | 1379 | |
ff5f149b | 1380 | entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx); |
430ad5a6 | 1381 | if (!entry) |
1e12a4a7 | 1382 | return; |
e08d1c65 | 1383 | |
a1a138d0 MH |
1384 | entry->func = (unsigned long)tp->rp.kp.addr; |
1385 | entry->ret_ip = (unsigned long)ri->ret_addr; | |
93ccae7a | 1386 | data = (u8 *)&entry[1]; |
a1a138d0 | 1387 | for (i = 0; i < tp->nr_args; i++) |
93ccae7a | 1388 | call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset); |
444a2a3b | 1389 | |
3771f077 | 1390 | head = this_cpu_ptr(call->perf_events); |
1c024eca | 1391 | perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head); |
e08d1c65 MH |
1392 | } |
1393 | ||
97d5a220 | 1394 | static int probe_perf_enable(struct ftrace_event_call *call) |
e08d1c65 MH |
1395 | { |
1396 | struct trace_probe *tp = (struct trace_probe *)call->data; | |
1397 | ||
50d78056 | 1398 | tp->flags |= TP_FLAG_PROFILE; |
d7a4b414 | 1399 | |
50d78056 | 1400 | if (probe_is_return(tp)) |
e08d1c65 | 1401 | return enable_kretprobe(&tp->rp); |
50d78056 | 1402 | else |
e08d1c65 | 1403 | return enable_kprobe(&tp->rp.kp); |
e08d1c65 MH |
1404 | } |
1405 | ||
97d5a220 | 1406 | static void probe_perf_disable(struct ftrace_event_call *call) |
e08d1c65 | 1407 | { |
50d78056 MH |
1408 | struct trace_probe *tp = (struct trace_probe *)call->data; |
1409 | ||
d7a4b414 | 1410 | tp->flags &= ~TP_FLAG_PROFILE; |
50d78056 | 1411 | |
d7a4b414 | 1412 | if (!(tp->flags & TP_FLAG_TRACE)) { |
50d78056 MH |
1413 | if (probe_is_return(tp)) |
1414 | disable_kretprobe(&tp->rp); | |
1415 | else | |
1416 | disable_kprobe(&tp->rp.kp); | |
1417 | } | |
e08d1c65 | 1418 | } |
07b139c8 | 1419 | #endif /* CONFIG_PERF_EVENTS */ |
50d78056 | 1420 | |
2239291a SR |
1421 | static __kprobes |
1422 | int kprobe_register(struct ftrace_event_call *event, enum trace_reg type) | |
1423 | { | |
1424 | switch (type) { | |
1425 | case TRACE_REG_REGISTER: | |
1426 | return probe_event_enable(event); | |
1427 | case TRACE_REG_UNREGISTER: | |
1428 | probe_event_disable(event); | |
1429 | return 0; | |
1430 | ||
1431 | #ifdef CONFIG_PERF_EVENTS | |
1432 | case TRACE_REG_PERF_REGISTER: | |
1433 | return probe_perf_enable(event); | |
1434 | case TRACE_REG_PERF_UNREGISTER: | |
1435 | probe_perf_disable(event); | |
1436 | return 0; | |
1437 | #endif | |
1438 | } | |
1439 | return 0; | |
1440 | } | |
50d78056 MH |
1441 | |
1442 | static __kprobes | |
1443 | int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs) | |
1444 | { | |
1445 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); | |
e08d1c65 | 1446 | |
50d78056 MH |
1447 | if (tp->flags & TP_FLAG_TRACE) |
1448 | kprobe_trace_func(kp, regs); | |
07b139c8 | 1449 | #ifdef CONFIG_PERF_EVENTS |
50d78056 | 1450 | if (tp->flags & TP_FLAG_PROFILE) |
97d5a220 | 1451 | kprobe_perf_func(kp, regs); |
07b139c8 | 1452 | #endif |
50d78056 MH |
1453 | return 0; /* We don't tweek kernel, so just return 0 */ |
1454 | } | |
1455 | ||
1456 | static __kprobes | |
1457 | int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs) | |
1458 | { | |
1459 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); | |
1460 | ||
1461 | if (tp->flags & TP_FLAG_TRACE) | |
1462 | kretprobe_trace_func(ri, regs); | |
07b139c8 | 1463 | #ifdef CONFIG_PERF_EVENTS |
50d78056 | 1464 | if (tp->flags & TP_FLAG_PROFILE) |
97d5a220 | 1465 | kretprobe_perf_func(ri, regs); |
07b139c8 | 1466 | #endif |
50d78056 MH |
1467 | return 0; /* We don't tweek kernel, so just return 0 */ |
1468 | } | |
e08d1c65 | 1469 | |
a9a57763 SR |
1470 | static struct trace_event_functions kretprobe_funcs = { |
1471 | .trace = print_kretprobe_event | |
1472 | }; | |
1473 | ||
1474 | static struct trace_event_functions kprobe_funcs = { | |
1475 | .trace = print_kprobe_event | |
1476 | }; | |
1477 | ||
413d37d1 MH |
1478 | static int register_probe_event(struct trace_probe *tp) |
1479 | { | |
1480 | struct ftrace_event_call *call = &tp->call; | |
1481 | int ret; | |
1482 | ||
1483 | /* Initialize ftrace_event_call */ | |
ffb9f995 | 1484 | INIT_LIST_HEAD(&call->class->fields); |
413d37d1 | 1485 | if (probe_is_return(tp)) { |
80decc70 | 1486 | call->event.funcs = &kretprobe_funcs; |
2e33af02 | 1487 | call->class->define_fields = kretprobe_event_define_fields; |
413d37d1 | 1488 | } else { |
80decc70 | 1489 | call->event.funcs = &kprobe_funcs; |
2e33af02 | 1490 | call->class->define_fields = kprobe_event_define_fields; |
413d37d1 | 1491 | } |
a342a028 LJ |
1492 | if (set_print_fmt(tp) < 0) |
1493 | return -ENOMEM; | |
32c0edae SR |
1494 | ret = register_ftrace_event(&call->event); |
1495 | if (!ret) { | |
a342a028 | 1496 | kfree(call->print_fmt); |
ff50d991 | 1497 | return -ENODEV; |
a342a028 | 1498 | } |
553552ce | 1499 | call->flags = 0; |
2239291a | 1500 | call->class->reg = kprobe_register; |
413d37d1 MH |
1501 | call->data = tp; |
1502 | ret = trace_add_event_call(call); | |
ff50d991 | 1503 | if (ret) { |
413d37d1 | 1504 | pr_info("Failed to register kprobe event: %s\n", call->name); |
a342a028 | 1505 | kfree(call->print_fmt); |
80decc70 | 1506 | unregister_ftrace_event(&call->event); |
ff50d991 | 1507 | } |
413d37d1 MH |
1508 | return ret; |
1509 | } | |
1510 | ||
1511 | static void unregister_probe_event(struct trace_probe *tp) | |
1512 | { | |
ff50d991 | 1513 | /* tp->event is unregistered in trace_remove_event_call() */ |
413d37d1 | 1514 | trace_remove_event_call(&tp->call); |
a342a028 | 1515 | kfree(tp->call.print_fmt); |
413d37d1 MH |
1516 | } |
1517 | ||
1518 | /* Make a debugfs interface for controling probe points */ | |
1519 | static __init int init_kprobe_trace(void) | |
1520 | { | |
1521 | struct dentry *d_tracer; | |
1522 | struct dentry *entry; | |
413d37d1 MH |
1523 | |
1524 | d_tracer = tracing_init_dentry(); | |
1525 | if (!d_tracer) | |
1526 | return 0; | |
1527 | ||
1528 | entry = debugfs_create_file("kprobe_events", 0644, d_tracer, | |
1529 | NULL, &kprobe_events_ops); | |
1530 | ||
cd7e7bd5 | 1531 | /* Event list interface */ |
413d37d1 MH |
1532 | if (!entry) |
1533 | pr_warning("Could not create debugfs " | |
1534 | "'kprobe_events' entry\n"); | |
cd7e7bd5 MH |
1535 | |
1536 | /* Profile interface */ | |
1537 | entry = debugfs_create_file("kprobe_profile", 0444, d_tracer, | |
1538 | NULL, &kprobe_profile_ops); | |
1539 | ||
1540 | if (!entry) | |
1541 | pr_warning("Could not create debugfs " | |
1542 | "'kprobe_profile' entry\n"); | |
413d37d1 MH |
1543 | return 0; |
1544 | } | |
1545 | fs_initcall(init_kprobe_trace); | |
1546 | ||
1547 | ||
1548 | #ifdef CONFIG_FTRACE_STARTUP_TEST | |
1549 | ||
1550 | static int kprobe_trace_selftest_target(int a1, int a2, int a3, | |
1551 | int a4, int a5, int a6) | |
1552 | { | |
1553 | return a1 + a2 + a3 + a4 + a5 + a6; | |
1554 | } | |
1555 | ||
1556 | static __init int kprobe_trace_self_tests_init(void) | |
1557 | { | |
231e36f4 | 1558 | int ret, warn = 0; |
413d37d1 | 1559 | int (*target)(int, int, int, int, int, int); |
231e36f4 | 1560 | struct trace_probe *tp; |
413d37d1 MH |
1561 | |
1562 | target = kprobe_trace_selftest_target; | |
1563 | ||
1564 | pr_info("Testing kprobe tracing: "); | |
1565 | ||
1566 | ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target " | |
231e36f4 MH |
1567 | "$stack $stack0 +0($stack)"); |
1568 | if (WARN_ON_ONCE(ret)) { | |
1569 | pr_warning("error on probing function entry.\n"); | |
1570 | warn++; | |
1571 | } else { | |
1572 | /* Enable trace point */ | |
1573 | tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM); | |
1574 | if (WARN_ON_ONCE(tp == NULL)) { | |
1575 | pr_warning("error on getting new probe.\n"); | |
1576 | warn++; | |
1577 | } else | |
1578 | probe_event_enable(&tp->call); | |
1579 | } | |
413d37d1 MH |
1580 | |
1581 | ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target " | |
f397af06 | 1582 | "$retval"); |
231e36f4 MH |
1583 | if (WARN_ON_ONCE(ret)) { |
1584 | pr_warning("error on probing function return.\n"); | |
1585 | warn++; | |
1586 | } else { | |
1587 | /* Enable trace point */ | |
1588 | tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM); | |
1589 | if (WARN_ON_ONCE(tp == NULL)) { | |
1590 | pr_warning("error on getting new probe.\n"); | |
1591 | warn++; | |
1592 | } else | |
1593 | probe_event_enable(&tp->call); | |
1594 | } | |
1595 | ||
1596 | if (warn) | |
1597 | goto end; | |
413d37d1 MH |
1598 | |
1599 | ret = target(1, 2, 3, 4, 5, 6); | |
1600 | ||
231e36f4 MH |
1601 | ret = command_trace_probe("-:testprobe"); |
1602 | if (WARN_ON_ONCE(ret)) { | |
1603 | pr_warning("error on deleting a probe.\n"); | |
1604 | warn++; | |
1605 | } | |
1606 | ||
1607 | ret = command_trace_probe("-:testprobe2"); | |
1608 | if (WARN_ON_ONCE(ret)) { | |
1609 | pr_warning("error on deleting a probe.\n"); | |
1610 | warn++; | |
1611 | } | |
413d37d1 | 1612 | |
231e36f4 MH |
1613 | end: |
1614 | cleanup_all_probes(); | |
1615 | if (warn) | |
1616 | pr_cont("NG: Some tests are failed. Please check them.\n"); | |
1617 | else | |
1618 | pr_cont("OK\n"); | |
413d37d1 MH |
1619 | return 0; |
1620 | } | |
1621 | ||
1622 | late_initcall(kprobe_trace_self_tests_init); | |
1623 | ||
1624 | #endif |