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