tracing/kprobes: Support module init function probing
[linux-3.10.git] / 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 #include <linux/stringify.h>
33 #include <linux/limits.h>
34 #include <asm/bitsperlong.h>
35
36 #include "trace.h"
37 #include "trace_output.h"
38
39 #define MAX_TRACE_ARGS 128
40 #define MAX_ARGSTR_LEN 63
41 #define MAX_EVENT_NAME_LEN 64
42 #define MAX_STRING_SIZE PATH_MAX
43 #define KPROBE_EVENT_SYSTEM "kprobes"
44
45 /* Reserved field names */
46 #define FIELD_STRING_IP "__probe_ip"
47 #define FIELD_STRING_RETIP "__probe_ret_ip"
48 #define FIELD_STRING_FUNC "__probe_func"
49
50 const char *reserved_field_names[] = {
51         "common_type",
52         "common_flags",
53         "common_preempt_count",
54         "common_pid",
55         "common_tgid",
56         FIELD_STRING_IP,
57         FIELD_STRING_RETIP,
58         FIELD_STRING_FUNC,
59 };
60
61 /* Printing function type */
62 typedef int (*print_type_func_t)(struct trace_seq *, const char *, void *,
63                                  void *);
64 #define PRINT_TYPE_FUNC_NAME(type)      print_type_##type
65 #define PRINT_TYPE_FMT_NAME(type)       print_type_format_##type
66
67 /* Printing  in basic type function template */
68 #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt, cast)                   \
69 static __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s,    \
70                                                 const char *name,       \
71                                                 void *data, void *ent)\
72 {                                                                       \
73         return trace_seq_printf(s, " %s=" fmt, name, (cast)*(type *)data);\
74 }                                                                       \
75 static const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
76
77 DEFINE_BASIC_PRINT_TYPE_FUNC(u8, "%x", unsigned int)
78 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "%x", unsigned int)
79 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "%lx", unsigned long)
80 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "%llx", unsigned long long)
81 DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d", int)
82 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d", int)
83 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%ld", long)
84 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%lld", long long)
85
86 /* data_rloc: data relative location, compatible with u32 */
87 #define make_data_rloc(len, roffs)      \
88         (((u32)(len) << 16) | ((u32)(roffs) & 0xffff))
89 #define get_rloc_len(dl)        ((u32)(dl) >> 16)
90 #define get_rloc_offs(dl)       ((u32)(dl) & 0xffff)
91
92 static inline void *get_rloc_data(u32 *dl)
93 {
94         return (u8 *)dl + get_rloc_offs(*dl);
95 }
96
97 /* For data_loc conversion */
98 static inline void *get_loc_data(u32 *dl, void *ent)
99 {
100         return (u8 *)ent + get_rloc_offs(*dl);
101 }
102
103 /*
104  * Convert data_rloc to data_loc:
105  *  data_rloc stores the offset from data_rloc itself, but data_loc
106  *  stores the offset from event entry.
107  */
108 #define convert_rloc_to_loc(dl, offs)   ((u32)(dl) + (offs))
109
110 /* For defining macros, define string/string_size types */
111 typedef u32 string;
112 typedef u32 string_size;
113
114 /* Print type function for string type */
115 static __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s,
116                                                   const char *name,
117                                                   void *data, void *ent)
118 {
119         int len = *(u32 *)data >> 16;
120
121         if (!len)
122                 return trace_seq_printf(s, " %s=(fault)", name);
123         else
124                 return trace_seq_printf(s, " %s=\"%s\"", name,
125                                         (const char *)get_loc_data(data, ent));
126 }
127 static const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
128
129 /* Data fetch function type */
130 typedef void (*fetch_func_t)(struct pt_regs *, void *, void *);
131
132 struct fetch_param {
133         fetch_func_t    fn;
134         void *data;
135 };
136
137 static __kprobes void call_fetch(struct fetch_param *fprm,
138                                  struct pt_regs *regs, void *dest)
139 {
140         return fprm->fn(regs, fprm->data, dest);
141 }
142
143 #define FETCH_FUNC_NAME(method, type)   fetch_##method##_##type
144 /*
145  * Define macro for basic types - we don't need to define s* types, because
146  * we have to care only about bitwidth at recording time.
147  */
148 #define DEFINE_BASIC_FETCH_FUNCS(method) \
149 DEFINE_FETCH_##method(u8)               \
150 DEFINE_FETCH_##method(u16)              \
151 DEFINE_FETCH_##method(u32)              \
152 DEFINE_FETCH_##method(u64)
153
154 #define CHECK_FETCH_FUNCS(method, fn)                   \
155         (((FETCH_FUNC_NAME(method, u8) == fn) ||        \
156           (FETCH_FUNC_NAME(method, u16) == fn) ||       \
157           (FETCH_FUNC_NAME(method, u32) == fn) ||       \
158           (FETCH_FUNC_NAME(method, u64) == fn) ||       \
159           (FETCH_FUNC_NAME(method, string) == fn) ||    \
160           (FETCH_FUNC_NAME(method, string_size) == fn)) \
161          && (fn != NULL))
162
163 /* Data fetch function templates */
164 #define DEFINE_FETCH_reg(type)                                          \
165 static __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs,  \
166                                         void *offset, void *dest)       \
167 {                                                                       \
168         *(type *)dest = (type)regs_get_register(regs,                   \
169                                 (unsigned int)((unsigned long)offset)); \
170 }
171 DEFINE_BASIC_FETCH_FUNCS(reg)
172 /* No string on the register */
173 #define fetch_reg_string NULL
174 #define fetch_reg_string_size NULL
175
176 #define DEFINE_FETCH_stack(type)                                        \
177 static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
178                                           void *offset, void *dest)     \
179 {                                                                       \
180         *(type *)dest = (type)regs_get_kernel_stack_nth(regs,           \
181                                 (unsigned int)((unsigned long)offset)); \
182 }
183 DEFINE_BASIC_FETCH_FUNCS(stack)
184 /* No string on the stack entry */
185 #define fetch_stack_string NULL
186 #define fetch_stack_string_size NULL
187
188 #define DEFINE_FETCH_retval(type)                                       \
189 static __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,\
190                                           void *dummy, void *dest)      \
191 {                                                                       \
192         *(type *)dest = (type)regs_return_value(regs);                  \
193 }
194 DEFINE_BASIC_FETCH_FUNCS(retval)
195 /* No string on the retval */
196 #define fetch_retval_string NULL
197 #define fetch_retval_string_size NULL
198
199 #define DEFINE_FETCH_memory(type)                                       \
200 static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
201                                           void *addr, void *dest)       \
202 {                                                                       \
203         type retval;                                                    \
204         if (probe_kernel_address(addr, retval))                         \
205                 *(type *)dest = 0;                                      \
206         else                                                            \
207                 *(type *)dest = retval;                                 \
208 }
209 DEFINE_BASIC_FETCH_FUNCS(memory)
210 /*
211  * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
212  * length and relative data location.
213  */
214 static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
215                                                       void *addr, void *dest)
216 {
217         long ret;
218         int maxlen = get_rloc_len(*(u32 *)dest);
219         u8 *dst = get_rloc_data(dest);
220         u8 *src = addr;
221         mm_segment_t old_fs = get_fs();
222         if (!maxlen)
223                 return;
224         /*
225          * Try to get string again, since the string can be changed while
226          * probing.
227          */
228         set_fs(KERNEL_DS);
229         pagefault_disable();
230         do
231                 ret = __copy_from_user_inatomic(dst++, src++, 1);
232         while (dst[-1] && ret == 0 && src - (u8 *)addr < maxlen);
233         dst[-1] = '\0';
234         pagefault_enable();
235         set_fs(old_fs);
236
237         if (ret < 0) {  /* Failed to fetch string */
238                 ((u8 *)get_rloc_data(dest))[0] = '\0';
239                 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
240         } else
241                 *(u32 *)dest = make_data_rloc(src - (u8 *)addr,
242                                               get_rloc_offs(*(u32 *)dest));
243 }
244 /* Return the length of string -- including null terminal byte */
245 static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
246                                                         void *addr, void *dest)
247 {
248         int ret, len = 0;
249         u8 c;
250         mm_segment_t old_fs = get_fs();
251
252         set_fs(KERNEL_DS);
253         pagefault_disable();
254         do {
255                 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
256                 len++;
257         } while (c && ret == 0 && len < MAX_STRING_SIZE);
258         pagefault_enable();
259         set_fs(old_fs);
260
261         if (ret < 0)    /* Failed to check the length */
262                 *(u32 *)dest = 0;
263         else
264                 *(u32 *)dest = len;
265 }
266
267 /* Memory fetching by symbol */
268 struct symbol_cache {
269         char *symbol;
270         long offset;
271         unsigned long addr;
272 };
273
274 static unsigned long update_symbol_cache(struct symbol_cache *sc)
275 {
276         sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
277         if (sc->addr)
278                 sc->addr += sc->offset;
279         return sc->addr;
280 }
281
282 static void free_symbol_cache(struct symbol_cache *sc)
283 {
284         kfree(sc->symbol);
285         kfree(sc);
286 }
287
288 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
289 {
290         struct symbol_cache *sc;
291
292         if (!sym || strlen(sym) == 0)
293                 return NULL;
294         sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
295         if (!sc)
296                 return NULL;
297
298         sc->symbol = kstrdup(sym, GFP_KERNEL);
299         if (!sc->symbol) {
300                 kfree(sc);
301                 return NULL;
302         }
303         sc->offset = offset;
304
305         update_symbol_cache(sc);
306         return sc;
307 }
308
309 #define DEFINE_FETCH_symbol(type)                                       \
310 static __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,\
311                                           void *data, void *dest)       \
312 {                                                                       \
313         struct symbol_cache *sc = data;                                 \
314         if (sc->addr)                                                   \
315                 fetch_memory_##type(regs, (void *)sc->addr, dest);      \
316         else                                                            \
317                 *(type *)dest = 0;                                      \
318 }
319 DEFINE_BASIC_FETCH_FUNCS(symbol)
320 DEFINE_FETCH_symbol(string)
321 DEFINE_FETCH_symbol(string_size)
322
323 /* Dereference memory access function */
324 struct deref_fetch_param {
325         struct fetch_param orig;
326         long offset;
327 };
328
329 #define DEFINE_FETCH_deref(type)                                        \
330 static __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,\
331                                             void *data, void *dest)     \
332 {                                                                       \
333         struct deref_fetch_param *dprm = data;                          \
334         unsigned long addr;                                             \
335         call_fetch(&dprm->orig, regs, &addr);                           \
336         if (addr) {                                                     \
337                 addr += dprm->offset;                                   \
338                 fetch_memory_##type(regs, (void *)addr, dest);          \
339         } else                                                          \
340                 *(type *)dest = 0;                                      \
341 }
342 DEFINE_BASIC_FETCH_FUNCS(deref)
343 DEFINE_FETCH_deref(string)
344 DEFINE_FETCH_deref(string_size)
345
346 static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
347 {
348         if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
349                 free_deref_fetch_param(data->orig.data);
350         else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
351                 free_symbol_cache(data->orig.data);
352         kfree(data);
353 }
354
355 /* Bitfield fetch function */
356 struct bitfield_fetch_param {
357         struct fetch_param orig;
358         unsigned char hi_shift;
359         unsigned char low_shift;
360 };
361
362 #define DEFINE_FETCH_bitfield(type)                                     \
363 static __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs,\
364                                             void *data, void *dest)     \
365 {                                                                       \
366         struct bitfield_fetch_param *bprm = data;                       \
367         type buf = 0;                                                   \
368         call_fetch(&bprm->orig, regs, &buf);                            \
369         if (buf) {                                                      \
370                 buf <<= bprm->hi_shift;                                 \
371                 buf >>= bprm->low_shift;                                \
372         }                                                               \
373         *(type *)dest = buf;                                            \
374 }
375 DEFINE_BASIC_FETCH_FUNCS(bitfield)
376 #define fetch_bitfield_string NULL
377 #define fetch_bitfield_string_size NULL
378
379 static __kprobes void
380 free_bitfield_fetch_param(struct bitfield_fetch_param *data)
381 {
382         /*
383          * Don't check the bitfield itself, because this must be the
384          * last fetch function.
385          */
386         if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
387                 free_deref_fetch_param(data->orig.data);
388         else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
389                 free_symbol_cache(data->orig.data);
390         kfree(data);
391 }
392 /* Default (unsigned long) fetch type */
393 #define __DEFAULT_FETCH_TYPE(t) u##t
394 #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
395 #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
396 #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
397
398 /* Fetch types */
399 enum {
400         FETCH_MTD_reg = 0,
401         FETCH_MTD_stack,
402         FETCH_MTD_retval,
403         FETCH_MTD_memory,
404         FETCH_MTD_symbol,
405         FETCH_MTD_deref,
406         FETCH_MTD_bitfield,
407         FETCH_MTD_END,
408 };
409
410 #define ASSIGN_FETCH_FUNC(method, type) \
411         [FETCH_MTD_##method] = FETCH_FUNC_NAME(method, type)
412
413 #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
414         {.name = _name,                         \
415          .size = _size,                                 \
416          .is_signed = sign,                             \
417          .print = PRINT_TYPE_FUNC_NAME(ptype),          \
418          .fmt = PRINT_TYPE_FMT_NAME(ptype),             \
419          .fmttype = _fmttype,                           \
420          .fetch = {                                     \
421 ASSIGN_FETCH_FUNC(reg, ftype),                          \
422 ASSIGN_FETCH_FUNC(stack, ftype),                        \
423 ASSIGN_FETCH_FUNC(retval, ftype),                       \
424 ASSIGN_FETCH_FUNC(memory, ftype),                       \
425 ASSIGN_FETCH_FUNC(symbol, ftype),                       \
426 ASSIGN_FETCH_FUNC(deref, ftype),                        \
427 ASSIGN_FETCH_FUNC(bitfield, ftype),                     \
428           }                                             \
429         }
430
431 #define ASSIGN_FETCH_TYPE(ptype, ftype, sign)                   \
432         __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #ptype)
433
434 #define FETCH_TYPE_STRING 0
435 #define FETCH_TYPE_STRSIZE 1
436
437 /* Fetch type information table */
438 static const struct fetch_type {
439         const char      *name;          /* Name of type */
440         size_t          size;           /* Byte size of type */
441         int             is_signed;      /* Signed flag */
442         print_type_func_t       print;  /* Print functions */
443         const char      *fmt;           /* Fromat string */
444         const char      *fmttype;       /* Name in format file */
445         /* Fetch functions */
446         fetch_func_t    fetch[FETCH_MTD_END];
447 } fetch_type_table[] = {
448         /* Special types */
449         [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
450                                         sizeof(u32), 1, "__data_loc char[]"),
451         [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
452                                         string_size, sizeof(u32), 0, "u32"),
453         /* Basic types */
454         ASSIGN_FETCH_TYPE(u8,  u8,  0),
455         ASSIGN_FETCH_TYPE(u16, u16, 0),
456         ASSIGN_FETCH_TYPE(u32, u32, 0),
457         ASSIGN_FETCH_TYPE(u64, u64, 0),
458         ASSIGN_FETCH_TYPE(s8,  u8,  1),
459         ASSIGN_FETCH_TYPE(s16, u16, 1),
460         ASSIGN_FETCH_TYPE(s32, u32, 1),
461         ASSIGN_FETCH_TYPE(s64, u64, 1),
462 };
463
464 static const struct fetch_type *find_fetch_type(const char *type)
465 {
466         int i;
467
468         if (!type)
469                 type = DEFAULT_FETCH_TYPE_STR;
470
471         /* Special case: bitfield */
472         if (*type == 'b') {
473                 unsigned long bs;
474                 type = strchr(type, '/');
475                 if (!type)
476                         goto fail;
477                 type++;
478                 if (strict_strtoul(type, 0, &bs))
479                         goto fail;
480                 switch (bs) {
481                 case 8:
482                         return find_fetch_type("u8");
483                 case 16:
484                         return find_fetch_type("u16");
485                 case 32:
486                         return find_fetch_type("u32");
487                 case 64:
488                         return find_fetch_type("u64");
489                 default:
490                         goto fail;
491                 }
492         }
493
494         for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++)
495                 if (strcmp(type, fetch_type_table[i].name) == 0)
496                         return &fetch_type_table[i];
497 fail:
498         return NULL;
499 }
500
501 /* Special function : only accept unsigned long */
502 static __kprobes void fetch_stack_address(struct pt_regs *regs,
503                                           void *dummy, void *dest)
504 {
505         *(unsigned long *)dest = kernel_stack_pointer(regs);
506 }
507
508 static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
509                                             fetch_func_t orig_fn)
510 {
511         int i;
512
513         if (type != &fetch_type_table[FETCH_TYPE_STRING])
514                 return NULL;    /* Only string type needs size function */
515         for (i = 0; i < FETCH_MTD_END; i++)
516                 if (type->fetch[i] == orig_fn)
517                         return fetch_type_table[FETCH_TYPE_STRSIZE].fetch[i];
518
519         WARN_ON(1);     /* This should not happen */
520         return NULL;
521 }
522
523 /**
524  * Kprobe event core functions
525  */
526
527 struct probe_arg {
528         struct fetch_param      fetch;
529         struct fetch_param      fetch_size;
530         unsigned int            offset; /* Offset from argument entry */
531         const char              *name;  /* Name of this argument */
532         const char              *comm;  /* Command of this argument */
533         const struct fetch_type *type;  /* Type of this argument */
534 };
535
536 /* Flags for trace_probe */
537 #define TP_FLAG_TRACE   1
538 #define TP_FLAG_PROFILE 2
539 #define TP_FLAG_REGISTERED 4
540
541 struct trace_probe {
542         struct list_head        list;
543         struct kretprobe        rp;     /* Use rp.kp for kprobe use */
544         unsigned long           nhit;
545         unsigned int            flags;  /* For TP_FLAG_* */
546         const char              *symbol;        /* symbol name */
547         struct ftrace_event_class       class;
548         struct ftrace_event_call        call;
549         ssize_t                 size;           /* trace entry size */
550         unsigned int            nr_args;
551         struct probe_arg        args[];
552 };
553
554 #define SIZEOF_TRACE_PROBE(n)                   \
555         (offsetof(struct trace_probe, args) +   \
556         (sizeof(struct probe_arg) * (n)))
557
558
559 static __kprobes int trace_probe_is_return(struct trace_probe *tp)
560 {
561         return tp->rp.handler != NULL;
562 }
563
564 static __kprobes const char *trace_probe_symbol(struct trace_probe *tp)
565 {
566         return tp->symbol ? tp->symbol : "unknown";
567 }
568
569 static __kprobes unsigned long trace_probe_offset(struct trace_probe *tp)
570 {
571         return tp->rp.kp.offset;
572 }
573
574 static __kprobes bool trace_probe_is_enabled(struct trace_probe *tp)
575 {
576         return !!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE));
577 }
578
579 static __kprobes bool trace_probe_is_registered(struct trace_probe *tp)
580 {
581         return !!(tp->flags & TP_FLAG_REGISTERED);
582 }
583
584 static __kprobes bool trace_probe_has_gone(struct trace_probe *tp)
585 {
586         return !!(kprobe_gone(&tp->rp.kp));
587 }
588
589 static __kprobes bool trace_probe_within_module(struct trace_probe *tp,
590                                                 struct module *mod)
591 {
592         int len = strlen(mod->name);
593         const char *name = trace_probe_symbol(tp);
594         return strncmp(mod->name, name, len) == 0 && name[len] == ':';
595 }
596
597 static __kprobes bool trace_probe_is_on_module(struct trace_probe *tp)
598 {
599         return !!strchr(trace_probe_symbol(tp), ':');
600 }
601
602 static int register_probe_event(struct trace_probe *tp);
603 static void unregister_probe_event(struct trace_probe *tp);
604
605 static DEFINE_MUTEX(probe_lock);
606 static LIST_HEAD(probe_list);
607
608 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
609 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
610                                 struct pt_regs *regs);
611
612 /* Check the name is good for event/group/fields */
613 static int is_good_name(const char *name)
614 {
615         if (!isalpha(*name) && *name != '_')
616                 return 0;
617         while (*++name != '\0') {
618                 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
619                         return 0;
620         }
621         return 1;
622 }
623
624 /*
625  * Allocate new trace_probe and initialize it (including kprobes).
626  */
627 static struct trace_probe *alloc_trace_probe(const char *group,
628                                              const char *event,
629                                              void *addr,
630                                              const char *symbol,
631                                              unsigned long offs,
632                                              int nargs, int is_return)
633 {
634         struct trace_probe *tp;
635         int ret = -ENOMEM;
636
637         tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
638         if (!tp)
639                 return ERR_PTR(ret);
640
641         if (symbol) {
642                 tp->symbol = kstrdup(symbol, GFP_KERNEL);
643                 if (!tp->symbol)
644                         goto error;
645                 tp->rp.kp.symbol_name = tp->symbol;
646                 tp->rp.kp.offset = offs;
647         } else
648                 tp->rp.kp.addr = addr;
649
650         if (is_return)
651                 tp->rp.handler = kretprobe_dispatcher;
652         else
653                 tp->rp.kp.pre_handler = kprobe_dispatcher;
654
655         if (!event || !is_good_name(event)) {
656                 ret = -EINVAL;
657                 goto error;
658         }
659
660         tp->call.class = &tp->class;
661         tp->call.name = kstrdup(event, GFP_KERNEL);
662         if (!tp->call.name)
663                 goto error;
664
665         if (!group || !is_good_name(group)) {
666                 ret = -EINVAL;
667                 goto error;
668         }
669
670         tp->class.system = kstrdup(group, GFP_KERNEL);
671         if (!tp->class.system)
672                 goto error;
673
674         INIT_LIST_HEAD(&tp->list);
675         return tp;
676 error:
677         kfree(tp->call.name);
678         kfree(tp->symbol);
679         kfree(tp);
680         return ERR_PTR(ret);
681 }
682
683 static void free_probe_arg(struct probe_arg *arg)
684 {
685         if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
686                 free_bitfield_fetch_param(arg->fetch.data);
687         else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
688                 free_deref_fetch_param(arg->fetch.data);
689         else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
690                 free_symbol_cache(arg->fetch.data);
691         kfree(arg->name);
692         kfree(arg->comm);
693 }
694
695 static void free_trace_probe(struct trace_probe *tp)
696 {
697         int i;
698
699         for (i = 0; i < tp->nr_args; i++)
700                 free_probe_arg(&tp->args[i]);
701
702         kfree(tp->call.class->system);
703         kfree(tp->call.name);
704         kfree(tp->symbol);
705         kfree(tp);
706 }
707
708 static struct trace_probe *find_trace_probe(const char *event,
709                                             const char *group)
710 {
711         struct trace_probe *tp;
712
713         list_for_each_entry(tp, &probe_list, list)
714                 if (strcmp(tp->call.name, event) == 0 &&
715                     strcmp(tp->call.class->system, group) == 0)
716                         return tp;
717         return NULL;
718 }
719
720 /* Enable trace_probe - @flag must be TP_FLAG_TRACE or TP_FLAG_PROFILE */
721 static int enable_trace_probe(struct trace_probe *tp, int flag)
722 {
723         int ret = 0;
724
725         tp->flags |= flag;
726         if (trace_probe_is_enabled(tp) && trace_probe_is_registered(tp) &&
727             !trace_probe_has_gone(tp)) {
728                 if (trace_probe_is_return(tp))
729                         ret = enable_kretprobe(&tp->rp);
730                 else
731                         ret = enable_kprobe(&tp->rp.kp);
732         }
733
734         return ret;
735 }
736
737 /* Disable trace_probe - @flag must be TP_FLAG_TRACE or TP_FLAG_PROFILE */
738 static void disable_trace_probe(struct trace_probe *tp, int flag)
739 {
740         tp->flags &= ~flag;
741         if (!trace_probe_is_enabled(tp) && trace_probe_is_registered(tp)) {
742                 if (trace_probe_is_return(tp))
743                         disable_kretprobe(&tp->rp);
744                 else
745                         disable_kprobe(&tp->rp.kp);
746         }
747 }
748
749 /* Internal register function - just handle k*probes and flags */
750 static int __register_trace_probe(struct trace_probe *tp)
751 {
752         int ret;
753
754         if (trace_probe_is_registered(tp))
755                 return -EINVAL;
756
757         /* Set/clear disabled flag according to tp->flag */
758         if (trace_probe_is_enabled(tp))
759                 tp->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
760         else
761                 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
762
763         if (trace_probe_is_return(tp))
764                 ret = register_kretprobe(&tp->rp);
765         else
766                 ret = register_kprobe(&tp->rp.kp);
767
768         if (ret == 0)
769                 tp->flags |= TP_FLAG_REGISTERED;
770         else {
771                 pr_warning("Could not insert probe at %s+%lu: %d\n",
772                            trace_probe_symbol(tp), trace_probe_offset(tp), ret);
773                 if (ret == -ENOENT && trace_probe_is_on_module(tp)) {
774                         pr_warning("This probe might be able to register after"
775                                    "target module is loaded. Continue.\n");
776                         ret = 0;
777                 } else if (ret == -EILSEQ) {
778                         pr_warning("Probing address(0x%p) is not an "
779                                    "instruction boundary.\n",
780                                    tp->rp.kp.addr);
781                         ret = -EINVAL;
782                 }
783         }
784
785         return ret;
786 }
787
788 /* Internal unregister function - just handle k*probes and flags */
789 static void __unregister_trace_probe(struct trace_probe *tp)
790 {
791         if (trace_probe_is_registered(tp)) {
792                 if (trace_probe_is_return(tp))
793                         unregister_kretprobe(&tp->rp);
794                 else
795                         unregister_kprobe(&tp->rp.kp);
796                 tp->flags &= ~TP_FLAG_REGISTERED;
797                 /* Cleanup kprobe for reuse */
798                 if (tp->rp.kp.symbol_name)
799                         tp->rp.kp.addr = NULL;
800         }
801 }
802
803 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
804 static void unregister_trace_probe(struct trace_probe *tp)
805 {
806         __unregister_trace_probe(tp);
807         list_del(&tp->list);
808         unregister_probe_event(tp);
809 }
810
811 /* Register a trace_probe and probe_event */
812 static int register_trace_probe(struct trace_probe *tp)
813 {
814         struct trace_probe *old_tp;
815         int ret;
816
817         mutex_lock(&probe_lock);
818
819         /* Delete old (same name) event if exist */
820         old_tp = find_trace_probe(tp->call.name, tp->call.class->system);
821         if (old_tp) {
822                 unregister_trace_probe(old_tp);
823                 free_trace_probe(old_tp);
824         }
825
826         /* Register new event */
827         ret = register_probe_event(tp);
828         if (ret) {
829                 pr_warning("Failed to register probe event(%d)\n", ret);
830                 goto end;
831         }
832
833         /* Register k*probe */
834         ret = __register_trace_probe(tp);
835         if (ret < 0)
836                 unregister_probe_event(tp);
837         else
838                 list_add_tail(&tp->list, &probe_list);
839
840 end:
841         mutex_unlock(&probe_lock);
842         return ret;
843 }
844
845 /* Module notifier call back, checking event on the module */
846 static int trace_probe_module_callback(struct notifier_block *nb,
847                                        unsigned long val, void *data)
848 {
849         struct module *mod = data;
850         struct trace_probe *tp;
851         int ret;
852
853         if (val != MODULE_STATE_COMING)
854                 return NOTIFY_DONE;
855
856         /* Update probes on coming module */
857         mutex_lock(&probe_lock);
858         list_for_each_entry(tp, &probe_list, list) {
859                 if (trace_probe_within_module(tp, mod)) {
860                         __unregister_trace_probe(tp);
861                         ret = __register_trace_probe(tp);
862                         if (ret)
863                                 pr_warning("Failed to re-register probe %s on"
864                                            "%s: %d\n",
865                                            tp->call.name, mod->name, ret);
866                 }
867         }
868         mutex_unlock(&probe_lock);
869
870         return NOTIFY_DONE;
871 }
872
873 static struct notifier_block trace_probe_module_nb = {
874         .notifier_call = trace_probe_module_callback,
875         .priority = 1   /* Invoked after kprobe module callback */
876 };
877
878 /* Split symbol and offset. */
879 static int split_symbol_offset(char *symbol, unsigned long *offset)
880 {
881         char *tmp;
882         int ret;
883
884         if (!offset)
885                 return -EINVAL;
886
887         tmp = strchr(symbol, '+');
888         if (tmp) {
889                 /* skip sign because strict_strtol doesn't accept '+' */
890                 ret = strict_strtoul(tmp + 1, 0, offset);
891                 if (ret)
892                         return ret;
893                 *tmp = '\0';
894         } else
895                 *offset = 0;
896         return 0;
897 }
898
899 #define PARAM_MAX_ARGS 16
900 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
901
902 static int parse_probe_vars(char *arg, const struct fetch_type *t,
903                             struct fetch_param *f, int is_return)
904 {
905         int ret = 0;
906         unsigned long param;
907
908         if (strcmp(arg, "retval") == 0) {
909                 if (is_return)
910                         f->fn = t->fetch[FETCH_MTD_retval];
911                 else
912                         ret = -EINVAL;
913         } else if (strncmp(arg, "stack", 5) == 0) {
914                 if (arg[5] == '\0') {
915                         if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
916                                 f->fn = fetch_stack_address;
917                         else
918                                 ret = -EINVAL;
919                 } else if (isdigit(arg[5])) {
920                         ret = strict_strtoul(arg + 5, 10, &param);
921                         if (ret || param > PARAM_MAX_STACK)
922                                 ret = -EINVAL;
923                         else {
924                                 f->fn = t->fetch[FETCH_MTD_stack];
925                                 f->data = (void *)param;
926                         }
927                 } else
928                         ret = -EINVAL;
929         } else
930                 ret = -EINVAL;
931         return ret;
932 }
933
934 /* Recursive argument parser */
935 static int __parse_probe_arg(char *arg, const struct fetch_type *t,
936                              struct fetch_param *f, int is_return)
937 {
938         int ret = 0;
939         unsigned long param;
940         long offset;
941         char *tmp;
942
943         switch (arg[0]) {
944         case '$':
945                 ret = parse_probe_vars(arg + 1, t, f, is_return);
946                 break;
947         case '%':       /* named register */
948                 ret = regs_query_register_offset(arg + 1);
949                 if (ret >= 0) {
950                         f->fn = t->fetch[FETCH_MTD_reg];
951                         f->data = (void *)(unsigned long)ret;
952                         ret = 0;
953                 }
954                 break;
955         case '@':       /* memory or symbol */
956                 if (isdigit(arg[1])) {
957                         ret = strict_strtoul(arg + 1, 0, &param);
958                         if (ret)
959                                 break;
960                         f->fn = t->fetch[FETCH_MTD_memory];
961                         f->data = (void *)param;
962                 } else {
963                         ret = split_symbol_offset(arg + 1, &offset);
964                         if (ret)
965                                 break;
966                         f->data = alloc_symbol_cache(arg + 1, offset);
967                         if (f->data)
968                                 f->fn = t->fetch[FETCH_MTD_symbol];
969                 }
970                 break;
971         case '+':       /* deref memory */
972                 arg++;  /* Skip '+', because strict_strtol() rejects it. */
973         case '-':
974                 tmp = strchr(arg, '(');
975                 if (!tmp)
976                         break;
977                 *tmp = '\0';
978                 ret = strict_strtol(arg, 0, &offset);
979                 if (ret)
980                         break;
981                 arg = tmp + 1;
982                 tmp = strrchr(arg, ')');
983                 if (tmp) {
984                         struct deref_fetch_param *dprm;
985                         const struct fetch_type *t2 = find_fetch_type(NULL);
986                         *tmp = '\0';
987                         dprm = kzalloc(sizeof(struct deref_fetch_param),
988                                        GFP_KERNEL);
989                         if (!dprm)
990                                 return -ENOMEM;
991                         dprm->offset = offset;
992                         ret = __parse_probe_arg(arg, t2, &dprm->orig,
993                                                 is_return);
994                         if (ret)
995                                 kfree(dprm);
996                         else {
997                                 f->fn = t->fetch[FETCH_MTD_deref];
998                                 f->data = (void *)dprm;
999                         }
1000                 }
1001                 break;
1002         }
1003         if (!ret && !f->fn) {   /* Parsed, but do not find fetch method */
1004                 pr_info("%s type has no corresponding fetch method.\n",
1005                         t->name);
1006                 ret = -EINVAL;
1007         }
1008         return ret;
1009 }
1010
1011 #define BYTES_TO_BITS(nb)       ((BITS_PER_LONG * (nb)) / sizeof(long))
1012
1013 /* Bitfield type needs to be parsed into a fetch function */
1014 static int __parse_bitfield_probe_arg(const char *bf,
1015                                       const struct fetch_type *t,
1016                                       struct fetch_param *f)
1017 {
1018         struct bitfield_fetch_param *bprm;
1019         unsigned long bw, bo;
1020         char *tail;
1021
1022         if (*bf != 'b')
1023                 return 0;
1024
1025         bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1026         if (!bprm)
1027                 return -ENOMEM;
1028         bprm->orig = *f;
1029         f->fn = t->fetch[FETCH_MTD_bitfield];
1030         f->data = (void *)bprm;
1031
1032         bw = simple_strtoul(bf + 1, &tail, 0);  /* Use simple one */
1033         if (bw == 0 || *tail != '@')
1034                 return -EINVAL;
1035
1036         bf = tail + 1;
1037         bo = simple_strtoul(bf, &tail, 0);
1038         if (tail == bf || *tail != '/')
1039                 return -EINVAL;
1040
1041         bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
1042         bprm->low_shift = bprm->hi_shift + bo;
1043         return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
1044 }
1045
1046 /* String length checking wrapper */
1047 static int parse_probe_arg(char *arg, struct trace_probe *tp,
1048                            struct probe_arg *parg, int is_return)
1049 {
1050         const char *t;
1051         int ret;
1052
1053         if (strlen(arg) > MAX_ARGSTR_LEN) {
1054                 pr_info("Argument is too long.: %s\n",  arg);
1055                 return -ENOSPC;
1056         }
1057         parg->comm = kstrdup(arg, GFP_KERNEL);
1058         if (!parg->comm) {
1059                 pr_info("Failed to allocate memory for command '%s'.\n", arg);
1060                 return -ENOMEM;
1061         }
1062         t = strchr(parg->comm, ':');
1063         if (t) {
1064                 arg[t - parg->comm] = '\0';
1065                 t++;
1066         }
1067         parg->type = find_fetch_type(t);
1068         if (!parg->type) {
1069                 pr_info("Unsupported type: %s\n", t);
1070                 return -EINVAL;
1071         }
1072         parg->offset = tp->size;
1073         tp->size += parg->type->size;
1074         ret = __parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
1075         if (ret >= 0 && t != NULL)
1076                 ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
1077         if (ret >= 0) {
1078                 parg->fetch_size.fn = get_fetch_size_function(parg->type,
1079                                                               parg->fetch.fn);
1080                 parg->fetch_size.data = parg->fetch.data;
1081         }
1082         return ret;
1083 }
1084
1085 /* Return 1 if name is reserved or already used by another argument */
1086 static int conflict_field_name(const char *name,
1087                                struct probe_arg *args, int narg)
1088 {
1089         int i;
1090         for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
1091                 if (strcmp(reserved_field_names[i], name) == 0)
1092                         return 1;
1093         for (i = 0; i < narg; i++)
1094                 if (strcmp(args[i].name, name) == 0)
1095                         return 1;
1096         return 0;
1097 }
1098
1099 static int create_trace_probe(int argc, char **argv)
1100 {
1101         /*
1102          * Argument syntax:
1103          *  - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
1104          *  - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
1105          * Fetch args:
1106          *  $retval     : fetch return value
1107          *  $stack      : fetch stack address
1108          *  $stackN     : fetch Nth of stack (N:0-)
1109          *  @ADDR       : fetch memory at ADDR (ADDR should be in kernel)
1110          *  @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
1111          *  %REG        : fetch register REG
1112          * Dereferencing memory fetch:
1113          *  +|-offs(ARG) : fetch memory at ARG +|- offs address.
1114          * Alias name of args:
1115          *  NAME=FETCHARG : set NAME as alias of FETCHARG.
1116          * Type of args:
1117          *  FETCHARG:TYPE : use TYPE instead of unsigned long.
1118          */
1119         struct trace_probe *tp;
1120         int i, ret = 0;
1121         int is_return = 0, is_delete = 0;
1122         char *symbol = NULL, *event = NULL, *group = NULL;
1123         char *arg;
1124         unsigned long offset = 0;
1125         void *addr = NULL;
1126         char buf[MAX_EVENT_NAME_LEN];
1127
1128         /* argc must be >= 1 */
1129         if (argv[0][0] == 'p')
1130                 is_return = 0;
1131         else if (argv[0][0] == 'r')
1132                 is_return = 1;
1133         else if (argv[0][0] == '-')
1134                 is_delete = 1;
1135         else {
1136                 pr_info("Probe definition must be started with 'p', 'r' or"
1137                         " '-'.\n");
1138                 return -EINVAL;
1139         }
1140
1141         if (argv[0][1] == ':') {
1142                 event = &argv[0][2];
1143                 if (strchr(event, '/')) {
1144                         group = event;
1145                         event = strchr(group, '/') + 1;
1146                         event[-1] = '\0';
1147                         if (strlen(group) == 0) {
1148                                 pr_info("Group name is not specified\n");
1149                                 return -EINVAL;
1150                         }
1151                 }
1152                 if (strlen(event) == 0) {
1153                         pr_info("Event name is not specified\n");
1154                         return -EINVAL;
1155                 }
1156         }
1157         if (!group)
1158                 group = KPROBE_EVENT_SYSTEM;
1159
1160         if (is_delete) {
1161                 if (!event) {
1162                         pr_info("Delete command needs an event name.\n");
1163                         return -EINVAL;
1164                 }
1165                 mutex_lock(&probe_lock);
1166                 tp = find_trace_probe(event, group);
1167                 if (!tp) {
1168                         mutex_unlock(&probe_lock);
1169                         pr_info("Event %s/%s doesn't exist.\n", group, event);
1170                         return -ENOENT;
1171                 }
1172                 /* delete an event */
1173                 unregister_trace_probe(tp);
1174                 free_trace_probe(tp);
1175                 mutex_unlock(&probe_lock);
1176                 return 0;
1177         }
1178
1179         if (argc < 2) {
1180                 pr_info("Probe point is not specified.\n");
1181                 return -EINVAL;
1182         }
1183         if (isdigit(argv[1][0])) {
1184                 if (is_return) {
1185                         pr_info("Return probe point must be a symbol.\n");
1186                         return -EINVAL;
1187                 }
1188                 /* an address specified */
1189                 ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
1190                 if (ret) {
1191                         pr_info("Failed to parse address.\n");
1192                         return ret;
1193                 }
1194         } else {
1195                 /* a symbol specified */
1196                 symbol = argv[1];
1197                 /* TODO: support .init module functions */
1198                 ret = split_symbol_offset(symbol, &offset);
1199                 if (ret) {
1200                         pr_info("Failed to parse symbol.\n");
1201                         return ret;
1202                 }
1203                 if (offset && is_return) {
1204                         pr_info("Return probe must be used without offset.\n");
1205                         return -EINVAL;
1206                 }
1207         }
1208         argc -= 2; argv += 2;
1209
1210         /* setup a probe */
1211         if (!event) {
1212                 /* Make a new event name */
1213                 if (symbol)
1214                         snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
1215                                  is_return ? 'r' : 'p', symbol, offset);
1216                 else
1217                         snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
1218                                  is_return ? 'r' : 'p', addr);
1219                 event = buf;
1220         }
1221         tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
1222                                is_return);
1223         if (IS_ERR(tp)) {
1224                 pr_info("Failed to allocate trace_probe.(%d)\n",
1225                         (int)PTR_ERR(tp));
1226                 return PTR_ERR(tp);
1227         }
1228
1229         /* parse arguments */
1230         ret = 0;
1231         for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
1232                 /* Increment count for freeing args in error case */
1233                 tp->nr_args++;
1234
1235                 /* Parse argument name */
1236                 arg = strchr(argv[i], '=');
1237                 if (arg) {
1238                         *arg++ = '\0';
1239                         tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
1240                 } else {
1241                         arg = argv[i];
1242                         /* If argument name is omitted, set "argN" */
1243                         snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
1244                         tp->args[i].name = kstrdup(buf, GFP_KERNEL);
1245                 }
1246
1247                 if (!tp->args[i].name) {
1248                         pr_info("Failed to allocate argument[%d] name.\n", i);
1249                         ret = -ENOMEM;
1250                         goto error;
1251                 }
1252
1253                 if (!is_good_name(tp->args[i].name)) {
1254                         pr_info("Invalid argument[%d] name: %s\n",
1255                                 i, tp->args[i].name);
1256                         ret = -EINVAL;
1257                         goto error;
1258                 }
1259
1260                 if (conflict_field_name(tp->args[i].name, tp->args, i)) {
1261                         pr_info("Argument[%d] name '%s' conflicts with "
1262                                 "another field.\n", i, argv[i]);
1263                         ret = -EINVAL;
1264                         goto error;
1265                 }
1266
1267                 /* Parse fetch argument */
1268                 ret = parse_probe_arg(arg, tp, &tp->args[i], is_return);
1269                 if (ret) {
1270                         pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
1271                         goto error;
1272                 }
1273         }
1274
1275         ret = register_trace_probe(tp);
1276         if (ret)
1277                 goto error;
1278         return 0;
1279
1280 error:
1281         free_trace_probe(tp);
1282         return ret;
1283 }
1284
1285 static void release_all_trace_probes(void)
1286 {
1287         struct trace_probe *tp;
1288
1289         mutex_lock(&probe_lock);
1290         /* TODO: Use batch unregistration */
1291         while (!list_empty(&probe_list)) {
1292                 tp = list_entry(probe_list.next, struct trace_probe, list);
1293                 unregister_trace_probe(tp);
1294                 free_trace_probe(tp);
1295         }
1296         mutex_unlock(&probe_lock);
1297 }
1298
1299 /* Probes listing interfaces */
1300 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
1301 {
1302         mutex_lock(&probe_lock);
1303         return seq_list_start(&probe_list, *pos);
1304 }
1305
1306 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
1307 {
1308         return seq_list_next(v, &probe_list, pos);
1309 }
1310
1311 static void probes_seq_stop(struct seq_file *m, void *v)
1312 {
1313         mutex_unlock(&probe_lock);
1314 }
1315
1316 static int probes_seq_show(struct seq_file *m, void *v)
1317 {
1318         struct trace_probe *tp = v;
1319         int i;
1320
1321         seq_printf(m, "%c", trace_probe_is_return(tp) ? 'r' : 'p');
1322         seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
1323
1324         if (!tp->symbol)
1325                 seq_printf(m, " 0x%p", tp->rp.kp.addr);
1326         else if (tp->rp.kp.offset)
1327                 seq_printf(m, " %s+%u", trace_probe_symbol(tp),
1328                            tp->rp.kp.offset);
1329         else
1330                 seq_printf(m, " %s", trace_probe_symbol(tp));
1331
1332         for (i = 0; i < tp->nr_args; i++)
1333                 seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
1334         seq_printf(m, "\n");
1335
1336         return 0;
1337 }
1338
1339 static const struct seq_operations probes_seq_op = {
1340         .start  = probes_seq_start,
1341         .next   = probes_seq_next,
1342         .stop   = probes_seq_stop,
1343         .show   = probes_seq_show
1344 };
1345
1346 static int probes_open(struct inode *inode, struct file *file)
1347 {
1348         if ((file->f_mode & FMODE_WRITE) &&
1349             (file->f_flags & O_TRUNC))
1350                 release_all_trace_probes();
1351
1352         return seq_open(file, &probes_seq_op);
1353 }
1354
1355 static int command_trace_probe(const char *buf)
1356 {
1357         char **argv;
1358         int argc = 0, ret = 0;
1359
1360         argv = argv_split(GFP_KERNEL, buf, &argc);
1361         if (!argv)
1362                 return -ENOMEM;
1363
1364         if (argc)
1365                 ret = create_trace_probe(argc, argv);
1366
1367         argv_free(argv);
1368         return ret;
1369 }
1370
1371 #define WRITE_BUFSIZE 4096
1372
1373 static ssize_t probes_write(struct file *file, const char __user *buffer,
1374                             size_t count, loff_t *ppos)
1375 {
1376         char *kbuf, *tmp;
1377         int ret;
1378         size_t done;
1379         size_t size;
1380
1381         kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
1382         if (!kbuf)
1383                 return -ENOMEM;
1384
1385         ret = done = 0;
1386         while (done < count) {
1387                 size = count - done;
1388                 if (size >= WRITE_BUFSIZE)
1389                         size = WRITE_BUFSIZE - 1;
1390                 if (copy_from_user(kbuf, buffer + done, size)) {
1391                         ret = -EFAULT;
1392                         goto out;
1393                 }
1394                 kbuf[size] = '\0';
1395                 tmp = strchr(kbuf, '\n');
1396                 if (tmp) {
1397                         *tmp = '\0';
1398                         size = tmp - kbuf + 1;
1399                 } else if (done + size < count) {
1400                         pr_warning("Line length is too long: "
1401                                    "Should be less than %d.", WRITE_BUFSIZE);
1402                         ret = -EINVAL;
1403                         goto out;
1404                 }
1405                 done += size;
1406                 /* Remove comments */
1407                 tmp = strchr(kbuf, '#');
1408                 if (tmp)
1409                         *tmp = '\0';
1410
1411                 ret = command_trace_probe(kbuf);
1412                 if (ret)
1413                         goto out;
1414         }
1415         ret = done;
1416 out:
1417         kfree(kbuf);
1418         return ret;
1419 }
1420
1421 static const struct file_operations kprobe_events_ops = {
1422         .owner          = THIS_MODULE,
1423         .open           = probes_open,
1424         .read           = seq_read,
1425         .llseek         = seq_lseek,
1426         .release        = seq_release,
1427         .write          = probes_write,
1428 };
1429
1430 /* Probes profiling interfaces */
1431 static int probes_profile_seq_show(struct seq_file *m, void *v)
1432 {
1433         struct trace_probe *tp = v;
1434
1435         seq_printf(m, "  %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
1436                    tp->rp.kp.nmissed);
1437
1438         return 0;
1439 }
1440
1441 static const struct seq_operations profile_seq_op = {
1442         .start  = probes_seq_start,
1443         .next   = probes_seq_next,
1444         .stop   = probes_seq_stop,
1445         .show   = probes_profile_seq_show
1446 };
1447
1448 static int profile_open(struct inode *inode, struct file *file)
1449 {
1450         return seq_open(file, &profile_seq_op);
1451 }
1452
1453 static const struct file_operations kprobe_profile_ops = {
1454         .owner          = THIS_MODULE,
1455         .open           = profile_open,
1456         .read           = seq_read,
1457         .llseek         = seq_lseek,
1458         .release        = seq_release,
1459 };
1460
1461 /* Sum up total data length for dynamic arraies (strings) */
1462 static __kprobes int __get_data_size(struct trace_probe *tp,
1463                                      struct pt_regs *regs)
1464 {
1465         int i, ret = 0;
1466         u32 len;
1467
1468         for (i = 0; i < tp->nr_args; i++)
1469                 if (unlikely(tp->args[i].fetch_size.fn)) {
1470                         call_fetch(&tp->args[i].fetch_size, regs, &len);
1471                         ret += len;
1472                 }
1473
1474         return ret;
1475 }
1476
1477 /* Store the value of each argument */
1478 static __kprobes void store_trace_args(int ent_size, struct trace_probe *tp,
1479                                        struct pt_regs *regs,
1480                                        u8 *data, int maxlen)
1481 {
1482         int i;
1483         u32 end = tp->size;
1484         u32 *dl;        /* Data (relative) location */
1485
1486         for (i = 0; i < tp->nr_args; i++) {
1487                 if (unlikely(tp->args[i].fetch_size.fn)) {
1488                         /*
1489                          * First, we set the relative location and
1490                          * maximum data length to *dl
1491                          */
1492                         dl = (u32 *)(data + tp->args[i].offset);
1493                         *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
1494                         /* Then try to fetch string or dynamic array data */
1495                         call_fetch(&tp->args[i].fetch, regs, dl);
1496                         /* Reduce maximum length */
1497                         end += get_rloc_len(*dl);
1498                         maxlen -= get_rloc_len(*dl);
1499                         /* Trick here, convert data_rloc to data_loc */
1500                         *dl = convert_rloc_to_loc(*dl,
1501                                  ent_size + tp->args[i].offset);
1502                 } else
1503                         /* Just fetching data normally */
1504                         call_fetch(&tp->args[i].fetch, regs,
1505                                    data + tp->args[i].offset);
1506         }
1507 }
1508
1509 /* Kprobe handler */
1510 static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
1511 {
1512         struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1513         struct kprobe_trace_entry_head *entry;
1514         struct ring_buffer_event *event;
1515         struct ring_buffer *buffer;
1516         int size, dsize, pc;
1517         unsigned long irq_flags;
1518         struct ftrace_event_call *call = &tp->call;
1519
1520         tp->nhit++;
1521
1522         local_save_flags(irq_flags);
1523         pc = preempt_count();
1524
1525         dsize = __get_data_size(tp, regs);
1526         size = sizeof(*entry) + tp->size + dsize;
1527
1528         event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
1529                                                   size, irq_flags, pc);
1530         if (!event)
1531                 return;
1532
1533         entry = ring_buffer_event_data(event);
1534         entry->ip = (unsigned long)kp->addr;
1535         store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1536
1537         if (!filter_current_check_discard(buffer, call, entry, event))
1538                 trace_nowake_buffer_unlock_commit_regs(buffer, event,
1539                                                        irq_flags, pc, regs);
1540 }
1541
1542 /* Kretprobe handler */
1543 static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
1544                                           struct pt_regs *regs)
1545 {
1546         struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1547         struct kretprobe_trace_entry_head *entry;
1548         struct ring_buffer_event *event;
1549         struct ring_buffer *buffer;
1550         int size, pc, dsize;
1551         unsigned long irq_flags;
1552         struct ftrace_event_call *call = &tp->call;
1553
1554         local_save_flags(irq_flags);
1555         pc = preempt_count();
1556
1557         dsize = __get_data_size(tp, regs);
1558         size = sizeof(*entry) + tp->size + dsize;
1559
1560         event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
1561                                                   size, irq_flags, pc);
1562         if (!event)
1563                 return;
1564
1565         entry = ring_buffer_event_data(event);
1566         entry->func = (unsigned long)tp->rp.kp.addr;
1567         entry->ret_ip = (unsigned long)ri->ret_addr;
1568         store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1569
1570         if (!filter_current_check_discard(buffer, call, entry, event))
1571                 trace_nowake_buffer_unlock_commit_regs(buffer, event,
1572                                                        irq_flags, pc, regs);
1573 }
1574
1575 /* Event entry printers */
1576 enum print_line_t
1577 print_kprobe_event(struct trace_iterator *iter, int flags,
1578                    struct trace_event *event)
1579 {
1580         struct kprobe_trace_entry_head *field;
1581         struct trace_seq *s = &iter->seq;
1582         struct trace_probe *tp;
1583         u8 *data;
1584         int i;
1585
1586         field = (struct kprobe_trace_entry_head *)iter->ent;
1587         tp = container_of(event, struct trace_probe, call.event);
1588
1589         if (!trace_seq_printf(s, "%s: (", tp->call.name))
1590                 goto partial;
1591
1592         if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1593                 goto partial;
1594
1595         if (!trace_seq_puts(s, ")"))
1596                 goto partial;
1597
1598         data = (u8 *)&field[1];
1599         for (i = 0; i < tp->nr_args; i++)
1600                 if (!tp->args[i].type->print(s, tp->args[i].name,
1601                                              data + tp->args[i].offset, field))
1602                         goto partial;
1603
1604         if (!trace_seq_puts(s, "\n"))
1605                 goto partial;
1606
1607         return TRACE_TYPE_HANDLED;
1608 partial:
1609         return TRACE_TYPE_PARTIAL_LINE;
1610 }
1611
1612 enum print_line_t
1613 print_kretprobe_event(struct trace_iterator *iter, int flags,
1614                       struct trace_event *event)
1615 {
1616         struct kretprobe_trace_entry_head *field;
1617         struct trace_seq *s = &iter->seq;
1618         struct trace_probe *tp;
1619         u8 *data;
1620         int i;
1621
1622         field = (struct kretprobe_trace_entry_head *)iter->ent;
1623         tp = container_of(event, struct trace_probe, call.event);
1624
1625         if (!trace_seq_printf(s, "%s: (", tp->call.name))
1626                 goto partial;
1627
1628         if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1629                 goto partial;
1630
1631         if (!trace_seq_puts(s, " <- "))
1632                 goto partial;
1633
1634         if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1635                 goto partial;
1636
1637         if (!trace_seq_puts(s, ")"))
1638                 goto partial;
1639
1640         data = (u8 *)&field[1];
1641         for (i = 0; i < tp->nr_args; i++)
1642                 if (!tp->args[i].type->print(s, tp->args[i].name,
1643                                              data + tp->args[i].offset, field))
1644                         goto partial;
1645
1646         if (!trace_seq_puts(s, "\n"))
1647                 goto partial;
1648
1649         return TRACE_TYPE_HANDLED;
1650 partial:
1651         return TRACE_TYPE_PARTIAL_LINE;
1652 }
1653
1654 #undef DEFINE_FIELD
1655 #define DEFINE_FIELD(type, item, name, is_signed)                       \
1656         do {                                                            \
1657                 ret = trace_define_field(event_call, #type, name,       \
1658                                          offsetof(typeof(field), item), \
1659                                          sizeof(field.item), is_signed, \
1660                                          FILTER_OTHER);                 \
1661                 if (ret)                                                \
1662                         return ret;                                     \
1663         } while (0)
1664
1665 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1666 {
1667         int ret, i;
1668         struct kprobe_trace_entry_head field;
1669         struct trace_probe *tp = (struct trace_probe *)event_call->data;
1670
1671         DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1672         /* Set argument names as fields */
1673         for (i = 0; i < tp->nr_args; i++) {
1674                 ret = trace_define_field(event_call, tp->args[i].type->fmttype,
1675                                          tp->args[i].name,
1676                                          sizeof(field) + tp->args[i].offset,
1677                                          tp->args[i].type->size,
1678                                          tp->args[i].type->is_signed,
1679                                          FILTER_OTHER);
1680                 if (ret)
1681                         return ret;
1682         }
1683         return 0;
1684 }
1685
1686 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1687 {
1688         int ret, i;
1689         struct kretprobe_trace_entry_head field;
1690         struct trace_probe *tp = (struct trace_probe *)event_call->data;
1691
1692         DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1693         DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1694         /* Set argument names as fields */
1695         for (i = 0; i < tp->nr_args; i++) {
1696                 ret = trace_define_field(event_call, tp->args[i].type->fmttype,
1697                                          tp->args[i].name,
1698                                          sizeof(field) + tp->args[i].offset,
1699                                          tp->args[i].type->size,
1700                                          tp->args[i].type->is_signed,
1701                                          FILTER_OTHER);
1702                 if (ret)
1703                         return ret;
1704         }
1705         return 0;
1706 }
1707
1708 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
1709 {
1710         int i;
1711         int pos = 0;
1712
1713         const char *fmt, *arg;
1714
1715         if (!trace_probe_is_return(tp)) {
1716                 fmt = "(%lx)";
1717                 arg = "REC->" FIELD_STRING_IP;
1718         } else {
1719                 fmt = "(%lx <- %lx)";
1720                 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1721         }
1722
1723         /* When len=0, we just calculate the needed length */
1724 #define LEN_OR_ZERO (len ? len - pos : 0)
1725
1726         pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1727
1728         for (i = 0; i < tp->nr_args; i++) {
1729                 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
1730                                 tp->args[i].name, tp->args[i].type->fmt);
1731         }
1732
1733         pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
1734
1735         for (i = 0; i < tp->nr_args; i++) {
1736                 if (strcmp(tp->args[i].type->name, "string") == 0)
1737                         pos += snprintf(buf + pos, LEN_OR_ZERO,
1738                                         ", __get_str(%s)",
1739                                         tp->args[i].name);
1740                 else
1741                         pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
1742                                         tp->args[i].name);
1743         }
1744
1745 #undef LEN_OR_ZERO
1746
1747         /* return the length of print_fmt */
1748         return pos;
1749 }
1750
1751 static int set_print_fmt(struct trace_probe *tp)
1752 {
1753         int len;
1754         char *print_fmt;
1755
1756         /* First: called with 0 length to calculate the needed length */
1757         len = __set_print_fmt(tp, NULL, 0);
1758         print_fmt = kmalloc(len + 1, GFP_KERNEL);
1759         if (!print_fmt)
1760                 return -ENOMEM;
1761
1762         /* Second: actually write the @print_fmt */
1763         __set_print_fmt(tp, print_fmt, len + 1);
1764         tp->call.print_fmt = print_fmt;
1765
1766         return 0;
1767 }
1768
1769 #ifdef CONFIG_PERF_EVENTS
1770
1771 /* Kprobe profile handler */
1772 static __kprobes void kprobe_perf_func(struct kprobe *kp,
1773                                          struct pt_regs *regs)
1774 {
1775         struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1776         struct ftrace_event_call *call = &tp->call;
1777         struct kprobe_trace_entry_head *entry;
1778         struct hlist_head *head;
1779         int size, __size, dsize;
1780         int rctx;
1781
1782         dsize = __get_data_size(tp, regs);
1783         __size = sizeof(*entry) + tp->size + dsize;
1784         size = ALIGN(__size + sizeof(u32), sizeof(u64));
1785         size -= sizeof(u32);
1786         if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
1787                      "profile buffer not large enough"))
1788                 return;
1789
1790         entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
1791         if (!entry)
1792                 return;
1793
1794         entry->ip = (unsigned long)kp->addr;
1795         memset(&entry[1], 0, dsize);
1796         store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1797
1798         head = this_cpu_ptr(call->perf_events);
1799         perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
1800 }
1801
1802 /* Kretprobe profile handler */
1803 static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
1804                                             struct pt_regs *regs)
1805 {
1806         struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1807         struct ftrace_event_call *call = &tp->call;
1808         struct kretprobe_trace_entry_head *entry;
1809         struct hlist_head *head;
1810         int size, __size, dsize;
1811         int rctx;
1812
1813         dsize = __get_data_size(tp, regs);
1814         __size = sizeof(*entry) + tp->size + dsize;
1815         size = ALIGN(__size + sizeof(u32), sizeof(u64));
1816         size -= sizeof(u32);
1817         if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
1818                      "profile buffer not large enough"))
1819                 return;
1820
1821         entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
1822         if (!entry)
1823                 return;
1824
1825         entry->func = (unsigned long)tp->rp.kp.addr;
1826         entry->ret_ip = (unsigned long)ri->ret_addr;
1827         store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1828
1829         head = this_cpu_ptr(call->perf_events);
1830         perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
1831 }
1832 #endif  /* CONFIG_PERF_EVENTS */
1833
1834 static __kprobes
1835 int kprobe_register(struct ftrace_event_call *event, enum trace_reg type)
1836 {
1837         struct trace_probe *tp = (struct trace_probe *)event->data;
1838
1839         switch (type) {
1840         case TRACE_REG_REGISTER:
1841                 return enable_trace_probe(tp, TP_FLAG_TRACE);
1842         case TRACE_REG_UNREGISTER:
1843                 disable_trace_probe(tp, TP_FLAG_TRACE);
1844                 return 0;
1845
1846 #ifdef CONFIG_PERF_EVENTS
1847         case TRACE_REG_PERF_REGISTER:
1848                 return enable_trace_probe(tp, TP_FLAG_PROFILE);
1849         case TRACE_REG_PERF_UNREGISTER:
1850                 disable_trace_probe(tp, TP_FLAG_PROFILE);
1851                 return 0;
1852 #endif
1853         }
1854         return 0;
1855 }
1856
1857 static __kprobes
1858 int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1859 {
1860         struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1861
1862         if (tp->flags & TP_FLAG_TRACE)
1863                 kprobe_trace_func(kp, regs);
1864 #ifdef CONFIG_PERF_EVENTS
1865         if (tp->flags & TP_FLAG_PROFILE)
1866                 kprobe_perf_func(kp, regs);
1867 #endif
1868         return 0;       /* We don't tweek kernel, so just return 0 */
1869 }
1870
1871 static __kprobes
1872 int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1873 {
1874         struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1875
1876         if (tp->flags & TP_FLAG_TRACE)
1877                 kretprobe_trace_func(ri, regs);
1878 #ifdef CONFIG_PERF_EVENTS
1879         if (tp->flags & TP_FLAG_PROFILE)
1880                 kretprobe_perf_func(ri, regs);
1881 #endif
1882         return 0;       /* We don't tweek kernel, so just return 0 */
1883 }
1884
1885 static struct trace_event_functions kretprobe_funcs = {
1886         .trace          = print_kretprobe_event
1887 };
1888
1889 static struct trace_event_functions kprobe_funcs = {
1890         .trace          = print_kprobe_event
1891 };
1892
1893 static int register_probe_event(struct trace_probe *tp)
1894 {
1895         struct ftrace_event_call *call = &tp->call;
1896         int ret;
1897
1898         /* Initialize ftrace_event_call */
1899         INIT_LIST_HEAD(&call->class->fields);
1900         if (trace_probe_is_return(tp)) {
1901                 call->event.funcs = &kretprobe_funcs;
1902                 call->class->define_fields = kretprobe_event_define_fields;
1903         } else {
1904                 call->event.funcs = &kprobe_funcs;
1905                 call->class->define_fields = kprobe_event_define_fields;
1906         }
1907         if (set_print_fmt(tp) < 0)
1908                 return -ENOMEM;
1909         ret = register_ftrace_event(&call->event);
1910         if (!ret) {
1911                 kfree(call->print_fmt);
1912                 return -ENODEV;
1913         }
1914         call->flags = 0;
1915         call->class->reg = kprobe_register;
1916         call->data = tp;
1917         ret = trace_add_event_call(call);
1918         if (ret) {
1919                 pr_info("Failed to register kprobe event: %s\n", call->name);
1920                 kfree(call->print_fmt);
1921                 unregister_ftrace_event(&call->event);
1922         }
1923         return ret;
1924 }
1925
1926 static void unregister_probe_event(struct trace_probe *tp)
1927 {
1928         /* tp->event is unregistered in trace_remove_event_call() */
1929         trace_remove_event_call(&tp->call);
1930         kfree(tp->call.print_fmt);
1931 }
1932
1933 /* Make a debugfs interface for controlling probe points */
1934 static __init int init_kprobe_trace(void)
1935 {
1936         struct dentry *d_tracer;
1937         struct dentry *entry;
1938
1939         if (register_module_notifier(&trace_probe_module_nb))
1940                 return -EINVAL;
1941
1942         d_tracer = tracing_init_dentry();
1943         if (!d_tracer)
1944                 return 0;
1945
1946         entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1947                                     NULL, &kprobe_events_ops);
1948
1949         /* Event list interface */
1950         if (!entry)
1951                 pr_warning("Could not create debugfs "
1952                            "'kprobe_events' entry\n");
1953
1954         /* Profile interface */
1955         entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1956                                     NULL, &kprobe_profile_ops);
1957
1958         if (!entry)
1959                 pr_warning("Could not create debugfs "
1960                            "'kprobe_profile' entry\n");
1961         return 0;
1962 }
1963 fs_initcall(init_kprobe_trace);
1964
1965
1966 #ifdef CONFIG_FTRACE_STARTUP_TEST
1967
1968 /*
1969  * The "__used" keeps gcc from removing the function symbol
1970  * from the kallsyms table.
1971  */
1972 static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
1973                                                int a4, int a5, int a6)
1974 {
1975         return a1 + a2 + a3 + a4 + a5 + a6;
1976 }
1977
1978 static __init int kprobe_trace_self_tests_init(void)
1979 {
1980         int ret, warn = 0;
1981         int (*target)(int, int, int, int, int, int);
1982         struct trace_probe *tp;
1983
1984         target = kprobe_trace_selftest_target;
1985
1986         pr_info("Testing kprobe tracing: ");
1987
1988         ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1989                                   "$stack $stack0 +0($stack)");
1990         if (WARN_ON_ONCE(ret)) {
1991                 pr_warning("error on probing function entry.\n");
1992                 warn++;
1993         } else {
1994                 /* Enable trace point */
1995                 tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
1996                 if (WARN_ON_ONCE(tp == NULL)) {
1997                         pr_warning("error on getting new probe.\n");
1998                         warn++;
1999                 } else
2000                         enable_trace_probe(tp, TP_FLAG_TRACE);
2001         }
2002
2003         ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
2004                                   "$retval");
2005         if (WARN_ON_ONCE(ret)) {
2006                 pr_warning("error on probing function return.\n");
2007                 warn++;
2008         } else {
2009                 /* Enable trace point */
2010                 tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
2011                 if (WARN_ON_ONCE(tp == NULL)) {
2012                         pr_warning("error on getting new probe.\n");
2013                         warn++;
2014                 } else
2015                         enable_trace_probe(tp, TP_FLAG_TRACE);
2016         }
2017
2018         if (warn)
2019                 goto end;
2020
2021         ret = target(1, 2, 3, 4, 5, 6);
2022
2023         ret = command_trace_probe("-:testprobe");
2024         if (WARN_ON_ONCE(ret)) {
2025                 pr_warning("error on deleting a probe.\n");
2026                 warn++;
2027         }
2028
2029         ret = command_trace_probe("-:testprobe2");
2030         if (WARN_ON_ONCE(ret)) {
2031                 pr_warning("error on deleting a probe.\n");
2032                 warn++;
2033         }
2034
2035 end:
2036         release_all_trace_probes();
2037         if (warn)
2038                 pr_cont("NG: Some tests are failed. Please check them.\n");
2039         else
2040                 pr_cont("OK\n");
2041         return 0;
2042 }
2043
2044 late_initcall(kprobe_trace_self_tests_init);
2045
2046 #endif