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
19 #define pr_fmt(fmt) "trace_kprobe: " fmt
21 #include <linux/module.h>
22 #include <linux/uaccess.h>
24 #include "trace_probe.h"
26 #define KPROBE_EVENT_SYSTEM "kprobes"
29 * Kprobe event core functions
32 struct list_head list
;
33 struct kretprobe rp
; /* Use rp.kp for kprobe use */
34 unsigned long __percpu
*nhit
;
35 const char *symbol
; /* symbol name */
36 struct trace_probe tp
;
39 #define SIZEOF_TRACE_KPROBE(n) \
40 (offsetof(struct trace_kprobe, tp.args) + \
41 (sizeof(struct probe_arg) * (n)))
44 static nokprobe_inline
bool trace_kprobe_is_return(struct trace_kprobe
*tk
)
46 return tk
->rp
.handler
!= NULL
;
49 static nokprobe_inline
const char *trace_kprobe_symbol(struct trace_kprobe
*tk
)
51 return tk
->symbol
? tk
->symbol
: "unknown";
54 static nokprobe_inline
unsigned long trace_kprobe_offset(struct trace_kprobe
*tk
)
56 return tk
->rp
.kp
.offset
;
59 static nokprobe_inline
bool trace_kprobe_has_gone(struct trace_kprobe
*tk
)
61 return !!(kprobe_gone(&tk
->rp
.kp
));
64 static nokprobe_inline
bool trace_kprobe_within_module(struct trace_kprobe
*tk
,
67 int len
= strlen(mod
->name
);
68 const char *name
= trace_kprobe_symbol(tk
);
69 return strncmp(mod
->name
, name
, len
) == 0 && name
[len
] == ':';
72 static nokprobe_inline
bool trace_kprobe_is_on_module(struct trace_kprobe
*tk
)
74 return !!strchr(trace_kprobe_symbol(tk
), ':');
77 static nokprobe_inline
unsigned long trace_kprobe_nhit(struct trace_kprobe
*tk
)
79 unsigned long nhit
= 0;
82 for_each_possible_cpu(cpu
)
83 nhit
+= *per_cpu_ptr(tk
->nhit
, cpu
);
88 static int register_kprobe_event(struct trace_kprobe
*tk
);
89 static int unregister_kprobe_event(struct trace_kprobe
*tk
);
91 static DEFINE_MUTEX(probe_lock
);
92 static LIST_HEAD(probe_list
);
94 static int kprobe_dispatcher(struct kprobe
*kp
, struct pt_regs
*regs
);
95 static int kretprobe_dispatcher(struct kretprobe_instance
*ri
,
96 struct pt_regs
*regs
);
98 /* Memory fetching by symbol */
105 unsigned long update_symbol_cache(struct symbol_cache
*sc
)
107 sc
->addr
= (unsigned long)kallsyms_lookup_name(sc
->symbol
);
110 sc
->addr
+= sc
->offset
;
115 void free_symbol_cache(struct symbol_cache
*sc
)
121 struct symbol_cache
*alloc_symbol_cache(const char *sym
, long offset
)
123 struct symbol_cache
*sc
;
125 if (!sym
|| strlen(sym
) == 0)
128 sc
= kzalloc(sizeof(struct symbol_cache
), GFP_KERNEL
);
132 sc
->symbol
= kstrdup(sym
, GFP_KERNEL
);
138 update_symbol_cache(sc
);
144 * Kprobes-specific fetch functions
146 #define DEFINE_FETCH_stack(type) \
147 static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
148 void *offset, void *dest) \
150 *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
151 (unsigned int)((unsigned long)offset)); \
153 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(stack, type));
155 DEFINE_BASIC_FETCH_FUNCS(stack
)
156 /* No string on the stack entry */
157 #define fetch_stack_string NULL
158 #define fetch_stack_string_size NULL
160 #define DEFINE_FETCH_memory(type) \
161 static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
162 void *addr, void *dest) \
165 if (probe_kernel_address(addr, retval)) \
168 *(type *)dest = retval; \
170 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, type));
172 DEFINE_BASIC_FETCH_FUNCS(memory
)
174 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
175 * length and relative data location.
177 static void FETCH_FUNC_NAME(memory
, string
)(struct pt_regs
*regs
,
178 void *addr
, void *dest
)
180 int maxlen
= get_rloc_len(*(u32
*)dest
);
181 u8
*dst
= get_rloc_data(dest
);
188 * Try to get string again, since the string can be changed while
191 ret
= strncpy_from_unsafe(dst
, addr
, maxlen
);
193 if (ret
< 0) { /* Failed to fetch string */
195 *(u32
*)dest
= make_data_rloc(0, get_rloc_offs(*(u32
*)dest
));
197 *(u32
*)dest
= make_data_rloc(ret
, get_rloc_offs(*(u32
*)dest
));
200 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory
, string
));
202 /* Return the length of string -- including null terminal byte */
203 static void FETCH_FUNC_NAME(memory
, string_size
)(struct pt_regs
*regs
,
204 void *addr
, void *dest
)
215 ret
= __copy_from_user_inatomic(&c
, (u8
*)addr
+ len
, 1);
217 } while (c
&& ret
== 0 && len
< MAX_STRING_SIZE
);
222 if (ret
< 0) /* Failed to check the length */
227 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory
, string_size
));
229 #define DEFINE_FETCH_symbol(type) \
230 void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, void *data, void *dest)\
232 struct symbol_cache *sc = data; \
234 fetch_memory_##type(regs, (void *)sc->addr, dest); \
238 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(symbol, type));
240 DEFINE_BASIC_FETCH_FUNCS(symbol
)
241 DEFINE_FETCH_symbol(string
)
242 DEFINE_FETCH_symbol(string_size
)
244 /* kprobes don't support file_offset fetch methods */
245 #define fetch_file_offset_u8 NULL
246 #define fetch_file_offset_u16 NULL
247 #define fetch_file_offset_u32 NULL
248 #define fetch_file_offset_u64 NULL
249 #define fetch_file_offset_string NULL
250 #define fetch_file_offset_string_size NULL
252 /* Fetch type information table */
253 static const struct fetch_type kprobes_fetch_type_table
[] = {
255 [FETCH_TYPE_STRING
] = __ASSIGN_FETCH_TYPE("string", string
, string
,
256 sizeof(u32
), 1, "__data_loc char[]"),
257 [FETCH_TYPE_STRSIZE
] = __ASSIGN_FETCH_TYPE("string_size", u32
,
258 string_size
, sizeof(u32
), 0, "u32"),
260 ASSIGN_FETCH_TYPE(u8
, u8
, 0),
261 ASSIGN_FETCH_TYPE(u16
, u16
, 0),
262 ASSIGN_FETCH_TYPE(u32
, u32
, 0),
263 ASSIGN_FETCH_TYPE(u64
, u64
, 0),
264 ASSIGN_FETCH_TYPE(s8
, u8
, 1),
265 ASSIGN_FETCH_TYPE(s16
, u16
, 1),
266 ASSIGN_FETCH_TYPE(s32
, u32
, 1),
267 ASSIGN_FETCH_TYPE(s64
, u64
, 1),
268 ASSIGN_FETCH_TYPE_ALIAS(x8
, u8
, u8
, 0),
269 ASSIGN_FETCH_TYPE_ALIAS(x16
, u16
, u16
, 0),
270 ASSIGN_FETCH_TYPE_ALIAS(x32
, u32
, u32
, 0),
271 ASSIGN_FETCH_TYPE_ALIAS(x64
, u64
, u64
, 0),
273 ASSIGN_FETCH_TYPE_END
277 * Allocate new trace_probe and initialize it (including kprobes).
279 static struct trace_kprobe
*alloc_trace_kprobe(const char *group
,
284 int nargs
, bool is_return
)
286 struct trace_kprobe
*tk
;
289 tk
= kzalloc(SIZEOF_TRACE_KPROBE(nargs
), GFP_KERNEL
);
293 tk
->nhit
= alloc_percpu(unsigned long);
298 tk
->symbol
= kstrdup(symbol
, GFP_KERNEL
);
301 tk
->rp
.kp
.symbol_name
= tk
->symbol
;
302 tk
->rp
.kp
.offset
= offs
;
304 tk
->rp
.kp
.addr
= addr
;
307 tk
->rp
.handler
= kretprobe_dispatcher
;
309 tk
->rp
.kp
.pre_handler
= kprobe_dispatcher
;
311 if (!event
|| !is_good_name(event
)) {
316 tk
->tp
.call
.class = &tk
->tp
.class;
317 tk
->tp
.call
.name
= kstrdup(event
, GFP_KERNEL
);
318 if (!tk
->tp
.call
.name
)
321 if (!group
|| !is_good_name(group
)) {
326 tk
->tp
.class.system
= kstrdup(group
, GFP_KERNEL
);
327 if (!tk
->tp
.class.system
)
330 INIT_LIST_HEAD(&tk
->list
);
331 INIT_LIST_HEAD(&tk
->tp
.files
);
334 kfree(tk
->tp
.call
.name
);
336 free_percpu(tk
->nhit
);
341 static void free_trace_kprobe(struct trace_kprobe
*tk
)
345 for (i
= 0; i
< tk
->tp
.nr_args
; i
++)
346 traceprobe_free_probe_arg(&tk
->tp
.args
[i
]);
348 kfree(tk
->tp
.call
.class->system
);
349 kfree(tk
->tp
.call
.name
);
351 free_percpu(tk
->nhit
);
355 static struct trace_kprobe
*find_trace_kprobe(const char *event
,
358 struct trace_kprobe
*tk
;
360 list_for_each_entry(tk
, &probe_list
, list
)
361 if (strcmp(trace_event_name(&tk
->tp
.call
), event
) == 0 &&
362 strcmp(tk
->tp
.call
.class->system
, group
) == 0)
369 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
372 enable_trace_kprobe(struct trace_kprobe
*tk
, struct trace_event_file
*file
)
377 struct event_file_link
*link
;
379 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
386 list_add_tail_rcu(&link
->list
, &tk
->tp
.files
);
388 tk
->tp
.flags
|= TP_FLAG_TRACE
;
390 tk
->tp
.flags
|= TP_FLAG_PROFILE
;
392 if (trace_probe_is_registered(&tk
->tp
) && !trace_kprobe_has_gone(tk
)) {
393 if (trace_kprobe_is_return(tk
))
394 ret
= enable_kretprobe(&tk
->rp
);
396 ret
= enable_kprobe(&tk
->rp
.kp
);
403 * Disable trace_probe
404 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
407 disable_trace_kprobe(struct trace_kprobe
*tk
, struct trace_event_file
*file
)
409 struct event_file_link
*link
= NULL
;
414 link
= find_event_file_link(&tk
->tp
, file
);
420 list_del_rcu(&link
->list
);
422 if (!list_empty(&tk
->tp
.files
))
425 tk
->tp
.flags
&= ~TP_FLAG_TRACE
;
427 tk
->tp
.flags
&= ~TP_FLAG_PROFILE
;
429 if (!trace_probe_is_enabled(&tk
->tp
) && trace_probe_is_registered(&tk
->tp
)) {
430 if (trace_kprobe_is_return(tk
))
431 disable_kretprobe(&tk
->rp
);
433 disable_kprobe(&tk
->rp
.kp
);
439 * Synchronize with kprobe_trace_func/kretprobe_trace_func
440 * to ensure disabled (all running handlers are finished).
441 * This is not only for kfree(), but also the caller,
442 * trace_remove_event_call() supposes it for releasing
443 * event_call related objects, which will be accessed in
444 * the kprobe_trace_func/kretprobe_trace_func.
447 kfree(link
); /* Ignored if link == NULL */
453 /* Internal register function - just handle k*probes and flags */
454 static int __register_trace_kprobe(struct trace_kprobe
*tk
)
458 if (trace_probe_is_registered(&tk
->tp
))
461 for (i
= 0; i
< tk
->tp
.nr_args
; i
++)
462 traceprobe_update_arg(&tk
->tp
.args
[i
]);
464 /* Set/clear disabled flag according to tp->flag */
465 if (trace_probe_is_enabled(&tk
->tp
))
466 tk
->rp
.kp
.flags
&= ~KPROBE_FLAG_DISABLED
;
468 tk
->rp
.kp
.flags
|= KPROBE_FLAG_DISABLED
;
470 if (trace_kprobe_is_return(tk
))
471 ret
= register_kretprobe(&tk
->rp
);
473 ret
= register_kprobe(&tk
->rp
.kp
);
476 tk
->tp
.flags
|= TP_FLAG_REGISTERED
;
478 pr_warn("Could not insert probe at %s+%lu: %d\n",
479 trace_kprobe_symbol(tk
), trace_kprobe_offset(tk
), ret
);
480 if (ret
== -ENOENT
&& trace_kprobe_is_on_module(tk
)) {
481 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
483 } else if (ret
== -EILSEQ
) {
484 pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
493 /* Internal unregister function - just handle k*probes and flags */
494 static void __unregister_trace_kprobe(struct trace_kprobe
*tk
)
496 if (trace_probe_is_registered(&tk
->tp
)) {
497 if (trace_kprobe_is_return(tk
))
498 unregister_kretprobe(&tk
->rp
);
500 unregister_kprobe(&tk
->rp
.kp
);
501 tk
->tp
.flags
&= ~TP_FLAG_REGISTERED
;
502 /* Cleanup kprobe for reuse */
503 if (tk
->rp
.kp
.symbol_name
)
504 tk
->rp
.kp
.addr
= NULL
;
508 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
509 static int unregister_trace_kprobe(struct trace_kprobe
*tk
)
511 /* Enabled event can not be unregistered */
512 if (trace_probe_is_enabled(&tk
->tp
))
515 /* Will fail if probe is being used by ftrace or perf */
516 if (unregister_kprobe_event(tk
))
519 __unregister_trace_kprobe(tk
);
525 /* Register a trace_probe and probe_event */
526 static int register_trace_kprobe(struct trace_kprobe
*tk
)
528 struct trace_kprobe
*old_tk
;
531 mutex_lock(&probe_lock
);
533 /* Delete old (same name) event if exist */
534 old_tk
= find_trace_kprobe(trace_event_name(&tk
->tp
.call
),
535 tk
->tp
.call
.class->system
);
537 ret
= unregister_trace_kprobe(old_tk
);
540 free_trace_kprobe(old_tk
);
543 /* Register new event */
544 ret
= register_kprobe_event(tk
);
546 pr_warn("Failed to register probe event(%d)\n", ret
);
550 /* Register k*probe */
551 ret
= __register_trace_kprobe(tk
);
553 unregister_kprobe_event(tk
);
555 list_add_tail(&tk
->list
, &probe_list
);
558 mutex_unlock(&probe_lock
);
562 /* Module notifier call back, checking event on the module */
563 static int trace_kprobe_module_callback(struct notifier_block
*nb
,
564 unsigned long val
, void *data
)
566 struct module
*mod
= data
;
567 struct trace_kprobe
*tk
;
570 if (val
!= MODULE_STATE_COMING
)
573 /* Update probes on coming module */
574 mutex_lock(&probe_lock
);
575 list_for_each_entry(tk
, &probe_list
, list
) {
576 if (trace_kprobe_within_module(tk
, mod
)) {
577 /* Don't need to check busy - this should have gone. */
578 __unregister_trace_kprobe(tk
);
579 ret
= __register_trace_kprobe(tk
);
581 pr_warn("Failed to re-register probe %s on %s: %d\n",
582 trace_event_name(&tk
->tp
.call
),
586 mutex_unlock(&probe_lock
);
591 static struct notifier_block trace_kprobe_module_nb
= {
592 .notifier_call
= trace_kprobe_module_callback
,
593 .priority
= 1 /* Invoked after kprobe module callback */
596 static int create_trace_kprobe(int argc
, char **argv
)
600 * - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
601 * - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
603 * $retval : fetch return value
604 * $stack : fetch stack address
605 * $stackN : fetch Nth of stack (N:0-)
606 * $comm : fetch current task comm
607 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
608 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
609 * %REG : fetch register REG
610 * Dereferencing memory fetch:
611 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
612 * Alias name of args:
613 * NAME=FETCHARG : set NAME as alias of FETCHARG.
615 * FETCHARG:TYPE : use TYPE instead of unsigned long.
617 struct trace_kprobe
*tk
;
619 bool is_return
= false, is_delete
= false;
620 char *symbol
= NULL
, *event
= NULL
, *group
= NULL
;
622 unsigned long offset
= 0;
624 char buf
[MAX_EVENT_NAME_LEN
];
626 /* argc must be >= 1 */
627 if (argv
[0][0] == 'p')
629 else if (argv
[0][0] == 'r')
631 else if (argv
[0][0] == '-')
634 pr_info("Probe definition must be started with 'p', 'r' or"
639 if (argv
[0][1] == ':') {
641 if (strchr(event
, '/')) {
643 event
= strchr(group
, '/') + 1;
645 if (strlen(group
) == 0) {
646 pr_info("Group name is not specified\n");
650 if (strlen(event
) == 0) {
651 pr_info("Event name is not specified\n");
656 group
= KPROBE_EVENT_SYSTEM
;
660 pr_info("Delete command needs an event name.\n");
663 mutex_lock(&probe_lock
);
664 tk
= find_trace_kprobe(event
, group
);
666 mutex_unlock(&probe_lock
);
667 pr_info("Event %s/%s doesn't exist.\n", group
, event
);
670 /* delete an event */
671 ret
= unregister_trace_kprobe(tk
);
673 free_trace_kprobe(tk
);
674 mutex_unlock(&probe_lock
);
679 pr_info("Probe point is not specified.\n");
682 if (isdigit(argv
[1][0])) {
684 pr_info("Return probe point must be a symbol.\n");
687 /* an address specified */
688 ret
= kstrtoul(&argv
[1][0], 0, (unsigned long *)&addr
);
690 pr_info("Failed to parse address.\n");
694 /* a symbol specified */
696 /* TODO: support .init module functions */
697 ret
= traceprobe_split_symbol_offset(symbol
, &offset
);
699 pr_info("Failed to parse symbol.\n");
702 if (offset
&& is_return
) {
703 pr_info("Return probe must be used without offset.\n");
707 argc
-= 2; argv
+= 2;
711 /* Make a new event name */
713 snprintf(buf
, MAX_EVENT_NAME_LEN
, "%c_%s_%ld",
714 is_return
? 'r' : 'p', symbol
, offset
);
716 snprintf(buf
, MAX_EVENT_NAME_LEN
, "%c_0x%p",
717 is_return
? 'r' : 'p', addr
);
720 tk
= alloc_trace_kprobe(group
, event
, addr
, symbol
, offset
, argc
,
723 pr_info("Failed to allocate trace_probe.(%d)\n",
728 /* parse arguments */
730 for (i
= 0; i
< argc
&& i
< MAX_TRACE_ARGS
; i
++) {
731 struct probe_arg
*parg
= &tk
->tp
.args
[i
];
733 /* Increment count for freeing args in error case */
736 /* Parse argument name */
737 arg
= strchr(argv
[i
], '=');
740 parg
->name
= kstrdup(argv
[i
], GFP_KERNEL
);
743 /* If argument name is omitted, set "argN" */
744 snprintf(buf
, MAX_EVENT_NAME_LEN
, "arg%d", i
+ 1);
745 parg
->name
= kstrdup(buf
, GFP_KERNEL
);
749 pr_info("Failed to allocate argument[%d] name.\n", i
);
754 if (!is_good_name(parg
->name
)) {
755 pr_info("Invalid argument[%d] name: %s\n",
761 if (traceprobe_conflict_field_name(parg
->name
,
763 pr_info("Argument[%d] name '%s' conflicts with "
764 "another field.\n", i
, argv
[i
]);
769 /* Parse fetch argument */
770 ret
= traceprobe_parse_probe_arg(arg
, &tk
->tp
.size
, parg
,
772 kprobes_fetch_type_table
);
774 pr_info("Parse error at argument[%d]. (%d)\n", i
, ret
);
779 ret
= register_trace_kprobe(tk
);
785 free_trace_kprobe(tk
);
789 static int release_all_trace_kprobes(void)
791 struct trace_kprobe
*tk
;
794 mutex_lock(&probe_lock
);
795 /* Ensure no probe is in use. */
796 list_for_each_entry(tk
, &probe_list
, list
)
797 if (trace_probe_is_enabled(&tk
->tp
)) {
801 /* TODO: Use batch unregistration */
802 while (!list_empty(&probe_list
)) {
803 tk
= list_entry(probe_list
.next
, struct trace_kprobe
, list
);
804 ret
= unregister_trace_kprobe(tk
);
807 free_trace_kprobe(tk
);
811 mutex_unlock(&probe_lock
);
816 /* Probes listing interfaces */
817 static void *probes_seq_start(struct seq_file
*m
, loff_t
*pos
)
819 mutex_lock(&probe_lock
);
820 return seq_list_start(&probe_list
, *pos
);
823 static void *probes_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
825 return seq_list_next(v
, &probe_list
, pos
);
828 static void probes_seq_stop(struct seq_file
*m
, void *v
)
830 mutex_unlock(&probe_lock
);
833 static int probes_seq_show(struct seq_file
*m
, void *v
)
835 struct trace_kprobe
*tk
= v
;
838 seq_putc(m
, trace_kprobe_is_return(tk
) ? 'r' : 'p');
839 seq_printf(m
, ":%s/%s", tk
->tp
.call
.class->system
,
840 trace_event_name(&tk
->tp
.call
));
843 seq_printf(m
, " 0x%p", tk
->rp
.kp
.addr
);
844 else if (tk
->rp
.kp
.offset
)
845 seq_printf(m
, " %s+%u", trace_kprobe_symbol(tk
),
848 seq_printf(m
, " %s", trace_kprobe_symbol(tk
));
850 for (i
= 0; i
< tk
->tp
.nr_args
; i
++)
851 seq_printf(m
, " %s=%s", tk
->tp
.args
[i
].name
, tk
->tp
.args
[i
].comm
);
857 static const struct seq_operations probes_seq_op
= {
858 .start
= probes_seq_start
,
859 .next
= probes_seq_next
,
860 .stop
= probes_seq_stop
,
861 .show
= probes_seq_show
864 static int probes_open(struct inode
*inode
, struct file
*file
)
868 if ((file
->f_mode
& FMODE_WRITE
) && (file
->f_flags
& O_TRUNC
)) {
869 ret
= release_all_trace_kprobes();
874 return seq_open(file
, &probes_seq_op
);
877 static ssize_t
probes_write(struct file
*file
, const char __user
*buffer
,
878 size_t count
, loff_t
*ppos
)
880 return traceprobe_probes_write(file
, buffer
, count
, ppos
,
881 create_trace_kprobe
);
884 static const struct file_operations kprobe_events_ops
= {
885 .owner
= THIS_MODULE
,
889 .release
= seq_release
,
890 .write
= probes_write
,
893 /* Probes profiling interfaces */
894 static int probes_profile_seq_show(struct seq_file
*m
, void *v
)
896 struct trace_kprobe
*tk
= v
;
898 seq_printf(m
, " %-44s %15lu %15lu\n",
899 trace_event_name(&tk
->tp
.call
),
900 trace_kprobe_nhit(tk
),
906 static const struct seq_operations profile_seq_op
= {
907 .start
= probes_seq_start
,
908 .next
= probes_seq_next
,
909 .stop
= probes_seq_stop
,
910 .show
= probes_profile_seq_show
913 static int profile_open(struct inode
*inode
, struct file
*file
)
915 return seq_open(file
, &profile_seq_op
);
918 static const struct file_operations kprobe_profile_ops
= {
919 .owner
= THIS_MODULE
,
920 .open
= profile_open
,
923 .release
= seq_release
,
927 static nokprobe_inline
void
928 __kprobe_trace_func(struct trace_kprobe
*tk
, struct pt_regs
*regs
,
929 struct trace_event_file
*trace_file
)
931 struct kprobe_trace_entry_head
*entry
;
932 struct ring_buffer_event
*event
;
933 struct ring_buffer
*buffer
;
935 unsigned long irq_flags
;
936 struct trace_event_call
*call
= &tk
->tp
.call
;
938 WARN_ON(call
!= trace_file
->event_call
);
940 if (trace_trigger_soft_disabled(trace_file
))
943 local_save_flags(irq_flags
);
944 pc
= preempt_count();
946 dsize
= __get_data_size(&tk
->tp
, regs
);
947 size
= sizeof(*entry
) + tk
->tp
.size
+ dsize
;
949 event
= trace_event_buffer_lock_reserve(&buffer
, trace_file
,
951 size
, irq_flags
, pc
);
955 entry
= ring_buffer_event_data(event
);
956 entry
->ip
= (unsigned long)tk
->rp
.kp
.addr
;
957 store_trace_args(sizeof(*entry
), &tk
->tp
, regs
, (u8
*)&entry
[1], dsize
);
959 event_trigger_unlock_commit_regs(trace_file
, buffer
, event
,
960 entry
, irq_flags
, pc
, regs
);
964 kprobe_trace_func(struct trace_kprobe
*tk
, struct pt_regs
*regs
)
966 struct event_file_link
*link
;
968 list_for_each_entry_rcu(link
, &tk
->tp
.files
, list
)
969 __kprobe_trace_func(tk
, regs
, link
->file
);
971 NOKPROBE_SYMBOL(kprobe_trace_func
);
973 /* Kretprobe handler */
974 static nokprobe_inline
void
975 __kretprobe_trace_func(struct trace_kprobe
*tk
, struct kretprobe_instance
*ri
,
976 struct pt_regs
*regs
,
977 struct trace_event_file
*trace_file
)
979 struct kretprobe_trace_entry_head
*entry
;
980 struct ring_buffer_event
*event
;
981 struct ring_buffer
*buffer
;
983 unsigned long irq_flags
;
984 struct trace_event_call
*call
= &tk
->tp
.call
;
986 WARN_ON(call
!= trace_file
->event_call
);
988 if (trace_trigger_soft_disabled(trace_file
))
991 local_save_flags(irq_flags
);
992 pc
= preempt_count();
994 dsize
= __get_data_size(&tk
->tp
, regs
);
995 size
= sizeof(*entry
) + tk
->tp
.size
+ dsize
;
997 event
= trace_event_buffer_lock_reserve(&buffer
, trace_file
,
999 size
, irq_flags
, pc
);
1003 entry
= ring_buffer_event_data(event
);
1004 entry
->func
= (unsigned long)tk
->rp
.kp
.addr
;
1005 entry
->ret_ip
= (unsigned long)ri
->ret_addr
;
1006 store_trace_args(sizeof(*entry
), &tk
->tp
, regs
, (u8
*)&entry
[1], dsize
);
1008 event_trigger_unlock_commit_regs(trace_file
, buffer
, event
,
1009 entry
, irq_flags
, pc
, regs
);
1013 kretprobe_trace_func(struct trace_kprobe
*tk
, struct kretprobe_instance
*ri
,
1014 struct pt_regs
*regs
)
1016 struct event_file_link
*link
;
1018 list_for_each_entry_rcu(link
, &tk
->tp
.files
, list
)
1019 __kretprobe_trace_func(tk
, ri
, regs
, link
->file
);
1021 NOKPROBE_SYMBOL(kretprobe_trace_func
);
1023 /* Event entry printers */
1024 static enum print_line_t
1025 print_kprobe_event(struct trace_iterator
*iter
, int flags
,
1026 struct trace_event
*event
)
1028 struct kprobe_trace_entry_head
*field
;
1029 struct trace_seq
*s
= &iter
->seq
;
1030 struct trace_probe
*tp
;
1034 field
= (struct kprobe_trace_entry_head
*)iter
->ent
;
1035 tp
= container_of(event
, struct trace_probe
, call
.event
);
1037 trace_seq_printf(s
, "%s: (", trace_event_name(&tp
->call
));
1039 if (!seq_print_ip_sym(s
, field
->ip
, flags
| TRACE_ITER_SYM_OFFSET
))
1042 trace_seq_putc(s
, ')');
1044 data
= (u8
*)&field
[1];
1045 for (i
= 0; i
< tp
->nr_args
; i
++)
1046 if (!tp
->args
[i
].type
->print(s
, tp
->args
[i
].name
,
1047 data
+ tp
->args
[i
].offset
, field
))
1050 trace_seq_putc(s
, '\n');
1052 return trace_handle_return(s
);
1055 static enum print_line_t
1056 print_kretprobe_event(struct trace_iterator
*iter
, int flags
,
1057 struct trace_event
*event
)
1059 struct kretprobe_trace_entry_head
*field
;
1060 struct trace_seq
*s
= &iter
->seq
;
1061 struct trace_probe
*tp
;
1065 field
= (struct kretprobe_trace_entry_head
*)iter
->ent
;
1066 tp
= container_of(event
, struct trace_probe
, call
.event
);
1068 trace_seq_printf(s
, "%s: (", trace_event_name(&tp
->call
));
1070 if (!seq_print_ip_sym(s
, field
->ret_ip
, flags
| TRACE_ITER_SYM_OFFSET
))
1073 trace_seq_puts(s
, " <- ");
1075 if (!seq_print_ip_sym(s
, field
->func
, flags
& ~TRACE_ITER_SYM_OFFSET
))
1078 trace_seq_putc(s
, ')');
1080 data
= (u8
*)&field
[1];
1081 for (i
= 0; i
< tp
->nr_args
; i
++)
1082 if (!tp
->args
[i
].type
->print(s
, tp
->args
[i
].name
,
1083 data
+ tp
->args
[i
].offset
, field
))
1086 trace_seq_putc(s
, '\n');
1089 return trace_handle_return(s
);
1093 static int kprobe_event_define_fields(struct trace_event_call
*event_call
)
1096 struct kprobe_trace_entry_head field
;
1097 struct trace_kprobe
*tk
= (struct trace_kprobe
*)event_call
->data
;
1099 DEFINE_FIELD(unsigned long, ip
, FIELD_STRING_IP
, 0);
1100 /* Set argument names as fields */
1101 for (i
= 0; i
< tk
->tp
.nr_args
; i
++) {
1102 struct probe_arg
*parg
= &tk
->tp
.args
[i
];
1104 ret
= trace_define_field(event_call
, parg
->type
->fmttype
,
1106 sizeof(field
) + parg
->offset
,
1108 parg
->type
->is_signed
,
1116 static int kretprobe_event_define_fields(struct trace_event_call
*event_call
)
1119 struct kretprobe_trace_entry_head field
;
1120 struct trace_kprobe
*tk
= (struct trace_kprobe
*)event_call
->data
;
1122 DEFINE_FIELD(unsigned long, func
, FIELD_STRING_FUNC
, 0);
1123 DEFINE_FIELD(unsigned long, ret_ip
, FIELD_STRING_RETIP
, 0);
1124 /* Set argument names as fields */
1125 for (i
= 0; i
< tk
->tp
.nr_args
; i
++) {
1126 struct probe_arg
*parg
= &tk
->tp
.args
[i
];
1128 ret
= trace_define_field(event_call
, parg
->type
->fmttype
,
1130 sizeof(field
) + parg
->offset
,
1132 parg
->type
->is_signed
,
1140 #ifdef CONFIG_PERF_EVENTS
1142 /* Kprobe profile handler */
1144 kprobe_perf_func(struct trace_kprobe
*tk
, struct pt_regs
*regs
)
1146 struct trace_event_call
*call
= &tk
->tp
.call
;
1147 struct bpf_prog
*prog
= call
->prog
;
1148 struct kprobe_trace_entry_head
*entry
;
1149 struct hlist_head
*head
;
1150 int size
, __size
, dsize
;
1153 if (prog
&& !trace_call_bpf(prog
, regs
))
1156 head
= this_cpu_ptr(call
->perf_events
);
1157 if (hlist_empty(head
))
1160 dsize
= __get_data_size(&tk
->tp
, regs
);
1161 __size
= sizeof(*entry
) + tk
->tp
.size
+ dsize
;
1162 size
= ALIGN(__size
+ sizeof(u32
), sizeof(u64
));
1163 size
-= sizeof(u32
);
1165 entry
= perf_trace_buf_alloc(size
, NULL
, &rctx
);
1169 entry
->ip
= (unsigned long)tk
->rp
.kp
.addr
;
1170 memset(&entry
[1], 0, dsize
);
1171 store_trace_args(sizeof(*entry
), &tk
->tp
, regs
, (u8
*)&entry
[1], dsize
);
1172 perf_trace_buf_submit(entry
, size
, rctx
, call
->event
.type
, 1, regs
,
1175 NOKPROBE_SYMBOL(kprobe_perf_func
);
1177 /* Kretprobe profile handler */
1179 kretprobe_perf_func(struct trace_kprobe
*tk
, struct kretprobe_instance
*ri
,
1180 struct pt_regs
*regs
)
1182 struct trace_event_call
*call
= &tk
->tp
.call
;
1183 struct bpf_prog
*prog
= call
->prog
;
1184 struct kretprobe_trace_entry_head
*entry
;
1185 struct hlist_head
*head
;
1186 int size
, __size
, dsize
;
1189 if (prog
&& !trace_call_bpf(prog
, regs
))
1192 head
= this_cpu_ptr(call
->perf_events
);
1193 if (hlist_empty(head
))
1196 dsize
= __get_data_size(&tk
->tp
, regs
);
1197 __size
= sizeof(*entry
) + tk
->tp
.size
+ dsize
;
1198 size
= ALIGN(__size
+ sizeof(u32
), sizeof(u64
));
1199 size
-= sizeof(u32
);
1201 entry
= perf_trace_buf_alloc(size
, NULL
, &rctx
);
1205 entry
->func
= (unsigned long)tk
->rp
.kp
.addr
;
1206 entry
->ret_ip
= (unsigned long)ri
->ret_addr
;
1207 store_trace_args(sizeof(*entry
), &tk
->tp
, regs
, (u8
*)&entry
[1], dsize
);
1208 perf_trace_buf_submit(entry
, size
, rctx
, call
->event
.type
, 1, regs
,
1211 NOKPROBE_SYMBOL(kretprobe_perf_func
);
1212 #endif /* CONFIG_PERF_EVENTS */
1215 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1217 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1218 * lockless, but we can't race with this __init function.
1220 static int kprobe_register(struct trace_event_call
*event
,
1221 enum trace_reg type
, void *data
)
1223 struct trace_kprobe
*tk
= (struct trace_kprobe
*)event
->data
;
1224 struct trace_event_file
*file
= data
;
1227 case TRACE_REG_REGISTER
:
1228 return enable_trace_kprobe(tk
, file
);
1229 case TRACE_REG_UNREGISTER
:
1230 return disable_trace_kprobe(tk
, file
);
1232 #ifdef CONFIG_PERF_EVENTS
1233 case TRACE_REG_PERF_REGISTER
:
1234 return enable_trace_kprobe(tk
, NULL
);
1235 case TRACE_REG_PERF_UNREGISTER
:
1236 return disable_trace_kprobe(tk
, NULL
);
1237 case TRACE_REG_PERF_OPEN
:
1238 case TRACE_REG_PERF_CLOSE
:
1239 case TRACE_REG_PERF_ADD
:
1240 case TRACE_REG_PERF_DEL
:
1247 static int kprobe_dispatcher(struct kprobe
*kp
, struct pt_regs
*regs
)
1249 struct trace_kprobe
*tk
= container_of(kp
, struct trace_kprobe
, rp
.kp
);
1251 raw_cpu_inc(*tk
->nhit
);
1253 if (tk
->tp
.flags
& TP_FLAG_TRACE
)
1254 kprobe_trace_func(tk
, regs
);
1255 #ifdef CONFIG_PERF_EVENTS
1256 if (tk
->tp
.flags
& TP_FLAG_PROFILE
)
1257 kprobe_perf_func(tk
, regs
);
1259 return 0; /* We don't tweek kernel, so just return 0 */
1261 NOKPROBE_SYMBOL(kprobe_dispatcher
);
1264 kretprobe_dispatcher(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
1266 struct trace_kprobe
*tk
= container_of(ri
->rp
, struct trace_kprobe
, rp
);
1268 raw_cpu_inc(*tk
->nhit
);
1270 if (tk
->tp
.flags
& TP_FLAG_TRACE
)
1271 kretprobe_trace_func(tk
, ri
, regs
);
1272 #ifdef CONFIG_PERF_EVENTS
1273 if (tk
->tp
.flags
& TP_FLAG_PROFILE
)
1274 kretprobe_perf_func(tk
, ri
, regs
);
1276 return 0; /* We don't tweek kernel, so just return 0 */
1278 NOKPROBE_SYMBOL(kretprobe_dispatcher
);
1280 static struct trace_event_functions kretprobe_funcs
= {
1281 .trace
= print_kretprobe_event
1284 static struct trace_event_functions kprobe_funcs
= {
1285 .trace
= print_kprobe_event
1288 static int register_kprobe_event(struct trace_kprobe
*tk
)
1290 struct trace_event_call
*call
= &tk
->tp
.call
;
1293 /* Initialize trace_event_call */
1294 INIT_LIST_HEAD(&call
->class->fields
);
1295 if (trace_kprobe_is_return(tk
)) {
1296 call
->event
.funcs
= &kretprobe_funcs
;
1297 call
->class->define_fields
= kretprobe_event_define_fields
;
1299 call
->event
.funcs
= &kprobe_funcs
;
1300 call
->class->define_fields
= kprobe_event_define_fields
;
1302 if (set_print_fmt(&tk
->tp
, trace_kprobe_is_return(tk
)) < 0)
1304 ret
= register_trace_event(&call
->event
);
1306 kfree(call
->print_fmt
);
1309 call
->flags
= TRACE_EVENT_FL_KPROBE
;
1310 call
->class->reg
= kprobe_register
;
1312 ret
= trace_add_event_call(call
);
1314 pr_info("Failed to register kprobe event: %s\n",
1315 trace_event_name(call
));
1316 kfree(call
->print_fmt
);
1317 unregister_trace_event(&call
->event
);
1322 static int unregister_kprobe_event(struct trace_kprobe
*tk
)
1326 /* tp->event is unregistered in trace_remove_event_call() */
1327 ret
= trace_remove_event_call(&tk
->tp
.call
);
1329 kfree(tk
->tp
.call
.print_fmt
);
1333 /* Make a tracefs interface for controlling probe points */
1334 static __init
int init_kprobe_trace(void)
1336 struct dentry
*d_tracer
;
1337 struct dentry
*entry
;
1339 if (register_module_notifier(&trace_kprobe_module_nb
))
1342 d_tracer
= tracing_init_dentry();
1343 if (IS_ERR(d_tracer
))
1346 entry
= tracefs_create_file("kprobe_events", 0644, d_tracer
,
1347 NULL
, &kprobe_events_ops
);
1349 /* Event list interface */
1351 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1353 /* Profile interface */
1354 entry
= tracefs_create_file("kprobe_profile", 0444, d_tracer
,
1355 NULL
, &kprobe_profile_ops
);
1358 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
1361 fs_initcall(init_kprobe_trace
);
1364 #ifdef CONFIG_FTRACE_STARTUP_TEST
1366 * The "__used" keeps gcc from removing the function symbol
1367 * from the kallsyms table. 'noinline' makes sure that there
1368 * isn't an inlined version used by the test method below
1370 static __used __init noinline
int
1371 kprobe_trace_selftest_target(int a1
, int a2
, int a3
, int a4
, int a5
, int a6
)
1373 return a1
+ a2
+ a3
+ a4
+ a5
+ a6
;
1376 static __init
struct trace_event_file
*
1377 find_trace_probe_file(struct trace_kprobe
*tk
, struct trace_array
*tr
)
1379 struct trace_event_file
*file
;
1381 list_for_each_entry(file
, &tr
->events
, list
)
1382 if (file
->event_call
== &tk
->tp
.call
)
1389 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
1390 * stage, we can do this lockless.
1392 static __init
int kprobe_trace_self_tests_init(void)
1395 int (*target
)(int, int, int, int, int, int);
1396 struct trace_kprobe
*tk
;
1397 struct trace_event_file
*file
;
1399 if (tracing_is_disabled())
1402 target
= kprobe_trace_selftest_target
;
1404 pr_info("Testing kprobe tracing: ");
1406 ret
= traceprobe_command("p:testprobe kprobe_trace_selftest_target "
1407 "$stack $stack0 +0($stack)",
1408 create_trace_kprobe
);
1409 if (WARN_ON_ONCE(ret
)) {
1410 pr_warn("error on probing function entry.\n");
1413 /* Enable trace point */
1414 tk
= find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM
);
1415 if (WARN_ON_ONCE(tk
== NULL
)) {
1416 pr_warn("error on getting new probe.\n");
1419 file
= find_trace_probe_file(tk
, top_trace_array());
1420 if (WARN_ON_ONCE(file
== NULL
)) {
1421 pr_warn("error on getting probe file.\n");
1424 enable_trace_kprobe(tk
, file
);
1428 ret
= traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
1429 "$retval", create_trace_kprobe
);
1430 if (WARN_ON_ONCE(ret
)) {
1431 pr_warn("error on probing function return.\n");
1434 /* Enable trace point */
1435 tk
= find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM
);
1436 if (WARN_ON_ONCE(tk
== NULL
)) {
1437 pr_warn("error on getting 2nd new probe.\n");
1440 file
= find_trace_probe_file(tk
, top_trace_array());
1441 if (WARN_ON_ONCE(file
== NULL
)) {
1442 pr_warn("error on getting probe file.\n");
1445 enable_trace_kprobe(tk
, file
);
1452 ret
= target(1, 2, 3, 4, 5, 6);
1455 * Not expecting an error here, the check is only to prevent the
1456 * optimizer from removing the call to target() as otherwise there
1457 * are no side-effects and the call is never performed.
1462 /* Disable trace points before removing it */
1463 tk
= find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM
);
1464 if (WARN_ON_ONCE(tk
== NULL
)) {
1465 pr_warn("error on getting test probe.\n");
1468 if (trace_kprobe_nhit(tk
) != 1) {
1469 pr_warn("incorrect number of testprobe hits\n");
1473 file
= find_trace_probe_file(tk
, top_trace_array());
1474 if (WARN_ON_ONCE(file
== NULL
)) {
1475 pr_warn("error on getting probe file.\n");
1478 disable_trace_kprobe(tk
, file
);
1481 tk
= find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM
);
1482 if (WARN_ON_ONCE(tk
== NULL
)) {
1483 pr_warn("error on getting 2nd test probe.\n");
1486 if (trace_kprobe_nhit(tk
) != 1) {
1487 pr_warn("incorrect number of testprobe2 hits\n");
1491 file
= find_trace_probe_file(tk
, top_trace_array());
1492 if (WARN_ON_ONCE(file
== NULL
)) {
1493 pr_warn("error on getting probe file.\n");
1496 disable_trace_kprobe(tk
, file
);
1499 ret
= traceprobe_command("-:testprobe", create_trace_kprobe
);
1500 if (WARN_ON_ONCE(ret
)) {
1501 pr_warn("error on deleting a probe.\n");
1505 ret
= traceprobe_command("-:testprobe2", create_trace_kprobe
);
1506 if (WARN_ON_ONCE(ret
)) {
1507 pr_warn("error on deleting a probe.\n");
1512 release_all_trace_kprobes();
1514 pr_cont("NG: Some tests are failed. Please check them.\n");
1520 late_initcall(kprobe_trace_self_tests_init
);