1 // SPDX-License-Identifier: GPL-2.0
3 * Kprobes-based tracing events
5 * Created by Masami Hiramatsu <mhiramat@redhat.com>
8 #define pr_fmt(fmt) "trace_kprobe: " fmt
10 #include <linux/module.h>
11 #include <linux/uaccess.h>
12 #include <linux/rculist.h>
13 #include <linux/error-injection.h>
15 #include "trace_kprobe_selftest.h"
16 #include "trace_probe.h"
18 #define KPROBE_EVENT_SYSTEM "kprobes"
19 #define KRETPROBE_MAXACTIVE_MAX 4096
22 * Kprobe event core functions
25 struct list_head list;
26 struct kretprobe rp; /* Use rp.kp for kprobe use */
27 unsigned long __percpu *nhit;
28 const char *symbol; /* symbol name */
29 struct trace_probe tp;
32 #define SIZEOF_TRACE_KPROBE(n) \
33 (offsetof(struct trace_kprobe, tp.args) + \
34 (sizeof(struct probe_arg) * (n)))
36 static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
38 return tk->rp.handler != NULL;
41 static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
43 return tk->symbol ? tk->symbol : "unknown";
46 static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
48 return tk->rp.kp.offset;
51 static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
53 return !!(kprobe_gone(&tk->rp.kp));
56 static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
59 int len = strlen(mod->name);
60 const char *name = trace_kprobe_symbol(tk);
61 return strncmp(mod->name, name, len) == 0 && name[len] == ':';
64 static nokprobe_inline bool trace_kprobe_is_on_module(struct trace_kprobe *tk)
66 return !!strchr(trace_kprobe_symbol(tk), ':');
69 static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
71 unsigned long nhit = 0;
74 for_each_possible_cpu(cpu)
75 nhit += *per_cpu_ptr(tk->nhit, cpu);
80 /* Return 0 if it fails to find the symbol address */
81 static nokprobe_inline
82 unsigned long trace_kprobe_address(struct trace_kprobe *tk)
87 addr = (unsigned long)
88 kallsyms_lookup_name(trace_kprobe_symbol(tk));
90 addr += tk->rp.kp.offset;
92 addr = (unsigned long)tk->rp.kp.addr;
97 bool trace_kprobe_on_func_entry(struct trace_event_call *call)
99 struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
101 return kprobe_on_func_entry(tk->rp.kp.addr,
102 tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
103 tk->rp.kp.addr ? 0 : tk->rp.kp.offset);
106 bool trace_kprobe_error_injectable(struct trace_event_call *call)
108 struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
110 return within_error_injection_list(trace_kprobe_address(tk));
113 static int register_kprobe_event(struct trace_kprobe *tk);
114 static int unregister_kprobe_event(struct trace_kprobe *tk);
116 static DEFINE_MUTEX(probe_lock);
117 static LIST_HEAD(probe_list);
119 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
120 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
121 struct pt_regs *regs);
123 /* Memory fetching by symbol */
124 struct symbol_cache {
130 unsigned long update_symbol_cache(struct symbol_cache *sc)
132 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
135 sc->addr += sc->offset;
140 void free_symbol_cache(struct symbol_cache *sc)
146 struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
148 struct symbol_cache *sc;
150 if (!sym || strlen(sym) == 0)
153 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
157 sc->symbol = kstrdup(sym, GFP_KERNEL);
163 update_symbol_cache(sc);
169 * Kprobes-specific fetch functions
171 #define DEFINE_FETCH_stack(type) \
172 static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
173 void *offset, void *dest) \
175 *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
176 (unsigned int)((unsigned long)offset)); \
178 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(stack, type));
180 DEFINE_BASIC_FETCH_FUNCS(stack)
181 /* No string on the stack entry */
182 #define fetch_stack_string NULL
183 #define fetch_stack_string_size NULL
185 #define DEFINE_FETCH_memory(type) \
186 static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
187 void *addr, void *dest) \
190 if (probe_kernel_address(addr, retval)) \
193 *(type *)dest = retval; \
195 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, type));
197 DEFINE_BASIC_FETCH_FUNCS(memory)
199 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
200 * length and relative data location.
202 static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
203 void *addr, void *dest)
205 int maxlen = get_rloc_len(*(u32 *)dest);
206 u8 *dst = get_rloc_data(dest);
213 * Try to get string again, since the string can be changed while
216 ret = strncpy_from_unsafe(dst, addr, maxlen);
218 if (ret < 0) { /* Failed to fetch string */
220 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
222 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest));
225 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string));
227 /* Return the length of string -- including null terminal byte */
228 static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
229 void *addr, void *dest)
240 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
242 } while (c && ret == 0 && len < MAX_STRING_SIZE);
247 if (ret < 0) /* Failed to check the length */
252 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string_size));
254 #define DEFINE_FETCH_symbol(type) \
255 void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, void *data, void *dest)\
257 struct symbol_cache *sc = data; \
259 fetch_memory_##type(regs, (void *)sc->addr, dest); \
263 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(symbol, type));
265 DEFINE_BASIC_FETCH_FUNCS(symbol)
266 DEFINE_FETCH_symbol(string)
267 DEFINE_FETCH_symbol(string_size)
269 /* kprobes don't support file_offset fetch methods */
270 #define fetch_file_offset_u8 NULL
271 #define fetch_file_offset_u16 NULL
272 #define fetch_file_offset_u32 NULL
273 #define fetch_file_offset_u64 NULL
274 #define fetch_file_offset_string NULL
275 #define fetch_file_offset_string_size NULL
277 /* Fetch type information table */
278 static const struct fetch_type kprobes_fetch_type_table[] = {
280 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
281 sizeof(u32), 1, "__data_loc char[]"),
282 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
283 string_size, sizeof(u32), 0, "u32"),
285 ASSIGN_FETCH_TYPE(u8, u8, 0),
286 ASSIGN_FETCH_TYPE(u16, u16, 0),
287 ASSIGN_FETCH_TYPE(u32, u32, 0),
288 ASSIGN_FETCH_TYPE(u64, u64, 0),
289 ASSIGN_FETCH_TYPE(s8, u8, 1),
290 ASSIGN_FETCH_TYPE(s16, u16, 1),
291 ASSIGN_FETCH_TYPE(s32, u32, 1),
292 ASSIGN_FETCH_TYPE(s64, u64, 1),
293 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
294 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
295 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
296 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
298 ASSIGN_FETCH_TYPE_END
302 * Allocate new trace_probe and initialize it (including kprobes).
304 static struct trace_kprobe *alloc_trace_kprobe(const char *group,
310 int nargs, bool is_return)
312 struct trace_kprobe *tk;
315 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
319 tk->nhit = alloc_percpu(unsigned long);
324 tk->symbol = kstrdup(symbol, GFP_KERNEL);
327 tk->rp.kp.symbol_name = tk->symbol;
328 tk->rp.kp.offset = offs;
330 tk->rp.kp.addr = addr;
333 tk->rp.handler = kretprobe_dispatcher;
335 tk->rp.kp.pre_handler = kprobe_dispatcher;
337 tk->rp.maxactive = maxactive;
339 if (!event || !is_good_name(event)) {
344 tk->tp.call.class = &tk->tp.class;
345 tk->tp.call.name = kstrdup(event, GFP_KERNEL);
346 if (!tk->tp.call.name)
349 if (!group || !is_good_name(group)) {
354 tk->tp.class.system = kstrdup(group, GFP_KERNEL);
355 if (!tk->tp.class.system)
358 INIT_LIST_HEAD(&tk->list);
359 INIT_LIST_HEAD(&tk->tp.files);
362 kfree(tk->tp.call.name);
364 free_percpu(tk->nhit);
369 static void free_trace_kprobe(struct trace_kprobe *tk)
373 for (i = 0; i < tk->tp.nr_args; i++)
374 traceprobe_free_probe_arg(&tk->tp.args[i]);
376 kfree(tk->tp.call.class->system);
377 kfree(tk->tp.call.name);
379 free_percpu(tk->nhit);
383 static struct trace_kprobe *find_trace_kprobe(const char *event,
386 struct trace_kprobe *tk;
388 list_for_each_entry(tk, &probe_list, list)
389 if (strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
390 strcmp(tk->tp.call.class->system, group) == 0)
395 static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
399 if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) {
400 if (trace_kprobe_is_return(tk))
401 ret = enable_kretprobe(&tk->rp);
403 ret = enable_kprobe(&tk->rp.kp);
411 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
414 enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
416 struct event_file_link *link;
420 link = kmalloc(sizeof(*link), GFP_KERNEL);
427 list_add_tail_rcu(&link->list, &tk->tp.files);
429 tk->tp.flags |= TP_FLAG_TRACE;
430 ret = __enable_trace_kprobe(tk);
432 list_del_rcu(&link->list);
434 tk->tp.flags &= ~TP_FLAG_TRACE;
438 tk->tp.flags |= TP_FLAG_PROFILE;
439 ret = __enable_trace_kprobe(tk);
441 tk->tp.flags &= ~TP_FLAG_PROFILE;
448 * Disable trace_probe
449 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
452 disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
454 struct event_file_link *link = NULL;
459 link = find_event_file_link(&tk->tp, file);
465 list_del_rcu(&link->list);
467 if (!list_empty(&tk->tp.files))
470 tk->tp.flags &= ~TP_FLAG_TRACE;
472 tk->tp.flags &= ~TP_FLAG_PROFILE;
474 if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
475 if (trace_kprobe_is_return(tk))
476 disable_kretprobe(&tk->rp);
478 disable_kprobe(&tk->rp.kp);
483 * if tk is not added to any list, it must be a local trace_kprobe
484 * created with perf_event_open. We don't need to wait for these
487 if (list_empty(&tk->list))
492 * Synchronize with kprobe_trace_func/kretprobe_trace_func
493 * to ensure disabled (all running handlers are finished).
494 * This is not only for kfree(), but also the caller,
495 * trace_remove_event_call() supposes it for releasing
496 * event_call related objects, which will be accessed in
497 * the kprobe_trace_func/kretprobe_trace_func.
500 kfree(link); /* Ignored if link == NULL */
506 #if defined(CONFIG_KPROBES_ON_FTRACE) && \
507 !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
508 static bool within_notrace_func(struct trace_kprobe *tk)
510 unsigned long offset, size, addr;
512 addr = trace_kprobe_address(tk);
513 if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
516 return !ftrace_location_range(addr - offset, addr - offset + size);
519 #define within_notrace_func(tk) (false)
522 /* Internal register function - just handle k*probes and flags */
523 static int __register_trace_kprobe(struct trace_kprobe *tk)
527 if (trace_probe_is_registered(&tk->tp))
530 if (within_notrace_func(tk)) {
531 pr_warn("Could not probe notrace function %s\n",
532 trace_kprobe_symbol(tk));
536 for (i = 0; i < tk->tp.nr_args; i++)
537 traceprobe_update_arg(&tk->tp.args[i]);
539 /* Set/clear disabled flag according to tp->flag */
540 if (trace_probe_is_enabled(&tk->tp))
541 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
543 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
545 if (trace_kprobe_is_return(tk))
546 ret = register_kretprobe(&tk->rp);
548 ret = register_kprobe(&tk->rp.kp);
551 tk->tp.flags |= TP_FLAG_REGISTERED;
553 if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
554 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
556 } else if (ret == -EILSEQ) {
557 pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
566 /* Internal unregister function - just handle k*probes and flags */
567 static void __unregister_trace_kprobe(struct trace_kprobe *tk)
569 if (trace_probe_is_registered(&tk->tp)) {
570 if (trace_kprobe_is_return(tk))
571 unregister_kretprobe(&tk->rp);
573 unregister_kprobe(&tk->rp.kp);
574 tk->tp.flags &= ~TP_FLAG_REGISTERED;
575 /* Cleanup kprobe for reuse */
576 if (tk->rp.kp.symbol_name)
577 tk->rp.kp.addr = NULL;
581 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
582 static int unregister_trace_kprobe(struct trace_kprobe *tk)
584 /* Enabled event can not be unregistered */
585 if (trace_probe_is_enabled(&tk->tp))
588 /* Will fail if probe is being used by ftrace or perf */
589 if (unregister_kprobe_event(tk))
592 __unregister_trace_kprobe(tk);
598 /* Register a trace_probe and probe_event */
599 static int register_trace_kprobe(struct trace_kprobe *tk)
601 struct trace_kprobe *old_tk;
604 mutex_lock(&probe_lock);
606 /* Delete old (same name) event if exist */
607 old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
608 tk->tp.call.class->system);
610 ret = unregister_trace_kprobe(old_tk);
613 free_trace_kprobe(old_tk);
616 /* Register new event */
617 ret = register_kprobe_event(tk);
619 pr_warn("Failed to register probe event(%d)\n", ret);
623 /* Register k*probe */
624 ret = __register_trace_kprobe(tk);
626 unregister_kprobe_event(tk);
628 list_add_tail(&tk->list, &probe_list);
631 mutex_unlock(&probe_lock);
635 /* Module notifier call back, checking event on the module */
636 static int trace_kprobe_module_callback(struct notifier_block *nb,
637 unsigned long val, void *data)
639 struct module *mod = data;
640 struct trace_kprobe *tk;
643 if (val != MODULE_STATE_COMING)
646 /* Update probes on coming module */
647 mutex_lock(&probe_lock);
648 list_for_each_entry(tk, &probe_list, list) {
649 if (trace_kprobe_within_module(tk, mod)) {
650 /* Don't need to check busy - this should have gone. */
651 __unregister_trace_kprobe(tk);
652 ret = __register_trace_kprobe(tk);
654 pr_warn("Failed to re-register probe %s on %s: %d\n",
655 trace_event_name(&tk->tp.call),
659 mutex_unlock(&probe_lock);
664 static struct notifier_block trace_kprobe_module_nb = {
665 .notifier_call = trace_kprobe_module_callback,
666 .priority = 1 /* Invoked after kprobe module callback */
669 /* Convert certain expected symbols into '_' when generating event names */
670 static inline void sanitize_event_name(char *name)
672 while (*name++ != '\0')
673 if (*name == ':' || *name == '.')
677 static int create_trace_kprobe(int argc, char **argv)
682 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
684 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
686 * $retval : fetch return value
687 * $stack : fetch stack address
688 * $stackN : fetch Nth of stack (N:0-)
689 * $comm : fetch current task comm
690 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
691 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
692 * %REG : fetch register REG
693 * Dereferencing memory fetch:
694 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
695 * Alias name of args:
696 * NAME=FETCHARG : set NAME as alias of FETCHARG.
698 * FETCHARG:TYPE : use TYPE instead of unsigned long.
700 struct trace_kprobe *tk;
702 bool is_return = false, is_delete = false;
703 char *symbol = NULL, *event = NULL, *group = NULL;
708 char buf[MAX_EVENT_NAME_LEN];
710 /* argc must be >= 1 */
711 if (argv[0][0] == 'p')
713 else if (argv[0][0] == 'r')
715 else if (argv[0][0] == '-')
718 pr_info("Probe definition must be started with 'p', 'r' or"
723 event = strchr(&argv[0][1], ':');
728 if (is_return && isdigit(argv[0][1])) {
729 ret = kstrtouint(&argv[0][1], 0, &maxactive);
731 pr_info("Failed to parse maxactive.\n");
734 /* kretprobes instances are iterated over via a list. The
735 * maximum should stay reasonable.
737 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
738 pr_info("Maxactive is too big (%d > %d).\n",
739 maxactive, KRETPROBE_MAXACTIVE_MAX);
745 if (strchr(event, '/')) {
747 event = strchr(group, '/') + 1;
749 if (strlen(group) == 0) {
750 pr_info("Group name is not specified\n");
754 if (strlen(event) == 0) {
755 pr_info("Event name is not specified\n");
760 group = KPROBE_EVENT_SYSTEM;
764 pr_info("Delete command needs an event name.\n");
767 mutex_lock(&probe_lock);
768 tk = find_trace_kprobe(event, group);
770 mutex_unlock(&probe_lock);
771 pr_info("Event %s/%s doesn't exist.\n", group, event);
774 /* delete an event */
775 ret = unregister_trace_kprobe(tk);
777 free_trace_kprobe(tk);
778 mutex_unlock(&probe_lock);
783 pr_info("Probe point is not specified.\n");
787 /* try to parse an address. if that fails, try to read the
788 * input as a symbol. */
789 if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
790 /* a symbol specified */
792 /* TODO: support .init module functions */
793 ret = traceprobe_split_symbol_offset(symbol, &offset);
794 if (ret || offset < 0 || offset > UINT_MAX) {
795 pr_info("Failed to parse either an address or a symbol.\n");
798 if (offset && is_return &&
799 !kprobe_on_func_entry(NULL, symbol, offset)) {
800 pr_info("Given offset is not valid for return probe.\n");
804 argc -= 2; argv += 2;
808 /* Make a new event name */
810 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
811 is_return ? 'r' : 'p', symbol, offset);
813 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
814 is_return ? 'r' : 'p', addr);
815 sanitize_event_name(buf);
818 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
821 pr_info("Failed to allocate trace_probe.(%d)\n",
826 /* parse arguments */
828 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
829 struct probe_arg *parg = &tk->tp.args[i];
831 /* Increment count for freeing args in error case */
834 /* Parse argument name */
835 arg = strchr(argv[i], '=');
838 parg->name = kstrdup(argv[i], GFP_KERNEL);
841 /* If argument name is omitted, set "argN" */
842 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
843 parg->name = kstrdup(buf, GFP_KERNEL);
847 pr_info("Failed to allocate argument[%d] name.\n", i);
852 if (!is_good_name(parg->name)) {
853 pr_info("Invalid argument[%d] name: %s\n",
859 if (traceprobe_conflict_field_name(parg->name,
861 pr_info("Argument[%d] name '%s' conflicts with "
862 "another field.\n", i, argv[i]);
867 /* Parse fetch argument */
868 ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
870 kprobes_fetch_type_table);
872 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
877 ret = register_trace_kprobe(tk);
883 free_trace_kprobe(tk);
887 static int release_all_trace_kprobes(void)
889 struct trace_kprobe *tk;
892 mutex_lock(&probe_lock);
893 /* Ensure no probe is in use. */
894 list_for_each_entry(tk, &probe_list, list)
895 if (trace_probe_is_enabled(&tk->tp)) {
899 /* TODO: Use batch unregistration */
900 while (!list_empty(&probe_list)) {
901 tk = list_entry(probe_list.next, struct trace_kprobe, list);
902 ret = unregister_trace_kprobe(tk);
905 free_trace_kprobe(tk);
909 mutex_unlock(&probe_lock);
914 /* Probes listing interfaces */
915 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
917 mutex_lock(&probe_lock);
918 return seq_list_start(&probe_list, *pos);
921 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
923 return seq_list_next(v, &probe_list, pos);
926 static void probes_seq_stop(struct seq_file *m, void *v)
928 mutex_unlock(&probe_lock);
931 static int probes_seq_show(struct seq_file *m, void *v)
933 struct trace_kprobe *tk = v;
936 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
937 seq_printf(m, ":%s/%s", tk->tp.call.class->system,
938 trace_event_name(&tk->tp.call));
941 seq_printf(m, " 0x%p", tk->rp.kp.addr);
942 else if (tk->rp.kp.offset)
943 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
946 seq_printf(m, " %s", trace_kprobe_symbol(tk));
948 for (i = 0; i < tk->tp.nr_args; i++)
949 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
955 static const struct seq_operations probes_seq_op = {
956 .start = probes_seq_start,
957 .next = probes_seq_next,
958 .stop = probes_seq_stop,
959 .show = probes_seq_show
962 static int probes_open(struct inode *inode, struct file *file)
966 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
967 ret = release_all_trace_kprobes();
972 return seq_open(file, &probes_seq_op);
975 static ssize_t probes_write(struct file *file, const char __user *buffer,
976 size_t count, loff_t *ppos)
978 return trace_parse_run_command(file, buffer, count, ppos,
979 create_trace_kprobe);
982 static const struct file_operations kprobe_events_ops = {
983 .owner = THIS_MODULE,
987 .release = seq_release,
988 .write = probes_write,
991 /* Probes profiling interfaces */
992 static int probes_profile_seq_show(struct seq_file *m, void *v)
994 struct trace_kprobe *tk = v;
996 seq_printf(m, " %-44s %15lu %15lu\n",
997 trace_event_name(&tk->tp.call),
998 trace_kprobe_nhit(tk),
1004 static const struct seq_operations profile_seq_op = {
1005 .start = probes_seq_start,
1006 .next = probes_seq_next,
1007 .stop = probes_seq_stop,
1008 .show = probes_profile_seq_show
1011 static int profile_open(struct inode *inode, struct file *file)
1013 return seq_open(file, &profile_seq_op);
1016 static const struct file_operations kprobe_profile_ops = {
1017 .owner = THIS_MODULE,
1018 .open = profile_open,
1020 .llseek = seq_lseek,
1021 .release = seq_release,
1024 /* Kprobe handler */
1025 static nokprobe_inline void
1026 __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
1027 struct trace_event_file *trace_file)
1029 struct kprobe_trace_entry_head *entry;
1030 struct ring_buffer_event *event;
1031 struct ring_buffer *buffer;
1032 int size, dsize, pc;
1033 unsigned long irq_flags;
1034 struct trace_event_call *call = &tk->tp.call;
1036 WARN_ON(call != trace_file->event_call);
1038 if (trace_trigger_soft_disabled(trace_file))
1041 local_save_flags(irq_flags);
1042 pc = preempt_count();
1044 dsize = __get_data_size(&tk->tp, regs);
1045 size = sizeof(*entry) + tk->tp.size + dsize;
1047 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1049 size, irq_flags, pc);
1053 entry = ring_buffer_event_data(event);
1054 entry->ip = (unsigned long)tk->rp.kp.addr;
1055 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1057 event_trigger_unlock_commit_regs(trace_file, buffer, event,
1058 entry, irq_flags, pc, regs);
1062 kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
1064 struct event_file_link *link;
1066 list_for_each_entry_rcu(link, &tk->tp.files, list)
1067 __kprobe_trace_func(tk, regs, link->file);
1069 NOKPROBE_SYMBOL(kprobe_trace_func);
1071 /* Kretprobe handler */
1072 static nokprobe_inline void
1073 __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1074 struct pt_regs *regs,
1075 struct trace_event_file *trace_file)
1077 struct kretprobe_trace_entry_head *entry;
1078 struct ring_buffer_event *event;
1079 struct ring_buffer *buffer;
1080 int size, pc, dsize;
1081 unsigned long irq_flags;
1082 struct trace_event_call *call = &tk->tp.call;
1084 WARN_ON(call != trace_file->event_call);
1086 if (trace_trigger_soft_disabled(trace_file))
1089 local_save_flags(irq_flags);
1090 pc = preempt_count();
1092 dsize = __get_data_size(&tk->tp, regs);
1093 size = sizeof(*entry) + tk->tp.size + dsize;
1095 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1097 size, irq_flags, pc);
1101 entry = ring_buffer_event_data(event);
1102 entry->func = (unsigned long)tk->rp.kp.addr;
1103 entry->ret_ip = (unsigned long)ri->ret_addr;
1104 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1106 event_trigger_unlock_commit_regs(trace_file, buffer, event,
1107 entry, irq_flags, pc, regs);
1111 kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1112 struct pt_regs *regs)
1114 struct event_file_link *link;
1116 list_for_each_entry_rcu(link, &tk->tp.files, list)
1117 __kretprobe_trace_func(tk, ri, regs, link->file);
1119 NOKPROBE_SYMBOL(kretprobe_trace_func);
1121 /* Event entry printers */
1122 static enum print_line_t
1123 print_kprobe_event(struct trace_iterator *iter, int flags,
1124 struct trace_event *event)
1126 struct kprobe_trace_entry_head *field;
1127 struct trace_seq *s = &iter->seq;
1128 struct trace_probe *tp;
1132 field = (struct kprobe_trace_entry_head *)iter->ent;
1133 tp = container_of(event, struct trace_probe, call.event);
1135 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1137 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1140 trace_seq_putc(s, ')');
1142 data = (u8 *)&field[1];
1143 for (i = 0; i < tp->nr_args; i++)
1144 if (!tp->args[i].type->print(s, tp->args[i].name,
1145 data + tp->args[i].offset, field))
1148 trace_seq_putc(s, '\n');
1150 return trace_handle_return(s);
1153 static enum print_line_t
1154 print_kretprobe_event(struct trace_iterator *iter, int flags,
1155 struct trace_event *event)
1157 struct kretprobe_trace_entry_head *field;
1158 struct trace_seq *s = &iter->seq;
1159 struct trace_probe *tp;
1163 field = (struct kretprobe_trace_entry_head *)iter->ent;
1164 tp = container_of(event, struct trace_probe, call.event);
1166 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1168 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1171 trace_seq_puts(s, " <- ");
1173 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1176 trace_seq_putc(s, ')');
1178 data = (u8 *)&field[1];
1179 for (i = 0; i < tp->nr_args; i++)
1180 if (!tp->args[i].type->print(s, tp->args[i].name,
1181 data + tp->args[i].offset, field))
1184 trace_seq_putc(s, '\n');
1187 return trace_handle_return(s);
1191 static int kprobe_event_define_fields(struct trace_event_call *event_call)
1194 struct kprobe_trace_entry_head field;
1195 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1197 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1198 /* Set argument names as fields */
1199 for (i = 0; i < tk->tp.nr_args; i++) {
1200 struct probe_arg *parg = &tk->tp.args[i];
1202 ret = trace_define_field(event_call, parg->type->fmttype,
1204 sizeof(field) + parg->offset,
1206 parg->type->is_signed,
1214 static int kretprobe_event_define_fields(struct trace_event_call *event_call)
1217 struct kretprobe_trace_entry_head field;
1218 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1220 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1221 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1222 /* Set argument names as fields */
1223 for (i = 0; i < tk->tp.nr_args; i++) {
1224 struct probe_arg *parg = &tk->tp.args[i];
1226 ret = trace_define_field(event_call, parg->type->fmttype,
1228 sizeof(field) + parg->offset,
1230 parg->type->is_signed,
1238 #ifdef CONFIG_PERF_EVENTS
1240 /* Kprobe profile handler */
1242 kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
1244 struct trace_event_call *call = &tk->tp.call;
1245 struct kprobe_trace_entry_head *entry;
1246 struct hlist_head *head;
1247 int size, __size, dsize;
1250 if (bpf_prog_array_valid(call)) {
1251 unsigned long orig_ip = instruction_pointer(regs);
1254 ret = trace_call_bpf(call, regs);
1257 * We need to check and see if we modified the pc of the
1258 * pt_regs, and if so return 1 so that we don't do the
1261 if (orig_ip != instruction_pointer(regs))
1267 head = this_cpu_ptr(call->perf_events);
1268 if (hlist_empty(head))
1271 dsize = __get_data_size(&tk->tp, regs);
1272 __size = sizeof(*entry) + tk->tp.size + dsize;
1273 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1274 size -= sizeof(u32);
1276 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1280 entry->ip = (unsigned long)tk->rp.kp.addr;
1281 memset(&entry[1], 0, dsize);
1282 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1283 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1287 NOKPROBE_SYMBOL(kprobe_perf_func);
1289 /* Kretprobe profile handler */
1291 kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1292 struct pt_regs *regs)
1294 struct trace_event_call *call = &tk->tp.call;
1295 struct kretprobe_trace_entry_head *entry;
1296 struct hlist_head *head;
1297 int size, __size, dsize;
1300 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1303 head = this_cpu_ptr(call->perf_events);
1304 if (hlist_empty(head))
1307 dsize = __get_data_size(&tk->tp, regs);
1308 __size = sizeof(*entry) + tk->tp.size + dsize;
1309 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1310 size -= sizeof(u32);
1312 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1316 entry->func = (unsigned long)tk->rp.kp.addr;
1317 entry->ret_ip = (unsigned long)ri->ret_addr;
1318 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1319 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1322 NOKPROBE_SYMBOL(kretprobe_perf_func);
1324 int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1325 const char **symbol, u64 *probe_offset,
1326 u64 *probe_addr, bool perf_type_tracepoint)
1328 const char *pevent = trace_event_name(event->tp_event);
1329 const char *group = event->tp_event->class->system;
1330 struct trace_kprobe *tk;
1332 if (perf_type_tracepoint)
1333 tk = find_trace_kprobe(pevent, group);
1335 tk = event->tp_event->data;
1339 *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1340 : BPF_FD_TYPE_KPROBE;
1342 *symbol = tk->symbol;
1343 *probe_offset = tk->rp.kp.offset;
1348 *probe_addr = (unsigned long)tk->rp.kp.addr;
1352 #endif /* CONFIG_PERF_EVENTS */
1355 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1357 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1358 * lockless, but we can't race with this __init function.
1360 static int kprobe_register(struct trace_event_call *event,
1361 enum trace_reg type, void *data)
1363 struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
1364 struct trace_event_file *file = data;
1367 case TRACE_REG_REGISTER:
1368 return enable_trace_kprobe(tk, file);
1369 case TRACE_REG_UNREGISTER:
1370 return disable_trace_kprobe(tk, file);
1372 #ifdef CONFIG_PERF_EVENTS
1373 case TRACE_REG_PERF_REGISTER:
1374 return enable_trace_kprobe(tk, NULL);
1375 case TRACE_REG_PERF_UNREGISTER:
1376 return disable_trace_kprobe(tk, NULL);
1377 case TRACE_REG_PERF_OPEN:
1378 case TRACE_REG_PERF_CLOSE:
1379 case TRACE_REG_PERF_ADD:
1380 case TRACE_REG_PERF_DEL:
1387 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1389 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
1392 raw_cpu_inc(*tk->nhit);
1394 if (tk->tp.flags & TP_FLAG_TRACE)
1395 kprobe_trace_func(tk, regs);
1396 #ifdef CONFIG_PERF_EVENTS
1397 if (tk->tp.flags & TP_FLAG_PROFILE)
1398 ret = kprobe_perf_func(tk, regs);
1402 NOKPROBE_SYMBOL(kprobe_dispatcher);
1405 kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1407 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
1409 raw_cpu_inc(*tk->nhit);
1411 if (tk->tp.flags & TP_FLAG_TRACE)
1412 kretprobe_trace_func(tk, ri, regs);
1413 #ifdef CONFIG_PERF_EVENTS
1414 if (tk->tp.flags & TP_FLAG_PROFILE)
1415 kretprobe_perf_func(tk, ri, regs);
1417 return 0; /* We don't tweek kernel, so just return 0 */
1419 NOKPROBE_SYMBOL(kretprobe_dispatcher);
1421 static struct trace_event_functions kretprobe_funcs = {
1422 .trace = print_kretprobe_event
1425 static struct trace_event_functions kprobe_funcs = {
1426 .trace = print_kprobe_event
1429 static inline void init_trace_event_call(struct trace_kprobe *tk,
1430 struct trace_event_call *call)
1432 INIT_LIST_HEAD(&call->class->fields);
1433 if (trace_kprobe_is_return(tk)) {
1434 call->event.funcs = &kretprobe_funcs;
1435 call->class->define_fields = kretprobe_event_define_fields;
1437 call->event.funcs = &kprobe_funcs;
1438 call->class->define_fields = kprobe_event_define_fields;
1441 call->flags = TRACE_EVENT_FL_KPROBE;
1442 call->class->reg = kprobe_register;
1446 static int register_kprobe_event(struct trace_kprobe *tk)
1448 struct trace_event_call *call = &tk->tp.call;
1451 init_trace_event_call(tk, call);
1453 if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
1455 ret = register_trace_event(&call->event);
1457 kfree(call->print_fmt);
1460 ret = trace_add_event_call(call);
1462 pr_info("Failed to register kprobe event: %s\n",
1463 trace_event_name(call));
1464 kfree(call->print_fmt);
1465 unregister_trace_event(&call->event);
1470 static int unregister_kprobe_event(struct trace_kprobe *tk)
1474 /* tp->event is unregistered in trace_remove_event_call() */
1475 ret = trace_remove_event_call(&tk->tp.call);
1477 kfree(tk->tp.call.print_fmt);
1481 #ifdef CONFIG_PERF_EVENTS
1482 /* create a trace_kprobe, but don't add it to global lists */
1483 struct trace_event_call *
1484 create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1487 struct trace_kprobe *tk;
1492 * local trace_kprobes are not added to probe_list, so they are never
1493 * searched in find_trace_kprobe(). Therefore, there is no concern of
1494 * duplicated name here.
1496 event = func ? func : "DUMMY_EVENT";
1498 tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1499 offs, 0 /* maxactive */, 0 /* nargs */,
1503 pr_info("Failed to allocate trace_probe.(%d)\n",
1505 return ERR_CAST(tk);
1508 init_trace_event_call(tk, &tk->tp.call);
1510 if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
1515 ret = __register_trace_kprobe(tk);
1517 kfree(tk->tp.call.print_fmt);
1521 return &tk->tp.call;
1523 free_trace_kprobe(tk);
1524 return ERR_PTR(ret);
1527 void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1529 struct trace_kprobe *tk;
1531 tk = container_of(event_call, struct trace_kprobe, tp.call);
1533 if (trace_probe_is_enabled(&tk->tp)) {
1538 __unregister_trace_kprobe(tk);
1540 kfree(tk->tp.call.print_fmt);
1541 free_trace_kprobe(tk);
1543 #endif /* CONFIG_PERF_EVENTS */
1545 /* Make a tracefs interface for controlling probe points */
1546 static __init int init_kprobe_trace(void)
1548 struct dentry *d_tracer;
1549 struct dentry *entry;
1551 if (register_module_notifier(&trace_kprobe_module_nb))
1554 d_tracer = tracing_init_dentry();
1555 if (IS_ERR(d_tracer))
1558 entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
1559 NULL, &kprobe_events_ops);
1561 /* Event list interface */
1563 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1565 /* Profile interface */
1566 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
1567 NULL, &kprobe_profile_ops);
1570 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
1573 fs_initcall(init_kprobe_trace);
1576 #ifdef CONFIG_FTRACE_STARTUP_TEST
1577 static __init struct trace_event_file *
1578 find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
1580 struct trace_event_file *file;
1582 list_for_each_entry(file, &tr->events, list)
1583 if (file->event_call == &tk->tp.call)
1590 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
1591 * stage, we can do this lockless.
1593 static __init int kprobe_trace_self_tests_init(void)
1596 int (*target)(int, int, int, int, int, int);
1597 struct trace_kprobe *tk;
1598 struct trace_event_file *file;
1600 if (tracing_is_disabled())
1603 target = kprobe_trace_selftest_target;
1605 pr_info("Testing kprobe tracing: ");
1607 ret = trace_run_command("p:testprobe kprobe_trace_selftest_target "
1608 "$stack $stack0 +0($stack)",
1609 create_trace_kprobe);
1610 if (WARN_ON_ONCE(ret)) {
1611 pr_warn("error on probing function entry.\n");
1614 /* Enable trace point */
1615 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1616 if (WARN_ON_ONCE(tk == NULL)) {
1617 pr_warn("error on getting new probe.\n");
1620 file = find_trace_probe_file(tk, top_trace_array());
1621 if (WARN_ON_ONCE(file == NULL)) {
1622 pr_warn("error on getting probe file.\n");
1625 enable_trace_kprobe(tk, file);
1629 ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target "
1630 "$retval", create_trace_kprobe);
1631 if (WARN_ON_ONCE(ret)) {
1632 pr_warn("error on probing function return.\n");
1635 /* Enable trace point */
1636 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1637 if (WARN_ON_ONCE(tk == NULL)) {
1638 pr_warn("error on getting 2nd new probe.\n");
1641 file = find_trace_probe_file(tk, top_trace_array());
1642 if (WARN_ON_ONCE(file == NULL)) {
1643 pr_warn("error on getting probe file.\n");
1646 enable_trace_kprobe(tk, file);
1653 ret = target(1, 2, 3, 4, 5, 6);
1656 * Not expecting an error here, the check is only to prevent the
1657 * optimizer from removing the call to target() as otherwise there
1658 * are no side-effects and the call is never performed.
1663 /* Disable trace points before removing it */
1664 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1665 if (WARN_ON_ONCE(tk == NULL)) {
1666 pr_warn("error on getting test probe.\n");
1669 if (trace_kprobe_nhit(tk) != 1) {
1670 pr_warn("incorrect number of testprobe hits\n");
1674 file = find_trace_probe_file(tk, top_trace_array());
1675 if (WARN_ON_ONCE(file == NULL)) {
1676 pr_warn("error on getting probe file.\n");
1679 disable_trace_kprobe(tk, file);
1682 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1683 if (WARN_ON_ONCE(tk == NULL)) {
1684 pr_warn("error on getting 2nd test probe.\n");
1687 if (trace_kprobe_nhit(tk) != 1) {
1688 pr_warn("incorrect number of testprobe2 hits\n");
1692 file = find_trace_probe_file(tk, top_trace_array());
1693 if (WARN_ON_ONCE(file == NULL)) {
1694 pr_warn("error on getting probe file.\n");
1697 disable_trace_kprobe(tk, file);
1700 ret = trace_run_command("-:testprobe", create_trace_kprobe);
1701 if (WARN_ON_ONCE(ret)) {
1702 pr_warn("error on deleting a probe.\n");
1706 ret = trace_run_command("-:testprobe2", create_trace_kprobe);
1707 if (WARN_ON_ONCE(ret)) {
1708 pr_warn("error on deleting a probe.\n");
1713 release_all_trace_kprobes();
1715 * Wait for the optimizer work to finish. Otherwise it might fiddle
1716 * with probes in already freed __init text.
1718 wait_for_kprobe_optimizer();
1720 pr_cont("NG: Some tests are failed. Please check them.\n");
1726 late_initcall(kprobe_trace_self_tests_init);