]>
Commit | Line | Data |
---|---|---|
413d37d1 MH |
1 | /* |
2 | * kprobe based kernel tracer | |
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> | |
e08d1c65 | 31 | #include <linux/perf_counter.h> |
413d37d1 MH |
32 | |
33 | #include "trace.h" | |
34 | #include "trace_output.h" | |
35 | ||
a82378d8 | 36 | #define MAX_TRACE_ARGS 128 |
413d37d1 | 37 | #define MAX_ARGSTR_LEN 63 |
4263565d | 38 | #define MAX_EVENT_NAME_LEN 64 |
f52487e9 | 39 | #define KPROBE_EVENT_SYSTEM "kprobes" |
413d37d1 MH |
40 | |
41 | /* currently, trace_kprobe only supports X86. */ | |
42 | ||
43 | struct fetch_func { | |
44 | unsigned long (*func)(struct pt_regs *, void *); | |
45 | void *data; | |
46 | }; | |
47 | ||
48 | static __kprobes unsigned long call_fetch(struct fetch_func *f, | |
49 | struct pt_regs *regs) | |
50 | { | |
51 | return f->func(regs, f->data); | |
52 | } | |
53 | ||
54 | /* fetch handlers */ | |
55 | static __kprobes unsigned long fetch_register(struct pt_regs *regs, | |
56 | void *offset) | |
57 | { | |
58 | return regs_get_register(regs, (unsigned int)((unsigned long)offset)); | |
59 | } | |
60 | ||
61 | static __kprobes unsigned long fetch_stack(struct pt_regs *regs, | |
62 | void *num) | |
63 | { | |
64 | return regs_get_kernel_stack_nth(regs, | |
65 | (unsigned int)((unsigned long)num)); | |
66 | } | |
67 | ||
68 | static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr) | |
69 | { | |
70 | unsigned long retval; | |
71 | ||
72 | if (probe_kernel_address(addr, retval)) | |
73 | return 0; | |
74 | return retval; | |
75 | } | |
76 | ||
77 | static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num) | |
78 | { | |
79 | return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num)); | |
80 | } | |
81 | ||
82 | static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs, | |
83 | void *dummy) | |
84 | { | |
85 | return regs_return_value(regs); | |
86 | } | |
87 | ||
88 | static __kprobes unsigned long fetch_ip(struct pt_regs *regs, void *dummy) | |
89 | { | |
90 | return instruction_pointer(regs); | |
91 | } | |
92 | ||
93 | static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs, | |
94 | void *dummy) | |
95 | { | |
96 | return kernel_stack_pointer(regs); | |
97 | } | |
98 | ||
99 | /* Memory fetching by symbol */ | |
100 | struct symbol_cache { | |
101 | char *symbol; | |
102 | long offset; | |
103 | unsigned long addr; | |
104 | }; | |
105 | ||
106 | static unsigned long update_symbol_cache(struct symbol_cache *sc) | |
107 | { | |
108 | sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol); | |
109 | if (sc->addr) | |
110 | sc->addr += sc->offset; | |
111 | return sc->addr; | |
112 | } | |
113 | ||
114 | static void free_symbol_cache(struct symbol_cache *sc) | |
115 | { | |
116 | kfree(sc->symbol); | |
117 | kfree(sc); | |
118 | } | |
119 | ||
120 | static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset) | |
121 | { | |
122 | struct symbol_cache *sc; | |
123 | ||
124 | if (!sym || strlen(sym) == 0) | |
125 | return NULL; | |
126 | sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL); | |
127 | if (!sc) | |
128 | return NULL; | |
129 | ||
130 | sc->symbol = kstrdup(sym, GFP_KERNEL); | |
131 | if (!sc->symbol) { | |
132 | kfree(sc); | |
133 | return NULL; | |
134 | } | |
135 | sc->offset = offset; | |
136 | ||
137 | update_symbol_cache(sc); | |
138 | return sc; | |
139 | } | |
140 | ||
141 | static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data) | |
142 | { | |
143 | struct symbol_cache *sc = data; | |
144 | ||
145 | if (sc->addr) | |
146 | return fetch_memory(regs, (void *)sc->addr); | |
147 | else | |
148 | return 0; | |
149 | } | |
150 | ||
151 | /* Special indirect memory access interface */ | |
152 | struct indirect_fetch_data { | |
153 | struct fetch_func orig; | |
154 | long offset; | |
155 | }; | |
156 | ||
157 | static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data) | |
158 | { | |
159 | struct indirect_fetch_data *ind = data; | |
160 | unsigned long addr; | |
161 | ||
162 | addr = call_fetch(&ind->orig, regs); | |
163 | if (addr) { | |
164 | addr += ind->offset; | |
165 | return fetch_memory(regs, (void *)addr); | |
166 | } else | |
167 | return 0; | |
168 | } | |
169 | ||
170 | static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data) | |
171 | { | |
172 | if (data->orig.func == fetch_indirect) | |
173 | free_indirect_fetch_data(data->orig.data); | |
174 | else if (data->orig.func == fetch_symbol) | |
175 | free_symbol_cache(data->orig.data); | |
176 | kfree(data); | |
177 | } | |
178 | ||
179 | /** | |
eca0d916 | 180 | * Kprobe tracer core functions |
413d37d1 MH |
181 | */ |
182 | ||
eca0d916 MH |
183 | struct probe_arg { |
184 | struct fetch_func fetch; | |
185 | const char *name; | |
186 | }; | |
187 | ||
413d37d1 MH |
188 | struct trace_probe { |
189 | struct list_head list; | |
4a846b44 | 190 | struct kretprobe rp; /* Use rp.kp for kprobe use */ |
cd7e7bd5 | 191 | unsigned long nhit; |
413d37d1 | 192 | const char *symbol; /* symbol name */ |
413d37d1 | 193 | struct ftrace_event_call call; |
ff50d991 | 194 | struct trace_event event; |
a82378d8 | 195 | unsigned int nr_args; |
eca0d916 | 196 | struct probe_arg args[]; |
413d37d1 MH |
197 | }; |
198 | ||
a82378d8 MH |
199 | #define SIZEOF_TRACE_PROBE(n) \ |
200 | (offsetof(struct trace_probe, args) + \ | |
eca0d916 | 201 | (sizeof(struct probe_arg) * (n))) |
a82378d8 | 202 | |
413d37d1 MH |
203 | static int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs); |
204 | static int kretprobe_trace_func(struct kretprobe_instance *ri, | |
205 | struct pt_regs *regs); | |
206 | ||
207 | static __kprobes int probe_is_return(struct trace_probe *tp) | |
208 | { | |
4a846b44 | 209 | return tp->rp.handler != NULL; |
413d37d1 MH |
210 | } |
211 | ||
212 | static __kprobes const char *probe_symbol(struct trace_probe *tp) | |
213 | { | |
214 | return tp->symbol ? tp->symbol : "unknown"; | |
215 | } | |
216 | ||
30a7e073 | 217 | static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff) |
413d37d1 MH |
218 | { |
219 | int ret = -EINVAL; | |
220 | ||
221 | if (ff->func == fetch_argument) | |
222 | ret = snprintf(buf, n, "a%lu", (unsigned long)ff->data); | |
223 | else if (ff->func == fetch_register) { | |
224 | const char *name; | |
225 | name = regs_query_register_name((unsigned int)((long)ff->data)); | |
226 | ret = snprintf(buf, n, "%%%s", name); | |
227 | } else if (ff->func == fetch_stack) | |
228 | ret = snprintf(buf, n, "s%lu", (unsigned long)ff->data); | |
229 | else if (ff->func == fetch_memory) | |
230 | ret = snprintf(buf, n, "@0x%p", ff->data); | |
231 | else if (ff->func == fetch_symbol) { | |
232 | struct symbol_cache *sc = ff->data; | |
233 | ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset); | |
234 | } else if (ff->func == fetch_retvalue) | |
235 | ret = snprintf(buf, n, "rv"); | |
236 | else if (ff->func == fetch_ip) | |
237 | ret = snprintf(buf, n, "ra"); | |
238 | else if (ff->func == fetch_stack_address) | |
239 | ret = snprintf(buf, n, "sa"); | |
240 | else if (ff->func == fetch_indirect) { | |
241 | struct indirect_fetch_data *id = ff->data; | |
242 | size_t l = 0; | |
243 | ret = snprintf(buf, n, "%+ld(", id->offset); | |
244 | if (ret >= n) | |
245 | goto end; | |
246 | l += ret; | |
30a7e073 | 247 | ret = probe_arg_string(buf + l, n - l, &id->orig); |
413d37d1 MH |
248 | if (ret < 0) |
249 | goto end; | |
250 | l += ret; | |
251 | ret = snprintf(buf + l, n - l, ")"); | |
252 | ret += l; | |
253 | } | |
254 | end: | |
255 | if (ret >= n) | |
256 | return -ENOSPC; | |
257 | return ret; | |
258 | } | |
259 | ||
260 | static int register_probe_event(struct trace_probe *tp); | |
261 | static void unregister_probe_event(struct trace_probe *tp); | |
262 | ||
263 | static DEFINE_MUTEX(probe_lock); | |
264 | static LIST_HEAD(probe_list); | |
265 | ||
4a846b44 MH |
266 | /* |
267 | * Allocate new trace_probe and initialize it (including kprobes). | |
268 | */ | |
f52487e9 MH |
269 | static struct trace_probe *alloc_trace_probe(const char *group, |
270 | const char *event, | |
4a846b44 MH |
271 | void *addr, |
272 | const char *symbol, | |
273 | unsigned long offs, | |
274 | int nargs, int is_return) | |
413d37d1 MH |
275 | { |
276 | struct trace_probe *tp; | |
277 | ||
a82378d8 | 278 | tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL); |
413d37d1 MH |
279 | if (!tp) |
280 | return ERR_PTR(-ENOMEM); | |
281 | ||
282 | if (symbol) { | |
283 | tp->symbol = kstrdup(symbol, GFP_KERNEL); | |
284 | if (!tp->symbol) | |
285 | goto error; | |
4a846b44 MH |
286 | tp->rp.kp.symbol_name = tp->symbol; |
287 | tp->rp.kp.offset = offs; | |
288 | } else | |
289 | tp->rp.kp.addr = addr; | |
290 | ||
e08d1c65 | 291 | /* Set handler here for checking whether this probe is return or not. */ |
4a846b44 MH |
292 | if (is_return) |
293 | tp->rp.handler = kretprobe_trace_func; | |
294 | else | |
295 | tp->rp.kp.pre_handler = kprobe_trace_func; | |
296 | ||
4263565d MH |
297 | if (!event) |
298 | goto error; | |
299 | tp->call.name = kstrdup(event, GFP_KERNEL); | |
300 | if (!tp->call.name) | |
301 | goto error; | |
413d37d1 | 302 | |
f52487e9 MH |
303 | if (!group) |
304 | goto error; | |
305 | tp->call.system = kstrdup(group, GFP_KERNEL); | |
306 | if (!tp->call.system) | |
307 | goto error; | |
308 | ||
413d37d1 MH |
309 | INIT_LIST_HEAD(&tp->list); |
310 | return tp; | |
311 | error: | |
f52487e9 | 312 | kfree(tp->call.name); |
413d37d1 MH |
313 | kfree(tp->symbol); |
314 | kfree(tp); | |
315 | return ERR_PTR(-ENOMEM); | |
316 | } | |
317 | ||
eca0d916 MH |
318 | static void free_probe_arg(struct probe_arg *arg) |
319 | { | |
320 | if (arg->fetch.func == fetch_symbol) | |
321 | free_symbol_cache(arg->fetch.data); | |
322 | else if (arg->fetch.func == fetch_indirect) | |
323 | free_indirect_fetch_data(arg->fetch.data); | |
324 | kfree(arg->name); | |
325 | } | |
326 | ||
413d37d1 MH |
327 | static void free_trace_probe(struct trace_probe *tp) |
328 | { | |
329 | int i; | |
330 | ||
331 | for (i = 0; i < tp->nr_args; i++) | |
eca0d916 | 332 | free_probe_arg(&tp->args[i]); |
413d37d1 | 333 | |
f52487e9 | 334 | kfree(tp->call.system); |
413d37d1 MH |
335 | kfree(tp->call.name); |
336 | kfree(tp->symbol); | |
337 | kfree(tp); | |
338 | } | |
339 | ||
340 | static struct trace_probe *find_probe_event(const char *event) | |
341 | { | |
342 | struct trace_probe *tp; | |
343 | ||
344 | list_for_each_entry(tp, &probe_list, list) | |
4263565d | 345 | if (!strcmp(tp->call.name, event)) |
413d37d1 MH |
346 | return tp; |
347 | return NULL; | |
348 | } | |
349 | ||
2d5e067e MH |
350 | /* Unregister a trace_probe and probe_event: call with locking probe_lock */ |
351 | static void unregister_trace_probe(struct trace_probe *tp) | |
413d37d1 MH |
352 | { |
353 | if (probe_is_return(tp)) | |
354 | unregister_kretprobe(&tp->rp); | |
355 | else | |
4a846b44 | 356 | unregister_kprobe(&tp->rp.kp); |
413d37d1 | 357 | list_del(&tp->list); |
2d5e067e | 358 | unregister_probe_event(tp); |
413d37d1 MH |
359 | } |
360 | ||
361 | /* Register a trace_probe and probe_event */ | |
362 | static int register_trace_probe(struct trace_probe *tp) | |
363 | { | |
364 | struct trace_probe *old_tp; | |
365 | int ret; | |
366 | ||
367 | mutex_lock(&probe_lock); | |
368 | ||
2d5e067e MH |
369 | /* register as an event */ |
370 | old_tp = find_probe_event(tp->call.name); | |
371 | if (old_tp) { | |
372 | /* delete old event */ | |
373 | unregister_trace_probe(old_tp); | |
374 | free_trace_probe(old_tp); | |
375 | } | |
376 | ret = register_probe_event(tp); | |
377 | if (ret) { | |
378 | pr_warning("Faild to register probe event(%d)\n", ret); | |
379 | goto end; | |
380 | } | |
381 | ||
413d37d1 MH |
382 | if (probe_is_return(tp)) |
383 | ret = register_kretprobe(&tp->rp); | |
384 | else | |
4a846b44 | 385 | ret = register_kprobe(&tp->rp.kp); |
413d37d1 MH |
386 | |
387 | if (ret) { | |
388 | pr_warning("Could not insert probe(%d)\n", ret); | |
389 | if (ret == -EILSEQ) { | |
390 | pr_warning("Probing address(0x%p) is not an " | |
391 | "instruction boundary.\n", | |
4a846b44 | 392 | tp->rp.kp.addr); |
413d37d1 MH |
393 | ret = -EINVAL; |
394 | } | |
2d5e067e MH |
395 | unregister_probe_event(tp); |
396 | } else | |
397 | list_add_tail(&tp->list, &probe_list); | |
413d37d1 MH |
398 | end: |
399 | mutex_unlock(&probe_lock); | |
400 | return ret; | |
401 | } | |
402 | ||
403 | /* Split symbol and offset. */ | |
2fba0c88 | 404 | static int split_symbol_offset(char *symbol, unsigned long *offset) |
413d37d1 MH |
405 | { |
406 | char *tmp; | |
407 | int ret; | |
408 | ||
409 | if (!offset) | |
410 | return -EINVAL; | |
411 | ||
412 | tmp = strchr(symbol, '+'); | |
413d37d1 MH |
413 | if (tmp) { |
414 | /* skip sign because strict_strtol doesn't accept '+' */ | |
2fba0c88 | 415 | ret = strict_strtoul(tmp + 1, 0, offset); |
413d37d1 MH |
416 | if (ret) |
417 | return ret; | |
413d37d1 MH |
418 | *tmp = '\0'; |
419 | } else | |
420 | *offset = 0; | |
421 | return 0; | |
422 | } | |
423 | ||
424 | #define PARAM_MAX_ARGS 16 | |
425 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) | |
426 | ||
30a7e073 | 427 | static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return) |
413d37d1 MH |
428 | { |
429 | int ret = 0; | |
430 | unsigned long param; | |
431 | long offset; | |
432 | char *tmp; | |
433 | ||
434 | switch (arg[0]) { | |
435 | case 'a': /* argument */ | |
436 | ret = strict_strtoul(arg + 1, 10, ¶m); | |
437 | if (ret || param > PARAM_MAX_ARGS) | |
438 | ret = -EINVAL; | |
439 | else { | |
440 | ff->func = fetch_argument; | |
441 | ff->data = (void *)param; | |
442 | } | |
443 | break; | |
444 | case 'r': /* retval or retaddr */ | |
445 | if (is_return && arg[1] == 'v') { | |
446 | ff->func = fetch_retvalue; | |
447 | ff->data = NULL; | |
448 | } else if (is_return && arg[1] == 'a') { | |
449 | ff->func = fetch_ip; | |
450 | ff->data = NULL; | |
451 | } else | |
452 | ret = -EINVAL; | |
453 | break; | |
454 | case '%': /* named register */ | |
455 | ret = regs_query_register_offset(arg + 1); | |
456 | if (ret >= 0) { | |
457 | ff->func = fetch_register; | |
458 | ff->data = (void *)(unsigned long)ret; | |
459 | ret = 0; | |
460 | } | |
461 | break; | |
462 | case 's': /* stack */ | |
463 | if (arg[1] == 'a') { | |
464 | ff->func = fetch_stack_address; | |
465 | ff->data = NULL; | |
466 | } else { | |
467 | ret = strict_strtoul(arg + 1, 10, ¶m); | |
468 | if (ret || param > PARAM_MAX_STACK) | |
469 | ret = -EINVAL; | |
470 | else { | |
471 | ff->func = fetch_stack; | |
472 | ff->data = (void *)param; | |
473 | } | |
474 | } | |
475 | break; | |
476 | case '@': /* memory or symbol */ | |
477 | if (isdigit(arg[1])) { | |
478 | ret = strict_strtoul(arg + 1, 0, ¶m); | |
479 | if (ret) | |
480 | break; | |
481 | ff->func = fetch_memory; | |
482 | ff->data = (void *)param; | |
483 | } else { | |
484 | ret = split_symbol_offset(arg + 1, &offset); | |
485 | if (ret) | |
486 | break; | |
487 | ff->data = alloc_symbol_cache(arg + 1, | |
488 | offset); | |
489 | if (ff->data) | |
490 | ff->func = fetch_symbol; | |
491 | else | |
492 | ret = -EINVAL; | |
493 | } | |
494 | break; | |
495 | case '+': /* indirect memory */ | |
496 | case '-': | |
497 | tmp = strchr(arg, '('); | |
498 | if (!tmp) { | |
499 | ret = -EINVAL; | |
500 | break; | |
501 | } | |
502 | *tmp = '\0'; | |
503 | ret = strict_strtol(arg + 1, 0, &offset); | |
504 | if (ret) | |
505 | break; | |
506 | if (arg[0] == '-') | |
507 | offset = -offset; | |
508 | arg = tmp + 1; | |
509 | tmp = strrchr(arg, ')'); | |
510 | if (tmp) { | |
511 | struct indirect_fetch_data *id; | |
512 | *tmp = '\0'; | |
513 | id = kzalloc(sizeof(struct indirect_fetch_data), | |
514 | GFP_KERNEL); | |
515 | if (!id) | |
516 | return -ENOMEM; | |
517 | id->offset = offset; | |
30a7e073 | 518 | ret = parse_probe_arg(arg, &id->orig, is_return); |
413d37d1 MH |
519 | if (ret) |
520 | kfree(id); | |
521 | else { | |
522 | ff->func = fetch_indirect; | |
523 | ff->data = (void *)id; | |
524 | } | |
525 | } else | |
526 | ret = -EINVAL; | |
527 | break; | |
528 | default: | |
529 | /* TODO: support custom handler */ | |
530 | ret = -EINVAL; | |
531 | } | |
532 | return ret; | |
533 | } | |
534 | ||
535 | static int create_trace_probe(int argc, char **argv) | |
536 | { | |
537 | /* | |
538 | * Argument syntax: | |
f52487e9 MH |
539 | * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS] |
540 | * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS] | |
413d37d1 MH |
541 | * Fetch args: |
542 | * aN : fetch Nth of function argument. (N:0-) | |
543 | * rv : fetch return value | |
544 | * ra : fetch return address | |
545 | * sa : fetch stack address | |
546 | * sN : fetch Nth of stack (N:0-) | |
547 | * @ADDR : fetch memory at ADDR (ADDR should be in kernel) | |
548 | * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol) | |
549 | * %REG : fetch register REG | |
550 | * Indirect memory fetch: | |
551 | * +|-offs(ARG) : fetch memory at ARG +|- offs address. | |
eca0d916 MH |
552 | * Alias name of args: |
553 | * NAME=FETCHARG : set NAME as alias of FETCHARG. | |
413d37d1 MH |
554 | */ |
555 | struct trace_probe *tp; | |
413d37d1 MH |
556 | int i, ret = 0; |
557 | int is_return = 0; | |
f52487e9 | 558 | char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL; |
2fba0c88 | 559 | unsigned long offset = 0; |
413d37d1 | 560 | void *addr = NULL; |
4a846b44 | 561 | char buf[MAX_EVENT_NAME_LEN]; |
413d37d1 MH |
562 | |
563 | if (argc < 2) | |
564 | return -EINVAL; | |
565 | ||
566 | if (argv[0][0] == 'p') | |
567 | is_return = 0; | |
568 | else if (argv[0][0] == 'r') | |
569 | is_return = 1; | |
570 | else | |
571 | return -EINVAL; | |
572 | ||
573 | if (argv[0][1] == ':') { | |
574 | event = &argv[0][2]; | |
f52487e9 MH |
575 | if (strchr(event, '/')) { |
576 | group = event; | |
577 | event = strchr(group, '/') + 1; | |
578 | event[-1] = '\0'; | |
579 | if (strlen(group) == 0) { | |
580 | pr_info("Group name is not specifiled\n"); | |
581 | return -EINVAL; | |
582 | } | |
583 | } | |
413d37d1 MH |
584 | if (strlen(event) == 0) { |
585 | pr_info("Event name is not specifiled\n"); | |
586 | return -EINVAL; | |
587 | } | |
588 | } | |
589 | ||
590 | if (isdigit(argv[1][0])) { | |
591 | if (is_return) | |
592 | return -EINVAL; | |
593 | /* an address specified */ | |
594 | ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr); | |
595 | if (ret) | |
596 | return ret; | |
597 | } else { | |
598 | /* a symbol specified */ | |
599 | symbol = argv[1]; | |
600 | /* TODO: support .init module functions */ | |
601 | ret = split_symbol_offset(symbol, &offset); | |
602 | if (ret) | |
603 | return ret; | |
604 | if (offset && is_return) | |
605 | return -EINVAL; | |
606 | } | |
a82378d8 | 607 | argc -= 2; argv += 2; |
413d37d1 MH |
608 | |
609 | /* setup a probe */ | |
f52487e9 MH |
610 | if (!group) |
611 | group = KPROBE_EVENT_SYSTEM; | |
4263565d MH |
612 | if (!event) { |
613 | /* Make a new event name */ | |
4263565d MH |
614 | if (symbol) |
615 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld", | |
616 | is_return ? 'r' : 'p', symbol, offset); | |
617 | else | |
618 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p", | |
619 | is_return ? 'r' : 'p', addr); | |
4a846b44 MH |
620 | event = buf; |
621 | } | |
f52487e9 MH |
622 | tp = alloc_trace_probe(group, event, addr, symbol, offset, argc, |
623 | is_return); | |
413d37d1 MH |
624 | if (IS_ERR(tp)) |
625 | return PTR_ERR(tp); | |
626 | ||
413d37d1 | 627 | /* parse arguments */ |
a82378d8 MH |
628 | ret = 0; |
629 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { | |
eca0d916 MH |
630 | /* Parse argument name */ |
631 | arg = strchr(argv[i], '='); | |
632 | if (arg) | |
633 | *arg++ = '\0'; | |
634 | else | |
635 | arg = argv[i]; | |
636 | tp->args[i].name = kstrdup(argv[i], GFP_KERNEL); | |
637 | ||
638 | /* Parse fetch argument */ | |
639 | if (strlen(arg) > MAX_ARGSTR_LEN) { | |
640 | pr_info("Argument%d(%s) is too long.\n", i, arg); | |
413d37d1 MH |
641 | ret = -ENOSPC; |
642 | goto error; | |
643 | } | |
eca0d916 | 644 | ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return); |
413d37d1 MH |
645 | if (ret) |
646 | goto error; | |
647 | } | |
648 | tp->nr_args = i; | |
649 | ||
650 | ret = register_trace_probe(tp); | |
651 | if (ret) | |
652 | goto error; | |
653 | return 0; | |
654 | ||
655 | error: | |
656 | free_trace_probe(tp); | |
657 | return ret; | |
658 | } | |
659 | ||
660 | static void cleanup_all_probes(void) | |
661 | { | |
662 | struct trace_probe *tp; | |
663 | ||
664 | mutex_lock(&probe_lock); | |
665 | /* TODO: Use batch unregistration */ | |
666 | while (!list_empty(&probe_list)) { | |
667 | tp = list_entry(probe_list.next, struct trace_probe, list); | |
668 | unregister_trace_probe(tp); | |
669 | free_trace_probe(tp); | |
670 | } | |
671 | mutex_unlock(&probe_lock); | |
672 | } | |
673 | ||
674 | ||
675 | /* Probes listing interfaces */ | |
676 | static void *probes_seq_start(struct seq_file *m, loff_t *pos) | |
677 | { | |
678 | mutex_lock(&probe_lock); | |
679 | return seq_list_start(&probe_list, *pos); | |
680 | } | |
681 | ||
682 | static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos) | |
683 | { | |
684 | return seq_list_next(v, &probe_list, pos); | |
685 | } | |
686 | ||
687 | static void probes_seq_stop(struct seq_file *m, void *v) | |
688 | { | |
689 | mutex_unlock(&probe_lock); | |
690 | } | |
691 | ||
692 | static int probes_seq_show(struct seq_file *m, void *v) | |
693 | { | |
694 | struct trace_probe *tp = v; | |
695 | int i, ret; | |
696 | char buf[MAX_ARGSTR_LEN + 1]; | |
697 | ||
698 | seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p'); | |
4263565d | 699 | seq_printf(m, ":%s", tp->call.name); |
413d37d1 MH |
700 | |
701 | if (tp->symbol) | |
4a846b44 | 702 | seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset); |
413d37d1 | 703 | else |
4a846b44 | 704 | seq_printf(m, " 0x%p", tp->rp.kp.addr); |
413d37d1 MH |
705 | |
706 | for (i = 0; i < tp->nr_args; i++) { | |
eca0d916 | 707 | ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch); |
413d37d1 MH |
708 | if (ret < 0) { |
709 | pr_warning("Argument%d decoding error(%d).\n", i, ret); | |
710 | return ret; | |
711 | } | |
eca0d916 | 712 | seq_printf(m, " %s=%s", tp->args[i].name, buf); |
413d37d1 MH |
713 | } |
714 | seq_printf(m, "\n"); | |
715 | return 0; | |
716 | } | |
717 | ||
718 | static const struct seq_operations probes_seq_op = { | |
719 | .start = probes_seq_start, | |
720 | .next = probes_seq_next, | |
721 | .stop = probes_seq_stop, | |
722 | .show = probes_seq_show | |
723 | }; | |
724 | ||
725 | static int probes_open(struct inode *inode, struct file *file) | |
726 | { | |
727 | if ((file->f_mode & FMODE_WRITE) && | |
728 | (file->f_flags & O_TRUNC)) | |
729 | cleanup_all_probes(); | |
730 | ||
731 | return seq_open(file, &probes_seq_op); | |
732 | } | |
733 | ||
734 | static int command_trace_probe(const char *buf) | |
735 | { | |
736 | char **argv; | |
737 | int argc = 0, ret = 0; | |
738 | ||
739 | argv = argv_split(GFP_KERNEL, buf, &argc); | |
740 | if (!argv) | |
741 | return -ENOMEM; | |
742 | ||
743 | if (argc) | |
744 | ret = create_trace_probe(argc, argv); | |
745 | ||
746 | argv_free(argv); | |
747 | return ret; | |
748 | } | |
749 | ||
750 | #define WRITE_BUFSIZE 128 | |
751 | ||
752 | static ssize_t probes_write(struct file *file, const char __user *buffer, | |
753 | size_t count, loff_t *ppos) | |
754 | { | |
755 | char *kbuf, *tmp; | |
756 | int ret; | |
757 | size_t done; | |
758 | size_t size; | |
759 | ||
760 | kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL); | |
761 | if (!kbuf) | |
762 | return -ENOMEM; | |
763 | ||
764 | ret = done = 0; | |
765 | while (done < count) { | |
766 | size = count - done; | |
767 | if (size >= WRITE_BUFSIZE) | |
768 | size = WRITE_BUFSIZE - 1; | |
769 | if (copy_from_user(kbuf, buffer + done, size)) { | |
770 | ret = -EFAULT; | |
771 | goto out; | |
772 | } | |
773 | kbuf[size] = '\0'; | |
774 | tmp = strchr(kbuf, '\n'); | |
775 | if (tmp) { | |
776 | *tmp = '\0'; | |
777 | size = tmp - kbuf + 1; | |
778 | } else if (done + size < count) { | |
779 | pr_warning("Line length is too long: " | |
780 | "Should be less than %d.", WRITE_BUFSIZE); | |
781 | ret = -EINVAL; | |
782 | goto out; | |
783 | } | |
784 | done += size; | |
785 | /* Remove comments */ | |
786 | tmp = strchr(kbuf, '#'); | |
787 | if (tmp) | |
788 | *tmp = '\0'; | |
789 | ||
790 | ret = command_trace_probe(kbuf); | |
791 | if (ret) | |
792 | goto out; | |
793 | } | |
794 | ret = done; | |
795 | out: | |
796 | kfree(kbuf); | |
797 | return ret; | |
798 | } | |
799 | ||
800 | static const struct file_operations kprobe_events_ops = { | |
801 | .owner = THIS_MODULE, | |
802 | .open = probes_open, | |
803 | .read = seq_read, | |
804 | .llseek = seq_lseek, | |
805 | .release = seq_release, | |
806 | .write = probes_write, | |
807 | }; | |
808 | ||
cd7e7bd5 MH |
809 | /* Probes profiling interfaces */ |
810 | static int probes_profile_seq_show(struct seq_file *m, void *v) | |
811 | { | |
812 | struct trace_probe *tp = v; | |
813 | ||
814 | seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit, | |
4a846b44 | 815 | tp->rp.kp.nmissed); |
cd7e7bd5 MH |
816 | |
817 | return 0; | |
818 | } | |
819 | ||
820 | static const struct seq_operations profile_seq_op = { | |
821 | .start = probes_seq_start, | |
822 | .next = probes_seq_next, | |
823 | .stop = probes_seq_stop, | |
824 | .show = probes_profile_seq_show | |
825 | }; | |
826 | ||
827 | static int profile_open(struct inode *inode, struct file *file) | |
828 | { | |
829 | return seq_open(file, &profile_seq_op); | |
830 | } | |
831 | ||
832 | static const struct file_operations kprobe_profile_ops = { | |
833 | .owner = THIS_MODULE, | |
834 | .open = profile_open, | |
835 | .read = seq_read, | |
836 | .llseek = seq_lseek, | |
837 | .release = seq_release, | |
838 | }; | |
839 | ||
413d37d1 MH |
840 | /* Kprobe handler */ |
841 | static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs) | |
842 | { | |
4a846b44 | 843 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); |
413d37d1 MH |
844 | struct kprobe_trace_entry *entry; |
845 | struct ring_buffer_event *event; | |
8f8ffe24 | 846 | struct ring_buffer *buffer; |
413d37d1 MH |
847 | int size, i, pc; |
848 | unsigned long irq_flags; | |
4263565d | 849 | struct ftrace_event_call *call = &tp->call; |
413d37d1 | 850 | |
cd7e7bd5 MH |
851 | tp->nhit++; |
852 | ||
413d37d1 MH |
853 | local_save_flags(irq_flags); |
854 | pc = preempt_count(); | |
855 | ||
856 | size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args); | |
857 | ||
8f8ffe24 | 858 | event = trace_current_buffer_lock_reserve(&buffer, call->id, size, |
413d37d1 MH |
859 | irq_flags, pc); |
860 | if (!event) | |
861 | return 0; | |
862 | ||
863 | entry = ring_buffer_event_data(event); | |
864 | entry->nargs = tp->nr_args; | |
865 | entry->ip = (unsigned long)kp->addr; | |
866 | for (i = 0; i < tp->nr_args; i++) | |
eca0d916 | 867 | entry->args[i] = call_fetch(&tp->args[i].fetch, regs); |
413d37d1 | 868 | |
8f8ffe24 FW |
869 | if (!filter_current_check_discard(buffer, call, entry, event)) |
870 | trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc); | |
413d37d1 MH |
871 | return 0; |
872 | } | |
873 | ||
874 | /* Kretprobe handler */ | |
875 | static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri, | |
876 | struct pt_regs *regs) | |
877 | { | |
878 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); | |
879 | struct kretprobe_trace_entry *entry; | |
880 | struct ring_buffer_event *event; | |
8f8ffe24 | 881 | struct ring_buffer *buffer; |
413d37d1 MH |
882 | int size, i, pc; |
883 | unsigned long irq_flags; | |
4263565d | 884 | struct ftrace_event_call *call = &tp->call; |
413d37d1 MH |
885 | |
886 | local_save_flags(irq_flags); | |
887 | pc = preempt_count(); | |
888 | ||
889 | size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args); | |
890 | ||
8f8ffe24 | 891 | event = trace_current_buffer_lock_reserve(&buffer, call->id, size, |
413d37d1 MH |
892 | irq_flags, pc); |
893 | if (!event) | |
894 | return 0; | |
895 | ||
896 | entry = ring_buffer_event_data(event); | |
897 | entry->nargs = tp->nr_args; | |
4a846b44 | 898 | entry->func = (unsigned long)tp->rp.kp.addr; |
413d37d1 MH |
899 | entry->ret_ip = (unsigned long)ri->ret_addr; |
900 | for (i = 0; i < tp->nr_args; i++) | |
eca0d916 | 901 | entry->args[i] = call_fetch(&tp->args[i].fetch, regs); |
413d37d1 | 902 | |
8f8ffe24 FW |
903 | if (!filter_current_check_discard(buffer, call, entry, event)) |
904 | trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc); | |
413d37d1 MH |
905 | |
906 | return 0; | |
907 | } | |
908 | ||
909 | /* Event entry printers */ | |
910 | enum print_line_t | |
911 | print_kprobe_event(struct trace_iterator *iter, int flags) | |
912 | { | |
913 | struct kprobe_trace_entry *field; | |
914 | struct trace_seq *s = &iter->seq; | |
eca0d916 MH |
915 | struct trace_event *event; |
916 | struct trace_probe *tp; | |
413d37d1 MH |
917 | int i; |
918 | ||
ff50d991 | 919 | field = (struct kprobe_trace_entry *)iter->ent; |
eca0d916 MH |
920 | event = ftrace_find_event(field->ent.type); |
921 | tp = container_of(event, struct trace_probe, event); | |
413d37d1 | 922 | |
6e9f23d1 MH |
923 | if (!trace_seq_printf(s, "%s: (", tp->call.name)) |
924 | goto partial; | |
925 | ||
413d37d1 MH |
926 | if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET)) |
927 | goto partial; | |
928 | ||
6e9f23d1 | 929 | if (!trace_seq_puts(s, ")")) |
413d37d1 MH |
930 | goto partial; |
931 | ||
932 | for (i = 0; i < field->nargs; i++) | |
eca0d916 MH |
933 | if (!trace_seq_printf(s, " %s=%lx", |
934 | tp->args[i].name, field->args[i])) | |
413d37d1 MH |
935 | goto partial; |
936 | ||
937 | if (!trace_seq_puts(s, "\n")) | |
938 | goto partial; | |
939 | ||
940 | return TRACE_TYPE_HANDLED; | |
941 | partial: | |
942 | return TRACE_TYPE_PARTIAL_LINE; | |
943 | } | |
944 | ||
945 | enum print_line_t | |
946 | print_kretprobe_event(struct trace_iterator *iter, int flags) | |
947 | { | |
948 | struct kretprobe_trace_entry *field; | |
949 | struct trace_seq *s = &iter->seq; | |
eca0d916 MH |
950 | struct trace_event *event; |
951 | struct trace_probe *tp; | |
413d37d1 MH |
952 | int i; |
953 | ||
ff50d991 | 954 | field = (struct kretprobe_trace_entry *)iter->ent; |
eca0d916 MH |
955 | event = ftrace_find_event(field->ent.type); |
956 | tp = container_of(event, struct trace_probe, event); | |
413d37d1 | 957 | |
6e9f23d1 MH |
958 | if (!trace_seq_printf(s, "%s: (", tp->call.name)) |
959 | goto partial; | |
960 | ||
413d37d1 MH |
961 | if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET)) |
962 | goto partial; | |
963 | ||
964 | if (!trace_seq_puts(s, " <- ")) | |
965 | goto partial; | |
966 | ||
967 | if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET)) | |
968 | goto partial; | |
969 | ||
6e9f23d1 | 970 | if (!trace_seq_puts(s, ")")) |
413d37d1 MH |
971 | goto partial; |
972 | ||
973 | for (i = 0; i < field->nargs; i++) | |
eca0d916 MH |
974 | if (!trace_seq_printf(s, " %s=%lx", |
975 | tp->args[i].name, field->args[i])) | |
413d37d1 MH |
976 | goto partial; |
977 | ||
978 | if (!trace_seq_puts(s, "\n")) | |
979 | goto partial; | |
980 | ||
981 | return TRACE_TYPE_HANDLED; | |
982 | partial: | |
983 | return TRACE_TYPE_PARTIAL_LINE; | |
984 | } | |
985 | ||
413d37d1 MH |
986 | static int probe_event_enable(struct ftrace_event_call *call) |
987 | { | |
988 | struct trace_probe *tp = (struct trace_probe *)call->data; | |
989 | ||
e08d1c65 MH |
990 | if (probe_is_return(tp)) { |
991 | tp->rp.handler = kretprobe_trace_func; | |
413d37d1 | 992 | return enable_kretprobe(&tp->rp); |
e08d1c65 MH |
993 | } else { |
994 | tp->rp.kp.pre_handler = kprobe_trace_func; | |
4a846b44 | 995 | return enable_kprobe(&tp->rp.kp); |
e08d1c65 | 996 | } |
413d37d1 MH |
997 | } |
998 | ||
999 | static void probe_event_disable(struct ftrace_event_call *call) | |
1000 | { | |
1001 | struct trace_probe *tp = (struct trace_probe *)call->data; | |
1002 | ||
1003 | if (probe_is_return(tp)) | |
1004 | disable_kretprobe(&tp->rp); | |
1005 | else | |
4a846b44 | 1006 | disable_kprobe(&tp->rp.kp); |
413d37d1 MH |
1007 | } |
1008 | ||
1009 | static int probe_event_raw_init(struct ftrace_event_call *event_call) | |
1010 | { | |
1011 | INIT_LIST_HEAD(&event_call->fields); | |
8f8ffe24 | 1012 | |
413d37d1 MH |
1013 | return 0; |
1014 | } | |
1015 | ||
1016 | #undef DEFINE_FIELD | |
1017 | #define DEFINE_FIELD(type, item, name, is_signed) \ | |
1018 | do { \ | |
1019 | ret = trace_define_field(event_call, #type, name, \ | |
1020 | offsetof(typeof(field), item), \ | |
1021 | sizeof(field.item), is_signed, \ | |
1022 | FILTER_OTHER); \ | |
1023 | if (ret) \ | |
1024 | return ret; \ | |
1025 | } while (0) | |
1026 | ||
1027 | static int kprobe_event_define_fields(struct ftrace_event_call *event_call) | |
1028 | { | |
1029 | int ret, i; | |
1030 | struct kprobe_trace_entry field; | |
413d37d1 MH |
1031 | struct trace_probe *tp = (struct trace_probe *)event_call->data; |
1032 | ||
1033 | ret = trace_define_common_fields(event_call); | |
1034 | if (!ret) | |
1035 | return ret; | |
1036 | ||
1037 | DEFINE_FIELD(unsigned long, ip, "ip", 0); | |
1038 | DEFINE_FIELD(int, nargs, "nargs", 1); | |
eca0d916 MH |
1039 | /* Set argument names as fields */ |
1040 | for (i = 0; i < tp->nr_args; i++) | |
1041 | DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0); | |
413d37d1 MH |
1042 | return 0; |
1043 | } | |
1044 | ||
1045 | static int kretprobe_event_define_fields(struct ftrace_event_call *event_call) | |
1046 | { | |
1047 | int ret, i; | |
1048 | struct kretprobe_trace_entry field; | |
413d37d1 MH |
1049 | struct trace_probe *tp = (struct trace_probe *)event_call->data; |
1050 | ||
1051 | ret = trace_define_common_fields(event_call); | |
1052 | if (!ret) | |
1053 | return ret; | |
1054 | ||
1055 | DEFINE_FIELD(unsigned long, func, "func", 0); | |
1056 | DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0); | |
1057 | DEFINE_FIELD(int, nargs, "nargs", 1); | |
eca0d916 MH |
1058 | /* Set argument names as fields */ |
1059 | for (i = 0; i < tp->nr_args; i++) | |
1060 | DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0); | |
413d37d1 MH |
1061 | return 0; |
1062 | } | |
1063 | ||
1064 | static int __probe_event_show_format(struct trace_seq *s, | |
1065 | struct trace_probe *tp, const char *fmt, | |
1066 | const char *arg) | |
1067 | { | |
eca0d916 | 1068 | int i; |
413d37d1 | 1069 | |
413d37d1 MH |
1070 | /* Show format */ |
1071 | if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt)) | |
1072 | return 0; | |
1073 | ||
1074 | for (i = 0; i < tp->nr_args; i++) | |
eca0d916 | 1075 | if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name)) |
413d37d1 MH |
1076 | return 0; |
1077 | ||
1078 | if (!trace_seq_printf(s, "\", %s", arg)) | |
1079 | return 0; | |
1080 | ||
1081 | for (i = 0; i < tp->nr_args; i++) | |
eca0d916 | 1082 | if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name)) |
413d37d1 MH |
1083 | return 0; |
1084 | ||
1085 | return trace_seq_puts(s, "\n"); | |
1086 | } | |
1087 | ||
1088 | #undef SHOW_FIELD | |
1089 | #define SHOW_FIELD(type, item, name) \ | |
1090 | do { \ | |
1091 | ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \ | |
38a47497 | 1092 | "offset:%u;\tsize:%u;\n", name, \ |
413d37d1 MH |
1093 | (unsigned int)offsetof(typeof(field), item),\ |
1094 | (unsigned int)sizeof(type)); \ | |
1095 | if (!ret) \ | |
1096 | return 0; \ | |
1097 | } while (0) | |
1098 | ||
1099 | static int kprobe_event_show_format(struct ftrace_event_call *call, | |
1100 | struct trace_seq *s) | |
1101 | { | |
1102 | struct kprobe_trace_entry field __attribute__((unused)); | |
1103 | int ret, i; | |
413d37d1 MH |
1104 | struct trace_probe *tp = (struct trace_probe *)call->data; |
1105 | ||
1106 | SHOW_FIELD(unsigned long, ip, "ip"); | |
1107 | SHOW_FIELD(int, nargs, "nargs"); | |
1108 | ||
1109 | /* Show fields */ | |
eca0d916 MH |
1110 | for (i = 0; i < tp->nr_args; i++) |
1111 | SHOW_FIELD(unsigned long, args[i], tp->args[i].name); | |
413d37d1 MH |
1112 | trace_seq_puts(s, "\n"); |
1113 | ||
6e9f23d1 | 1114 | return __probe_event_show_format(s, tp, "(%lx)", "REC->ip"); |
413d37d1 MH |
1115 | } |
1116 | ||
1117 | static int kretprobe_event_show_format(struct ftrace_event_call *call, | |
1118 | struct trace_seq *s) | |
1119 | { | |
1120 | struct kretprobe_trace_entry field __attribute__((unused)); | |
1121 | int ret, i; | |
413d37d1 MH |
1122 | struct trace_probe *tp = (struct trace_probe *)call->data; |
1123 | ||
1124 | SHOW_FIELD(unsigned long, func, "func"); | |
1125 | SHOW_FIELD(unsigned long, ret_ip, "ret_ip"); | |
1126 | SHOW_FIELD(int, nargs, "nargs"); | |
1127 | ||
1128 | /* Show fields */ | |
eca0d916 MH |
1129 | for (i = 0; i < tp->nr_args; i++) |
1130 | SHOW_FIELD(unsigned long, args[i], tp->args[i].name); | |
413d37d1 MH |
1131 | trace_seq_puts(s, "\n"); |
1132 | ||
6e9f23d1 MH |
1133 | return __probe_event_show_format(s, tp, "(%lx <- %lx)", |
1134 | "REC->func, REC->ret_ip"); | |
413d37d1 MH |
1135 | } |
1136 | ||
e08d1c65 MH |
1137 | #ifdef CONFIG_EVENT_PROFILE |
1138 | ||
1139 | /* Kprobe profile handler */ | |
1140 | static __kprobes int kprobe_profile_func(struct kprobe *kp, | |
1141 | struct pt_regs *regs) | |
1142 | { | |
1143 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); | |
1144 | struct ftrace_event_call *call = &tp->call; | |
1145 | struct kprobe_trace_entry *entry; | |
1146 | int size, i, pc; | |
1147 | unsigned long irq_flags; | |
1148 | ||
1149 | local_save_flags(irq_flags); | |
1150 | pc = preempt_count(); | |
1151 | ||
1152 | size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args); | |
1153 | ||
1154 | do { | |
1155 | char raw_data[size]; | |
1156 | struct trace_entry *ent; | |
1157 | ||
1158 | *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL; | |
1159 | entry = (struct kprobe_trace_entry *)raw_data; | |
1160 | ent = &entry->ent; | |
1161 | ||
1162 | tracing_generic_entry_update(ent, irq_flags, pc); | |
1163 | ent->type = call->id; | |
1164 | entry->nargs = tp->nr_args; | |
1165 | entry->ip = (unsigned long)kp->addr; | |
1166 | for (i = 0; i < tp->nr_args; i++) | |
eca0d916 | 1167 | entry->args[i] = call_fetch(&tp->args[i].fetch, regs); |
e08d1c65 MH |
1168 | perf_tpcounter_event(call->id, entry->ip, 1, entry, size); |
1169 | } while (0); | |
1170 | return 0; | |
1171 | } | |
1172 | ||
1173 | /* Kretprobe profile handler */ | |
1174 | static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri, | |
1175 | struct pt_regs *regs) | |
1176 | { | |
1177 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); | |
1178 | struct ftrace_event_call *call = &tp->call; | |
1179 | struct kretprobe_trace_entry *entry; | |
1180 | int size, i, pc; | |
1181 | unsigned long irq_flags; | |
1182 | ||
1183 | local_save_flags(irq_flags); | |
1184 | pc = preempt_count(); | |
1185 | ||
1186 | size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args); | |
1187 | ||
1188 | do { | |
1189 | char raw_data[size]; | |
1190 | struct trace_entry *ent; | |
1191 | ||
1192 | *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL; | |
1193 | entry = (struct kretprobe_trace_entry *)raw_data; | |
1194 | ent = &entry->ent; | |
1195 | ||
1196 | tracing_generic_entry_update(ent, irq_flags, pc); | |
1197 | ent->type = call->id; | |
1198 | entry->nargs = tp->nr_args; | |
1199 | entry->func = (unsigned long)tp->rp.kp.addr; | |
1200 | entry->ret_ip = (unsigned long)ri->ret_addr; | |
1201 | for (i = 0; i < tp->nr_args; i++) | |
eca0d916 | 1202 | entry->args[i] = call_fetch(&tp->args[i].fetch, regs); |
e08d1c65 MH |
1203 | perf_tpcounter_event(call->id, entry->ret_ip, 1, entry, size); |
1204 | } while (0); | |
1205 | return 0; | |
1206 | } | |
1207 | ||
1208 | static int probe_profile_enable(struct ftrace_event_call *call) | |
1209 | { | |
1210 | struct trace_probe *tp = (struct trace_probe *)call->data; | |
1211 | ||
1212 | if (atomic_inc_return(&call->profile_count)) | |
1213 | return 0; | |
1214 | ||
1215 | if (probe_is_return(tp)) { | |
1216 | tp->rp.handler = kretprobe_profile_func; | |
1217 | return enable_kretprobe(&tp->rp); | |
1218 | } else { | |
1219 | tp->rp.kp.pre_handler = kprobe_profile_func; | |
1220 | return enable_kprobe(&tp->rp.kp); | |
1221 | } | |
1222 | } | |
1223 | ||
1224 | static void probe_profile_disable(struct ftrace_event_call *call) | |
1225 | { | |
1226 | if (atomic_add_negative(-1, &call->profile_count)) | |
1227 | probe_event_disable(call); | |
1228 | } | |
1229 | ||
1230 | #endif /* CONFIG_EVENT_PROFILE */ | |
1231 | ||
413d37d1 MH |
1232 | static int register_probe_event(struct trace_probe *tp) |
1233 | { | |
1234 | struct ftrace_event_call *call = &tp->call; | |
1235 | int ret; | |
1236 | ||
1237 | /* Initialize ftrace_event_call */ | |
413d37d1 | 1238 | if (probe_is_return(tp)) { |
ff50d991 | 1239 | tp->event.trace = print_kretprobe_event; |
413d37d1 MH |
1240 | call->raw_init = probe_event_raw_init; |
1241 | call->show_format = kretprobe_event_show_format; | |
1242 | call->define_fields = kretprobe_event_define_fields; | |
1243 | } else { | |
ff50d991 | 1244 | tp->event.trace = print_kprobe_event; |
413d37d1 MH |
1245 | call->raw_init = probe_event_raw_init; |
1246 | call->show_format = kprobe_event_show_format; | |
1247 | call->define_fields = kprobe_event_define_fields; | |
1248 | } | |
ff50d991 MH |
1249 | call->event = &tp->event; |
1250 | call->id = register_ftrace_event(&tp->event); | |
1251 | if (!call->id) | |
1252 | return -ENODEV; | |
413d37d1 MH |
1253 | call->enabled = 1; |
1254 | call->regfunc = probe_event_enable; | |
1255 | call->unregfunc = probe_event_disable; | |
e08d1c65 MH |
1256 | |
1257 | #ifdef CONFIG_EVENT_PROFILE | |
1258 | atomic_set(&call->profile_count, -1); | |
1259 | call->profile_enable = probe_profile_enable; | |
1260 | call->profile_disable = probe_profile_disable; | |
1261 | #endif | |
413d37d1 MH |
1262 | call->data = tp; |
1263 | ret = trace_add_event_call(call); | |
ff50d991 | 1264 | if (ret) { |
413d37d1 | 1265 | pr_info("Failed to register kprobe event: %s\n", call->name); |
ff50d991 MH |
1266 | unregister_ftrace_event(&tp->event); |
1267 | } | |
413d37d1 MH |
1268 | return ret; |
1269 | } | |
1270 | ||
1271 | static void unregister_probe_event(struct trace_probe *tp) | |
1272 | { | |
ff50d991 | 1273 | /* tp->event is unregistered in trace_remove_event_call() */ |
413d37d1 MH |
1274 | trace_remove_event_call(&tp->call); |
1275 | } | |
1276 | ||
1277 | /* Make a debugfs interface for controling probe points */ | |
1278 | static __init int init_kprobe_trace(void) | |
1279 | { | |
1280 | struct dentry *d_tracer; | |
1281 | struct dentry *entry; | |
413d37d1 MH |
1282 | |
1283 | d_tracer = tracing_init_dentry(); | |
1284 | if (!d_tracer) | |
1285 | return 0; | |
1286 | ||
1287 | entry = debugfs_create_file("kprobe_events", 0644, d_tracer, | |
1288 | NULL, &kprobe_events_ops); | |
1289 | ||
cd7e7bd5 | 1290 | /* Event list interface */ |
413d37d1 MH |
1291 | if (!entry) |
1292 | pr_warning("Could not create debugfs " | |
1293 | "'kprobe_events' entry\n"); | |
cd7e7bd5 MH |
1294 | |
1295 | /* Profile interface */ | |
1296 | entry = debugfs_create_file("kprobe_profile", 0444, d_tracer, | |
1297 | NULL, &kprobe_profile_ops); | |
1298 | ||
1299 | if (!entry) | |
1300 | pr_warning("Could not create debugfs " | |
1301 | "'kprobe_profile' entry\n"); | |
413d37d1 MH |
1302 | return 0; |
1303 | } | |
1304 | fs_initcall(init_kprobe_trace); | |
1305 | ||
1306 | ||
1307 | #ifdef CONFIG_FTRACE_STARTUP_TEST | |
1308 | ||
1309 | static int kprobe_trace_selftest_target(int a1, int a2, int a3, | |
1310 | int a4, int a5, int a6) | |
1311 | { | |
1312 | return a1 + a2 + a3 + a4 + a5 + a6; | |
1313 | } | |
1314 | ||
1315 | static __init int kprobe_trace_self_tests_init(void) | |
1316 | { | |
1317 | int ret; | |
1318 | int (*target)(int, int, int, int, int, int); | |
1319 | ||
1320 | target = kprobe_trace_selftest_target; | |
1321 | ||
1322 | pr_info("Testing kprobe tracing: "); | |
1323 | ||
1324 | ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target " | |
1325 | "a1 a2 a3 a4 a5 a6"); | |
1326 | if (WARN_ON_ONCE(ret)) | |
1327 | pr_warning("error enabling function entry\n"); | |
1328 | ||
1329 | ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target " | |
1330 | "ra rv"); | |
1331 | if (WARN_ON_ONCE(ret)) | |
1332 | pr_warning("error enabling function return\n"); | |
1333 | ||
1334 | ret = target(1, 2, 3, 4, 5, 6); | |
1335 | ||
1336 | cleanup_all_probes(); | |
1337 | ||
1338 | pr_cont("OK\n"); | |
1339 | return 0; | |
1340 | } | |
1341 | ||
1342 | late_initcall(kprobe_trace_self_tests_init); | |
1343 | ||
1344 | #endif |