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