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