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