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/bpf-cgroup.h>
11 #include <linux/security.h>
12 #include <linux/module.h>
13 #include <linux/uaccess.h>
14 #include <linux/rculist.h>
15 #include <linux/error-injection.h>
17 #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
19 #include "trace_dynevent.h"
20 #include "trace_kprobe_selftest.h"
21 #include "trace_probe.h"
22 #include "trace_probe_tmpl.h"
23 #include "trace_probe_kernel.h"
25 #define KPROBE_EVENT_SYSTEM "kprobes"
26 #define KRETPROBE_MAXACTIVE_MAX 4096
28 /* Kprobe early definition from command line */
29 static char kprobe_boot_events_buf[COMMAND_LINE_SIZE] __initdata;
31 static int __init set_kprobe_boot_events(char *str)
33 strscpy(kprobe_boot_events_buf, str, COMMAND_LINE_SIZE);
34 disable_tracing_selftest("running kprobe events");
38 __setup("kprobe_event=", set_kprobe_boot_events);
40 static int trace_kprobe_create(const char *raw_command);
41 static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev);
42 static int trace_kprobe_release(struct dyn_event *ev);
43 static bool trace_kprobe_is_busy(struct dyn_event *ev);
44 static bool trace_kprobe_match(const char *system, const char *event,
45 int argc, const char **argv, struct dyn_event *ev);
47 static struct dyn_event_operations trace_kprobe_ops = {
48 .create = trace_kprobe_create,
49 .show = trace_kprobe_show,
50 .is_busy = trace_kprobe_is_busy,
51 .free = trace_kprobe_release,
52 .match = trace_kprobe_match,
56 * Kprobe event core functions
59 struct dyn_event devent;
60 struct kretprobe rp; /* Use rp.kp for kprobe use */
61 unsigned long __percpu *nhit;
62 const char *symbol; /* symbol name */
63 struct trace_probe tp;
66 static bool is_trace_kprobe(struct dyn_event *ev)
68 return ev->ops == &trace_kprobe_ops;
71 static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev)
73 return container_of(ev, struct trace_kprobe, devent);
77 * for_each_trace_kprobe - iterate over the trace_kprobe list
78 * @pos: the struct trace_kprobe * for each entry
79 * @dpos: the struct dyn_event * to use as a loop cursor
81 #define for_each_trace_kprobe(pos, dpos) \
82 for_each_dyn_event(dpos) \
83 if (is_trace_kprobe(dpos) && (pos = to_trace_kprobe(dpos)))
85 static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
87 return tk->rp.handler != NULL;
90 static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
92 return tk->symbol ? tk->symbol : "unknown";
95 static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
97 return tk->rp.kp.offset;
100 static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
102 return kprobe_gone(&tk->rp.kp);
105 static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
108 int len = strlen(module_name(mod));
109 const char *name = trace_kprobe_symbol(tk);
111 return strncmp(module_name(mod), name, len) == 0 && name[len] == ':';
114 #ifdef CONFIG_MODULES
115 static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
122 p = strchr(tk->symbol, ':');
126 rcu_read_lock_sched();
127 ret = !!find_module(tk->symbol);
128 rcu_read_unlock_sched();
134 static inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
140 static bool trace_kprobe_is_busy(struct dyn_event *ev)
142 struct trace_kprobe *tk = to_trace_kprobe(ev);
144 return trace_probe_is_enabled(&tk->tp);
147 static bool trace_kprobe_match_command_head(struct trace_kprobe *tk,
148 int argc, const char **argv)
150 char buf[MAX_ARGSTR_LEN + 1];
156 snprintf(buf, sizeof(buf), "0x%p", tk->rp.kp.addr);
157 else if (tk->rp.kp.offset)
158 snprintf(buf, sizeof(buf), "%s+%u",
159 trace_kprobe_symbol(tk), tk->rp.kp.offset);
161 snprintf(buf, sizeof(buf), "%s", trace_kprobe_symbol(tk));
162 if (strcmp(buf, argv[0]))
166 return trace_probe_match_command_args(&tk->tp, argc, argv);
169 static bool trace_kprobe_match(const char *system, const char *event,
170 int argc, const char **argv, struct dyn_event *ev)
172 struct trace_kprobe *tk = to_trace_kprobe(ev);
174 return (event[0] == '\0' ||
175 strcmp(trace_probe_name(&tk->tp), event) == 0) &&
176 (!system || strcmp(trace_probe_group_name(&tk->tp), system) == 0) &&
177 trace_kprobe_match_command_head(tk, argc, argv);
180 static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
182 unsigned long nhit = 0;
185 for_each_possible_cpu(cpu)
186 nhit += *per_cpu_ptr(tk->nhit, cpu);
191 static nokprobe_inline bool trace_kprobe_is_registered(struct trace_kprobe *tk)
193 return !(list_empty(&tk->rp.kp.list) &&
194 hlist_unhashed(&tk->rp.kp.hlist));
197 /* Return 0 if it fails to find the symbol address */
198 static nokprobe_inline
199 unsigned long trace_kprobe_address(struct trace_kprobe *tk)
204 addr = (unsigned long)
205 kallsyms_lookup_name(trace_kprobe_symbol(tk));
207 addr += tk->rp.kp.offset;
209 addr = (unsigned long)tk->rp.kp.addr;
214 static nokprobe_inline struct trace_kprobe *
215 trace_kprobe_primary_from_call(struct trace_event_call *call)
217 struct trace_probe *tp;
219 tp = trace_probe_primary_from_call(call);
220 if (WARN_ON_ONCE(!tp))
223 return container_of(tp, struct trace_kprobe, tp);
226 bool trace_kprobe_on_func_entry(struct trace_event_call *call)
228 struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
230 return tk ? (kprobe_on_func_entry(tk->rp.kp.addr,
231 tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
232 tk->rp.kp.addr ? 0 : tk->rp.kp.offset) == 0) : false;
235 bool trace_kprobe_error_injectable(struct trace_event_call *call)
237 struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
239 return tk ? within_error_injection_list(trace_kprobe_address(tk)) :
243 static int register_kprobe_event(struct trace_kprobe *tk);
244 static int unregister_kprobe_event(struct trace_kprobe *tk);
246 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
247 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
248 struct pt_regs *regs);
250 static void free_trace_kprobe(struct trace_kprobe *tk)
253 trace_probe_cleanup(&tk->tp);
255 free_percpu(tk->nhit);
261 * Allocate new trace_probe and initialize it (including kprobes).
263 static struct trace_kprobe *alloc_trace_kprobe(const char *group,
269 int nargs, bool is_return)
271 struct trace_kprobe *tk;
274 tk = kzalloc(struct_size(tk, tp.args, nargs), GFP_KERNEL);
278 tk->nhit = alloc_percpu(unsigned long);
283 tk->symbol = kstrdup(symbol, GFP_KERNEL);
286 tk->rp.kp.symbol_name = tk->symbol;
287 tk->rp.kp.offset = offs;
289 tk->rp.kp.addr = addr;
292 tk->rp.handler = kretprobe_dispatcher;
294 tk->rp.kp.pre_handler = kprobe_dispatcher;
296 tk->rp.maxactive = maxactive;
297 INIT_HLIST_NODE(&tk->rp.kp.hlist);
298 INIT_LIST_HEAD(&tk->rp.kp.list);
300 ret = trace_probe_init(&tk->tp, event, group, false, nargs);
304 dyn_event_init(&tk->devent, &trace_kprobe_ops);
307 free_trace_kprobe(tk);
311 static struct trace_kprobe *find_trace_kprobe(const char *event,
314 struct dyn_event *pos;
315 struct trace_kprobe *tk;
317 for_each_trace_kprobe(tk, pos)
318 if (strcmp(trace_probe_name(&tk->tp), event) == 0 &&
319 strcmp(trace_probe_group_name(&tk->tp), group) == 0)
324 static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
328 if (trace_kprobe_is_registered(tk) && !trace_kprobe_has_gone(tk)) {
329 if (trace_kprobe_is_return(tk))
330 ret = enable_kretprobe(&tk->rp);
332 ret = enable_kprobe(&tk->rp.kp);
338 static void __disable_trace_kprobe(struct trace_probe *tp)
340 struct trace_kprobe *tk;
342 list_for_each_entry(tk, trace_probe_probe_list(tp), tp.list) {
343 if (!trace_kprobe_is_registered(tk))
345 if (trace_kprobe_is_return(tk))
346 disable_kretprobe(&tk->rp);
348 disable_kprobe(&tk->rp.kp);
354 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
356 static int enable_trace_kprobe(struct trace_event_call *call,
357 struct trace_event_file *file)
359 struct trace_probe *tp;
360 struct trace_kprobe *tk;
364 tp = trace_probe_primary_from_call(call);
365 if (WARN_ON_ONCE(!tp))
367 enabled = trace_probe_is_enabled(tp);
369 /* This also changes "enabled" state */
371 ret = trace_probe_add_file(tp, file);
375 trace_probe_set_flag(tp, TP_FLAG_PROFILE);
380 list_for_each_entry(tk, trace_probe_probe_list(tp), tp.list) {
381 if (trace_kprobe_has_gone(tk))
383 ret = __enable_trace_kprobe(tk);
390 /* Failed to enable one of them. Roll back all */
392 __disable_trace_kprobe(tp);
394 trace_probe_remove_file(tp, file);
396 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
403 * Disable trace_probe
404 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
406 static int disable_trace_kprobe(struct trace_event_call *call,
407 struct trace_event_file *file)
409 struct trace_probe *tp;
411 tp = trace_probe_primary_from_call(call);
412 if (WARN_ON_ONCE(!tp))
416 if (!trace_probe_get_file_link(tp, file))
418 if (!trace_probe_has_single_file(tp))
420 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
422 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
424 if (!trace_probe_is_enabled(tp))
425 __disable_trace_kprobe(tp);
430 * Synchronization is done in below function. For perf event,
431 * file == NULL and perf_trace_event_unreg() calls
432 * tracepoint_synchronize_unregister() to ensure synchronize
433 * event. We don't need to care about it.
435 trace_probe_remove_file(tp, file);
440 #if defined(CONFIG_DYNAMIC_FTRACE) && \
441 !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
442 static bool __within_notrace_func(unsigned long addr)
444 unsigned long offset, size;
446 if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
449 /* Get the entry address of the target function */
453 * Since ftrace_location_range() does inclusive range check, we need
454 * to subtract 1 byte from the end address.
456 return !ftrace_location_range(addr, addr + size - 1);
459 static bool within_notrace_func(struct trace_kprobe *tk)
461 unsigned long addr = trace_kprobe_address(tk);
462 char symname[KSYM_NAME_LEN], *p;
464 if (!__within_notrace_func(addr))
467 /* Check if the address is on a suffixed-symbol */
468 if (!lookup_symbol_name(addr, symname)) {
469 p = strchr(symname, '.');
473 addr = (unsigned long)kprobe_lookup_name(symname, 0);
475 return __within_notrace_func(addr);
481 #define within_notrace_func(tk) (false)
484 /* Internal register function - just handle k*probes and flags */
485 static int __register_trace_kprobe(struct trace_kprobe *tk)
489 ret = security_locked_down(LOCKDOWN_KPROBES);
493 if (trace_kprobe_is_registered(tk))
496 if (within_notrace_func(tk)) {
497 pr_warn("Could not probe notrace function %ps\n",
498 (void *)trace_kprobe_address(tk));
502 for (i = 0; i < tk->tp.nr_args; i++) {
503 ret = traceprobe_update_arg(&tk->tp.args[i]);
508 /* Set/clear disabled flag according to tp->flag */
509 if (trace_probe_is_enabled(&tk->tp))
510 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
512 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
514 if (trace_kprobe_is_return(tk))
515 ret = register_kretprobe(&tk->rp);
517 ret = register_kprobe(&tk->rp.kp);
522 /* Internal unregister function - just handle k*probes and flags */
523 static void __unregister_trace_kprobe(struct trace_kprobe *tk)
525 if (trace_kprobe_is_registered(tk)) {
526 if (trace_kprobe_is_return(tk))
527 unregister_kretprobe(&tk->rp);
529 unregister_kprobe(&tk->rp.kp);
530 /* Cleanup kprobe for reuse and mark it unregistered */
531 INIT_HLIST_NODE(&tk->rp.kp.hlist);
532 INIT_LIST_HEAD(&tk->rp.kp.list);
533 if (tk->rp.kp.symbol_name)
534 tk->rp.kp.addr = NULL;
538 /* Unregister a trace_probe and probe_event */
539 static int unregister_trace_kprobe(struct trace_kprobe *tk)
541 /* If other probes are on the event, just unregister kprobe */
542 if (trace_probe_has_sibling(&tk->tp))
545 /* Enabled event can not be unregistered */
546 if (trace_probe_is_enabled(&tk->tp))
549 /* If there's a reference to the dynamic event */
550 if (trace_event_dyn_busy(trace_probe_event_call(&tk->tp)))
553 /* Will fail if probe is being used by ftrace or perf */
554 if (unregister_kprobe_event(tk))
558 __unregister_trace_kprobe(tk);
559 dyn_event_remove(&tk->devent);
560 trace_probe_unlink(&tk->tp);
565 static bool trace_kprobe_has_same_kprobe(struct trace_kprobe *orig,
566 struct trace_kprobe *comp)
568 struct trace_probe_event *tpe = orig->tp.event;
571 list_for_each_entry(orig, &tpe->probes, tp.list) {
572 if (strcmp(trace_kprobe_symbol(orig),
573 trace_kprobe_symbol(comp)) ||
574 trace_kprobe_offset(orig) != trace_kprobe_offset(comp))
578 * trace_probe_compare_arg_type() ensured that nr_args and
579 * each argument name and type are same. Let's compare comm.
581 for (i = 0; i < orig->tp.nr_args; i++) {
582 if (strcmp(orig->tp.args[i].comm,
583 comp->tp.args[i].comm))
587 if (i == orig->tp.nr_args)
594 static int append_trace_kprobe(struct trace_kprobe *tk, struct trace_kprobe *to)
598 ret = trace_probe_compare_arg_type(&tk->tp, &to->tp);
600 /* Note that argument starts index = 2 */
601 trace_probe_log_set_index(ret + 1);
602 trace_probe_log_err(0, DIFF_ARG_TYPE);
605 if (trace_kprobe_has_same_kprobe(to, tk)) {
606 trace_probe_log_set_index(0);
607 trace_probe_log_err(0, SAME_PROBE);
611 /* Append to existing event */
612 ret = trace_probe_append(&tk->tp, &to->tp);
616 /* Register k*probe */
617 ret = __register_trace_kprobe(tk);
618 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
619 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
624 trace_probe_unlink(&tk->tp);
626 dyn_event_add(&tk->devent, trace_probe_event_call(&tk->tp));
631 /* Register a trace_probe and probe_event */
632 static int register_trace_kprobe(struct trace_kprobe *tk)
634 struct trace_kprobe *old_tk;
637 mutex_lock(&event_mutex);
639 old_tk = find_trace_kprobe(trace_probe_name(&tk->tp),
640 trace_probe_group_name(&tk->tp));
642 if (trace_kprobe_is_return(tk) != trace_kprobe_is_return(old_tk)) {
643 trace_probe_log_set_index(0);
644 trace_probe_log_err(0, DIFF_PROBE_TYPE);
647 ret = append_trace_kprobe(tk, old_tk);
652 /* Register new event */
653 ret = register_kprobe_event(tk);
655 if (ret == -EEXIST) {
656 trace_probe_log_set_index(0);
657 trace_probe_log_err(0, EVENT_EXIST);
659 pr_warn("Failed to register probe event(%d)\n", ret);
663 /* Register k*probe */
664 ret = __register_trace_kprobe(tk);
665 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
666 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
671 unregister_kprobe_event(tk);
673 dyn_event_add(&tk->devent, trace_probe_event_call(&tk->tp));
676 mutex_unlock(&event_mutex);
680 #ifdef CONFIG_MODULES
681 /* Module notifier call back, checking event on the module */
682 static int trace_kprobe_module_callback(struct notifier_block *nb,
683 unsigned long val, void *data)
685 struct module *mod = data;
686 struct dyn_event *pos;
687 struct trace_kprobe *tk;
690 if (val != MODULE_STATE_COMING)
693 /* Update probes on coming module */
694 mutex_lock(&event_mutex);
695 for_each_trace_kprobe(tk, pos) {
696 if (trace_kprobe_within_module(tk, mod)) {
697 /* Don't need to check busy - this should have gone. */
698 __unregister_trace_kprobe(tk);
699 ret = __register_trace_kprobe(tk);
701 pr_warn("Failed to re-register probe %s on %s: %d\n",
702 trace_probe_name(&tk->tp),
703 module_name(mod), ret);
706 mutex_unlock(&event_mutex);
711 static struct notifier_block trace_kprobe_module_nb = {
712 .notifier_call = trace_kprobe_module_callback,
713 .priority = 1 /* Invoked after kprobe module callback */
715 static int trace_kprobe_register_module_notifier(void)
717 return register_module_notifier(&trace_kprobe_module_nb);
720 static int trace_kprobe_register_module_notifier(void)
724 #endif /* CONFIG_MODULES */
726 static int count_symbols(void *data, unsigned long unused)
728 unsigned int *count = data;
735 struct sym_count_ctx {
740 static int count_mod_symbols(void *data, const char *name, unsigned long unused)
742 struct sym_count_ctx *ctx = data;
744 if (strcmp(name, ctx->name) == 0)
750 static unsigned int number_of_same_symbols(char *func_name)
752 struct sym_count_ctx ctx = { .count = 0, .name = func_name };
754 kallsyms_on_each_match_symbol(count_symbols, func_name, &ctx.count);
756 module_kallsyms_on_each_symbol(NULL, count_mod_symbols, &ctx);
761 static int trace_kprobe_entry_handler(struct kretprobe_instance *ri,
762 struct pt_regs *regs);
764 static int __trace_kprobe_create(int argc, const char *argv[])
769 * p[:[GRP/][EVENT]] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
771 * r[MAXACTIVE][:[GRP/][EVENT]] [MOD:]KSYM[+0] [FETCHARGS]
773 * p[:[GRP/][EVENT]] [MOD:]KSYM[+0]%return [FETCHARGS]
776 * $retval : fetch return value
777 * $stack : fetch stack address
778 * $stackN : fetch Nth of stack (N:0-)
779 * $comm : fetch current task comm
780 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
781 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
782 * %REG : fetch register REG
783 * Dereferencing memory fetch:
784 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
785 * Alias name of args:
786 * NAME=FETCHARG : set NAME as alias of FETCHARG.
788 * FETCHARG:TYPE : use TYPE instead of unsigned long.
790 struct trace_kprobe *tk = NULL;
791 int i, len, new_argc = 0, ret = 0;
792 bool is_return = false;
793 char *symbol = NULL, *tmp = NULL;
794 const char **new_argv = NULL;
795 const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
796 enum probe_print_type ptype;
800 char buf[MAX_EVENT_NAME_LEN];
801 char gbuf[MAX_EVENT_NAME_LEN];
802 char abuf[MAX_BTF_ARGS_LEN];
804 struct traceprobe_parse_context ctx = { .flags = TPARG_FL_KERNEL };
806 switch (argv[0][0]) {
818 trace_probe_log_init("trace_kprobe", argc, argv);
820 event = strchr(&argv[0][1], ':');
824 if (isdigit(argv[0][1])) {
826 trace_probe_log_err(1, BAD_MAXACT_TYPE);
830 len = event - &argv[0][1] - 1;
832 len = strlen(&argv[0][1]);
833 if (len > MAX_EVENT_NAME_LEN - 1) {
834 trace_probe_log_err(1, BAD_MAXACT);
837 memcpy(buf, &argv[0][1], len);
839 ret = kstrtouint(buf, 0, &maxactive);
840 if (ret || !maxactive) {
841 trace_probe_log_err(1, BAD_MAXACT);
844 /* kretprobes instances are iterated over via a list. The
845 * maximum should stay reasonable.
847 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
848 trace_probe_log_err(1, MAXACT_TOO_BIG);
853 /* try to parse an address. if that fails, try to read the
854 * input as a symbol. */
855 if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
856 trace_probe_log_set_index(1);
857 /* Check whether uprobe event specified */
858 if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
862 /* a symbol specified */
863 symbol = kstrdup(argv[1], GFP_KERNEL);
867 tmp = strchr(symbol, '%');
869 if (!strcmp(tmp, "%return")) {
873 trace_probe_log_err(tmp - symbol, BAD_ADDR_SUFFIX);
878 /* TODO: support .init module functions */
879 ret = traceprobe_split_symbol_offset(symbol, &offset);
880 if (ret || offset < 0 || offset > UINT_MAX) {
881 trace_probe_log_err(0, BAD_PROBE_ADDR);
885 ctx.flags |= TPARG_FL_RETURN;
886 ret = kprobe_on_func_entry(NULL, symbol, offset);
887 if (ret == 0 && !is_return)
888 ctx.flags |= TPARG_FL_FENTRY;
889 /* Defer the ENOENT case until register kprobe */
890 if (ret == -EINVAL && is_return) {
891 trace_probe_log_err(0, BAD_RETPROBE);
896 if (symbol && !strchr(symbol, ':')) {
899 count = number_of_same_symbols(symbol);
902 * Users should use ADDR to remove the ambiguity of
905 trace_probe_log_err(0, NON_UNIQ_SYMBOL);
906 ret = -EADDRNOTAVAIL;
909 } else if (count == 0) {
911 * We can return ENOENT earlier than when register the
914 trace_probe_log_err(0, BAD_PROBE_ADDR);
921 trace_probe_log_set_index(0);
923 ret = traceprobe_parse_event_name(&event, &group, gbuf,
930 /* Make a new event name */
932 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
933 is_return ? 'r' : 'p', symbol, offset);
935 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
936 is_return ? 'r' : 'p', addr);
937 sanitize_event_name(buf);
941 argc -= 2; argv += 2;
942 ctx.funcname = symbol;
943 new_argv = traceprobe_expand_meta_args(argc, argv, &new_argc,
944 abuf, MAX_BTF_ARGS_LEN, &ctx);
945 if (IS_ERR(new_argv)) {
946 ret = PTR_ERR(new_argv);
955 ret = traceprobe_expand_dentry_args(argc, argv, &dbuf);
960 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
964 /* This must return -ENOMEM, else there is a bug */
965 WARN_ON_ONCE(ret != -ENOMEM);
966 goto out; /* We know tk is not allocated */
969 /* parse arguments */
970 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
971 trace_probe_log_set_index(i + 2);
973 ret = traceprobe_parse_probe_arg(&tk->tp, i, argv[i], &ctx);
975 goto error; /* This can be -ENOMEM */
977 /* entry handler for kretprobe */
978 if (is_return && tk->tp.entry_arg) {
979 tk->rp.entry_handler = trace_kprobe_entry_handler;
980 tk->rp.data_size = traceprobe_get_entry_data_size(&tk->tp);
983 ptype = is_return ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
984 ret = traceprobe_set_print_fmt(&tk->tp, ptype);
988 ret = register_trace_kprobe(tk);
990 trace_probe_log_set_index(1);
992 trace_probe_log_err(0, BAD_INSN_BNDRY);
993 else if (ret == -ENOENT)
994 trace_probe_log_err(0, BAD_PROBE_ADDR);
995 else if (ret != -ENOMEM && ret != -EEXIST)
996 trace_probe_log_err(0, FAIL_REG_PROBE);
1001 traceprobe_finish_parse(&ctx);
1002 trace_probe_log_clear();
1011 free_trace_kprobe(tk);
1015 static int trace_kprobe_create(const char *raw_command)
1017 return trace_probe_create(raw_command, __trace_kprobe_create);
1020 static int create_or_delete_trace_kprobe(const char *raw_command)
1024 if (raw_command[0] == '-')
1025 return dyn_event_release(raw_command, &trace_kprobe_ops);
1027 ret = trace_kprobe_create(raw_command);
1028 return ret == -ECANCELED ? -EINVAL : ret;
1031 static int trace_kprobe_run_command(struct dynevent_cmd *cmd)
1033 return create_or_delete_trace_kprobe(cmd->seq.buffer);
1037 * kprobe_event_cmd_init - Initialize a kprobe event command object
1038 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1039 * @buf: A pointer to the buffer used to build the command
1040 * @maxlen: The length of the buffer passed in @buf
1042 * Initialize a synthetic event command object. Use this before
1043 * calling any of the other kprobe_event functions.
1045 void kprobe_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1047 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_KPROBE,
1048 trace_kprobe_run_command);
1050 EXPORT_SYMBOL_GPL(kprobe_event_cmd_init);
1053 * __kprobe_event_gen_cmd_start - Generate a kprobe event command from arg list
1054 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1055 * @kretprobe: Is this a return probe?
1056 * @name: The name of the kprobe event
1057 * @loc: The location of the kprobe event
1058 * @...: Variable number of arg (pairs), one pair for each field
1060 * NOTE: Users normally won't want to call this function directly, but
1061 * rather use the kprobe_event_gen_cmd_start() wrapper, which automatically
1062 * adds a NULL to the end of the arg list. If this function is used
1063 * directly, make sure the last arg in the variable arg list is NULL.
1065 * Generate a kprobe event command to be executed by
1066 * kprobe_event_gen_cmd_end(). This function can be used to generate the
1067 * complete command or only the first part of it; in the latter case,
1068 * kprobe_event_add_fields() can be used to add more fields following this.
1070 * Unlikely the synth_event_gen_cmd_start(), @loc must be specified. This
1071 * returns -EINVAL if @loc == NULL.
1073 * Return: 0 if successful, error otherwise.
1075 int __kprobe_event_gen_cmd_start(struct dynevent_cmd *cmd, bool kretprobe,
1076 const char *name, const char *loc, ...)
1078 char buf[MAX_EVENT_NAME_LEN];
1079 struct dynevent_arg arg;
1083 if (cmd->type != DYNEVENT_TYPE_KPROBE)
1090 snprintf(buf, MAX_EVENT_NAME_LEN, "r:kprobes/%s", name);
1092 snprintf(buf, MAX_EVENT_NAME_LEN, "p:kprobes/%s", name);
1094 ret = dynevent_str_add(cmd, buf);
1098 dynevent_arg_init(&arg, 0);
1100 ret = dynevent_arg_add(cmd, &arg, NULL);
1104 va_start(args, loc);
1108 field = va_arg(args, const char *);
1112 if (++cmd->n_fields > MAX_TRACE_ARGS) {
1118 ret = dynevent_arg_add(cmd, &arg, NULL);
1126 EXPORT_SYMBOL_GPL(__kprobe_event_gen_cmd_start);
1129 * __kprobe_event_add_fields - Add probe fields to a kprobe command from arg list
1130 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1131 * @...: Variable number of arg (pairs), one pair for each field
1133 * NOTE: Users normally won't want to call this function directly, but
1134 * rather use the kprobe_event_add_fields() wrapper, which
1135 * automatically adds a NULL to the end of the arg list. If this
1136 * function is used directly, make sure the last arg in the variable
1139 * Add probe fields to an existing kprobe command using a variable
1140 * list of args. Fields are added in the same order they're listed.
1142 * Return: 0 if successful, error otherwise.
1144 int __kprobe_event_add_fields(struct dynevent_cmd *cmd, ...)
1146 struct dynevent_arg arg;
1150 if (cmd->type != DYNEVENT_TYPE_KPROBE)
1153 dynevent_arg_init(&arg, 0);
1155 va_start(args, cmd);
1159 field = va_arg(args, const char *);
1163 if (++cmd->n_fields > MAX_TRACE_ARGS) {
1169 ret = dynevent_arg_add(cmd, &arg, NULL);
1177 EXPORT_SYMBOL_GPL(__kprobe_event_add_fields);
1180 * kprobe_event_delete - Delete a kprobe event
1181 * @name: The name of the kprobe event to delete
1183 * Delete a kprobe event with the give @name from kernel code rather
1184 * than directly from the command line.
1186 * Return: 0 if successful, error otherwise.
1188 int kprobe_event_delete(const char *name)
1190 char buf[MAX_EVENT_NAME_LEN];
1192 snprintf(buf, MAX_EVENT_NAME_LEN, "-:%s", name);
1194 return create_or_delete_trace_kprobe(buf);
1196 EXPORT_SYMBOL_GPL(kprobe_event_delete);
1198 static int trace_kprobe_release(struct dyn_event *ev)
1200 struct trace_kprobe *tk = to_trace_kprobe(ev);
1201 int ret = unregister_trace_kprobe(tk);
1204 free_trace_kprobe(tk);
1208 static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)
1210 struct trace_kprobe *tk = to_trace_kprobe(ev);
1213 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
1214 if (trace_kprobe_is_return(tk) && tk->rp.maxactive)
1215 seq_printf(m, "%d", tk->rp.maxactive);
1216 seq_printf(m, ":%s/%s", trace_probe_group_name(&tk->tp),
1217 trace_probe_name(&tk->tp));
1220 seq_printf(m, " 0x%p", tk->rp.kp.addr);
1221 else if (tk->rp.kp.offset)
1222 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
1225 seq_printf(m, " %s", trace_kprobe_symbol(tk));
1227 for (i = 0; i < tk->tp.nr_args; i++)
1228 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
1234 static int probes_seq_show(struct seq_file *m, void *v)
1236 struct dyn_event *ev = v;
1238 if (!is_trace_kprobe(ev))
1241 return trace_kprobe_show(m, ev);
1244 static const struct seq_operations probes_seq_op = {
1245 .start = dyn_event_seq_start,
1246 .next = dyn_event_seq_next,
1247 .stop = dyn_event_seq_stop,
1248 .show = probes_seq_show
1251 static int probes_open(struct inode *inode, struct file *file)
1255 ret = security_locked_down(LOCKDOWN_TRACEFS);
1259 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
1260 ret = dyn_events_release_all(&trace_kprobe_ops);
1265 return seq_open(file, &probes_seq_op);
1268 static ssize_t probes_write(struct file *file, const char __user *buffer,
1269 size_t count, loff_t *ppos)
1271 return trace_parse_run_command(file, buffer, count, ppos,
1272 create_or_delete_trace_kprobe);
1275 static const struct file_operations kprobe_events_ops = {
1276 .owner = THIS_MODULE,
1277 .open = probes_open,
1279 .llseek = seq_lseek,
1280 .release = seq_release,
1281 .write = probes_write,
1284 static unsigned long trace_kprobe_missed(struct trace_kprobe *tk)
1286 return trace_kprobe_is_return(tk) ?
1287 tk->rp.kp.nmissed + tk->rp.nmissed : tk->rp.kp.nmissed;
1290 /* Probes profiling interfaces */
1291 static int probes_profile_seq_show(struct seq_file *m, void *v)
1293 struct dyn_event *ev = v;
1294 struct trace_kprobe *tk;
1295 unsigned long nmissed;
1297 if (!is_trace_kprobe(ev))
1300 tk = to_trace_kprobe(ev);
1301 nmissed = trace_kprobe_missed(tk);
1302 seq_printf(m, " %-44s %15lu %15lu\n",
1303 trace_probe_name(&tk->tp),
1304 trace_kprobe_nhit(tk),
1310 static const struct seq_operations profile_seq_op = {
1311 .start = dyn_event_seq_start,
1312 .next = dyn_event_seq_next,
1313 .stop = dyn_event_seq_stop,
1314 .show = probes_profile_seq_show
1317 static int profile_open(struct inode *inode, struct file *file)
1321 ret = security_locked_down(LOCKDOWN_TRACEFS);
1325 return seq_open(file, &profile_seq_op);
1328 static const struct file_operations kprobe_profile_ops = {
1329 .owner = THIS_MODULE,
1330 .open = profile_open,
1332 .llseek = seq_lseek,
1333 .release = seq_release,
1336 /* Note that we don't verify it, since the code does not come from user space */
1338 process_fetch_insn(struct fetch_insn *code, void *rec, void *edata,
1339 void *dest, void *base)
1341 struct pt_regs *regs = rec;
1346 /* 1st stage: get value from context */
1349 val = regs_get_register(regs, code->param);
1351 case FETCH_OP_STACK:
1352 val = regs_get_kernel_stack_nth(regs, code->param);
1354 case FETCH_OP_STACKP:
1355 val = kernel_stack_pointer(regs);
1357 case FETCH_OP_RETVAL:
1358 val = regs_return_value(regs);
1360 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
1362 val = regs_get_kernel_argument(regs, code->param);
1364 case FETCH_OP_EDATA:
1365 val = *(unsigned long *)((unsigned long)edata + code->offset);
1368 case FETCH_NOP_SYMBOL: /* Ignore a place holder */
1372 ret = process_common_fetch_insn(code, &val);
1378 return process_fetch_insn_bottom(code, val, dest, base);
1380 NOKPROBE_SYMBOL(process_fetch_insn)
1382 /* Kprobe handler */
1383 static nokprobe_inline void
1384 __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
1385 struct trace_event_file *trace_file)
1387 struct kprobe_trace_entry_head *entry;
1388 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1389 struct trace_event_buffer fbuffer;
1392 WARN_ON(call != trace_file->event_call);
1394 if (trace_trigger_soft_disabled(trace_file))
1397 dsize = __get_data_size(&tk->tp, regs, NULL);
1399 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
1400 sizeof(*entry) + tk->tp.size + dsize);
1404 fbuffer.regs = regs;
1405 entry->ip = (unsigned long)tk->rp.kp.addr;
1406 store_trace_args(&entry[1], &tk->tp, regs, NULL, sizeof(*entry), dsize);
1408 trace_event_buffer_commit(&fbuffer);
1412 kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
1414 struct event_file_link *link;
1416 trace_probe_for_each_link_rcu(link, &tk->tp)
1417 __kprobe_trace_func(tk, regs, link->file);
1419 NOKPROBE_SYMBOL(kprobe_trace_func);
1421 /* Kretprobe handler */
1423 static int trace_kprobe_entry_handler(struct kretprobe_instance *ri,
1424 struct pt_regs *regs)
1426 struct kretprobe *rp = get_kretprobe(ri);
1427 struct trace_kprobe *tk;
1430 * There is a small chance that get_kretprobe(ri) returns NULL when
1431 * the kretprobe is unregister on another CPU between kretprobe's
1432 * trampoline_handler and this function.
1437 tk = container_of(rp, struct trace_kprobe, rp);
1439 /* store argument values into ri->data as entry data */
1440 if (tk->tp.entry_arg)
1441 store_trace_entry_data(ri->data, &tk->tp, regs);
1447 static nokprobe_inline void
1448 __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1449 struct pt_regs *regs,
1450 struct trace_event_file *trace_file)
1452 struct kretprobe_trace_entry_head *entry;
1453 struct trace_event_buffer fbuffer;
1454 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1457 WARN_ON(call != trace_file->event_call);
1459 if (trace_trigger_soft_disabled(trace_file))
1462 dsize = __get_data_size(&tk->tp, regs, ri->data);
1464 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
1465 sizeof(*entry) + tk->tp.size + dsize);
1469 fbuffer.regs = regs;
1470 entry->func = (unsigned long)tk->rp.kp.addr;
1471 entry->ret_ip = get_kretprobe_retaddr(ri);
1472 store_trace_args(&entry[1], &tk->tp, regs, ri->data, sizeof(*entry), dsize);
1474 trace_event_buffer_commit(&fbuffer);
1478 kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1479 struct pt_regs *regs)
1481 struct event_file_link *link;
1483 trace_probe_for_each_link_rcu(link, &tk->tp)
1484 __kretprobe_trace_func(tk, ri, regs, link->file);
1486 NOKPROBE_SYMBOL(kretprobe_trace_func);
1488 /* Event entry printers */
1489 static enum print_line_t
1490 print_kprobe_event(struct trace_iterator *iter, int flags,
1491 struct trace_event *event)
1493 struct kprobe_trace_entry_head *field;
1494 struct trace_seq *s = &iter->seq;
1495 struct trace_probe *tp;
1497 field = (struct kprobe_trace_entry_head *)iter->ent;
1498 tp = trace_probe_primary_from_call(
1499 container_of(event, struct trace_event_call, event));
1500 if (WARN_ON_ONCE(!tp))
1503 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
1505 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1508 trace_seq_putc(s, ')');
1510 if (trace_probe_print_args(s, tp->args, tp->nr_args,
1511 (u8 *)&field[1], field) < 0)
1514 trace_seq_putc(s, '\n');
1516 return trace_handle_return(s);
1519 static enum print_line_t
1520 print_kretprobe_event(struct trace_iterator *iter, int flags,
1521 struct trace_event *event)
1523 struct kretprobe_trace_entry_head *field;
1524 struct trace_seq *s = &iter->seq;
1525 struct trace_probe *tp;
1527 field = (struct kretprobe_trace_entry_head *)iter->ent;
1528 tp = trace_probe_primary_from_call(
1529 container_of(event, struct trace_event_call, event));
1530 if (WARN_ON_ONCE(!tp))
1533 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
1535 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1538 trace_seq_puts(s, " <- ");
1540 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1543 trace_seq_putc(s, ')');
1545 if (trace_probe_print_args(s, tp->args, tp->nr_args,
1546 (u8 *)&field[1], field) < 0)
1549 trace_seq_putc(s, '\n');
1552 return trace_handle_return(s);
1556 static int kprobe_event_define_fields(struct trace_event_call *event_call)
1559 struct kprobe_trace_entry_head field;
1560 struct trace_probe *tp;
1562 tp = trace_probe_primary_from_call(event_call);
1563 if (WARN_ON_ONCE(!tp))
1566 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1568 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
1571 static int kretprobe_event_define_fields(struct trace_event_call *event_call)
1574 struct kretprobe_trace_entry_head field;
1575 struct trace_probe *tp;
1577 tp = trace_probe_primary_from_call(event_call);
1578 if (WARN_ON_ONCE(!tp))
1581 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1582 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1584 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
1587 #ifdef CONFIG_PERF_EVENTS
1589 /* Kprobe profile handler */
1591 kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
1593 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1594 struct kprobe_trace_entry_head *entry;
1595 struct hlist_head *head;
1596 int size, __size, dsize;
1599 if (bpf_prog_array_valid(call)) {
1600 unsigned long orig_ip = instruction_pointer(regs);
1603 ret = trace_call_bpf(call, regs);
1606 * We need to check and see if we modified the pc of the
1607 * pt_regs, and if so return 1 so that we don't do the
1610 if (orig_ip != instruction_pointer(regs))
1616 head = this_cpu_ptr(call->perf_events);
1617 if (hlist_empty(head))
1620 dsize = __get_data_size(&tk->tp, regs, NULL);
1621 __size = sizeof(*entry) + tk->tp.size + dsize;
1622 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1623 size -= sizeof(u32);
1625 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1629 entry->ip = (unsigned long)tk->rp.kp.addr;
1630 memset(&entry[1], 0, dsize);
1631 store_trace_args(&entry[1], &tk->tp, regs, NULL, sizeof(*entry), dsize);
1632 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1636 NOKPROBE_SYMBOL(kprobe_perf_func);
1638 /* Kretprobe profile handler */
1640 kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1641 struct pt_regs *regs)
1643 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1644 struct kretprobe_trace_entry_head *entry;
1645 struct hlist_head *head;
1646 int size, __size, dsize;
1649 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1652 head = this_cpu_ptr(call->perf_events);
1653 if (hlist_empty(head))
1656 dsize = __get_data_size(&tk->tp, regs, ri->data);
1657 __size = sizeof(*entry) + tk->tp.size + dsize;
1658 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1659 size -= sizeof(u32);
1661 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1665 entry->func = (unsigned long)tk->rp.kp.addr;
1666 entry->ret_ip = get_kretprobe_retaddr(ri);
1667 store_trace_args(&entry[1], &tk->tp, regs, ri->data, sizeof(*entry), dsize);
1668 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1671 NOKPROBE_SYMBOL(kretprobe_perf_func);
1673 int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1674 const char **symbol, u64 *probe_offset,
1675 u64 *probe_addr, unsigned long *missed,
1676 bool perf_type_tracepoint)
1678 const char *pevent = trace_event_name(event->tp_event);
1679 const char *group = event->tp_event->class->system;
1680 struct trace_kprobe *tk;
1682 if (perf_type_tracepoint)
1683 tk = find_trace_kprobe(pevent, group);
1685 tk = trace_kprobe_primary_from_call(event->tp_event);
1689 *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1690 : BPF_FD_TYPE_KPROBE;
1691 *probe_offset = tk->rp.kp.offset;
1692 *probe_addr = kallsyms_show_value(current_cred()) ?
1693 (unsigned long)tk->rp.kp.addr : 0;
1694 *symbol = tk->symbol;
1696 *missed = trace_kprobe_missed(tk);
1699 #endif /* CONFIG_PERF_EVENTS */
1702 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1704 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1705 * lockless, but we can't race with this __init function.
1707 static int kprobe_register(struct trace_event_call *event,
1708 enum trace_reg type, void *data)
1710 struct trace_event_file *file = data;
1713 case TRACE_REG_REGISTER:
1714 return enable_trace_kprobe(event, file);
1715 case TRACE_REG_UNREGISTER:
1716 return disable_trace_kprobe(event, file);
1718 #ifdef CONFIG_PERF_EVENTS
1719 case TRACE_REG_PERF_REGISTER:
1720 return enable_trace_kprobe(event, NULL);
1721 case TRACE_REG_PERF_UNREGISTER:
1722 return disable_trace_kprobe(event, NULL);
1723 case TRACE_REG_PERF_OPEN:
1724 case TRACE_REG_PERF_CLOSE:
1725 case TRACE_REG_PERF_ADD:
1726 case TRACE_REG_PERF_DEL:
1733 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1735 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
1738 raw_cpu_inc(*tk->nhit);
1740 if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
1741 kprobe_trace_func(tk, regs);
1742 #ifdef CONFIG_PERF_EVENTS
1743 if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
1744 ret = kprobe_perf_func(tk, regs);
1748 NOKPROBE_SYMBOL(kprobe_dispatcher);
1751 kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1753 struct kretprobe *rp = get_kretprobe(ri);
1754 struct trace_kprobe *tk;
1757 * There is a small chance that get_kretprobe(ri) returns NULL when
1758 * the kretprobe is unregister on another CPU between kretprobe's
1759 * trampoline_handler and this function.
1764 tk = container_of(rp, struct trace_kprobe, rp);
1765 raw_cpu_inc(*tk->nhit);
1767 if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
1768 kretprobe_trace_func(tk, ri, regs);
1769 #ifdef CONFIG_PERF_EVENTS
1770 if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
1771 kretprobe_perf_func(tk, ri, regs);
1773 return 0; /* We don't tweak kernel, so just return 0 */
1775 NOKPROBE_SYMBOL(kretprobe_dispatcher);
1777 static struct trace_event_functions kretprobe_funcs = {
1778 .trace = print_kretprobe_event
1781 static struct trace_event_functions kprobe_funcs = {
1782 .trace = print_kprobe_event
1785 static struct trace_event_fields kretprobe_fields_array[] = {
1786 { .type = TRACE_FUNCTION_TYPE,
1787 .define_fields = kretprobe_event_define_fields },
1791 static struct trace_event_fields kprobe_fields_array[] = {
1792 { .type = TRACE_FUNCTION_TYPE,
1793 .define_fields = kprobe_event_define_fields },
1797 static inline void init_trace_event_call(struct trace_kprobe *tk)
1799 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1801 if (trace_kprobe_is_return(tk)) {
1802 call->event.funcs = &kretprobe_funcs;
1803 call->class->fields_array = kretprobe_fields_array;
1805 call->event.funcs = &kprobe_funcs;
1806 call->class->fields_array = kprobe_fields_array;
1809 call->flags = TRACE_EVENT_FL_KPROBE;
1810 call->class->reg = kprobe_register;
1813 static int register_kprobe_event(struct trace_kprobe *tk)
1815 init_trace_event_call(tk);
1817 return trace_probe_register_event_call(&tk->tp);
1820 static int unregister_kprobe_event(struct trace_kprobe *tk)
1822 return trace_probe_unregister_event_call(&tk->tp);
1825 #ifdef CONFIG_PERF_EVENTS
1827 /* create a trace_kprobe, but don't add it to global lists */
1828 struct trace_event_call *
1829 create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1832 enum probe_print_type ptype;
1833 struct trace_kprobe *tk;
1840 count = number_of_same_symbols(func);
1843 * Users should use addr to remove the ambiguity of
1846 return ERR_PTR(-EADDRNOTAVAIL);
1847 else if (count == 0)
1849 * We can return ENOENT earlier than when register the
1852 return ERR_PTR(-ENOENT);
1856 * local trace_kprobes are not added to dyn_event, so they are never
1857 * searched in find_trace_kprobe(). Therefore, there is no concern of
1858 * duplicated name here.
1860 event = func ? func : "DUMMY_EVENT";
1862 tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1863 offs, 0 /* maxactive */, 0 /* nargs */,
1867 pr_info("Failed to allocate trace_probe.(%d)\n",
1869 return ERR_CAST(tk);
1872 init_trace_event_call(tk);
1874 ptype = trace_kprobe_is_return(tk) ?
1875 PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
1876 if (traceprobe_set_print_fmt(&tk->tp, ptype) < 0) {
1881 ret = __register_trace_kprobe(tk);
1885 return trace_probe_event_call(&tk->tp);
1887 free_trace_kprobe(tk);
1888 return ERR_PTR(ret);
1891 void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1893 struct trace_kprobe *tk;
1895 tk = trace_kprobe_primary_from_call(event_call);
1899 if (trace_probe_is_enabled(&tk->tp)) {
1904 __unregister_trace_kprobe(tk);
1906 free_trace_kprobe(tk);
1908 #endif /* CONFIG_PERF_EVENTS */
1910 static __init void enable_boot_kprobe_events(void)
1912 struct trace_array *tr = top_trace_array();
1913 struct trace_event_file *file;
1914 struct trace_kprobe *tk;
1915 struct dyn_event *pos;
1917 mutex_lock(&event_mutex);
1918 for_each_trace_kprobe(tk, pos) {
1919 list_for_each_entry(file, &tr->events, list)
1920 if (file->event_call == trace_probe_event_call(&tk->tp))
1921 trace_event_enable_disable(file, 1, 0);
1923 mutex_unlock(&event_mutex);
1926 static __init void setup_boot_kprobe_events(void)
1928 char *p, *cmd = kprobe_boot_events_buf;
1931 strreplace(kprobe_boot_events_buf, ',', ' ');
1933 while (cmd && *cmd != '\0') {
1934 p = strchr(cmd, ';');
1938 ret = create_or_delete_trace_kprobe(cmd);
1940 pr_warn("Failed to add event(%d): %s\n", ret, cmd);
1945 enable_boot_kprobe_events();
1949 * Register dynevent at core_initcall. This allows kernel to setup kprobe
1950 * events in postcore_initcall without tracefs.
1952 static __init int init_kprobe_trace_early(void)
1956 ret = dyn_event_register(&trace_kprobe_ops);
1960 if (trace_kprobe_register_module_notifier())
1965 core_initcall(init_kprobe_trace_early);
1967 /* Make a tracefs interface for controlling probe points */
1968 static __init int init_kprobe_trace(void)
1972 ret = tracing_init_dentry();
1976 /* Event list interface */
1977 trace_create_file("kprobe_events", TRACE_MODE_WRITE,
1978 NULL, NULL, &kprobe_events_ops);
1980 /* Profile interface */
1981 trace_create_file("kprobe_profile", TRACE_MODE_READ,
1982 NULL, NULL, &kprobe_profile_ops);
1984 setup_boot_kprobe_events();
1988 fs_initcall(init_kprobe_trace);
1991 #ifdef CONFIG_FTRACE_STARTUP_TEST
1992 static __init struct trace_event_file *
1993 find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
1995 struct trace_event_file *file;
1997 list_for_each_entry(file, &tr->events, list)
1998 if (file->event_call == trace_probe_event_call(&tk->tp))
2005 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
2006 * stage, we can do this lockless.
2008 static __init int kprobe_trace_self_tests_init(void)
2011 int (*target)(int, int, int, int, int, int);
2012 struct trace_kprobe *tk;
2013 struct trace_event_file *file;
2015 if (tracing_is_disabled())
2018 if (tracing_selftest_disabled)
2021 target = kprobe_trace_selftest_target;
2023 pr_info("Testing kprobe tracing: ");
2025 ret = create_or_delete_trace_kprobe("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)");
2026 if (WARN_ON_ONCE(ret)) {
2027 pr_warn("error on probing function entry.\n");
2030 /* Enable trace point */
2031 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
2032 if (WARN_ON_ONCE(tk == NULL)) {
2033 pr_warn("error on getting new probe.\n");
2036 file = find_trace_probe_file(tk, top_trace_array());
2037 if (WARN_ON_ONCE(file == NULL)) {
2038 pr_warn("error on getting probe file.\n");
2041 enable_trace_kprobe(
2042 trace_probe_event_call(&tk->tp), file);
2046 ret = create_or_delete_trace_kprobe("r:testprobe2 kprobe_trace_selftest_target $retval");
2047 if (WARN_ON_ONCE(ret)) {
2048 pr_warn("error on probing function return.\n");
2051 /* Enable trace point */
2052 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
2053 if (WARN_ON_ONCE(tk == NULL)) {
2054 pr_warn("error on getting 2nd new probe.\n");
2057 file = find_trace_probe_file(tk, top_trace_array());
2058 if (WARN_ON_ONCE(file == NULL)) {
2059 pr_warn("error on getting probe file.\n");
2062 enable_trace_kprobe(
2063 trace_probe_event_call(&tk->tp), file);
2070 ret = target(1, 2, 3, 4, 5, 6);
2073 * Not expecting an error here, the check is only to prevent the
2074 * optimizer from removing the call to target() as otherwise there
2075 * are no side-effects and the call is never performed.
2080 /* Disable trace points before removing it */
2081 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
2082 if (WARN_ON_ONCE(tk == NULL)) {
2083 pr_warn("error on getting test probe.\n");
2086 if (trace_kprobe_nhit(tk) != 1) {
2087 pr_warn("incorrect number of testprobe hits\n");
2091 file = find_trace_probe_file(tk, top_trace_array());
2092 if (WARN_ON_ONCE(file == NULL)) {
2093 pr_warn("error on getting probe file.\n");
2096 disable_trace_kprobe(
2097 trace_probe_event_call(&tk->tp), file);
2100 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
2101 if (WARN_ON_ONCE(tk == NULL)) {
2102 pr_warn("error on getting 2nd test probe.\n");
2105 if (trace_kprobe_nhit(tk) != 1) {
2106 pr_warn("incorrect number of testprobe2 hits\n");
2110 file = find_trace_probe_file(tk, top_trace_array());
2111 if (WARN_ON_ONCE(file == NULL)) {
2112 pr_warn("error on getting probe file.\n");
2115 disable_trace_kprobe(
2116 trace_probe_event_call(&tk->tp), file);
2119 ret = create_or_delete_trace_kprobe("-:testprobe");
2120 if (WARN_ON_ONCE(ret)) {
2121 pr_warn("error on deleting a probe.\n");
2125 ret = create_or_delete_trace_kprobe("-:testprobe2");
2126 if (WARN_ON_ONCE(ret)) {
2127 pr_warn("error on deleting a probe.\n");
2132 ret = dyn_events_release_all(&trace_kprobe_ops);
2133 if (WARN_ON_ONCE(ret)) {
2134 pr_warn("error on cleaning up probes.\n");
2138 * Wait for the optimizer work to finish. Otherwise it might fiddle
2139 * with probes in already freed __init text.
2141 wait_for_kprobe_optimizer();
2143 pr_cont("NG: Some tests are failed. Please check them.\n");
2149 late_initcall(kprobe_trace_self_tests_init);