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"
17 #include "trace_probe_tmpl.h"
19 #define KPROBE_EVENT_SYSTEM "kprobes"
20 #define KRETPROBE_MAXACTIVE_MAX 4096
23 * Kprobe event core functions
26 struct list_head list;
27 struct kretprobe rp; /* Use rp.kp for kprobe use */
28 unsigned long __percpu *nhit;
29 const char *symbol; /* symbol name */
30 struct trace_probe tp;
33 #define SIZEOF_TRACE_KPROBE(n) \
34 (offsetof(struct trace_kprobe, tp.args) + \
35 (sizeof(struct probe_arg) * (n)))
37 static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
39 return tk->rp.handler != NULL;
42 static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
44 return tk->symbol ? tk->symbol : "unknown";
47 static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
49 return tk->rp.kp.offset;
52 static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
54 return !!(kprobe_gone(&tk->rp.kp));
57 static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
60 int len = strlen(mod->name);
61 const char *name = trace_kprobe_symbol(tk);
62 return strncmp(mod->name, name, len) == 0 && name[len] == ':';
65 static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
72 p = strchr(tk->symbol, ':');
76 mutex_lock(&module_mutex);
77 ret = !!find_module(tk->symbol);
78 mutex_unlock(&module_mutex);
84 static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
86 unsigned long nhit = 0;
89 for_each_possible_cpu(cpu)
90 nhit += *per_cpu_ptr(tk->nhit, cpu);
95 /* Return 0 if it fails to find the symbol address */
96 static nokprobe_inline
97 unsigned long trace_kprobe_address(struct trace_kprobe *tk)
102 addr = (unsigned long)
103 kallsyms_lookup_name(trace_kprobe_symbol(tk));
105 addr += tk->rp.kp.offset;
107 addr = (unsigned long)tk->rp.kp.addr;
112 bool trace_kprobe_on_func_entry(struct trace_event_call *call)
114 struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
116 return kprobe_on_func_entry(tk->rp.kp.addr,
117 tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
118 tk->rp.kp.addr ? 0 : tk->rp.kp.offset);
121 bool trace_kprobe_error_injectable(struct trace_event_call *call)
123 struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
125 return within_error_injection_list(trace_kprobe_address(tk));
128 static int register_kprobe_event(struct trace_kprobe *tk);
129 static int unregister_kprobe_event(struct trace_kprobe *tk);
131 static DEFINE_MUTEX(probe_lock);
132 static LIST_HEAD(probe_list);
134 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
135 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
136 struct pt_regs *regs);
139 * Allocate new trace_probe and initialize it (including kprobes).
141 static struct trace_kprobe *alloc_trace_kprobe(const char *group,
147 int nargs, bool is_return)
149 struct trace_kprobe *tk;
152 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
156 tk->nhit = alloc_percpu(unsigned long);
161 tk->symbol = kstrdup(symbol, GFP_KERNEL);
164 tk->rp.kp.symbol_name = tk->symbol;
165 tk->rp.kp.offset = offs;
167 tk->rp.kp.addr = addr;
170 tk->rp.handler = kretprobe_dispatcher;
172 tk->rp.kp.pre_handler = kprobe_dispatcher;
174 tk->rp.maxactive = maxactive;
176 if (!event || !is_good_name(event)) {
181 tk->tp.call.class = &tk->tp.class;
182 tk->tp.call.name = kstrdup(event, GFP_KERNEL);
183 if (!tk->tp.call.name)
186 if (!group || !is_good_name(group)) {
191 tk->tp.class.system = kstrdup(group, GFP_KERNEL);
192 if (!tk->tp.class.system)
195 INIT_LIST_HEAD(&tk->list);
196 INIT_LIST_HEAD(&tk->tp.files);
199 kfree(tk->tp.call.name);
201 free_percpu(tk->nhit);
206 static void free_trace_kprobe(struct trace_kprobe *tk)
210 for (i = 0; i < tk->tp.nr_args; i++)
211 traceprobe_free_probe_arg(&tk->tp.args[i]);
213 kfree(tk->tp.call.class->system);
214 kfree(tk->tp.call.name);
216 free_percpu(tk->nhit);
220 static struct trace_kprobe *find_trace_kprobe(const char *event,
223 struct trace_kprobe *tk;
225 list_for_each_entry(tk, &probe_list, list)
226 if (strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
227 strcmp(tk->tp.call.class->system, group) == 0)
232 static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
236 if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) {
237 if (trace_kprobe_is_return(tk))
238 ret = enable_kretprobe(&tk->rp);
240 ret = enable_kprobe(&tk->rp.kp);
248 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
251 enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
253 struct event_file_link *link;
257 link = kmalloc(sizeof(*link), GFP_KERNEL);
264 list_add_tail_rcu(&link->list, &tk->tp.files);
266 tk->tp.flags |= TP_FLAG_TRACE;
267 ret = __enable_trace_kprobe(tk);
269 list_del_rcu(&link->list);
271 tk->tp.flags &= ~TP_FLAG_TRACE;
275 tk->tp.flags |= TP_FLAG_PROFILE;
276 ret = __enable_trace_kprobe(tk);
278 tk->tp.flags &= ~TP_FLAG_PROFILE;
285 * Disable trace_probe
286 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
289 disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
291 struct event_file_link *link = NULL;
296 link = find_event_file_link(&tk->tp, file);
302 list_del_rcu(&link->list);
304 if (!list_empty(&tk->tp.files))
307 tk->tp.flags &= ~TP_FLAG_TRACE;
309 tk->tp.flags &= ~TP_FLAG_PROFILE;
311 if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
312 if (trace_kprobe_is_return(tk))
313 disable_kretprobe(&tk->rp);
315 disable_kprobe(&tk->rp.kp);
320 * if tk is not added to any list, it must be a local trace_kprobe
321 * created with perf_event_open. We don't need to wait for these
324 if (list_empty(&tk->list))
329 * Synchronize with kprobe_trace_func/kretprobe_trace_func
330 * to ensure disabled (all running handlers are finished).
331 * This is not only for kfree(), but also the caller,
332 * trace_remove_event_call() supposes it for releasing
333 * event_call related objects, which will be accessed in
334 * the kprobe_trace_func/kretprobe_trace_func.
337 kfree(link); /* Ignored if link == NULL */
343 #if defined(CONFIG_KPROBES_ON_FTRACE) && \
344 !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
345 static bool within_notrace_func(struct trace_kprobe *tk)
347 unsigned long offset, size, addr;
349 addr = trace_kprobe_address(tk);
350 if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
353 /* Get the entry address of the target function */
357 * Since ftrace_location_range() does inclusive range check, we need
358 * to subtract 1 byte from the end address.
360 return !ftrace_location_range(addr, addr + size - 1);
363 #define within_notrace_func(tk) (false)
366 /* Internal register function - just handle k*probes and flags */
367 static int __register_trace_kprobe(struct trace_kprobe *tk)
371 if (trace_probe_is_registered(&tk->tp))
374 if (within_notrace_func(tk)) {
375 pr_warn("Could not probe notrace function %s\n",
376 trace_kprobe_symbol(tk));
380 for (i = 0; i < tk->tp.nr_args; i++) {
381 ret = traceprobe_update_arg(&tk->tp.args[i]);
386 /* Set/clear disabled flag according to tp->flag */
387 if (trace_probe_is_enabled(&tk->tp))
388 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
390 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
392 if (trace_kprobe_is_return(tk))
393 ret = register_kretprobe(&tk->rp);
395 ret = register_kprobe(&tk->rp.kp);
398 tk->tp.flags |= TP_FLAG_REGISTERED;
399 } else if (ret == -EILSEQ) {
400 pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
407 /* Internal unregister function - just handle k*probes and flags */
408 static void __unregister_trace_kprobe(struct trace_kprobe *tk)
410 if (trace_probe_is_registered(&tk->tp)) {
411 if (trace_kprobe_is_return(tk))
412 unregister_kretprobe(&tk->rp);
414 unregister_kprobe(&tk->rp.kp);
415 tk->tp.flags &= ~TP_FLAG_REGISTERED;
416 /* Cleanup kprobe for reuse */
417 if (tk->rp.kp.symbol_name)
418 tk->rp.kp.addr = NULL;
422 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
423 static int unregister_trace_kprobe(struct trace_kprobe *tk)
425 /* Enabled event can not be unregistered */
426 if (trace_probe_is_enabled(&tk->tp))
429 /* Will fail if probe is being used by ftrace or perf */
430 if (unregister_kprobe_event(tk))
433 __unregister_trace_kprobe(tk);
439 /* Register a trace_probe and probe_event */
440 static int register_trace_kprobe(struct trace_kprobe *tk)
442 struct trace_kprobe *old_tk;
445 mutex_lock(&probe_lock);
447 /* Delete old (same name) event if exist */
448 old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
449 tk->tp.call.class->system);
451 ret = unregister_trace_kprobe(old_tk);
454 free_trace_kprobe(old_tk);
457 /* Register new event */
458 ret = register_kprobe_event(tk);
460 pr_warn("Failed to register probe event(%d)\n", ret);
464 /* Register k*probe */
465 ret = __register_trace_kprobe(tk);
466 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
467 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
472 unregister_kprobe_event(tk);
474 list_add_tail(&tk->list, &probe_list);
477 mutex_unlock(&probe_lock);
481 /* Module notifier call back, checking event on the module */
482 static int trace_kprobe_module_callback(struct notifier_block *nb,
483 unsigned long val, void *data)
485 struct module *mod = data;
486 struct trace_kprobe *tk;
489 if (val != MODULE_STATE_COMING)
492 /* Update probes on coming module */
493 mutex_lock(&probe_lock);
494 list_for_each_entry(tk, &probe_list, list) {
495 if (trace_kprobe_within_module(tk, mod)) {
496 /* Don't need to check busy - this should have gone. */
497 __unregister_trace_kprobe(tk);
498 ret = __register_trace_kprobe(tk);
500 pr_warn("Failed to re-register probe %s on %s: %d\n",
501 trace_event_name(&tk->tp.call),
505 mutex_unlock(&probe_lock);
510 static struct notifier_block trace_kprobe_module_nb = {
511 .notifier_call = trace_kprobe_module_callback,
512 .priority = 1 /* Invoked after kprobe module callback */
515 /* Convert certain expected symbols into '_' when generating event names */
516 static inline void sanitize_event_name(char *name)
518 while (*name++ != '\0')
519 if (*name == ':' || *name == '.')
523 static int create_trace_kprobe(int argc, char **argv)
528 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
530 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
532 * $retval : fetch return value
533 * $stack : fetch stack address
534 * $stackN : fetch Nth of stack (N:0-)
535 * $comm : fetch current task comm
536 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
537 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
538 * %REG : fetch register REG
539 * Dereferencing memory fetch:
540 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
541 * Alias name of args:
542 * NAME=FETCHARG : set NAME as alias of FETCHARG.
544 * FETCHARG:TYPE : use TYPE instead of unsigned long.
546 struct trace_kprobe *tk;
548 bool is_return = false, is_delete = false;
549 char *symbol = NULL, *event = NULL, *group = NULL;
554 char buf[MAX_EVENT_NAME_LEN];
555 unsigned int flags = TPARG_FL_KERNEL;
557 /* argc must be >= 1 */
558 if (argv[0][0] == 'p')
560 else if (argv[0][0] == 'r') {
562 flags |= TPARG_FL_RETURN;
563 } else if (argv[0][0] == '-')
566 pr_info("Probe definition must be started with 'p', 'r' or"
571 event = strchr(&argv[0][1], ':');
576 if (is_return && isdigit(argv[0][1])) {
577 ret = kstrtouint(&argv[0][1], 0, &maxactive);
579 pr_info("Failed to parse maxactive.\n");
582 /* kretprobes instances are iterated over via a list. The
583 * maximum should stay reasonable.
585 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
586 pr_info("Maxactive is too big (%d > %d).\n",
587 maxactive, KRETPROBE_MAXACTIVE_MAX);
595 slash = strchr(event, '/');
600 if (strlen(group) == 0) {
601 pr_info("Group name is not specified\n");
605 if (strlen(event) == 0) {
606 pr_info("Event name is not specified\n");
611 group = KPROBE_EVENT_SYSTEM;
615 pr_info("Delete command needs an event name.\n");
618 mutex_lock(&probe_lock);
619 tk = find_trace_kprobe(event, group);
621 mutex_unlock(&probe_lock);
622 pr_info("Event %s/%s doesn't exist.\n", group, event);
625 /* delete an event */
626 ret = unregister_trace_kprobe(tk);
628 free_trace_kprobe(tk);
629 mutex_unlock(&probe_lock);
634 pr_info("Probe point is not specified.\n");
638 /* try to parse an address. if that fails, try to read the
639 * input as a symbol. */
640 if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
641 /* a symbol specified */
643 /* TODO: support .init module functions */
644 ret = traceprobe_split_symbol_offset(symbol, &offset);
645 if (ret || offset < 0 || offset > UINT_MAX) {
646 pr_info("Failed to parse either an address or a symbol.\n");
649 if (kprobe_on_func_entry(NULL, symbol, offset))
650 flags |= TPARG_FL_FENTRY;
651 if (offset && is_return && !(flags & TPARG_FL_FENTRY)) {
652 pr_info("Given offset is not valid for return probe.\n");
656 argc -= 2; argv += 2;
660 /* Make a new event name */
662 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
663 is_return ? 'r' : 'p', symbol, offset);
665 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
666 is_return ? 'r' : 'p', addr);
667 sanitize_event_name(buf);
670 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
673 pr_info("Failed to allocate trace_probe.(%d)\n",
678 /* parse arguments */
680 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
681 struct probe_arg *parg = &tk->tp.args[i];
683 /* Increment count for freeing args in error case */
686 /* Parse argument name */
687 arg = strchr(argv[i], '=');
690 parg->name = kstrdup(argv[i], GFP_KERNEL);
693 /* If argument name is omitted, set "argN" */
694 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
695 parg->name = kstrdup(buf, GFP_KERNEL);
699 pr_info("Failed to allocate argument[%d] name.\n", i);
704 if (!is_good_name(parg->name)) {
705 pr_info("Invalid argument[%d] name: %s\n",
711 if (traceprobe_conflict_field_name(parg->name,
713 pr_info("Argument[%d] name '%s' conflicts with "
714 "another field.\n", i, argv[i]);
719 /* Parse fetch argument */
720 ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
723 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
728 ret = register_trace_kprobe(tk);
734 free_trace_kprobe(tk);
738 static int release_all_trace_kprobes(void)
740 struct trace_kprobe *tk;
743 mutex_lock(&probe_lock);
744 /* Ensure no probe is in use. */
745 list_for_each_entry(tk, &probe_list, list)
746 if (trace_probe_is_enabled(&tk->tp)) {
750 /* TODO: Use batch unregistration */
751 while (!list_empty(&probe_list)) {
752 tk = list_entry(probe_list.next, struct trace_kprobe, list);
753 ret = unregister_trace_kprobe(tk);
756 free_trace_kprobe(tk);
760 mutex_unlock(&probe_lock);
765 /* Probes listing interfaces */
766 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
768 mutex_lock(&probe_lock);
769 return seq_list_start(&probe_list, *pos);
772 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
774 return seq_list_next(v, &probe_list, pos);
777 static void probes_seq_stop(struct seq_file *m, void *v)
779 mutex_unlock(&probe_lock);
782 static int probes_seq_show(struct seq_file *m, void *v)
784 struct trace_kprobe *tk = v;
787 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
788 seq_printf(m, ":%s/%s", tk->tp.call.class->system,
789 trace_event_name(&tk->tp.call));
792 seq_printf(m, " 0x%p", tk->rp.kp.addr);
793 else if (tk->rp.kp.offset)
794 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
797 seq_printf(m, " %s", trace_kprobe_symbol(tk));
799 for (i = 0; i < tk->tp.nr_args; i++)
800 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
806 static const struct seq_operations probes_seq_op = {
807 .start = probes_seq_start,
808 .next = probes_seq_next,
809 .stop = probes_seq_stop,
810 .show = probes_seq_show
813 static int probes_open(struct inode *inode, struct file *file)
817 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
818 ret = release_all_trace_kprobes();
823 return seq_open(file, &probes_seq_op);
826 static ssize_t probes_write(struct file *file, const char __user *buffer,
827 size_t count, loff_t *ppos)
829 return trace_parse_run_command(file, buffer, count, ppos,
830 create_trace_kprobe);
833 static const struct file_operations kprobe_events_ops = {
834 .owner = THIS_MODULE,
838 .release = seq_release,
839 .write = probes_write,
842 /* Probes profiling interfaces */
843 static int probes_profile_seq_show(struct seq_file *m, void *v)
845 struct trace_kprobe *tk = v;
847 seq_printf(m, " %-44s %15lu %15lu\n",
848 trace_event_name(&tk->tp.call),
849 trace_kprobe_nhit(tk),
855 static const struct seq_operations profile_seq_op = {
856 .start = probes_seq_start,
857 .next = probes_seq_next,
858 .stop = probes_seq_stop,
859 .show = probes_profile_seq_show
862 static int profile_open(struct inode *inode, struct file *file)
864 return seq_open(file, &profile_seq_op);
867 static const struct file_operations kprobe_profile_ops = {
868 .owner = THIS_MODULE,
869 .open = profile_open,
872 .release = seq_release,
875 /* Kprobe specific fetch functions */
877 /* Return the length of string -- including null terminal byte */
878 static nokprobe_inline int
879 fetch_store_strlen(unsigned long addr)
890 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
892 } while (c && ret == 0 && len < MAX_STRING_SIZE);
897 return (ret < 0) ? ret : len;
901 * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
902 * length and relative data location.
904 static nokprobe_inline int
905 fetch_store_string(unsigned long addr, void *dest, void *base)
907 int maxlen = get_loc_len(*(u32 *)dest);
908 u8 *dst = get_loc_data(dest, base);
911 if (unlikely(!maxlen))
914 * Try to get string again, since the string can be changed while
917 ret = strncpy_from_unsafe(dst, (void *)addr, maxlen);
920 *(u32 *)dest = make_data_loc(ret, (void *)dst - base);
924 static nokprobe_inline int
925 probe_mem_read(void *dest, void *src, size_t size)
927 return probe_kernel_read(dest, src, size);
930 /* Note that we don't verify it, since the code does not come from user space */
932 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
938 /* 1st stage: get value from context */
941 val = regs_get_register(regs, code->param);
944 val = regs_get_kernel_stack_nth(regs, code->param);
946 case FETCH_OP_STACKP:
947 val = kernel_stack_pointer(regs);
949 case FETCH_OP_RETVAL:
950 val = regs_return_value(regs);
953 val = code->immediate;
956 val = (unsigned long)current->comm;
958 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
960 val = regs_get_kernel_argument(regs, code->param);
963 case FETCH_NOP_SYMBOL: /* Ignore a place holder */
971 return process_fetch_insn_bottom(code, val, dest, base);
973 NOKPROBE_SYMBOL(process_fetch_insn)
976 static nokprobe_inline void
977 __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
978 struct trace_event_file *trace_file)
980 struct kprobe_trace_entry_head *entry;
981 struct ring_buffer_event *event;
982 struct ring_buffer *buffer;
984 unsigned long irq_flags;
985 struct trace_event_call *call = &tk->tp.call;
987 WARN_ON(call != trace_file->event_call);
989 if (trace_trigger_soft_disabled(trace_file))
992 local_save_flags(irq_flags);
993 pc = preempt_count();
995 dsize = __get_data_size(&tk->tp, regs);
996 size = sizeof(*entry) + tk->tp.size + dsize;
998 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1000 size, irq_flags, pc);
1004 entry = ring_buffer_event_data(event);
1005 entry->ip = (unsigned long)tk->rp.kp.addr;
1006 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1008 event_trigger_unlock_commit_regs(trace_file, buffer, event,
1009 entry, irq_flags, pc, regs);
1013 kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
1015 struct event_file_link *link;
1017 list_for_each_entry_rcu(link, &tk->tp.files, list)
1018 __kprobe_trace_func(tk, regs, link->file);
1020 NOKPROBE_SYMBOL(kprobe_trace_func);
1022 /* Kretprobe handler */
1023 static nokprobe_inline void
1024 __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1025 struct pt_regs *regs,
1026 struct trace_event_file *trace_file)
1028 struct kretprobe_trace_entry_head *entry;
1029 struct ring_buffer_event *event;
1030 struct ring_buffer *buffer;
1031 int size, pc, dsize;
1032 unsigned long irq_flags;
1033 struct trace_event_call *call = &tk->tp.call;
1035 WARN_ON(call != trace_file->event_call);
1037 if (trace_trigger_soft_disabled(trace_file))
1040 local_save_flags(irq_flags);
1041 pc = preempt_count();
1043 dsize = __get_data_size(&tk->tp, regs);
1044 size = sizeof(*entry) + tk->tp.size + dsize;
1046 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1048 size, irq_flags, pc);
1052 entry = ring_buffer_event_data(event);
1053 entry->func = (unsigned long)tk->rp.kp.addr;
1054 entry->ret_ip = (unsigned long)ri->ret_addr;
1055 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1057 event_trigger_unlock_commit_regs(trace_file, buffer, event,
1058 entry, irq_flags, pc, regs);
1062 kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1063 struct pt_regs *regs)
1065 struct event_file_link *link;
1067 list_for_each_entry_rcu(link, &tk->tp.files, list)
1068 __kretprobe_trace_func(tk, ri, regs, link->file);
1070 NOKPROBE_SYMBOL(kretprobe_trace_func);
1072 /* Event entry printers */
1073 static enum print_line_t
1074 print_kprobe_event(struct trace_iterator *iter, int flags,
1075 struct trace_event *event)
1077 struct kprobe_trace_entry_head *field;
1078 struct trace_seq *s = &iter->seq;
1079 struct trace_probe *tp;
1081 field = (struct kprobe_trace_entry_head *)iter->ent;
1082 tp = container_of(event, struct trace_probe, call.event);
1084 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1086 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1089 trace_seq_putc(s, ')');
1091 if (print_probe_args(s, tp->args, tp->nr_args,
1092 (u8 *)&field[1], field) < 0)
1095 trace_seq_putc(s, '\n');
1097 return trace_handle_return(s);
1100 static enum print_line_t
1101 print_kretprobe_event(struct trace_iterator *iter, int flags,
1102 struct trace_event *event)
1104 struct kretprobe_trace_entry_head *field;
1105 struct trace_seq *s = &iter->seq;
1106 struct trace_probe *tp;
1108 field = (struct kretprobe_trace_entry_head *)iter->ent;
1109 tp = container_of(event, struct trace_probe, call.event);
1111 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1113 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1116 trace_seq_puts(s, " <- ");
1118 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1121 trace_seq_putc(s, ')');
1123 if (print_probe_args(s, tp->args, tp->nr_args,
1124 (u8 *)&field[1], field) < 0)
1127 trace_seq_putc(s, '\n');
1130 return trace_handle_return(s);
1134 static int kprobe_event_define_fields(struct trace_event_call *event_call)
1137 struct kprobe_trace_entry_head field;
1138 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1140 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1142 return traceprobe_define_arg_fields(event_call, sizeof(field), &tk->tp);
1145 static int kretprobe_event_define_fields(struct trace_event_call *event_call)
1148 struct kretprobe_trace_entry_head field;
1149 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1151 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1152 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1154 return traceprobe_define_arg_fields(event_call, sizeof(field), &tk->tp);
1157 #ifdef CONFIG_PERF_EVENTS
1159 /* Kprobe profile handler */
1161 kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
1163 struct trace_event_call *call = &tk->tp.call;
1164 struct kprobe_trace_entry_head *entry;
1165 struct hlist_head *head;
1166 int size, __size, dsize;
1169 if (bpf_prog_array_valid(call)) {
1170 unsigned long orig_ip = instruction_pointer(regs);
1173 ret = trace_call_bpf(call, regs);
1176 * We need to check and see if we modified the pc of the
1177 * pt_regs, and if so return 1 so that we don't do the
1180 if (orig_ip != instruction_pointer(regs))
1186 head = this_cpu_ptr(call->perf_events);
1187 if (hlist_empty(head))
1190 dsize = __get_data_size(&tk->tp, regs);
1191 __size = sizeof(*entry) + tk->tp.size + dsize;
1192 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1193 size -= sizeof(u32);
1195 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1199 entry->ip = (unsigned long)tk->rp.kp.addr;
1200 memset(&entry[1], 0, dsize);
1201 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1202 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1206 NOKPROBE_SYMBOL(kprobe_perf_func);
1208 /* Kretprobe profile handler */
1210 kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1211 struct pt_regs *regs)
1213 struct trace_event_call *call = &tk->tp.call;
1214 struct kretprobe_trace_entry_head *entry;
1215 struct hlist_head *head;
1216 int size, __size, dsize;
1219 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1222 head = this_cpu_ptr(call->perf_events);
1223 if (hlist_empty(head))
1226 dsize = __get_data_size(&tk->tp, regs);
1227 __size = sizeof(*entry) + tk->tp.size + dsize;
1228 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1229 size -= sizeof(u32);
1231 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1235 entry->func = (unsigned long)tk->rp.kp.addr;
1236 entry->ret_ip = (unsigned long)ri->ret_addr;
1237 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1238 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1241 NOKPROBE_SYMBOL(kretprobe_perf_func);
1243 int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1244 const char **symbol, u64 *probe_offset,
1245 u64 *probe_addr, bool perf_type_tracepoint)
1247 const char *pevent = trace_event_name(event->tp_event);
1248 const char *group = event->tp_event->class->system;
1249 struct trace_kprobe *tk;
1251 if (perf_type_tracepoint)
1252 tk = find_trace_kprobe(pevent, group);
1254 tk = event->tp_event->data;
1258 *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1259 : BPF_FD_TYPE_KPROBE;
1261 *symbol = tk->symbol;
1262 *probe_offset = tk->rp.kp.offset;
1267 *probe_addr = (unsigned long)tk->rp.kp.addr;
1271 #endif /* CONFIG_PERF_EVENTS */
1274 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1276 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1277 * lockless, but we can't race with this __init function.
1279 static int kprobe_register(struct trace_event_call *event,
1280 enum trace_reg type, void *data)
1282 struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
1283 struct trace_event_file *file = data;
1286 case TRACE_REG_REGISTER:
1287 return enable_trace_kprobe(tk, file);
1288 case TRACE_REG_UNREGISTER:
1289 return disable_trace_kprobe(tk, file);
1291 #ifdef CONFIG_PERF_EVENTS
1292 case TRACE_REG_PERF_REGISTER:
1293 return enable_trace_kprobe(tk, NULL);
1294 case TRACE_REG_PERF_UNREGISTER:
1295 return disable_trace_kprobe(tk, NULL);
1296 case TRACE_REG_PERF_OPEN:
1297 case TRACE_REG_PERF_CLOSE:
1298 case TRACE_REG_PERF_ADD:
1299 case TRACE_REG_PERF_DEL:
1306 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1308 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
1311 raw_cpu_inc(*tk->nhit);
1313 if (tk->tp.flags & TP_FLAG_TRACE)
1314 kprobe_trace_func(tk, regs);
1315 #ifdef CONFIG_PERF_EVENTS
1316 if (tk->tp.flags & TP_FLAG_PROFILE)
1317 ret = kprobe_perf_func(tk, regs);
1321 NOKPROBE_SYMBOL(kprobe_dispatcher);
1324 kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1326 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
1328 raw_cpu_inc(*tk->nhit);
1330 if (tk->tp.flags & TP_FLAG_TRACE)
1331 kretprobe_trace_func(tk, ri, regs);
1332 #ifdef CONFIG_PERF_EVENTS
1333 if (tk->tp.flags & TP_FLAG_PROFILE)
1334 kretprobe_perf_func(tk, ri, regs);
1336 return 0; /* We don't tweek kernel, so just return 0 */
1338 NOKPROBE_SYMBOL(kretprobe_dispatcher);
1340 static struct trace_event_functions kretprobe_funcs = {
1341 .trace = print_kretprobe_event
1344 static struct trace_event_functions kprobe_funcs = {
1345 .trace = print_kprobe_event
1348 static inline void init_trace_event_call(struct trace_kprobe *tk,
1349 struct trace_event_call *call)
1351 INIT_LIST_HEAD(&call->class->fields);
1352 if (trace_kprobe_is_return(tk)) {
1353 call->event.funcs = &kretprobe_funcs;
1354 call->class->define_fields = kretprobe_event_define_fields;
1356 call->event.funcs = &kprobe_funcs;
1357 call->class->define_fields = kprobe_event_define_fields;
1360 call->flags = TRACE_EVENT_FL_KPROBE;
1361 call->class->reg = kprobe_register;
1365 static int register_kprobe_event(struct trace_kprobe *tk)
1367 struct trace_event_call *call = &tk->tp.call;
1370 init_trace_event_call(tk, call);
1372 if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
1374 ret = register_trace_event(&call->event);
1376 kfree(call->print_fmt);
1379 ret = trace_add_event_call(call);
1381 pr_info("Failed to register kprobe event: %s\n",
1382 trace_event_name(call));
1383 kfree(call->print_fmt);
1384 unregister_trace_event(&call->event);
1389 static int unregister_kprobe_event(struct trace_kprobe *tk)
1393 /* tp->event is unregistered in trace_remove_event_call() */
1394 ret = trace_remove_event_call(&tk->tp.call);
1396 kfree(tk->tp.call.print_fmt);
1400 #ifdef CONFIG_PERF_EVENTS
1401 /* create a trace_kprobe, but don't add it to global lists */
1402 struct trace_event_call *
1403 create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1406 struct trace_kprobe *tk;
1411 * local trace_kprobes are not added to probe_list, so they are never
1412 * searched in find_trace_kprobe(). Therefore, there is no concern of
1413 * duplicated name here.
1415 event = func ? func : "DUMMY_EVENT";
1417 tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1418 offs, 0 /* maxactive */, 0 /* nargs */,
1422 pr_info("Failed to allocate trace_probe.(%d)\n",
1424 return ERR_CAST(tk);
1427 init_trace_event_call(tk, &tk->tp.call);
1429 if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
1434 ret = __register_trace_kprobe(tk);
1436 kfree(tk->tp.call.print_fmt);
1440 return &tk->tp.call;
1442 free_trace_kprobe(tk);
1443 return ERR_PTR(ret);
1446 void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1448 struct trace_kprobe *tk;
1450 tk = container_of(event_call, struct trace_kprobe, tp.call);
1452 if (trace_probe_is_enabled(&tk->tp)) {
1457 __unregister_trace_kprobe(tk);
1459 kfree(tk->tp.call.print_fmt);
1460 free_trace_kprobe(tk);
1462 #endif /* CONFIG_PERF_EVENTS */
1464 /* Make a tracefs interface for controlling probe points */
1465 static __init int init_kprobe_trace(void)
1467 struct dentry *d_tracer;
1468 struct dentry *entry;
1470 if (register_module_notifier(&trace_kprobe_module_nb))
1473 d_tracer = tracing_init_dentry();
1474 if (IS_ERR(d_tracer))
1477 entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
1478 NULL, &kprobe_events_ops);
1480 /* Event list interface */
1482 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1484 /* Profile interface */
1485 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
1486 NULL, &kprobe_profile_ops);
1489 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
1492 fs_initcall(init_kprobe_trace);
1495 #ifdef CONFIG_FTRACE_STARTUP_TEST
1496 static __init struct trace_event_file *
1497 find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
1499 struct trace_event_file *file;
1501 list_for_each_entry(file, &tr->events, list)
1502 if (file->event_call == &tk->tp.call)
1509 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
1510 * stage, we can do this lockless.
1512 static __init int kprobe_trace_self_tests_init(void)
1515 int (*target)(int, int, int, int, int, int);
1516 struct trace_kprobe *tk;
1517 struct trace_event_file *file;
1519 if (tracing_is_disabled())
1522 target = kprobe_trace_selftest_target;
1524 pr_info("Testing kprobe tracing: ");
1526 ret = trace_run_command("p:testprobe kprobe_trace_selftest_target "
1527 "$stack $stack0 +0($stack)",
1528 create_trace_kprobe);
1529 if (WARN_ON_ONCE(ret)) {
1530 pr_warn("error on probing function entry.\n");
1533 /* Enable trace point */
1534 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1535 if (WARN_ON_ONCE(tk == NULL)) {
1536 pr_warn("error on getting new probe.\n");
1539 file = find_trace_probe_file(tk, top_trace_array());
1540 if (WARN_ON_ONCE(file == NULL)) {
1541 pr_warn("error on getting probe file.\n");
1544 enable_trace_kprobe(tk, file);
1548 ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target "
1549 "$retval", create_trace_kprobe);
1550 if (WARN_ON_ONCE(ret)) {
1551 pr_warn("error on probing function return.\n");
1554 /* Enable trace point */
1555 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1556 if (WARN_ON_ONCE(tk == NULL)) {
1557 pr_warn("error on getting 2nd new probe.\n");
1560 file = find_trace_probe_file(tk, top_trace_array());
1561 if (WARN_ON_ONCE(file == NULL)) {
1562 pr_warn("error on getting probe file.\n");
1565 enable_trace_kprobe(tk, file);
1572 ret = target(1, 2, 3, 4, 5, 6);
1575 * Not expecting an error here, the check is only to prevent the
1576 * optimizer from removing the call to target() as otherwise there
1577 * are no side-effects and the call is never performed.
1582 /* Disable trace points before removing it */
1583 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1584 if (WARN_ON_ONCE(tk == NULL)) {
1585 pr_warn("error on getting test probe.\n");
1588 if (trace_kprobe_nhit(tk) != 1) {
1589 pr_warn("incorrect number of testprobe hits\n");
1593 file = find_trace_probe_file(tk, top_trace_array());
1594 if (WARN_ON_ONCE(file == NULL)) {
1595 pr_warn("error on getting probe file.\n");
1598 disable_trace_kprobe(tk, file);
1601 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1602 if (WARN_ON_ONCE(tk == NULL)) {
1603 pr_warn("error on getting 2nd test probe.\n");
1606 if (trace_kprobe_nhit(tk) != 1) {
1607 pr_warn("incorrect number of testprobe2 hits\n");
1611 file = find_trace_probe_file(tk, top_trace_array());
1612 if (WARN_ON_ONCE(file == NULL)) {
1613 pr_warn("error on getting probe file.\n");
1616 disable_trace_kprobe(tk, file);
1619 ret = trace_run_command("-:testprobe", create_trace_kprobe);
1620 if (WARN_ON_ONCE(ret)) {
1621 pr_warn("error on deleting a probe.\n");
1625 ret = trace_run_command("-:testprobe2", create_trace_kprobe);
1626 if (WARN_ON_ONCE(ret)) {
1627 pr_warn("error on deleting a probe.\n");
1632 release_all_trace_kprobes();
1634 * Wait for the optimizer work to finish. Otherwise it might fiddle
1635 * with probes in already freed __init text.
1637 wait_for_kprobe_optimizer();
1639 pr_cont("NG: Some tests are failed. Please check them.\n");
1645 late_initcall(kprobe_trace_self_tests_init);