2 * kprobe based kernel tracer
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>
33 #include "trace_output.h"
35 #define MAX_TRACE_ARGS 128
36 #define MAX_ARGSTR_LEN 63
37 #define MAX_EVENT_NAME_LEN 64
39 /* currently, trace_kprobe only supports X86. */
42 unsigned long (*func
)(struct pt_regs
*, void *);
46 static __kprobes
unsigned long call_fetch(struct fetch_func
*f
,
49 return f
->func(regs
, f
->data
);
53 static __kprobes
unsigned long fetch_register(struct pt_regs
*regs
,
56 return regs_get_register(regs
, (unsigned int)((unsigned long)offset
));
59 static __kprobes
unsigned long fetch_stack(struct pt_regs
*regs
,
62 return regs_get_kernel_stack_nth(regs
,
63 (unsigned int)((unsigned long)num
));
66 static __kprobes
unsigned long fetch_memory(struct pt_regs
*regs
, void *addr
)
70 if (probe_kernel_address(addr
, retval
))
75 static __kprobes
unsigned long fetch_argument(struct pt_regs
*regs
, void *num
)
77 return regs_get_argument_nth(regs
, (unsigned int)((unsigned long)num
));
80 static __kprobes
unsigned long fetch_retvalue(struct pt_regs
*regs
,
83 return regs_return_value(regs
);
86 static __kprobes
unsigned long fetch_ip(struct pt_regs
*regs
, void *dummy
)
88 return instruction_pointer(regs
);
91 static __kprobes
unsigned long fetch_stack_address(struct pt_regs
*regs
,
94 return kernel_stack_pointer(regs
);
97 /* Memory fetching by symbol */
104 static unsigned long update_symbol_cache(struct symbol_cache
*sc
)
106 sc
->addr
= (unsigned long)kallsyms_lookup_name(sc
->symbol
);
108 sc
->addr
+= sc
->offset
;
112 static void free_symbol_cache(struct symbol_cache
*sc
)
118 static struct symbol_cache
*alloc_symbol_cache(const char *sym
, long offset
)
120 struct symbol_cache
*sc
;
122 if (!sym
|| strlen(sym
) == 0)
124 sc
= kzalloc(sizeof(struct symbol_cache
), GFP_KERNEL
);
128 sc
->symbol
= kstrdup(sym
, GFP_KERNEL
);
135 update_symbol_cache(sc
);
139 static __kprobes
unsigned long fetch_symbol(struct pt_regs
*regs
, void *data
)
141 struct symbol_cache
*sc
= data
;
144 return fetch_memory(regs
, (void *)sc
->addr
);
149 /* Special indirect memory access interface */
150 struct indirect_fetch_data
{
151 struct fetch_func orig
;
155 static __kprobes
unsigned long fetch_indirect(struct pt_regs
*regs
, void *data
)
157 struct indirect_fetch_data
*ind
= data
;
160 addr
= call_fetch(&ind
->orig
, regs
);
163 return fetch_memory(regs
, (void *)addr
);
168 static __kprobes
void free_indirect_fetch_data(struct indirect_fetch_data
*data
)
170 if (data
->orig
.func
== fetch_indirect
)
171 free_indirect_fetch_data(data
->orig
.data
);
172 else if (data
->orig
.func
== fetch_symbol
)
173 free_symbol_cache(data
->orig
.data
);
182 struct list_head list
;
187 const char *symbol
; /* symbol name */
188 struct ftrace_event_call call
;
189 struct trace_event event
;
190 unsigned int nr_args
;
191 struct fetch_func args
[];
194 #define SIZEOF_TRACE_PROBE(n) \
195 (offsetof(struct trace_probe, args) + \
196 (sizeof(struct fetch_func) * (n)))
198 static int kprobe_trace_func(struct kprobe
*kp
, struct pt_regs
*regs
);
199 static int kretprobe_trace_func(struct kretprobe_instance
*ri
,
200 struct pt_regs
*regs
);
202 static __kprobes
int probe_is_return(struct trace_probe
*tp
)
204 return (tp
->rp
.handler
== kretprobe_trace_func
);
207 static __kprobes
const char *probe_symbol(struct trace_probe
*tp
)
209 return tp
->symbol
? tp
->symbol
: "unknown";
212 static __kprobes
long probe_offset(struct trace_probe
*tp
)
214 return (probe_is_return(tp
)) ? tp
->rp
.kp
.offset
: tp
->kp
.offset
;
217 static __kprobes
void *probe_address(struct trace_probe
*tp
)
219 return (probe_is_return(tp
)) ? tp
->rp
.kp
.addr
: tp
->kp
.addr
;
222 static int trace_arg_string(char *buf
, size_t n
, struct fetch_func
*ff
)
226 if (ff
->func
== fetch_argument
)
227 ret
= snprintf(buf
, n
, "a%lu", (unsigned long)ff
->data
);
228 else if (ff
->func
== fetch_register
) {
230 name
= regs_query_register_name((unsigned int)((long)ff
->data
));
231 ret
= snprintf(buf
, n
, "%%%s", name
);
232 } else if (ff
->func
== fetch_stack
)
233 ret
= snprintf(buf
, n
, "s%lu", (unsigned long)ff
->data
);
234 else if (ff
->func
== fetch_memory
)
235 ret
= snprintf(buf
, n
, "@0x%p", ff
->data
);
236 else if (ff
->func
== fetch_symbol
) {
237 struct symbol_cache
*sc
= ff
->data
;
238 ret
= snprintf(buf
, n
, "@%s%+ld", sc
->symbol
, sc
->offset
);
239 } else if (ff
->func
== fetch_retvalue
)
240 ret
= snprintf(buf
, n
, "rv");
241 else if (ff
->func
== fetch_ip
)
242 ret
= snprintf(buf
, n
, "ra");
243 else if (ff
->func
== fetch_stack_address
)
244 ret
= snprintf(buf
, n
, "sa");
245 else if (ff
->func
== fetch_indirect
) {
246 struct indirect_fetch_data
*id
= ff
->data
;
248 ret
= snprintf(buf
, n
, "%+ld(", id
->offset
);
252 ret
= trace_arg_string(buf
+ l
, n
- l
, &id
->orig
);
256 ret
= snprintf(buf
+ l
, n
- l
, ")");
265 static int register_probe_event(struct trace_probe
*tp
);
266 static void unregister_probe_event(struct trace_probe
*tp
);
268 static DEFINE_MUTEX(probe_lock
);
269 static LIST_HEAD(probe_list
);
271 static struct trace_probe
*alloc_trace_probe(const char *symbol
,
272 const char *event
, int nargs
)
274 struct trace_probe
*tp
;
276 tp
= kzalloc(SIZEOF_TRACE_PROBE(nargs
), GFP_KERNEL
);
278 return ERR_PTR(-ENOMEM
);
281 tp
->symbol
= kstrdup(symbol
, GFP_KERNEL
);
287 tp
->call
.name
= kstrdup(event
, GFP_KERNEL
);
291 INIT_LIST_HEAD(&tp
->list
);
296 return ERR_PTR(-ENOMEM
);
299 static void free_trace_probe(struct trace_probe
*tp
)
303 for (i
= 0; i
< tp
->nr_args
; i
++)
304 if (tp
->args
[i
].func
== fetch_symbol
)
305 free_symbol_cache(tp
->args
[i
].data
);
306 else if (tp
->args
[i
].func
== fetch_indirect
)
307 free_indirect_fetch_data(tp
->args
[i
].data
);
309 kfree(tp
->call
.name
);
314 static struct trace_probe
*find_probe_event(const char *event
)
316 struct trace_probe
*tp
;
318 list_for_each_entry(tp
, &probe_list
, list
)
319 if (!strcmp(tp
->call
.name
, event
))
324 static void __unregister_trace_probe(struct trace_probe
*tp
)
326 if (probe_is_return(tp
))
327 unregister_kretprobe(&tp
->rp
);
329 unregister_kprobe(&tp
->kp
);
332 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
333 static void unregister_trace_probe(struct trace_probe
*tp
)
335 unregister_probe_event(tp
);
336 __unregister_trace_probe(tp
);
340 /* Register a trace_probe and probe_event */
341 static int register_trace_probe(struct trace_probe
*tp
)
343 struct trace_probe
*old_tp
;
346 mutex_lock(&probe_lock
);
348 if (probe_is_return(tp
))
349 ret
= register_kretprobe(&tp
->rp
);
351 ret
= register_kprobe(&tp
->kp
);
354 pr_warning("Could not insert probe(%d)\n", ret
);
355 if (ret
== -EILSEQ
) {
356 pr_warning("Probing address(0x%p) is not an "
357 "instruction boundary.\n",
363 /* register as an event */
364 old_tp
= find_probe_event(tp
->call
.name
);
366 /* delete old event */
367 unregister_trace_probe(old_tp
);
368 free_trace_probe(old_tp
);
370 ret
= register_probe_event(tp
);
372 pr_warning("Faild to register probe event(%d)\n", ret
);
373 __unregister_trace_probe(tp
);
375 list_add_tail(&tp
->list
, &probe_list
);
377 mutex_unlock(&probe_lock
);
381 /* Split symbol and offset. */
382 static int split_symbol_offset(char *symbol
, long *offset
)
390 tmp
= strchr(symbol
, '+');
392 tmp
= strchr(symbol
, '-');
395 /* skip sign because strict_strtol doesn't accept '+' */
396 ret
= strict_strtol(tmp
+ 1, 0, offset
);
400 *offset
= -(*offset
);
407 #define PARAM_MAX_ARGS 16
408 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
410 static int parse_trace_arg(char *arg
, struct fetch_func
*ff
, int is_return
)
418 case 'a': /* argument */
419 ret
= strict_strtoul(arg
+ 1, 10, ¶m
);
420 if (ret
|| param
> PARAM_MAX_ARGS
)
423 ff
->func
= fetch_argument
;
424 ff
->data
= (void *)param
;
427 case 'r': /* retval or retaddr */
428 if (is_return
&& arg
[1] == 'v') {
429 ff
->func
= fetch_retvalue
;
431 } else if (is_return
&& arg
[1] == 'a') {
437 case '%': /* named register */
438 ret
= regs_query_register_offset(arg
+ 1);
440 ff
->func
= fetch_register
;
441 ff
->data
= (void *)(unsigned long)ret
;
445 case 's': /* stack */
447 ff
->func
= fetch_stack_address
;
450 ret
= strict_strtoul(arg
+ 1, 10, ¶m
);
451 if (ret
|| param
> PARAM_MAX_STACK
)
454 ff
->func
= fetch_stack
;
455 ff
->data
= (void *)param
;
459 case '@': /* memory or symbol */
460 if (isdigit(arg
[1])) {
461 ret
= strict_strtoul(arg
+ 1, 0, ¶m
);
464 ff
->func
= fetch_memory
;
465 ff
->data
= (void *)param
;
467 ret
= split_symbol_offset(arg
+ 1, &offset
);
470 ff
->data
= alloc_symbol_cache(arg
+ 1,
473 ff
->func
= fetch_symbol
;
478 case '+': /* indirect memory */
480 tmp
= strchr(arg
, '(');
486 ret
= strict_strtol(arg
+ 1, 0, &offset
);
492 tmp
= strrchr(arg
, ')');
494 struct indirect_fetch_data
*id
;
496 id
= kzalloc(sizeof(struct indirect_fetch_data
),
501 ret
= parse_trace_arg(arg
, &id
->orig
, is_return
);
505 ff
->func
= fetch_indirect
;
506 ff
->data
= (void *)id
;
512 /* TODO: support custom handler */
518 static int create_trace_probe(int argc
, char **argv
)
522 * - Add kprobe: p[:EVENT] SYMBOL[+OFFS|-OFFS]|ADDRESS [FETCHARGS]
523 * - Add kretprobe: r[:EVENT] SYMBOL[+0] [FETCHARGS]
525 * aN : fetch Nth of function argument. (N:0-)
526 * rv : fetch return value
527 * ra : fetch return address
528 * sa : fetch stack address
529 * sN : fetch Nth of stack (N:0-)
530 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
531 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
532 * %REG : fetch register REG
533 * Indirect memory fetch:
534 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
536 struct trace_probe
*tp
;
540 char *symbol
= NULL
, *event
= NULL
;
547 if (argv
[0][0] == 'p')
549 else if (argv
[0][0] == 'r')
554 if (argv
[0][1] == ':') {
556 if (strlen(event
) == 0) {
557 pr_info("Event name is not specifiled\n");
562 if (isdigit(argv
[1][0])) {
565 /* an address specified */
566 ret
= strict_strtoul(&argv
[0][2], 0, (unsigned long *)&addr
);
570 /* a symbol specified */
572 /* TODO: support .init module functions */
573 ret
= split_symbol_offset(symbol
, &offset
);
576 if (offset
&& is_return
)
579 argc
-= 2; argv
+= 2;
583 /* Make a new event name */
584 char buf
[MAX_EVENT_NAME_LEN
];
586 snprintf(buf
, MAX_EVENT_NAME_LEN
, "%c@%s%+ld",
587 is_return
? 'r' : 'p', symbol
, offset
);
589 snprintf(buf
, MAX_EVENT_NAME_LEN
, "%c@0x%p",
590 is_return
? 'r' : 'p', addr
);
591 tp
= alloc_trace_probe(symbol
, buf
, argc
);
593 tp
= alloc_trace_probe(symbol
, event
, argc
);
599 tp
->rp
.handler
= kretprobe_trace_func
;
602 tp
->kp
.pre_handler
= kprobe_trace_func
;
606 kp
->symbol_name
= tp
->symbol
;
611 /* parse arguments */
613 for (i
= 0; i
< argc
&& i
< MAX_TRACE_ARGS
; i
++) {
614 if (strlen(argv
[i
]) > MAX_ARGSTR_LEN
) {
615 pr_info("Argument%d(%s) is too long.\n", i
, argv
[i
]);
619 ret
= parse_trace_arg(argv
[i
], &tp
->args
[i
], is_return
);
625 ret
= register_trace_probe(tp
);
631 free_trace_probe(tp
);
635 static void cleanup_all_probes(void)
637 struct trace_probe
*tp
;
639 mutex_lock(&probe_lock
);
640 /* TODO: Use batch unregistration */
641 while (!list_empty(&probe_list
)) {
642 tp
= list_entry(probe_list
.next
, struct trace_probe
, list
);
643 unregister_trace_probe(tp
);
644 free_trace_probe(tp
);
646 mutex_unlock(&probe_lock
);
650 /* Probes listing interfaces */
651 static void *probes_seq_start(struct seq_file
*m
, loff_t
*pos
)
653 mutex_lock(&probe_lock
);
654 return seq_list_start(&probe_list
, *pos
);
657 static void *probes_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
659 return seq_list_next(v
, &probe_list
, pos
);
662 static void probes_seq_stop(struct seq_file
*m
, void *v
)
664 mutex_unlock(&probe_lock
);
667 static int probes_seq_show(struct seq_file
*m
, void *v
)
669 struct trace_probe
*tp
= v
;
671 char buf
[MAX_ARGSTR_LEN
+ 1];
673 seq_printf(m
, "%c", probe_is_return(tp
) ? 'r' : 'p');
674 seq_printf(m
, ":%s", tp
->call
.name
);
677 seq_printf(m
, " %s%+ld", probe_symbol(tp
), probe_offset(tp
));
679 seq_printf(m
, " 0x%p", probe_address(tp
));
681 for (i
= 0; i
< tp
->nr_args
; i
++) {
682 ret
= trace_arg_string(buf
, MAX_ARGSTR_LEN
, &tp
->args
[i
]);
684 pr_warning("Argument%d decoding error(%d).\n", i
, ret
);
687 seq_printf(m
, " %s", buf
);
693 static const struct seq_operations probes_seq_op
= {
694 .start
= probes_seq_start
,
695 .next
= probes_seq_next
,
696 .stop
= probes_seq_stop
,
697 .show
= probes_seq_show
700 static int probes_open(struct inode
*inode
, struct file
*file
)
702 if ((file
->f_mode
& FMODE_WRITE
) &&
703 (file
->f_flags
& O_TRUNC
))
704 cleanup_all_probes();
706 return seq_open(file
, &probes_seq_op
);
709 static int command_trace_probe(const char *buf
)
712 int argc
= 0, ret
= 0;
714 argv
= argv_split(GFP_KERNEL
, buf
, &argc
);
719 ret
= create_trace_probe(argc
, argv
);
725 #define WRITE_BUFSIZE 128
727 static ssize_t
probes_write(struct file
*file
, const char __user
*buffer
,
728 size_t count
, loff_t
*ppos
)
735 kbuf
= kmalloc(WRITE_BUFSIZE
, GFP_KERNEL
);
740 while (done
< count
) {
742 if (size
>= WRITE_BUFSIZE
)
743 size
= WRITE_BUFSIZE
- 1;
744 if (copy_from_user(kbuf
, buffer
+ done
, size
)) {
749 tmp
= strchr(kbuf
, '\n');
752 size
= tmp
- kbuf
+ 1;
753 } else if (done
+ size
< count
) {
754 pr_warning("Line length is too long: "
755 "Should be less than %d.", WRITE_BUFSIZE
);
760 /* Remove comments */
761 tmp
= strchr(kbuf
, '#');
765 ret
= command_trace_probe(kbuf
);
775 static const struct file_operations kprobe_events_ops
= {
776 .owner
= THIS_MODULE
,
780 .release
= seq_release
,
781 .write
= probes_write
,
785 static __kprobes
int kprobe_trace_func(struct kprobe
*kp
, struct pt_regs
*regs
)
787 struct trace_probe
*tp
= container_of(kp
, struct trace_probe
, kp
);
788 struct kprobe_trace_entry
*entry
;
789 struct ring_buffer_event
*event
;
791 unsigned long irq_flags
;
792 struct ftrace_event_call
*call
= &tp
->call
;
794 local_save_flags(irq_flags
);
795 pc
= preempt_count();
797 size
= SIZEOF_KPROBE_TRACE_ENTRY(tp
->nr_args
);
799 event
= trace_current_buffer_lock_reserve(call
->id
, size
,
804 entry
= ring_buffer_event_data(event
);
805 entry
->nargs
= tp
->nr_args
;
806 entry
->ip
= (unsigned long)kp
->addr
;
807 for (i
= 0; i
< tp
->nr_args
; i
++)
808 entry
->args
[i
] = call_fetch(&tp
->args
[i
], regs
);
810 if (!filter_current_check_discard(call
, entry
, event
))
811 trace_nowake_buffer_unlock_commit(event
, irq_flags
, pc
);
815 /* Kretprobe handler */
816 static __kprobes
int kretprobe_trace_func(struct kretprobe_instance
*ri
,
817 struct pt_regs
*regs
)
819 struct trace_probe
*tp
= container_of(ri
->rp
, struct trace_probe
, rp
);
820 struct kretprobe_trace_entry
*entry
;
821 struct ring_buffer_event
*event
;
823 unsigned long irq_flags
;
824 struct ftrace_event_call
*call
= &tp
->call
;
826 local_save_flags(irq_flags
);
827 pc
= preempt_count();
829 size
= SIZEOF_KRETPROBE_TRACE_ENTRY(tp
->nr_args
);
831 event
= trace_current_buffer_lock_reserve(call
->id
, size
,
836 entry
= ring_buffer_event_data(event
);
837 entry
->nargs
= tp
->nr_args
;
838 entry
->func
= (unsigned long)probe_address(tp
);
839 entry
->ret_ip
= (unsigned long)ri
->ret_addr
;
840 for (i
= 0; i
< tp
->nr_args
; i
++)
841 entry
->args
[i
] = call_fetch(&tp
->args
[i
], regs
);
843 if (!filter_current_check_discard(call
, entry
, event
))
844 trace_nowake_buffer_unlock_commit(event
, irq_flags
, pc
);
849 /* Event entry printers */
851 print_kprobe_event(struct trace_iterator
*iter
, int flags
)
853 struct kprobe_trace_entry
*field
;
854 struct trace_seq
*s
= &iter
->seq
;
857 field
= (struct kprobe_trace_entry
*)iter
->ent
;
859 if (!seq_print_ip_sym(s
, field
->ip
, flags
| TRACE_ITER_SYM_OFFSET
))
862 if (!trace_seq_puts(s
, ":"))
865 for (i
= 0; i
< field
->nargs
; i
++)
866 if (!trace_seq_printf(s
, " 0x%lx", field
->args
[i
]))
869 if (!trace_seq_puts(s
, "\n"))
872 return TRACE_TYPE_HANDLED
;
874 return TRACE_TYPE_PARTIAL_LINE
;
878 print_kretprobe_event(struct trace_iterator
*iter
, int flags
)
880 struct kretprobe_trace_entry
*field
;
881 struct trace_seq
*s
= &iter
->seq
;
884 field
= (struct kretprobe_trace_entry
*)iter
->ent
;
886 if (!seq_print_ip_sym(s
, field
->ret_ip
, flags
| TRACE_ITER_SYM_OFFSET
))
889 if (!trace_seq_puts(s
, " <- "))
892 if (!seq_print_ip_sym(s
, field
->func
, flags
& ~TRACE_ITER_SYM_OFFSET
))
895 if (!trace_seq_puts(s
, ":"))
898 for (i
= 0; i
< field
->nargs
; i
++)
899 if (!trace_seq_printf(s
, " 0x%lx", field
->args
[i
]))
902 if (!trace_seq_puts(s
, "\n"))
905 return TRACE_TYPE_HANDLED
;
907 return TRACE_TYPE_PARTIAL_LINE
;
910 static int probe_event_enable(struct ftrace_event_call
*call
)
912 struct trace_probe
*tp
= (struct trace_probe
*)call
->data
;
914 if (probe_is_return(tp
))
915 return enable_kretprobe(&tp
->rp
);
917 return enable_kprobe(&tp
->kp
);
920 static void probe_event_disable(struct ftrace_event_call
*call
)
922 struct trace_probe
*tp
= (struct trace_probe
*)call
->data
;
924 if (probe_is_return(tp
))
925 disable_kretprobe(&tp
->rp
);
927 disable_kprobe(&tp
->kp
);
930 static int probe_event_raw_init(struct ftrace_event_call
*event_call
)
932 INIT_LIST_HEAD(&event_call
->fields
);
933 init_preds(event_call
);
938 #define DEFINE_FIELD(type, item, name, is_signed) \
940 ret = trace_define_field(event_call, #type, name, \
941 offsetof(typeof(field), item), \
942 sizeof(field.item), is_signed, \
948 static int kprobe_event_define_fields(struct ftrace_event_call
*event_call
)
951 struct kprobe_trace_entry field
;
952 char buf
[MAX_ARGSTR_LEN
+ 1];
953 struct trace_probe
*tp
= (struct trace_probe
*)event_call
->data
;
955 ret
= trace_define_common_fields(event_call
);
959 DEFINE_FIELD(unsigned long, ip
, "ip", 0);
960 DEFINE_FIELD(int, nargs
, "nargs", 1);
961 for (i
= 0; i
< tp
->nr_args
; i
++) {
962 /* Set argN as a field */
963 sprintf(buf
, "arg%d", i
);
964 DEFINE_FIELD(unsigned long, args
[i
], buf
, 0);
965 /* Set argument string as an alias field */
966 ret
= trace_arg_string(buf
, MAX_ARGSTR_LEN
, &tp
->args
[i
]);
969 DEFINE_FIELD(unsigned long, args
[i
], buf
, 0);
974 static int kretprobe_event_define_fields(struct ftrace_event_call
*event_call
)
977 struct kretprobe_trace_entry field
;
978 char buf
[MAX_ARGSTR_LEN
+ 1];
979 struct trace_probe
*tp
= (struct trace_probe
*)event_call
->data
;
981 ret
= trace_define_common_fields(event_call
);
985 DEFINE_FIELD(unsigned long, func
, "func", 0);
986 DEFINE_FIELD(unsigned long, ret_ip
, "ret_ip", 0);
987 DEFINE_FIELD(int, nargs
, "nargs", 1);
988 for (i
= 0; i
< tp
->nr_args
; i
++) {
989 /* Set argN as a field */
990 sprintf(buf
, "arg%d", i
);
991 DEFINE_FIELD(unsigned long, args
[i
], buf
, 0);
992 /* Set argument string as an alias field */
993 ret
= trace_arg_string(buf
, MAX_ARGSTR_LEN
, &tp
->args
[i
]);
996 DEFINE_FIELD(unsigned long, args
[i
], buf
, 0);
1001 static int __probe_event_show_format(struct trace_seq
*s
,
1002 struct trace_probe
*tp
, const char *fmt
,
1006 char buf
[MAX_ARGSTR_LEN
+ 1];
1009 for (i
= 0; i
< tp
->nr_args
; i
++) {
1010 ret
= trace_arg_string(buf
, MAX_ARGSTR_LEN
, &tp
->args
[i
]);
1013 if (!trace_seq_printf(s
, "\talias: %s;\toriginal: arg%d;\n",
1018 if (!trace_seq_printf(s
, "\nprint fmt: \"%s", fmt
))
1021 for (i
= 0; i
< tp
->nr_args
; i
++)
1022 if (!trace_seq_puts(s
, " 0x%lx"))
1025 if (!trace_seq_printf(s
, "\", %s", arg
))
1028 for (i
= 0; i
< tp
->nr_args
; i
++)
1029 if (!trace_seq_printf(s
, ", arg%d", i
))
1032 return trace_seq_puts(s
, "\n");
1036 #define SHOW_FIELD(type, item, name) \
1038 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
1039 "offset:%u;tsize:%u;\n", name, \
1040 (unsigned int)offsetof(typeof(field), item),\
1041 (unsigned int)sizeof(type)); \
1046 static int kprobe_event_show_format(struct ftrace_event_call
*call
,
1047 struct trace_seq
*s
)
1049 struct kprobe_trace_entry field
__attribute__((unused
));
1052 struct trace_probe
*tp
= (struct trace_probe
*)call
->data
;
1054 SHOW_FIELD(unsigned long, ip
, "ip");
1055 SHOW_FIELD(int, nargs
, "nargs");
1058 for (i
= 0; i
< tp
->nr_args
; i
++) {
1059 sprintf(buf
, "arg%d", i
);
1060 SHOW_FIELD(unsigned long, args
[i
], buf
);
1062 trace_seq_puts(s
, "\n");
1064 return __probe_event_show_format(s
, tp
, "%lx:", "ip");
1067 static int kretprobe_event_show_format(struct ftrace_event_call
*call
,
1068 struct trace_seq
*s
)
1070 struct kretprobe_trace_entry field
__attribute__((unused
));
1073 struct trace_probe
*tp
= (struct trace_probe
*)call
->data
;
1075 SHOW_FIELD(unsigned long, func
, "func");
1076 SHOW_FIELD(unsigned long, ret_ip
, "ret_ip");
1077 SHOW_FIELD(int, nargs
, "nargs");
1080 for (i
= 0; i
< tp
->nr_args
; i
++) {
1081 sprintf(buf
, "arg%d", i
);
1082 SHOW_FIELD(unsigned long, args
[i
], buf
);
1084 trace_seq_puts(s
, "\n");
1086 return __probe_event_show_format(s
, tp
, "%lx <- %lx:",
1090 static int register_probe_event(struct trace_probe
*tp
)
1092 struct ftrace_event_call
*call
= &tp
->call
;
1095 /* Initialize ftrace_event_call */
1096 call
->system
= "kprobes";
1097 if (probe_is_return(tp
)) {
1098 tp
->event
.trace
= print_kretprobe_event
;
1099 call
->raw_init
= probe_event_raw_init
;
1100 call
->show_format
= kretprobe_event_show_format
;
1101 call
->define_fields
= kretprobe_event_define_fields
;
1103 tp
->event
.trace
= print_kprobe_event
;
1104 call
->raw_init
= probe_event_raw_init
;
1105 call
->show_format
= kprobe_event_show_format
;
1106 call
->define_fields
= kprobe_event_define_fields
;
1108 call
->event
= &tp
->event
;
1109 call
->id
= register_ftrace_event(&tp
->event
);
1113 call
->regfunc
= probe_event_enable
;
1114 call
->unregfunc
= probe_event_disable
;
1116 ret
= trace_add_event_call(call
);
1118 pr_info("Failed to register kprobe event: %s\n", call
->name
);
1119 unregister_ftrace_event(&tp
->event
);
1124 static void unregister_probe_event(struct trace_probe
*tp
)
1126 /* tp->event is unregistered in trace_remove_event_call() */
1127 trace_remove_event_call(&tp
->call
);
1130 /* Make a debugfs interface for controling probe points */
1131 static __init
int init_kprobe_trace(void)
1133 struct dentry
*d_tracer
;
1134 struct dentry
*entry
;
1136 d_tracer
= tracing_init_dentry();
1140 entry
= debugfs_create_file("kprobe_events", 0644, d_tracer
,
1141 NULL
, &kprobe_events_ops
);
1144 pr_warning("Could not create debugfs "
1145 "'kprobe_events' entry\n");
1148 fs_initcall(init_kprobe_trace
);
1151 #ifdef CONFIG_FTRACE_STARTUP_TEST
1153 static int kprobe_trace_selftest_target(int a1
, int a2
, int a3
,
1154 int a4
, int a5
, int a6
)
1156 return a1
+ a2
+ a3
+ a4
+ a5
+ a6
;
1159 static __init
int kprobe_trace_self_tests_init(void)
1162 int (*target
)(int, int, int, int, int, int);
1164 target
= kprobe_trace_selftest_target
;
1166 pr_info("Testing kprobe tracing: ");
1168 ret
= command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1169 "a1 a2 a3 a4 a5 a6");
1170 if (WARN_ON_ONCE(ret
))
1171 pr_warning("error enabling function entry\n");
1173 ret
= command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1175 if (WARN_ON_ONCE(ret
))
1176 pr_warning("error enabling function return\n");
1178 ret
= target(1, 2, 3, 4, 5, 6);
1180 cleanup_all_probes();
1186 late_initcall(kprobe_trace_self_tests_init
);