Merge commit 'tracing/core' into tracing/kprobes
[pandora-kernel.git] / 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 #define MAX_EVENT_NAME_LEN 64
38
39 /* currently, trace_kprobe only supports X86. */
40
41 struct fetch_func {
42         unsigned long (*func)(struct pt_regs *, void *);
43         void *data;
44 };
45
46 static __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 */
53 static __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
59 static __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
66 static __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
75 static __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
80 static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
81                                               void *dummy)
82 {
83         return regs_return_value(regs);
84 }
85
86 static __kprobes unsigned long fetch_ip(struct pt_regs *regs, void *dummy)
87 {
88         return instruction_pointer(regs);
89 }
90
91 static __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 */
98 struct symbol_cache {
99         char *symbol;
100         long offset;
101         unsigned long addr;
102 };
103
104 static 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
112 static void free_symbol_cache(struct symbol_cache *sc)
113 {
114         kfree(sc->symbol);
115         kfree(sc);
116 }
117
118 static 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
139 static __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 */
150 struct indirect_fetch_data {
151         struct fetch_func orig;
152         long offset;
153 };
154
155 static __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
168 static __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
181 struct trace_probe {
182         struct list_head        list;
183         union {
184                 struct kprobe           kp;
185                 struct kretprobe        rp;
186         };
187         unsigned long           nhit;
188         const char              *symbol;        /* symbol name */
189         struct ftrace_event_call        call;
190         struct trace_event              event;
191         unsigned int            nr_args;
192         struct fetch_func       args[];
193 };
194
195 #define SIZEOF_TRACE_PROBE(n)                   \
196         (offsetof(struct trace_probe, args) +   \
197         (sizeof(struct fetch_func) * (n)))
198
199 static int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs);
200 static int kretprobe_trace_func(struct kretprobe_instance *ri,
201                                 struct pt_regs *regs);
202
203 static __kprobes int probe_is_return(struct trace_probe *tp)
204 {
205         return (tp->rp.handler == kretprobe_trace_func);
206 }
207
208 static __kprobes const char *probe_symbol(struct trace_probe *tp)
209 {
210         return tp->symbol ? tp->symbol : "unknown";
211 }
212
213 static __kprobes long probe_offset(struct trace_probe *tp)
214 {
215         return (probe_is_return(tp)) ? tp->rp.kp.offset : tp->kp.offset;
216 }
217
218 static __kprobes void *probe_address(struct trace_probe *tp)
219 {
220         return (probe_is_return(tp)) ? tp->rp.kp.addr : tp->kp.addr;
221 }
222
223 static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
224 {
225         int ret = -EINVAL;
226
227         if (ff->func == fetch_argument)
228                 ret = snprintf(buf, n, "a%lu", (unsigned long)ff->data);
229         else if (ff->func == fetch_register) {
230                 const char *name;
231                 name = regs_query_register_name((unsigned int)((long)ff->data));
232                 ret = snprintf(buf, n, "%%%s", name);
233         } else if (ff->func == fetch_stack)
234                 ret = snprintf(buf, n, "s%lu", (unsigned long)ff->data);
235         else if (ff->func == fetch_memory)
236                 ret = snprintf(buf, n, "@0x%p", ff->data);
237         else if (ff->func == fetch_symbol) {
238                 struct symbol_cache *sc = ff->data;
239                 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
240         } else if (ff->func == fetch_retvalue)
241                 ret = snprintf(buf, n, "rv");
242         else if (ff->func == fetch_ip)
243                 ret = snprintf(buf, n, "ra");
244         else if (ff->func == fetch_stack_address)
245                 ret = snprintf(buf, n, "sa");
246         else if (ff->func == fetch_indirect) {
247                 struct indirect_fetch_data *id = ff->data;
248                 size_t l = 0;
249                 ret = snprintf(buf, n, "%+ld(", id->offset);
250                 if (ret >= n)
251                         goto end;
252                 l += ret;
253                 ret = probe_arg_string(buf + l, n - l, &id->orig);
254                 if (ret < 0)
255                         goto end;
256                 l += ret;
257                 ret = snprintf(buf + l, n - l, ")");
258                 ret += l;
259         }
260 end:
261         if (ret >= n)
262                 return -ENOSPC;
263         return ret;
264 }
265
266 static int register_probe_event(struct trace_probe *tp);
267 static void unregister_probe_event(struct trace_probe *tp);
268
269 static DEFINE_MUTEX(probe_lock);
270 static LIST_HEAD(probe_list);
271
272 static struct trace_probe *alloc_trace_probe(const char *symbol,
273                                              const char *event, int nargs)
274 {
275         struct trace_probe *tp;
276
277         tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
278         if (!tp)
279                 return ERR_PTR(-ENOMEM);
280
281         if (symbol) {
282                 tp->symbol = kstrdup(symbol, GFP_KERNEL);
283                 if (!tp->symbol)
284                         goto error;
285         }
286         if (!event)
287                 goto error;
288         tp->call.name = kstrdup(event, GFP_KERNEL);
289         if (!tp->call.name)
290                 goto error;
291
292         INIT_LIST_HEAD(&tp->list);
293         return tp;
294 error:
295         kfree(tp->symbol);
296         kfree(tp);
297         return ERR_PTR(-ENOMEM);
298 }
299
300 static void free_trace_probe(struct trace_probe *tp)
301 {
302         int i;
303
304         for (i = 0; i < tp->nr_args; i++)
305                 if (tp->args[i].func == fetch_symbol)
306                         free_symbol_cache(tp->args[i].data);
307                 else if (tp->args[i].func == fetch_indirect)
308                         free_indirect_fetch_data(tp->args[i].data);
309
310         kfree(tp->call.name);
311         kfree(tp->symbol);
312         kfree(tp);
313 }
314
315 static struct trace_probe *find_probe_event(const char *event)
316 {
317         struct trace_probe *tp;
318
319         list_for_each_entry(tp, &probe_list, list)
320                 if (!strcmp(tp->call.name, event))
321                         return tp;
322         return NULL;
323 }
324
325 static void __unregister_trace_probe(struct trace_probe *tp)
326 {
327         if (probe_is_return(tp))
328                 unregister_kretprobe(&tp->rp);
329         else
330                 unregister_kprobe(&tp->kp);
331 }
332
333 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
334 static void unregister_trace_probe(struct trace_probe *tp)
335 {
336         unregister_probe_event(tp);
337         __unregister_trace_probe(tp);
338         list_del(&tp->list);
339 }
340
341 /* Register a trace_probe and probe_event */
342 static int register_trace_probe(struct trace_probe *tp)
343 {
344         struct trace_probe *old_tp;
345         int ret;
346
347         mutex_lock(&probe_lock);
348
349         if (probe_is_return(tp))
350                 ret = register_kretprobe(&tp->rp);
351         else
352                 ret = register_kprobe(&tp->kp);
353
354         if (ret) {
355                 pr_warning("Could not insert probe(%d)\n", ret);
356                 if (ret == -EILSEQ) {
357                         pr_warning("Probing address(0x%p) is not an "
358                                    "instruction boundary.\n",
359                                    probe_address(tp));
360                         ret = -EINVAL;
361                 }
362                 goto end;
363         }
364         /* register as an event */
365         old_tp = find_probe_event(tp->call.name);
366         if (old_tp) {
367                 /* delete old event */
368                 unregister_trace_probe(old_tp);
369                 free_trace_probe(old_tp);
370         }
371         ret = register_probe_event(tp);
372         if (ret) {
373                 pr_warning("Faild to register probe event(%d)\n", ret);
374                 __unregister_trace_probe(tp);
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_probe_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_probe_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         if (!event) {
584                 /* Make a new event name */
585                 char buf[MAX_EVENT_NAME_LEN];
586                 if (symbol)
587                         snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
588                                  is_return ? 'r' : 'p', symbol, offset);
589                 else
590                         snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
591                                  is_return ? 'r' : 'p', addr);
592                 tp = alloc_trace_probe(symbol, buf, argc);
593         } else
594                 tp = alloc_trace_probe(symbol, event, argc);
595         if (IS_ERR(tp))
596                 return PTR_ERR(tp);
597
598         if (is_return) {
599                 kp = &tp->rp.kp;
600                 tp->rp.handler = kretprobe_trace_func;
601         } else {
602                 kp = &tp->kp;
603                 tp->kp.pre_handler = kprobe_trace_func;
604         }
605
606         if (tp->symbol) {
607                 kp->symbol_name = tp->symbol;
608                 kp->offset = offset;
609         } else
610                 kp->addr = addr;
611
612         /* parse arguments */
613         ret = 0;
614         for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
615                 if (strlen(argv[i]) > MAX_ARGSTR_LEN) {
616                         pr_info("Argument%d(%s) is too long.\n", i, argv[i]);
617                         ret = -ENOSPC;
618                         goto error;
619                 }
620                 ret = parse_probe_arg(argv[i], &tp->args[i], is_return);
621                 if (ret)
622                         goto error;
623         }
624         tp->nr_args = i;
625
626         ret = register_trace_probe(tp);
627         if (ret)
628                 goto error;
629         return 0;
630
631 error:
632         free_trace_probe(tp);
633         return ret;
634 }
635
636 static void cleanup_all_probes(void)
637 {
638         struct trace_probe *tp;
639
640         mutex_lock(&probe_lock);
641         /* TODO: Use batch unregistration */
642         while (!list_empty(&probe_list)) {
643                 tp = list_entry(probe_list.next, struct trace_probe, list);
644                 unregister_trace_probe(tp);
645                 free_trace_probe(tp);
646         }
647         mutex_unlock(&probe_lock);
648 }
649
650
651 /* Probes listing interfaces */
652 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
653 {
654         mutex_lock(&probe_lock);
655         return seq_list_start(&probe_list, *pos);
656 }
657
658 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
659 {
660         return seq_list_next(v, &probe_list, pos);
661 }
662
663 static void probes_seq_stop(struct seq_file *m, void *v)
664 {
665         mutex_unlock(&probe_lock);
666 }
667
668 static int probes_seq_show(struct seq_file *m, void *v)
669 {
670         struct trace_probe *tp = v;
671         int i, ret;
672         char buf[MAX_ARGSTR_LEN + 1];
673
674         seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
675         seq_printf(m, ":%s", tp->call.name);
676
677         if (tp->symbol)
678                 seq_printf(m, " %s%+ld", probe_symbol(tp), probe_offset(tp));
679         else
680                 seq_printf(m, " 0x%p", probe_address(tp));
681
682         for (i = 0; i < tp->nr_args; i++) {
683                 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
684                 if (ret < 0) {
685                         pr_warning("Argument%d decoding error(%d).\n", i, ret);
686                         return ret;
687                 }
688                 seq_printf(m, " %s", buf);
689         }
690         seq_printf(m, "\n");
691         return 0;
692 }
693
694 static const struct seq_operations probes_seq_op = {
695         .start  = probes_seq_start,
696         .next   = probes_seq_next,
697         .stop   = probes_seq_stop,
698         .show   = probes_seq_show
699 };
700
701 static int probes_open(struct inode *inode, struct file *file)
702 {
703         if ((file->f_mode & FMODE_WRITE) &&
704             (file->f_flags & O_TRUNC))
705                 cleanup_all_probes();
706
707         return seq_open(file, &probes_seq_op);
708 }
709
710 static int command_trace_probe(const char *buf)
711 {
712         char **argv;
713         int argc = 0, ret = 0;
714
715         argv = argv_split(GFP_KERNEL, buf, &argc);
716         if (!argv)
717                 return -ENOMEM;
718
719         if (argc)
720                 ret = create_trace_probe(argc, argv);
721
722         argv_free(argv);
723         return ret;
724 }
725
726 #define WRITE_BUFSIZE 128
727
728 static ssize_t probes_write(struct file *file, const char __user *buffer,
729                             size_t count, loff_t *ppos)
730 {
731         char *kbuf, *tmp;
732         int ret;
733         size_t done;
734         size_t size;
735
736         kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
737         if (!kbuf)
738                 return -ENOMEM;
739
740         ret = done = 0;
741         while (done < count) {
742                 size = count - done;
743                 if (size >= WRITE_BUFSIZE)
744                         size = WRITE_BUFSIZE - 1;
745                 if (copy_from_user(kbuf, buffer + done, size)) {
746                         ret = -EFAULT;
747                         goto out;
748                 }
749                 kbuf[size] = '\0';
750                 tmp = strchr(kbuf, '\n');
751                 if (tmp) {
752                         *tmp = '\0';
753                         size = tmp - kbuf + 1;
754                 } else if (done + size < count) {
755                         pr_warning("Line length is too long: "
756                                    "Should be less than %d.", WRITE_BUFSIZE);
757                         ret = -EINVAL;
758                         goto out;
759                 }
760                 done += size;
761                 /* Remove comments */
762                 tmp = strchr(kbuf, '#');
763                 if (tmp)
764                         *tmp = '\0';
765
766                 ret = command_trace_probe(kbuf);
767                 if (ret)
768                         goto out;
769         }
770         ret = done;
771 out:
772         kfree(kbuf);
773         return ret;
774 }
775
776 static const struct file_operations kprobe_events_ops = {
777         .owner          = THIS_MODULE,
778         .open           = probes_open,
779         .read           = seq_read,
780         .llseek         = seq_lseek,
781         .release        = seq_release,
782         .write          = probes_write,
783 };
784
785 /* Probes profiling interfaces */
786 static int probes_profile_seq_show(struct seq_file *m, void *v)
787 {
788         struct trace_probe *tp = v;
789
790         seq_printf(m, "  %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
791                    probe_is_return(tp) ? tp->rp.kp.nmissed : tp->kp.nmissed);
792
793         return 0;
794 }
795
796 static const struct seq_operations profile_seq_op = {
797         .start  = probes_seq_start,
798         .next   = probes_seq_next,
799         .stop   = probes_seq_stop,
800         .show   = probes_profile_seq_show
801 };
802
803 static int profile_open(struct inode *inode, struct file *file)
804 {
805         return seq_open(file, &profile_seq_op);
806 }
807
808 static const struct file_operations kprobe_profile_ops = {
809         .owner          = THIS_MODULE,
810         .open           = profile_open,
811         .read           = seq_read,
812         .llseek         = seq_lseek,
813         .release        = seq_release,
814 };
815
816 /* Kprobe handler */
817 static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
818 {
819         struct trace_probe *tp = container_of(kp, struct trace_probe, kp);
820         struct kprobe_trace_entry *entry;
821         struct ring_buffer_event *event;
822         struct ring_buffer *buffer;
823         int size, i, pc;
824         unsigned long irq_flags;
825         struct ftrace_event_call *call = &tp->call;
826
827         tp->nhit++;
828
829         local_save_flags(irq_flags);
830         pc = preempt_count();
831
832         size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
833
834         event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
835                                                   irq_flags, pc);
836         if (!event)
837                 return 0;
838
839         entry = ring_buffer_event_data(event);
840         entry->nargs = tp->nr_args;
841         entry->ip = (unsigned long)kp->addr;
842         for (i = 0; i < tp->nr_args; i++)
843                 entry->args[i] = call_fetch(&tp->args[i], regs);
844
845         if (!filter_current_check_discard(buffer, call, entry, event))
846                 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
847         return 0;
848 }
849
850 /* Kretprobe handler */
851 static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
852                                           struct pt_regs *regs)
853 {
854         struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
855         struct kretprobe_trace_entry *entry;
856         struct ring_buffer_event *event;
857         struct ring_buffer *buffer;
858         int size, i, pc;
859         unsigned long irq_flags;
860         struct ftrace_event_call *call = &tp->call;
861
862         local_save_flags(irq_flags);
863         pc = preempt_count();
864
865         size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
866
867         event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
868                                                   irq_flags, pc);
869         if (!event)
870                 return 0;
871
872         entry = ring_buffer_event_data(event);
873         entry->nargs = tp->nr_args;
874         entry->func = (unsigned long)probe_address(tp);
875         entry->ret_ip = (unsigned long)ri->ret_addr;
876         for (i = 0; i < tp->nr_args; i++)
877                 entry->args[i] = call_fetch(&tp->args[i], regs);
878
879         if (!filter_current_check_discard(buffer, call, entry, event))
880                 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
881
882         return 0;
883 }
884
885 /* Event entry printers */
886 enum print_line_t
887 print_kprobe_event(struct trace_iterator *iter, int flags)
888 {
889         struct kprobe_trace_entry *field;
890         struct trace_seq *s = &iter->seq;
891         int i;
892
893         field = (struct kprobe_trace_entry *)iter->ent;
894
895         if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
896                 goto partial;
897
898         if (!trace_seq_puts(s, ":"))
899                 goto partial;
900
901         for (i = 0; i < field->nargs; i++)
902                 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
903                         goto partial;
904
905         if (!trace_seq_puts(s, "\n"))
906                 goto partial;
907
908         return TRACE_TYPE_HANDLED;
909 partial:
910         return TRACE_TYPE_PARTIAL_LINE;
911 }
912
913 enum print_line_t
914 print_kretprobe_event(struct trace_iterator *iter, int flags)
915 {
916         struct kretprobe_trace_entry *field;
917         struct trace_seq *s = &iter->seq;
918         int i;
919
920         field = (struct kretprobe_trace_entry *)iter->ent;
921
922         if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
923                 goto partial;
924
925         if (!trace_seq_puts(s, " <- "))
926                 goto partial;
927
928         if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
929                 goto partial;
930
931         if (!trace_seq_puts(s, ":"))
932                 goto partial;
933
934         for (i = 0; i < field->nargs; i++)
935                 if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
936                         goto partial;
937
938         if (!trace_seq_puts(s, "\n"))
939                 goto partial;
940
941         return TRACE_TYPE_HANDLED;
942 partial:
943         return TRACE_TYPE_PARTIAL_LINE;
944 }
945
946 static int probe_event_enable(struct ftrace_event_call *call)
947 {
948         struct trace_probe *tp = (struct trace_probe *)call->data;
949
950         if (probe_is_return(tp))
951                 return enable_kretprobe(&tp->rp);
952         else
953                 return enable_kprobe(&tp->kp);
954 }
955
956 static void probe_event_disable(struct ftrace_event_call *call)
957 {
958         struct trace_probe *tp = (struct trace_probe *)call->data;
959
960         if (probe_is_return(tp))
961                 disable_kretprobe(&tp->rp);
962         else
963                 disable_kprobe(&tp->kp);
964 }
965
966 static int probe_event_raw_init(struct ftrace_event_call *event_call)
967 {
968         INIT_LIST_HEAD(&event_call->fields);
969
970         return 0;
971 }
972
973 #undef DEFINE_FIELD
974 #define DEFINE_FIELD(type, item, name, is_signed)                       \
975         do {                                                            \
976                 ret = trace_define_field(event_call, #type, name,       \
977                                          offsetof(typeof(field), item), \
978                                          sizeof(field.item), is_signed, \
979                                          FILTER_OTHER);                 \
980                 if (ret)                                                \
981                         return ret;                                     \
982         } while (0)
983
984 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
985 {
986         int ret, i;
987         struct kprobe_trace_entry field;
988         char buf[MAX_ARGSTR_LEN + 1];
989         struct trace_probe *tp = (struct trace_probe *)event_call->data;
990
991         ret = trace_define_common_fields(event_call);
992         if (!ret)
993                 return ret;
994
995         DEFINE_FIELD(unsigned long, ip, "ip", 0);
996         DEFINE_FIELD(int, nargs, "nargs", 1);
997         for (i = 0; i < tp->nr_args; i++) {
998                 /* Set argN as a field */
999                 sprintf(buf, "arg%d", i);
1000                 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1001                 /* Set argument string as an alias field */
1002                 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1003                 if (ret < 0)
1004                         return ret;
1005                 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1006         }
1007         return 0;
1008 }
1009
1010 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1011 {
1012         int ret, i;
1013         struct kretprobe_trace_entry field;
1014         char buf[MAX_ARGSTR_LEN + 1];
1015         struct trace_probe *tp = (struct trace_probe *)event_call->data;
1016
1017         ret = trace_define_common_fields(event_call);
1018         if (!ret)
1019                 return ret;
1020
1021         DEFINE_FIELD(unsigned long, func, "func", 0);
1022         DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
1023         DEFINE_FIELD(int, nargs, "nargs", 1);
1024         for (i = 0; i < tp->nr_args; i++) {
1025                 /* Set argN as a field */
1026                 sprintf(buf, "arg%d", i);
1027                 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1028                 /* Set argument string as an alias field */
1029                 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1030                 if (ret < 0)
1031                         return ret;
1032                 DEFINE_FIELD(unsigned long, args[i], buf, 0);
1033         }
1034         return 0;
1035 }
1036
1037 static int __probe_event_show_format(struct trace_seq *s,
1038                                      struct trace_probe *tp, const char *fmt,
1039                                      const char *arg)
1040 {
1041         int i, ret;
1042         char buf[MAX_ARGSTR_LEN + 1];
1043
1044         /* Show aliases */
1045         for (i = 0; i < tp->nr_args; i++) {
1046                 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
1047                 if (ret < 0)
1048                         return ret;
1049                 if (!trace_seq_printf(s, "\talias: %s;\toriginal: arg%d;\n",
1050                                       buf, i))
1051                         return 0;
1052         }
1053         /* Show format */
1054         if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1055                 return 0;
1056
1057         for (i = 0; i < tp->nr_args; i++)
1058                 if (!trace_seq_puts(s, " 0x%lx"))
1059                         return 0;
1060
1061         if (!trace_seq_printf(s, "\", %s", arg))
1062                 return 0;
1063
1064         for (i = 0; i < tp->nr_args; i++)
1065                 if (!trace_seq_printf(s, ", arg%d", i))
1066                         return 0;
1067
1068         return trace_seq_puts(s, "\n");
1069 }
1070
1071 #undef SHOW_FIELD
1072 #define SHOW_FIELD(type, item, name)                                    \
1073         do {                                                            \
1074                 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t"    \
1075                                 "offset:%u;\tsize:%u;\n", name,         \
1076                                 (unsigned int)offsetof(typeof(field), item),\
1077                                 (unsigned int)sizeof(type));            \
1078                 if (!ret)                                               \
1079                         return 0;                                       \
1080         } while (0)
1081
1082 static int kprobe_event_show_format(struct ftrace_event_call *call,
1083                                     struct trace_seq *s)
1084 {
1085         struct kprobe_trace_entry field __attribute__((unused));
1086         int ret, i;
1087         char buf[8];
1088         struct trace_probe *tp = (struct trace_probe *)call->data;
1089
1090         SHOW_FIELD(unsigned long, ip, "ip");
1091         SHOW_FIELD(int, nargs, "nargs");
1092
1093         /* Show fields */
1094         for (i = 0; i < tp->nr_args; i++) {
1095                 sprintf(buf, "arg%d", i);
1096                 SHOW_FIELD(unsigned long, args[i], buf);
1097         }
1098         trace_seq_puts(s, "\n");
1099
1100         return __probe_event_show_format(s, tp, "%lx:", "ip");
1101 }
1102
1103 static int kretprobe_event_show_format(struct ftrace_event_call *call,
1104                                        struct trace_seq *s)
1105 {
1106         struct kretprobe_trace_entry field __attribute__((unused));
1107         int ret, i;
1108         char buf[8];
1109         struct trace_probe *tp = (struct trace_probe *)call->data;
1110
1111         SHOW_FIELD(unsigned long, func, "func");
1112         SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
1113         SHOW_FIELD(int, nargs, "nargs");
1114
1115         /* Show fields */
1116         for (i = 0; i < tp->nr_args; i++) {
1117                 sprintf(buf, "arg%d", i);
1118                 SHOW_FIELD(unsigned long, args[i], buf);
1119         }
1120         trace_seq_puts(s, "\n");
1121
1122         return __probe_event_show_format(s, tp, "%lx <- %lx:",
1123                                           "func, ret_ip");
1124 }
1125
1126 static int register_probe_event(struct trace_probe *tp)
1127 {
1128         struct ftrace_event_call *call = &tp->call;
1129         int ret;
1130
1131         /* Initialize ftrace_event_call */
1132         call->system = "kprobes";
1133         if (probe_is_return(tp)) {
1134                 tp->event.trace = print_kretprobe_event;
1135                 call->raw_init = probe_event_raw_init;
1136                 call->show_format = kretprobe_event_show_format;
1137                 call->define_fields = kretprobe_event_define_fields;
1138         } else {
1139                 tp->event.trace = print_kprobe_event;
1140                 call->raw_init = probe_event_raw_init;
1141                 call->show_format = kprobe_event_show_format;
1142                 call->define_fields = kprobe_event_define_fields;
1143         }
1144         call->event = &tp->event;
1145         call->id = register_ftrace_event(&tp->event);
1146         if (!call->id)
1147                 return -ENODEV;
1148         call->enabled = 1;
1149         call->regfunc = probe_event_enable;
1150         call->unregfunc = probe_event_disable;
1151         call->data = tp;
1152         ret = trace_add_event_call(call);
1153         if (ret) {
1154                 pr_info("Failed to register kprobe event: %s\n", call->name);
1155                 unregister_ftrace_event(&tp->event);
1156         }
1157         return ret;
1158 }
1159
1160 static void unregister_probe_event(struct trace_probe *tp)
1161 {
1162         /* tp->event is unregistered in trace_remove_event_call() */
1163         trace_remove_event_call(&tp->call);
1164 }
1165
1166 /* Make a debugfs interface for controling probe points */
1167 static __init int init_kprobe_trace(void)
1168 {
1169         struct dentry *d_tracer;
1170         struct dentry *entry;
1171
1172         d_tracer = tracing_init_dentry();
1173         if (!d_tracer)
1174                 return 0;
1175
1176         entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1177                                     NULL, &kprobe_events_ops);
1178
1179         /* Event list interface */
1180         if (!entry)
1181                 pr_warning("Could not create debugfs "
1182                            "'kprobe_events' entry\n");
1183
1184         /* Profile interface */
1185         entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1186                                     NULL, &kprobe_profile_ops);
1187
1188         if (!entry)
1189                 pr_warning("Could not create debugfs "
1190                            "'kprobe_profile' entry\n");
1191         return 0;
1192 }
1193 fs_initcall(init_kprobe_trace);
1194
1195
1196 #ifdef CONFIG_FTRACE_STARTUP_TEST
1197
1198 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1199                                         int a4, int a5, int a6)
1200 {
1201         return a1 + a2 + a3 + a4 + a5 + a6;
1202 }
1203
1204 static __init int kprobe_trace_self_tests_init(void)
1205 {
1206         int ret;
1207         int (*target)(int, int, int, int, int, int);
1208
1209         target = kprobe_trace_selftest_target;
1210
1211         pr_info("Testing kprobe tracing: ");
1212
1213         ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1214                                   "a1 a2 a3 a4 a5 a6");
1215         if (WARN_ON_ONCE(ret))
1216                 pr_warning("error enabling function entry\n");
1217
1218         ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1219                                   "ra rv");
1220         if (WARN_ON_ONCE(ret))
1221                 pr_warning("error enabling function return\n");
1222
1223         ret = target(1, 2, 3, 4, 5, 6);
1224
1225         cleanup_all_probes();
1226
1227         pr_cont("OK\n");
1228         return 0;
1229 }
1230
1231 late_initcall(kprobe_trace_self_tests_init);
1232
1233 #endif