2 * Kprobes-based tracing events
4 * Created by Masami Hiramatsu <mhiramat@redhat.com>
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.
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.
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
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>
37 #include "trace_output.h"
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"
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"
50 const char *reserved_field_names[] = {
53 "common_preempt_count",
62 /* Printing function type */
63 typedef int (*print_type_func_t)(struct trace_seq *, const char *, void *,
65 #define PRINT_TYPE_FUNC_NAME(type) print_type_##type
66 #define PRINT_TYPE_FMT_NAME(type) print_type_format_##type
68 /* Printing in basic type function template */
69 #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt, cast) \
70 static __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \
72 void *data, void *ent)\
74 return trace_seq_printf(s, " %s=" fmt, name, (cast)*(type *)data);\
76 static const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
78 DEFINE_BASIC_PRINT_TYPE_FUNC(u8, "%x", unsigned int)
79 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "%x", unsigned int)
80 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "%lx", unsigned long)
81 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "%llx", unsigned long long)
82 DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d", int)
83 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d", int)
84 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%ld", long)
85 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%lld", long long)
87 /* data_rloc: data relative location, compatible with u32 */
88 #define make_data_rloc(len, roffs) \
89 (((u32)(len) << 16) | ((u32)(roffs) & 0xffff))
90 #define get_rloc_len(dl) ((u32)(dl) >> 16)
91 #define get_rloc_offs(dl) ((u32)(dl) & 0xffff)
93 static inline void *get_rloc_data(u32 *dl)
95 return (u8 *)dl + get_rloc_offs(*dl);
98 /* For data_loc conversion */
99 static inline void *get_loc_data(u32 *dl, void *ent)
101 return (u8 *)ent + get_rloc_offs(*dl);
105 * Convert data_rloc to data_loc:
106 * data_rloc stores the offset from data_rloc itself, but data_loc
107 * stores the offset from event entry.
109 #define convert_rloc_to_loc(dl, offs) ((u32)(dl) + (offs))
111 /* For defining macros, define string/string_size types */
113 typedef u32 string_size;
115 /* Print type function for string type */
116 static __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s,
118 void *data, void *ent)
120 int len = *(u32 *)data >> 16;
123 return trace_seq_printf(s, " %s=(fault)", name);
125 return trace_seq_printf(s, " %s=\"%s\"", name,
126 (const char *)get_loc_data(data, ent));
128 static const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
130 /* Data fetch function type */
131 typedef void (*fetch_func_t)(struct pt_regs *, void *, void *);
138 static __kprobes void call_fetch(struct fetch_param *fprm,
139 struct pt_regs *regs, void *dest)
141 return fprm->fn(regs, fprm->data, dest);
144 #define FETCH_FUNC_NAME(method, type) fetch_##method##_##type
146 * Define macro for basic types - we don't need to define s* types, because
147 * we have to care only about bitwidth at recording time.
149 #define DEFINE_BASIC_FETCH_FUNCS(method) \
150 DEFINE_FETCH_##method(u8) \
151 DEFINE_FETCH_##method(u16) \
152 DEFINE_FETCH_##method(u32) \
153 DEFINE_FETCH_##method(u64)
155 #define CHECK_FETCH_FUNCS(method, fn) \
156 (((FETCH_FUNC_NAME(method, u8) == fn) || \
157 (FETCH_FUNC_NAME(method, u16) == fn) || \
158 (FETCH_FUNC_NAME(method, u32) == fn) || \
159 (FETCH_FUNC_NAME(method, u64) == fn) || \
160 (FETCH_FUNC_NAME(method, string) == fn) || \
161 (FETCH_FUNC_NAME(method, string_size) == fn)) \
164 /* Data fetch function templates */
165 #define DEFINE_FETCH_reg(type) \
166 static __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \
167 void *offset, void *dest) \
169 *(type *)dest = (type)regs_get_register(regs, \
170 (unsigned int)((unsigned long)offset)); \
172 DEFINE_BASIC_FETCH_FUNCS(reg)
173 /* No string on the register */
174 #define fetch_reg_string NULL
175 #define fetch_reg_string_size NULL
177 #define DEFINE_FETCH_stack(type) \
178 static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
179 void *offset, void *dest) \
181 *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
182 (unsigned int)((unsigned long)offset)); \
184 DEFINE_BASIC_FETCH_FUNCS(stack)
185 /* No string on the stack entry */
186 #define fetch_stack_string NULL
187 #define fetch_stack_string_size NULL
189 #define DEFINE_FETCH_retval(type) \
190 static __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,\
191 void *dummy, void *dest) \
193 *(type *)dest = (type)regs_return_value(regs); \
195 DEFINE_BASIC_FETCH_FUNCS(retval)
196 /* No string on the retval */
197 #define fetch_retval_string NULL
198 #define fetch_retval_string_size NULL
200 #define DEFINE_FETCH_memory(type) \
201 static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
202 void *addr, void *dest) \
205 if (probe_kernel_address(addr, retval)) \
208 *(type *)dest = retval; \
210 DEFINE_BASIC_FETCH_FUNCS(memory)
212 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
213 * length and relative data location.
215 static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
216 void *addr, void *dest)
219 int maxlen = get_rloc_len(*(u32 *)dest);
220 u8 *dst = get_rloc_data(dest);
222 mm_segment_t old_fs = get_fs();
226 * Try to get string again, since the string can be changed while
232 ret = __copy_from_user_inatomic(dst++, src++, 1);
233 while (dst[-1] && ret == 0 && src - (u8 *)addr < maxlen);
238 if (ret < 0) { /* Failed to fetch string */
239 ((u8 *)get_rloc_data(dest))[0] = '\0';
240 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
242 *(u32 *)dest = make_data_rloc(src - (u8 *)addr,
243 get_rloc_offs(*(u32 *)dest));
245 /* Return the length of string -- including null terminal byte */
246 static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
247 void *addr, void *dest)
251 mm_segment_t old_fs = get_fs();
256 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
258 } while (c && ret == 0 && len < MAX_STRING_SIZE);
262 if (ret < 0) /* Failed to check the length */
268 /* Memory fetching by symbol */
269 struct symbol_cache {
275 static unsigned long update_symbol_cache(struct symbol_cache *sc)
277 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
279 sc->addr += sc->offset;
283 static void free_symbol_cache(struct symbol_cache *sc)
289 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
291 struct symbol_cache *sc;
293 if (!sym || strlen(sym) == 0)
295 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
299 sc->symbol = kstrdup(sym, GFP_KERNEL);
306 update_symbol_cache(sc);
310 #define DEFINE_FETCH_symbol(type) \
311 static __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,\
312 void *data, void *dest) \
314 struct symbol_cache *sc = data; \
316 fetch_memory_##type(regs, (void *)sc->addr, dest); \
320 DEFINE_BASIC_FETCH_FUNCS(symbol)
321 DEFINE_FETCH_symbol(string)
322 DEFINE_FETCH_symbol(string_size)
324 /* Dereference memory access function */
325 struct deref_fetch_param {
326 struct fetch_param orig;
330 #define DEFINE_FETCH_deref(type) \
331 static __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,\
332 void *data, void *dest) \
334 struct deref_fetch_param *dprm = data; \
335 unsigned long addr; \
336 call_fetch(&dprm->orig, regs, &addr); \
338 addr += dprm->offset; \
339 fetch_memory_##type(regs, (void *)addr, dest); \
343 DEFINE_BASIC_FETCH_FUNCS(deref)
344 DEFINE_FETCH_deref(string)
345 DEFINE_FETCH_deref(string_size)
347 static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
349 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
350 free_deref_fetch_param(data->orig.data);
351 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
352 free_symbol_cache(data->orig.data);
356 /* Bitfield fetch function */
357 struct bitfield_fetch_param {
358 struct fetch_param orig;
359 unsigned char hi_shift;
360 unsigned char low_shift;
363 #define DEFINE_FETCH_bitfield(type) \
364 static __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs,\
365 void *data, void *dest) \
367 struct bitfield_fetch_param *bprm = data; \
369 call_fetch(&bprm->orig, regs, &buf); \
371 buf <<= bprm->hi_shift; \
372 buf >>= bprm->low_shift; \
374 *(type *)dest = buf; \
376 DEFINE_BASIC_FETCH_FUNCS(bitfield)
377 #define fetch_bitfield_string NULL
378 #define fetch_bitfield_string_size NULL
380 static __kprobes void
381 free_bitfield_fetch_param(struct bitfield_fetch_param *data)
384 * Don't check the bitfield itself, because this must be the
385 * last fetch function.
387 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
388 free_deref_fetch_param(data->orig.data);
389 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
390 free_symbol_cache(data->orig.data);
393 /* Default (unsigned long) fetch type */
394 #define __DEFAULT_FETCH_TYPE(t) u##t
395 #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
396 #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
397 #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
411 #define ASSIGN_FETCH_FUNC(method, type) \
412 [FETCH_MTD_##method] = FETCH_FUNC_NAME(method, type)
414 #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
418 .print = PRINT_TYPE_FUNC_NAME(ptype), \
419 .fmt = PRINT_TYPE_FMT_NAME(ptype), \
420 .fmttype = _fmttype, \
422 ASSIGN_FETCH_FUNC(reg, ftype), \
423 ASSIGN_FETCH_FUNC(stack, ftype), \
424 ASSIGN_FETCH_FUNC(retval, ftype), \
425 ASSIGN_FETCH_FUNC(memory, ftype), \
426 ASSIGN_FETCH_FUNC(symbol, ftype), \
427 ASSIGN_FETCH_FUNC(deref, ftype), \
428 ASSIGN_FETCH_FUNC(bitfield, ftype), \
432 #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
433 __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #ptype)
435 #define FETCH_TYPE_STRING 0
436 #define FETCH_TYPE_STRSIZE 1
438 /* Fetch type information table */
439 static const struct fetch_type {
440 const char *name; /* Name of type */
441 size_t size; /* Byte size of type */
442 int is_signed; /* Signed flag */
443 print_type_func_t print; /* Print functions */
444 const char *fmt; /* Fromat string */
445 const char *fmttype; /* Name in format file */
446 /* Fetch functions */
447 fetch_func_t fetch[FETCH_MTD_END];
448 } fetch_type_table[] = {
450 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
451 sizeof(u32), 1, "__data_loc char[]"),
452 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
453 string_size, sizeof(u32), 0, "u32"),
455 ASSIGN_FETCH_TYPE(u8, u8, 0),
456 ASSIGN_FETCH_TYPE(u16, u16, 0),
457 ASSIGN_FETCH_TYPE(u32, u32, 0),
458 ASSIGN_FETCH_TYPE(u64, u64, 0),
459 ASSIGN_FETCH_TYPE(s8, u8, 1),
460 ASSIGN_FETCH_TYPE(s16, u16, 1),
461 ASSIGN_FETCH_TYPE(s32, u32, 1),
462 ASSIGN_FETCH_TYPE(s64, u64, 1),
465 static const struct fetch_type *find_fetch_type(const char *type)
470 type = DEFAULT_FETCH_TYPE_STR;
472 /* Special case: bitfield */
475 type = strchr(type, '/');
479 if (strict_strtoul(type, 0, &bs))
483 return find_fetch_type("u8");
485 return find_fetch_type("u16");
487 return find_fetch_type("u32");
489 return find_fetch_type("u64");
495 for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++)
496 if (strcmp(type, fetch_type_table[i].name) == 0)
497 return &fetch_type_table[i];
502 /* Special function : only accept unsigned long */
503 static __kprobes void fetch_stack_address(struct pt_regs *regs,
504 void *dummy, void *dest)
506 *(unsigned long *)dest = kernel_stack_pointer(regs);
509 static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
510 fetch_func_t orig_fn)
514 if (type != &fetch_type_table[FETCH_TYPE_STRING])
515 return NULL; /* Only string type needs size function */
516 for (i = 0; i < FETCH_MTD_END; i++)
517 if (type->fetch[i] == orig_fn)
518 return fetch_type_table[FETCH_TYPE_STRSIZE].fetch[i];
520 WARN_ON(1); /* This should not happen */
525 * Kprobe event core functions
529 struct fetch_param fetch;
530 struct fetch_param fetch_size;
531 unsigned int offset; /* Offset from argument entry */
532 const char *name; /* Name of this argument */
533 const char *comm; /* Command of this argument */
534 const struct fetch_type *type; /* Type of this argument */
537 /* Flags for trace_probe */
538 #define TP_FLAG_TRACE 1
539 #define TP_FLAG_PROFILE 2
542 struct list_head list;
543 struct kretprobe rp; /* Use rp.kp for kprobe use */
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[];
554 #define SIZEOF_TRACE_PROBE(n) \
555 (offsetof(struct trace_probe, args) + \
556 (sizeof(struct probe_arg) * (n)))
559 static __kprobes int probe_is_return(struct trace_probe *tp)
561 return tp->rp.handler != NULL;
564 static __kprobes const char *probe_symbol(struct trace_probe *tp)
566 return tp->symbol ? tp->symbol : "unknown";
569 static int register_probe_event(struct trace_probe *tp);
570 static void unregister_probe_event(struct trace_probe *tp);
572 static DEFINE_MUTEX(probe_lock);
573 static LIST_HEAD(probe_list);
575 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
576 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
577 struct pt_regs *regs);
579 /* Check the name is good for event/group/fields */
580 static int is_good_name(const char *name)
582 if (!isalpha(*name) && *name != '_')
584 while (*++name != '\0') {
585 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
592 * Allocate new trace_probe and initialize it (including kprobes).
594 static struct trace_probe *alloc_trace_probe(const char *group,
599 int nargs, int is_return)
601 struct trace_probe *tp;
604 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
609 tp->symbol = kstrdup(symbol, GFP_KERNEL);
612 tp->rp.kp.symbol_name = tp->symbol;
613 tp->rp.kp.offset = offs;
615 tp->rp.kp.addr = addr;
618 tp->rp.handler = kretprobe_dispatcher;
620 tp->rp.kp.pre_handler = kprobe_dispatcher;
622 if (!event || !is_good_name(event)) {
627 tp->call.class = &tp->class;
628 tp->call.name = kstrdup(event, GFP_KERNEL);
632 if (!group || !is_good_name(group)) {
637 tp->class.system = kstrdup(group, GFP_KERNEL);
638 if (!tp->class.system)
641 INIT_LIST_HEAD(&tp->list);
644 kfree(tp->call.name);
650 static void free_probe_arg(struct probe_arg *arg)
652 if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
653 free_bitfield_fetch_param(arg->fetch.data);
654 else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
655 free_deref_fetch_param(arg->fetch.data);
656 else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
657 free_symbol_cache(arg->fetch.data);
662 static void free_trace_probe(struct trace_probe *tp)
666 for (i = 0; i < tp->nr_args; i++)
667 free_probe_arg(&tp->args[i]);
669 kfree(tp->call.class->system);
670 kfree(tp->call.name);
675 static struct trace_probe *find_probe_event(const char *event,
678 struct trace_probe *tp;
680 list_for_each_entry(tp, &probe_list, list)
681 if (strcmp(tp->call.name, event) == 0 &&
682 strcmp(tp->call.class->system, group) == 0)
687 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
688 static void unregister_trace_probe(struct trace_probe *tp)
690 if (probe_is_return(tp))
691 unregister_kretprobe(&tp->rp);
693 unregister_kprobe(&tp->rp.kp);
695 unregister_probe_event(tp);
698 /* Register a trace_probe and probe_event */
699 static int register_trace_probe(struct trace_probe *tp)
701 struct trace_probe *old_tp;
704 mutex_lock(&probe_lock);
706 /* register as an event */
707 old_tp = find_probe_event(tp->call.name, tp->call.class->system);
709 /* delete old event */
710 unregister_trace_probe(old_tp);
711 free_trace_probe(old_tp);
713 ret = register_probe_event(tp);
715 pr_warning("Failed to register probe event(%d)\n", ret);
719 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
720 if (probe_is_return(tp))
721 ret = register_kretprobe(&tp->rp);
723 ret = register_kprobe(&tp->rp.kp);
726 pr_warning("Could not insert probe(%d)\n", ret);
727 if (ret == -EILSEQ) {
728 pr_warning("Probing address(0x%p) is not an "
729 "instruction boundary.\n",
733 unregister_probe_event(tp);
735 list_add_tail(&tp->list, &probe_list);
737 mutex_unlock(&probe_lock);
741 /* Split symbol and offset. */
742 static int split_symbol_offset(char *symbol, unsigned long *offset)
750 tmp = strchr(symbol, '+');
752 /* skip sign because strict_strtol doesn't accept '+' */
753 ret = strict_strtoul(tmp + 1, 0, offset);
762 #define PARAM_MAX_ARGS 16
763 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
765 static int parse_probe_vars(char *arg, const struct fetch_type *t,
766 struct fetch_param *f, int is_return)
771 if (strcmp(arg, "retval") == 0) {
773 f->fn = t->fetch[FETCH_MTD_retval];
776 } else if (strncmp(arg, "stack", 5) == 0) {
777 if (arg[5] == '\0') {
778 if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
779 f->fn = fetch_stack_address;
782 } else if (isdigit(arg[5])) {
783 ret = strict_strtoul(arg + 5, 10, ¶m);
784 if (ret || param > PARAM_MAX_STACK)
787 f->fn = t->fetch[FETCH_MTD_stack];
788 f->data = (void *)param;
797 /* Recursive argument parser */
798 static int __parse_probe_arg(char *arg, const struct fetch_type *t,
799 struct fetch_param *f, int is_return)
808 ret = parse_probe_vars(arg + 1, t, f, is_return);
810 case '%': /* named register */
811 ret = regs_query_register_offset(arg + 1);
813 f->fn = t->fetch[FETCH_MTD_reg];
814 f->data = (void *)(unsigned long)ret;
818 case '@': /* memory or symbol */
819 if (isdigit(arg[1])) {
820 ret = strict_strtoul(arg + 1, 0, ¶m);
823 f->fn = t->fetch[FETCH_MTD_memory];
824 f->data = (void *)param;
826 ret = split_symbol_offset(arg + 1, &offset);
829 f->data = alloc_symbol_cache(arg + 1, offset);
831 f->fn = t->fetch[FETCH_MTD_symbol];
834 case '+': /* deref memory */
835 arg++; /* Skip '+', because strict_strtol() rejects it. */
837 tmp = strchr(arg, '(');
841 ret = strict_strtol(arg, 0, &offset);
845 tmp = strrchr(arg, ')');
847 struct deref_fetch_param *dprm;
848 const struct fetch_type *t2 = find_fetch_type(NULL);
850 dprm = kzalloc(sizeof(struct deref_fetch_param),
854 dprm->offset = offset;
855 ret = __parse_probe_arg(arg, t2, &dprm->orig,
860 f->fn = t->fetch[FETCH_MTD_deref];
861 f->data = (void *)dprm;
866 if (!ret && !f->fn) { /* Parsed, but do not find fetch method */
867 pr_info("%s type has no corresponding fetch method.\n",
874 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
876 /* Bitfield type needs to be parsed into a fetch function */
877 static int __parse_bitfield_probe_arg(const char *bf,
878 const struct fetch_type *t,
879 struct fetch_param *f)
881 struct bitfield_fetch_param *bprm;
882 unsigned long bw, bo;
888 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
892 f->fn = t->fetch[FETCH_MTD_bitfield];
893 f->data = (void *)bprm;
895 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
896 if (bw == 0 || *tail != '@')
900 bo = simple_strtoul(bf, &tail, 0);
901 if (tail == bf || *tail != '/')
904 bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
905 bprm->low_shift = bprm->hi_shift + bo;
906 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
909 /* String length checking wrapper */
910 static int parse_probe_arg(char *arg, struct trace_probe *tp,
911 struct probe_arg *parg, int is_return)
916 if (strlen(arg) > MAX_ARGSTR_LEN) {
917 pr_info("Argument is too long.: %s\n", arg);
920 parg->comm = kstrdup(arg, GFP_KERNEL);
922 pr_info("Failed to allocate memory for command '%s'.\n", arg);
925 t = strchr(parg->comm, ':');
927 arg[t - parg->comm] = '\0';
930 parg->type = find_fetch_type(t);
932 pr_info("Unsupported type: %s\n", t);
935 parg->offset = tp->size;
936 tp->size += parg->type->size;
937 ret = __parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
938 if (ret >= 0 && t != NULL)
939 ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
941 parg->fetch_size.fn = get_fetch_size_function(parg->type,
943 parg->fetch_size.data = parg->fetch.data;
948 /* Return 1 if name is reserved or already used by another argument */
949 static int conflict_field_name(const char *name,
950 struct probe_arg *args, int narg)
953 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
954 if (strcmp(reserved_field_names[i], name) == 0)
956 for (i = 0; i < narg; i++)
957 if (strcmp(args[i].name, name) == 0)
962 static int create_trace_probe(int argc, char **argv)
966 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
967 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
969 * $retval : fetch return value
970 * $stack : fetch stack address
971 * $stackN : fetch Nth of stack (N:0-)
972 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
973 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
974 * %REG : fetch register REG
975 * Dereferencing memory fetch:
976 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
977 * Alias name of args:
978 * NAME=FETCHARG : set NAME as alias of FETCHARG.
980 * FETCHARG:TYPE : use TYPE instead of unsigned long.
982 struct trace_probe *tp;
984 int is_return = 0, is_delete = 0;
985 char *symbol = NULL, *event = NULL, *group = NULL;
987 unsigned long offset = 0;
989 char buf[MAX_EVENT_NAME_LEN];
991 /* argc must be >= 1 */
992 if (argv[0][0] == 'p')
994 else if (argv[0][0] == 'r')
996 else if (argv[0][0] == '-')
999 pr_info("Probe definition must be started with 'p', 'r' or"
1004 if (argv[0][1] == ':') {
1005 event = &argv[0][2];
1006 if (strchr(event, '/')) {
1008 event = strchr(group, '/') + 1;
1010 if (strlen(group) == 0) {
1011 pr_info("Group name is not specified\n");
1015 if (strlen(event) == 0) {
1016 pr_info("Event name is not specified\n");
1021 group = KPROBE_EVENT_SYSTEM;
1025 pr_info("Delete command needs an event name.\n");
1028 mutex_lock(&probe_lock);
1029 tp = find_probe_event(event, group);
1031 mutex_unlock(&probe_lock);
1032 pr_info("Event %s/%s doesn't exist.\n", group, event);
1035 /* delete an event */
1036 unregister_trace_probe(tp);
1037 free_trace_probe(tp);
1038 mutex_unlock(&probe_lock);
1043 pr_info("Probe point is not specified.\n");
1046 if (isdigit(argv[1][0])) {
1048 pr_info("Return probe point must be a symbol.\n");
1051 /* an address specified */
1052 ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
1054 pr_info("Failed to parse address.\n");
1058 /* a symbol specified */
1060 /* TODO: support .init module functions */
1061 ret = split_symbol_offset(symbol, &offset);
1063 pr_info("Failed to parse symbol.\n");
1066 if (offset && is_return) {
1067 pr_info("Return probe must be used without offset.\n");
1071 argc -= 2; argv += 2;
1075 /* Make a new event name */
1077 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
1078 is_return ? 'r' : 'p', symbol, offset);
1080 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
1081 is_return ? 'r' : 'p', addr);
1084 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
1087 pr_info("Failed to allocate trace_probe.(%d)\n",
1092 /* parse arguments */
1094 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
1095 /* Increment count for freeing args in error case */
1098 /* Parse argument name */
1099 arg = strchr(argv[i], '=');
1102 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
1105 /* If argument name is omitted, set "argN" */
1106 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
1107 tp->args[i].name = kstrdup(buf, GFP_KERNEL);
1110 if (!tp->args[i].name) {
1111 pr_info("Failed to allocate argument[%d] name.\n", i);
1116 if (!is_good_name(tp->args[i].name)) {
1117 pr_info("Invalid argument[%d] name: %s\n",
1118 i, tp->args[i].name);
1123 if (conflict_field_name(tp->args[i].name, tp->args, i)) {
1124 pr_info("Argument[%d] name '%s' conflicts with "
1125 "another field.\n", i, argv[i]);
1130 /* Parse fetch argument */
1131 ret = parse_probe_arg(arg, tp, &tp->args[i], is_return);
1133 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
1138 ret = register_trace_probe(tp);
1144 free_trace_probe(tp);
1148 static void cleanup_all_probes(void)
1150 struct trace_probe *tp;
1152 mutex_lock(&probe_lock);
1153 /* TODO: Use batch unregistration */
1154 while (!list_empty(&probe_list)) {
1155 tp = list_entry(probe_list.next, struct trace_probe, list);
1156 unregister_trace_probe(tp);
1157 free_trace_probe(tp);
1159 mutex_unlock(&probe_lock);
1163 /* Probes listing interfaces */
1164 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
1166 mutex_lock(&probe_lock);
1167 return seq_list_start(&probe_list, *pos);
1170 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
1172 return seq_list_next(v, &probe_list, pos);
1175 static void probes_seq_stop(struct seq_file *m, void *v)
1177 mutex_unlock(&probe_lock);
1180 static int probes_seq_show(struct seq_file *m, void *v)
1182 struct trace_probe *tp = v;
1185 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
1186 seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
1189 seq_printf(m, " 0x%p", tp->rp.kp.addr);
1190 else if (tp->rp.kp.offset)
1191 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
1193 seq_printf(m, " %s", probe_symbol(tp));
1195 for (i = 0; i < tp->nr_args; i++)
1196 seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
1197 seq_printf(m, "\n");
1202 static const struct seq_operations probes_seq_op = {
1203 .start = probes_seq_start,
1204 .next = probes_seq_next,
1205 .stop = probes_seq_stop,
1206 .show = probes_seq_show
1209 static int probes_open(struct inode *inode, struct file *file)
1211 if ((file->f_mode & FMODE_WRITE) &&
1212 (file->f_flags & O_TRUNC))
1213 cleanup_all_probes();
1215 return seq_open(file, &probes_seq_op);
1218 static int command_trace_probe(const char *buf)
1221 int argc = 0, ret = 0;
1223 argv = argv_split(GFP_KERNEL, buf, &argc);
1228 ret = create_trace_probe(argc, argv);
1234 #define WRITE_BUFSIZE 4096
1236 static ssize_t probes_write(struct file *file, const char __user *buffer,
1237 size_t count, loff_t *ppos)
1244 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
1249 while (done < count) {
1250 size = count - done;
1251 if (size >= WRITE_BUFSIZE)
1252 size = WRITE_BUFSIZE - 1;
1253 if (copy_from_user(kbuf, buffer + done, size)) {
1258 tmp = strchr(kbuf, '\n');
1261 size = tmp - kbuf + 1;
1262 } else if (done + size < count) {
1263 pr_warning("Line length is too long: "
1264 "Should be less than %d.", WRITE_BUFSIZE);
1269 /* Remove comments */
1270 tmp = strchr(kbuf, '#');
1274 ret = command_trace_probe(kbuf);
1284 static const struct file_operations kprobe_events_ops = {
1285 .owner = THIS_MODULE,
1286 .open = probes_open,
1288 .llseek = seq_lseek,
1289 .release = seq_release,
1290 .write = probes_write,
1293 /* Probes profiling interfaces */
1294 static int probes_profile_seq_show(struct seq_file *m, void *v)
1296 struct trace_probe *tp = v;
1298 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
1304 static const struct seq_operations profile_seq_op = {
1305 .start = probes_seq_start,
1306 .next = probes_seq_next,
1307 .stop = probes_seq_stop,
1308 .show = probes_profile_seq_show
1311 static int profile_open(struct inode *inode, struct file *file)
1313 return seq_open(file, &profile_seq_op);
1316 static const struct file_operations kprobe_profile_ops = {
1317 .owner = THIS_MODULE,
1318 .open = profile_open,
1320 .llseek = seq_lseek,
1321 .release = seq_release,
1324 /* Sum up total data length for dynamic arraies (strings) */
1325 static __kprobes int __get_data_size(struct trace_probe *tp,
1326 struct pt_regs *regs)
1331 for (i = 0; i < tp->nr_args; i++)
1332 if (unlikely(tp->args[i].fetch_size.fn)) {
1333 call_fetch(&tp->args[i].fetch_size, regs, &len);
1340 /* Store the value of each argument */
1341 static __kprobes void store_trace_args(int ent_size, struct trace_probe *tp,
1342 struct pt_regs *regs,
1343 u8 *data, int maxlen)
1347 u32 *dl; /* Data (relative) location */
1349 for (i = 0; i < tp->nr_args; i++) {
1350 if (unlikely(tp->args[i].fetch_size.fn)) {
1352 * First, we set the relative location and
1353 * maximum data length to *dl
1355 dl = (u32 *)(data + tp->args[i].offset);
1356 *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
1357 /* Then try to fetch string or dynamic array data */
1358 call_fetch(&tp->args[i].fetch, regs, dl);
1359 /* Reduce maximum length */
1360 end += get_rloc_len(*dl);
1361 maxlen -= get_rloc_len(*dl);
1362 /* Trick here, convert data_rloc to data_loc */
1363 *dl = convert_rloc_to_loc(*dl,
1364 ent_size + tp->args[i].offset);
1366 /* Just fetching data normally */
1367 call_fetch(&tp->args[i].fetch, regs,
1368 data + tp->args[i].offset);
1372 /* Kprobe handler */
1373 static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
1375 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1376 struct kprobe_trace_entry_head *entry;
1377 struct ring_buffer_event *event;
1378 struct ring_buffer *buffer;
1379 int size, dsize, pc;
1380 unsigned long irq_flags;
1381 struct ftrace_event_call *call = &tp->call;
1385 local_save_flags(irq_flags);
1386 pc = preempt_count();
1388 dsize = __get_data_size(tp, regs);
1389 size = sizeof(*entry) + tp->size + dsize;
1391 event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
1392 size, irq_flags, pc);
1396 entry = ring_buffer_event_data(event);
1397 entry->ip = (unsigned long)kp->addr;
1398 store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1400 if (!filter_current_check_discard(buffer, call, entry, event))
1401 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
1404 /* Kretprobe handler */
1405 static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
1406 struct pt_regs *regs)
1408 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1409 struct kretprobe_trace_entry_head *entry;
1410 struct ring_buffer_event *event;
1411 struct ring_buffer *buffer;
1412 int size, pc, dsize;
1413 unsigned long irq_flags;
1414 struct ftrace_event_call *call = &tp->call;
1416 local_save_flags(irq_flags);
1417 pc = preempt_count();
1419 dsize = __get_data_size(tp, regs);
1420 size = sizeof(*entry) + tp->size + dsize;
1422 event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
1423 size, irq_flags, pc);
1427 entry = ring_buffer_event_data(event);
1428 entry->func = (unsigned long)tp->rp.kp.addr;
1429 entry->ret_ip = (unsigned long)ri->ret_addr;
1430 store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1432 if (!filter_current_check_discard(buffer, call, entry, event))
1433 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
1436 /* Event entry printers */
1438 print_kprobe_event(struct trace_iterator *iter, int flags,
1439 struct trace_event *event)
1441 struct kprobe_trace_entry_head *field;
1442 struct trace_seq *s = &iter->seq;
1443 struct trace_probe *tp;
1447 field = (struct kprobe_trace_entry_head *)iter->ent;
1448 tp = container_of(event, struct trace_probe, call.event);
1450 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1453 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1456 if (!trace_seq_puts(s, ")"))
1459 data = (u8 *)&field[1];
1460 for (i = 0; i < tp->nr_args; i++)
1461 if (!tp->args[i].type->print(s, tp->args[i].name,
1462 data + tp->args[i].offset, field))
1465 if (!trace_seq_puts(s, "\n"))
1468 return TRACE_TYPE_HANDLED;
1470 return TRACE_TYPE_PARTIAL_LINE;
1474 print_kretprobe_event(struct trace_iterator *iter, int flags,
1475 struct trace_event *event)
1477 struct kretprobe_trace_entry_head *field;
1478 struct trace_seq *s = &iter->seq;
1479 struct trace_probe *tp;
1483 field = (struct kretprobe_trace_entry_head *)iter->ent;
1484 tp = container_of(event, struct trace_probe, call.event);
1486 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1489 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1492 if (!trace_seq_puts(s, " <- "))
1495 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1498 if (!trace_seq_puts(s, ")"))
1501 data = (u8 *)&field[1];
1502 for (i = 0; i < tp->nr_args; i++)
1503 if (!tp->args[i].type->print(s, tp->args[i].name,
1504 data + tp->args[i].offset, field))
1507 if (!trace_seq_puts(s, "\n"))
1510 return TRACE_TYPE_HANDLED;
1512 return TRACE_TYPE_PARTIAL_LINE;
1515 static int probe_event_enable(struct ftrace_event_call *call)
1517 struct trace_probe *tp = (struct trace_probe *)call->data;
1519 tp->flags |= TP_FLAG_TRACE;
1520 if (probe_is_return(tp))
1521 return enable_kretprobe(&tp->rp);
1523 return enable_kprobe(&tp->rp.kp);
1526 static void probe_event_disable(struct ftrace_event_call *call)
1528 struct trace_probe *tp = (struct trace_probe *)call->data;
1530 tp->flags &= ~TP_FLAG_TRACE;
1531 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1532 if (probe_is_return(tp))
1533 disable_kretprobe(&tp->rp);
1535 disable_kprobe(&tp->rp.kp);
1540 #define DEFINE_FIELD(type, item, name, is_signed) \
1542 ret = trace_define_field(event_call, #type, name, \
1543 offsetof(typeof(field), item), \
1544 sizeof(field.item), is_signed, \
1550 static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1553 struct kprobe_trace_entry_head field;
1554 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1556 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1557 /* Set argument names as fields */
1558 for (i = 0; i < tp->nr_args; i++) {
1559 ret = trace_define_field(event_call, tp->args[i].type->fmttype,
1561 sizeof(field) + tp->args[i].offset,
1562 tp->args[i].type->size,
1563 tp->args[i].type->is_signed,
1571 static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1574 struct kretprobe_trace_entry_head field;
1575 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1577 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1578 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1579 /* Set argument names as fields */
1580 for (i = 0; i < tp->nr_args; i++) {
1581 ret = trace_define_field(event_call, tp->args[i].type->fmttype,
1583 sizeof(field) + tp->args[i].offset,
1584 tp->args[i].type->size,
1585 tp->args[i].type->is_signed,
1593 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
1598 const char *fmt, *arg;
1600 if (!probe_is_return(tp)) {
1602 arg = "REC->" FIELD_STRING_IP;
1604 fmt = "(%lx <- %lx)";
1605 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1608 /* When len=0, we just calculate the needed length */
1609 #define LEN_OR_ZERO (len ? len - pos : 0)
1611 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1613 for (i = 0; i < tp->nr_args; i++) {
1614 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
1615 tp->args[i].name, tp->args[i].type->fmt);
1618 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
1620 for (i = 0; i < tp->nr_args; i++) {
1621 if (strcmp(tp->args[i].type->name, "string") == 0)
1622 pos += snprintf(buf + pos, LEN_OR_ZERO,
1626 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
1632 /* return the length of print_fmt */
1636 static int set_print_fmt(struct trace_probe *tp)
1641 /* First: called with 0 length to calculate the needed length */
1642 len = __set_print_fmt(tp, NULL, 0);
1643 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1647 /* Second: actually write the @print_fmt */
1648 __set_print_fmt(tp, print_fmt, len + 1);
1649 tp->call.print_fmt = print_fmt;
1654 #ifdef CONFIG_PERF_EVENTS
1656 /* Kprobe profile handler */
1657 static __kprobes void kprobe_perf_func(struct kprobe *kp,
1658 struct pt_regs *regs)
1660 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1661 struct ftrace_event_call *call = &tp->call;
1662 struct kprobe_trace_entry_head *entry;
1663 struct hlist_head *head;
1664 int size, __size, dsize;
1667 dsize = __get_data_size(tp, regs);
1668 __size = sizeof(*entry) + tp->size + dsize;
1669 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1670 size -= sizeof(u32);
1671 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
1672 "profile buffer not large enough"))
1675 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
1679 entry->ip = (unsigned long)kp->addr;
1680 memset(&entry[1], 0, dsize);
1681 store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1683 head = this_cpu_ptr(call->perf_events);
1684 perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
1687 /* Kretprobe profile handler */
1688 static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
1689 struct pt_regs *regs)
1691 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1692 struct ftrace_event_call *call = &tp->call;
1693 struct kretprobe_trace_entry_head *entry;
1694 struct hlist_head *head;
1695 int size, __size, dsize;
1698 dsize = __get_data_size(tp, regs);
1699 __size = sizeof(*entry) + tp->size + dsize;
1700 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1701 size -= sizeof(u32);
1702 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
1703 "profile buffer not large enough"))
1706 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
1710 entry->func = (unsigned long)tp->rp.kp.addr;
1711 entry->ret_ip = (unsigned long)ri->ret_addr;
1712 store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1714 head = this_cpu_ptr(call->perf_events);
1715 perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
1718 static int probe_perf_enable(struct ftrace_event_call *call)
1720 struct trace_probe *tp = (struct trace_probe *)call->data;
1722 tp->flags |= TP_FLAG_PROFILE;
1724 if (probe_is_return(tp))
1725 return enable_kretprobe(&tp->rp);
1727 return enable_kprobe(&tp->rp.kp);
1730 static void probe_perf_disable(struct ftrace_event_call *call)
1732 struct trace_probe *tp = (struct trace_probe *)call->data;
1734 tp->flags &= ~TP_FLAG_PROFILE;
1736 if (!(tp->flags & TP_FLAG_TRACE)) {
1737 if (probe_is_return(tp))
1738 disable_kretprobe(&tp->rp);
1740 disable_kprobe(&tp->rp.kp);
1743 #endif /* CONFIG_PERF_EVENTS */
1746 int kprobe_register(struct ftrace_event_call *event, enum trace_reg type)
1749 case TRACE_REG_REGISTER:
1750 return probe_event_enable(event);
1751 case TRACE_REG_UNREGISTER:
1752 probe_event_disable(event);
1755 #ifdef CONFIG_PERF_EVENTS
1756 case TRACE_REG_PERF_REGISTER:
1757 return probe_perf_enable(event);
1758 case TRACE_REG_PERF_UNREGISTER:
1759 probe_perf_disable(event);
1767 int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1769 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1771 if (tp->flags & TP_FLAG_TRACE)
1772 kprobe_trace_func(kp, regs);
1773 #ifdef CONFIG_PERF_EVENTS
1774 if (tp->flags & TP_FLAG_PROFILE)
1775 kprobe_perf_func(kp, regs);
1777 return 0; /* We don't tweek kernel, so just return 0 */
1781 int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1783 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1785 if (tp->flags & TP_FLAG_TRACE)
1786 kretprobe_trace_func(ri, regs);
1787 #ifdef CONFIG_PERF_EVENTS
1788 if (tp->flags & TP_FLAG_PROFILE)
1789 kretprobe_perf_func(ri, regs);
1791 return 0; /* We don't tweek kernel, so just return 0 */
1794 static struct trace_event_functions kretprobe_funcs = {
1795 .trace = print_kretprobe_event
1798 static struct trace_event_functions kprobe_funcs = {
1799 .trace = print_kprobe_event
1802 static int register_probe_event(struct trace_probe *tp)
1804 struct ftrace_event_call *call = &tp->call;
1807 /* Initialize ftrace_event_call */
1808 INIT_LIST_HEAD(&call->class->fields);
1809 if (probe_is_return(tp)) {
1810 call->event.funcs = &kretprobe_funcs;
1811 call->class->define_fields = kretprobe_event_define_fields;
1813 call->event.funcs = &kprobe_funcs;
1814 call->class->define_fields = kprobe_event_define_fields;
1816 if (set_print_fmt(tp) < 0)
1818 ret = register_ftrace_event(&call->event);
1820 kfree(call->print_fmt);
1824 call->class->reg = kprobe_register;
1826 ret = trace_add_event_call(call);
1828 pr_info("Failed to register kprobe event: %s\n", call->name);
1829 kfree(call->print_fmt);
1830 unregister_ftrace_event(&call->event);
1835 static void unregister_probe_event(struct trace_probe *tp)
1837 /* tp->event is unregistered in trace_remove_event_call() */
1838 trace_remove_event_call(&tp->call);
1839 kfree(tp->call.print_fmt);
1842 /* Make a debugfs interface for controlling probe points */
1843 static __init int init_kprobe_trace(void)
1845 struct dentry *d_tracer;
1846 struct dentry *entry;
1848 d_tracer = tracing_init_dentry();
1852 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1853 NULL, &kprobe_events_ops);
1855 /* Event list interface */
1857 pr_warning("Could not create debugfs "
1858 "'kprobe_events' entry\n");
1860 /* Profile interface */
1861 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1862 NULL, &kprobe_profile_ops);
1865 pr_warning("Could not create debugfs "
1866 "'kprobe_profile' entry\n");
1869 fs_initcall(init_kprobe_trace);
1872 #ifdef CONFIG_FTRACE_STARTUP_TEST
1874 static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1875 int a4, int a5, int a6)
1877 return a1 + a2 + a3 + a4 + a5 + a6;
1880 static __init int kprobe_trace_self_tests_init(void)
1883 int (*target)(int, int, int, int, int, int);
1884 struct trace_probe *tp;
1886 target = kprobe_trace_selftest_target;
1888 pr_info("Testing kprobe tracing: ");
1890 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1891 "$stack $stack0 +0($stack)");
1892 if (WARN_ON_ONCE(ret)) {
1893 pr_warning("error on probing function entry.\n");
1896 /* Enable trace point */
1897 tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
1898 if (WARN_ON_ONCE(tp == NULL)) {
1899 pr_warning("error on getting new probe.\n");
1902 probe_event_enable(&tp->call);
1905 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1907 if (WARN_ON_ONCE(ret)) {
1908 pr_warning("error on probing function return.\n");
1911 /* Enable trace point */
1912 tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
1913 if (WARN_ON_ONCE(tp == NULL)) {
1914 pr_warning("error on getting new probe.\n");
1917 probe_event_enable(&tp->call);
1923 ret = target(1, 2, 3, 4, 5, 6);
1925 ret = command_trace_probe("-:testprobe");
1926 if (WARN_ON_ONCE(ret)) {
1927 pr_warning("error on deleting a probe.\n");
1931 ret = command_trace_probe("-:testprobe2");
1932 if (WARN_ON_ONCE(ret)) {
1933 pr_warning("error on deleting a probe.\n");
1938 cleanup_all_probes();
1940 pr_cont("NG: Some tests are failed. Please check them.\n");
1946 late_initcall(kprobe_trace_self_tests_init);