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