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