2 * Kprobes-based tracing events
4 * Created by Masami Hiramatsu <mhiramat@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define pr_fmt(fmt) "trace_kprobe: " fmt
21 #include <linux/module.h>
22 #include <linux/uaccess.h>
23 #include <linux/rculist.h>
25 #include "trace_probe.h"
27 #define KPROBE_EVENT_SYSTEM "kprobes"
30 * Kprobe event core functions
33 struct list_head list;
34 struct kretprobe rp; /* Use rp.kp for kprobe use */
35 unsigned long __percpu *nhit;
36 const char *symbol; /* symbol name */
37 struct trace_probe tp;
40 #define SIZEOF_TRACE_KPROBE(n) \
41 (offsetof(struct trace_kprobe, tp.args) + \
42 (sizeof(struct probe_arg) * (n)))
45 static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
47 return tk->rp.handler != NULL;
50 static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
52 return tk->symbol ? tk->symbol : "unknown";
55 static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
57 return tk->rp.kp.offset;
60 static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
62 return !!(kprobe_gone(&tk->rp.kp));
65 static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
68 int len = strlen(mod->name);
69 const char *name = trace_kprobe_symbol(tk);
70 return strncmp(mod->name, name, len) == 0 && name[len] == ':';
73 static nokprobe_inline bool trace_kprobe_is_on_module(struct trace_kprobe *tk)
75 return !!strchr(trace_kprobe_symbol(tk), ':');
78 static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
80 unsigned long nhit = 0;
83 for_each_possible_cpu(cpu)
84 nhit += *per_cpu_ptr(tk->nhit, cpu);
89 static int register_kprobe_event(struct trace_kprobe *tk);
90 static int unregister_kprobe_event(struct trace_kprobe *tk);
92 static DEFINE_MUTEX(probe_lock);
93 static LIST_HEAD(probe_list);
95 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
96 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
97 struct pt_regs *regs);
99 /* Memory fetching by symbol */
100 struct symbol_cache {
106 unsigned long update_symbol_cache(struct symbol_cache *sc)
108 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
111 sc->addr += sc->offset;
116 void free_symbol_cache(struct symbol_cache *sc)
122 struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
124 struct symbol_cache *sc;
126 if (!sym || strlen(sym) == 0)
129 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
133 sc->symbol = kstrdup(sym, GFP_KERNEL);
139 update_symbol_cache(sc);
145 * Kprobes-specific fetch functions
147 #define DEFINE_FETCH_stack(type) \
148 static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
149 void *offset, void *dest) \
151 *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
152 (unsigned int)((unsigned long)offset)); \
154 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(stack, type));
156 DEFINE_BASIC_FETCH_FUNCS(stack)
157 /* No string on the stack entry */
158 #define fetch_stack_string NULL
159 #define fetch_stack_string_size NULL
161 #define DEFINE_FETCH_memory(type) \
162 static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
163 void *addr, void *dest) \
166 if (probe_kernel_address(addr, retval)) \
169 *(type *)dest = retval; \
171 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, type));
173 DEFINE_BASIC_FETCH_FUNCS(memory)
175 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
176 * length and relative data location.
178 static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
179 void *addr, void *dest)
181 int maxlen = get_rloc_len(*(u32 *)dest);
182 u8 *dst = get_rloc_data(dest);
189 * Try to get string again, since the string can be changed while
192 ret = strncpy_from_unsafe(dst, addr, maxlen);
194 if (ret < 0) { /* Failed to fetch string */
196 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
198 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest));
201 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string));
203 /* Return the length of string -- including null terminal byte */
204 static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
205 void *addr, void *dest)
216 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
218 } while (c && ret == 0 && len < MAX_STRING_SIZE);
223 if (ret < 0) /* Failed to check the length */
228 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string_size));
230 #define DEFINE_FETCH_symbol(type) \
231 void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, void *data, void *dest)\
233 struct symbol_cache *sc = data; \
235 fetch_memory_##type(regs, (void *)sc->addr, dest); \
239 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(symbol, type));
241 DEFINE_BASIC_FETCH_FUNCS(symbol)
242 DEFINE_FETCH_symbol(string)
243 DEFINE_FETCH_symbol(string_size)
245 /* kprobes don't support file_offset fetch methods */
246 #define fetch_file_offset_u8 NULL
247 #define fetch_file_offset_u16 NULL
248 #define fetch_file_offset_u32 NULL
249 #define fetch_file_offset_u64 NULL
250 #define fetch_file_offset_string NULL
251 #define fetch_file_offset_string_size NULL
253 /* Fetch type information table */
254 static const struct fetch_type kprobes_fetch_type_table[] = {
256 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
257 sizeof(u32), 1, "__data_loc char[]"),
258 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
259 string_size, sizeof(u32), 0, "u32"),
261 ASSIGN_FETCH_TYPE(u8, u8, 0),
262 ASSIGN_FETCH_TYPE(u16, u16, 0),
263 ASSIGN_FETCH_TYPE(u32, u32, 0),
264 ASSIGN_FETCH_TYPE(u64, u64, 0),
265 ASSIGN_FETCH_TYPE(s8, u8, 1),
266 ASSIGN_FETCH_TYPE(s16, u16, 1),
267 ASSIGN_FETCH_TYPE(s32, u32, 1),
268 ASSIGN_FETCH_TYPE(s64, u64, 1),
269 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
270 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
271 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
272 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
274 ASSIGN_FETCH_TYPE_END
278 * Allocate new trace_probe and initialize it (including kprobes).
280 static struct trace_kprobe *alloc_trace_kprobe(const char *group,
285 int nargs, bool is_return)
287 struct trace_kprobe *tk;
290 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
294 tk->nhit = alloc_percpu(unsigned long);
299 tk->symbol = kstrdup(symbol, GFP_KERNEL);
302 tk->rp.kp.symbol_name = tk->symbol;
303 tk->rp.kp.offset = offs;
305 tk->rp.kp.addr = addr;
308 tk->rp.handler = kretprobe_dispatcher;
310 tk->rp.kp.pre_handler = kprobe_dispatcher;
312 if (!event || !is_good_name(event)) {
317 tk->tp.call.class = &tk->tp.class;
318 tk->tp.call.name = kstrdup(event, GFP_KERNEL);
319 if (!tk->tp.call.name)
322 if (!group || !is_good_name(group)) {
327 tk->tp.class.system = kstrdup(group, GFP_KERNEL);
328 if (!tk->tp.class.system)
331 INIT_LIST_HEAD(&tk->list);
332 INIT_LIST_HEAD(&tk->tp.files);
335 kfree(tk->tp.call.name);
337 free_percpu(tk->nhit);
342 static void free_trace_kprobe(struct trace_kprobe *tk)
346 for (i = 0; i < tk->tp.nr_args; i++)
347 traceprobe_free_probe_arg(&tk->tp.args[i]);
349 kfree(tk->tp.call.class->system);
350 kfree(tk->tp.call.name);
352 free_percpu(tk->nhit);
356 static struct trace_kprobe *find_trace_kprobe(const char *event,
359 struct trace_kprobe *tk;
361 list_for_each_entry(tk, &probe_list, list)
362 if (strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
363 strcmp(tk->tp.call.class->system, group) == 0)
370 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
373 enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
378 struct event_file_link *link;
380 link = kmalloc(sizeof(*link), GFP_KERNEL);
387 list_add_tail_rcu(&link->list, &tk->tp.files);
389 tk->tp.flags |= TP_FLAG_TRACE;
391 tk->tp.flags |= TP_FLAG_PROFILE;
393 if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) {
394 if (trace_kprobe_is_return(tk))
395 ret = enable_kretprobe(&tk->rp);
397 ret = enable_kprobe(&tk->rp.kp);
404 * Disable trace_probe
405 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
408 disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
410 struct event_file_link *link = NULL;
415 link = find_event_file_link(&tk->tp, file);
421 list_del_rcu(&link->list);
423 if (!list_empty(&tk->tp.files))
426 tk->tp.flags &= ~TP_FLAG_TRACE;
428 tk->tp.flags &= ~TP_FLAG_PROFILE;
430 if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
431 if (trace_kprobe_is_return(tk))
432 disable_kretprobe(&tk->rp);
434 disable_kprobe(&tk->rp.kp);
440 * Synchronize with kprobe_trace_func/kretprobe_trace_func
441 * to ensure disabled (all running handlers are finished).
442 * This is not only for kfree(), but also the caller,
443 * trace_remove_event_call() supposes it for releasing
444 * event_call related objects, which will be accessed in
445 * the kprobe_trace_func/kretprobe_trace_func.
448 kfree(link); /* Ignored if link == NULL */
454 /* Internal register function - just handle k*probes and flags */
455 static int __register_trace_kprobe(struct trace_kprobe *tk)
459 if (trace_probe_is_registered(&tk->tp))
462 for (i = 0; i < tk->tp.nr_args; i++)
463 traceprobe_update_arg(&tk->tp.args[i]);
465 /* Set/clear disabled flag according to tp->flag */
466 if (trace_probe_is_enabled(&tk->tp))
467 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
469 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
471 if (trace_kprobe_is_return(tk))
472 ret = register_kretprobe(&tk->rp);
474 ret = register_kprobe(&tk->rp.kp);
477 tk->tp.flags |= TP_FLAG_REGISTERED;
479 pr_warn("Could not insert probe at %s+%lu: %d\n",
480 trace_kprobe_symbol(tk), trace_kprobe_offset(tk), ret);
481 if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
482 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
484 } else if (ret == -EILSEQ) {
485 pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
494 /* Internal unregister function - just handle k*probes and flags */
495 static void __unregister_trace_kprobe(struct trace_kprobe *tk)
497 if (trace_probe_is_registered(&tk->tp)) {
498 if (trace_kprobe_is_return(tk))
499 unregister_kretprobe(&tk->rp);
501 unregister_kprobe(&tk->rp.kp);
502 tk->tp.flags &= ~TP_FLAG_REGISTERED;
503 /* Cleanup kprobe for reuse */
504 if (tk->rp.kp.symbol_name)
505 tk->rp.kp.addr = NULL;
509 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
510 static int unregister_trace_kprobe(struct trace_kprobe *tk)
512 /* Enabled event can not be unregistered */
513 if (trace_probe_is_enabled(&tk->tp))
516 /* Will fail if probe is being used by ftrace or perf */
517 if (unregister_kprobe_event(tk))
520 __unregister_trace_kprobe(tk);
526 /* Register a trace_probe and probe_event */
527 static int register_trace_kprobe(struct trace_kprobe *tk)
529 struct trace_kprobe *old_tk;
532 mutex_lock(&probe_lock);
534 /* Delete old (same name) event if exist */
535 old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
536 tk->tp.call.class->system);
538 ret = unregister_trace_kprobe(old_tk);
541 free_trace_kprobe(old_tk);
544 /* Register new event */
545 ret = register_kprobe_event(tk);
547 pr_warn("Failed to register probe event(%d)\n", ret);
551 /* Register k*probe */
552 ret = __register_trace_kprobe(tk);
554 unregister_kprobe_event(tk);
556 list_add_tail(&tk->list, &probe_list);
559 mutex_unlock(&probe_lock);
563 /* Module notifier call back, checking event on the module */
564 static int trace_kprobe_module_callback(struct notifier_block *nb,
565 unsigned long val, void *data)
567 struct module *mod = data;
568 struct trace_kprobe *tk;
571 if (val != MODULE_STATE_COMING)
574 /* Update probes on coming module */
575 mutex_lock(&probe_lock);
576 list_for_each_entry(tk, &probe_list, list) {
577 if (trace_kprobe_within_module(tk, mod)) {
578 /* Don't need to check busy - this should have gone. */
579 __unregister_trace_kprobe(tk);
580 ret = __register_trace_kprobe(tk);
582 pr_warn("Failed to re-register probe %s on %s: %d\n",
583 trace_event_name(&tk->tp.call),
587 mutex_unlock(&probe_lock);
592 static struct notifier_block trace_kprobe_module_nb = {
593 .notifier_call = trace_kprobe_module_callback,
594 .priority = 1 /* Invoked after kprobe module callback */
597 static int create_trace_kprobe(int argc, char **argv)
601 * - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
602 * - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
604 * $retval : fetch return value
605 * $stack : fetch stack address
606 * $stackN : fetch Nth of stack (N:0-)
607 * $comm : fetch current task comm
608 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
609 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
610 * %REG : fetch register REG
611 * Dereferencing memory fetch:
612 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
613 * Alias name of args:
614 * NAME=FETCHARG : set NAME as alias of FETCHARG.
616 * FETCHARG:TYPE : use TYPE instead of unsigned long.
618 struct trace_kprobe *tk;
620 bool is_return = false, is_delete = false;
621 char *symbol = NULL, *event = NULL, *group = NULL;
623 unsigned long offset = 0;
625 char buf[MAX_EVENT_NAME_LEN];
627 /* argc must be >= 1 */
628 if (argv[0][0] == 'p')
630 else if (argv[0][0] == 'r')
632 else if (argv[0][0] == '-')
635 pr_info("Probe definition must be started with 'p', 'r' or"
640 if (argv[0][1] == ':') {
642 if (strchr(event, '/')) {
644 event = strchr(group, '/') + 1;
646 if (strlen(group) == 0) {
647 pr_info("Group name is not specified\n");
651 if (strlen(event) == 0) {
652 pr_info("Event name is not specified\n");
657 group = KPROBE_EVENT_SYSTEM;
661 pr_info("Delete command needs an event name.\n");
664 mutex_lock(&probe_lock);
665 tk = find_trace_kprobe(event, group);
667 mutex_unlock(&probe_lock);
668 pr_info("Event %s/%s doesn't exist.\n", group, event);
671 /* delete an event */
672 ret = unregister_trace_kprobe(tk);
674 free_trace_kprobe(tk);
675 mutex_unlock(&probe_lock);
680 pr_info("Probe point is not specified.\n");
683 if (isdigit(argv[1][0])) {
684 /* an address specified */
685 ret = kstrtoul(&argv[1][0], 0, (unsigned long *)&addr);
687 pr_info("Failed to parse address.\n");
691 /* a symbol specified */
693 /* TODO: support .init module functions */
694 ret = traceprobe_split_symbol_offset(symbol, &offset);
696 pr_info("Failed to parse symbol.\n");
699 if (offset && is_return &&
700 !arch_function_offset_within_entry(offset)) {
701 pr_info("Given offset is not valid for return probe.\n");
705 argc -= 2; argv += 2;
709 /* Make a new event name */
711 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
712 is_return ? 'r' : 'p', symbol, offset);
714 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
715 is_return ? 'r' : 'p', addr);
718 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, argc,
721 pr_info("Failed to allocate trace_probe.(%d)\n",
726 /* parse arguments */
728 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
729 struct probe_arg *parg = &tk->tp.args[i];
731 /* Increment count for freeing args in error case */
734 /* Parse argument name */
735 arg = strchr(argv[i], '=');
738 parg->name = kstrdup(argv[i], GFP_KERNEL);
741 /* If argument name is omitted, set "argN" */
742 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
743 parg->name = kstrdup(buf, GFP_KERNEL);
747 pr_info("Failed to allocate argument[%d] name.\n", i);
752 if (!is_good_name(parg->name)) {
753 pr_info("Invalid argument[%d] name: %s\n",
759 if (traceprobe_conflict_field_name(parg->name,
761 pr_info("Argument[%d] name '%s' conflicts with "
762 "another field.\n", i, argv[i]);
767 /* Parse fetch argument */
768 ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
770 kprobes_fetch_type_table);
772 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
777 ret = register_trace_kprobe(tk);
783 free_trace_kprobe(tk);
787 static int release_all_trace_kprobes(void)
789 struct trace_kprobe *tk;
792 mutex_lock(&probe_lock);
793 /* Ensure no probe is in use. */
794 list_for_each_entry(tk, &probe_list, list)
795 if (trace_probe_is_enabled(&tk->tp)) {
799 /* TODO: Use batch unregistration */
800 while (!list_empty(&probe_list)) {
801 tk = list_entry(probe_list.next, struct trace_kprobe, list);
802 ret = unregister_trace_kprobe(tk);
805 free_trace_kprobe(tk);
809 mutex_unlock(&probe_lock);
814 /* Probes listing interfaces */
815 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
817 mutex_lock(&probe_lock);
818 return seq_list_start(&probe_list, *pos);
821 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
823 return seq_list_next(v, &probe_list, pos);
826 static void probes_seq_stop(struct seq_file *m, void *v)
828 mutex_unlock(&probe_lock);
831 static int probes_seq_show(struct seq_file *m, void *v)
833 struct trace_kprobe *tk = v;
836 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
837 seq_printf(m, ":%s/%s", tk->tp.call.class->system,
838 trace_event_name(&tk->tp.call));
841 seq_printf(m, " 0x%p", tk->rp.kp.addr);
842 else if (tk->rp.kp.offset)
843 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
846 seq_printf(m, " %s", trace_kprobe_symbol(tk));
848 for (i = 0; i < tk->tp.nr_args; i++)
849 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
855 static const struct seq_operations probes_seq_op = {
856 .start = probes_seq_start,
857 .next = probes_seq_next,
858 .stop = probes_seq_stop,
859 .show = probes_seq_show
862 static int probes_open(struct inode *inode, struct file *file)
866 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
867 ret = release_all_trace_kprobes();
872 return seq_open(file, &probes_seq_op);
875 static ssize_t probes_write(struct file *file, const char __user *buffer,
876 size_t count, loff_t *ppos)
878 return traceprobe_probes_write(file, buffer, count, ppos,
879 create_trace_kprobe);
882 static const struct file_operations kprobe_events_ops = {
883 .owner = THIS_MODULE,
887 .release = seq_release,
888 .write = probes_write,
891 /* Probes profiling interfaces */
892 static int probes_profile_seq_show(struct seq_file *m, void *v)
894 struct trace_kprobe *tk = v;
896 seq_printf(m, " %-44s %15lu %15lu\n",
897 trace_event_name(&tk->tp.call),
898 trace_kprobe_nhit(tk),
904 static const struct seq_operations profile_seq_op = {
905 .start = probes_seq_start,
906 .next = probes_seq_next,
907 .stop = probes_seq_stop,
908 .show = probes_profile_seq_show
911 static int profile_open(struct inode *inode, struct file *file)
913 return seq_open(file, &profile_seq_op);
916 static const struct file_operations kprobe_profile_ops = {
917 .owner = THIS_MODULE,
918 .open = profile_open,
921 .release = seq_release,
925 static nokprobe_inline void
926 __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
927 struct trace_event_file *trace_file)
929 struct kprobe_trace_entry_head *entry;
930 struct ring_buffer_event *event;
931 struct ring_buffer *buffer;
933 unsigned long irq_flags;
934 struct trace_event_call *call = &tk->tp.call;
936 WARN_ON(call != trace_file->event_call);
938 if (trace_trigger_soft_disabled(trace_file))
941 local_save_flags(irq_flags);
942 pc = preempt_count();
944 dsize = __get_data_size(&tk->tp, regs);
945 size = sizeof(*entry) + tk->tp.size + dsize;
947 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
949 size, irq_flags, pc);
953 entry = ring_buffer_event_data(event);
954 entry->ip = (unsigned long)tk->rp.kp.addr;
955 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
957 event_trigger_unlock_commit_regs(trace_file, buffer, event,
958 entry, irq_flags, pc, regs);
962 kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
964 struct event_file_link *link;
966 list_for_each_entry_rcu(link, &tk->tp.files, list)
967 __kprobe_trace_func(tk, regs, link->file);
969 NOKPROBE_SYMBOL(kprobe_trace_func);
971 /* Kretprobe handler */
972 static nokprobe_inline void
973 __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
974 struct pt_regs *regs,
975 struct trace_event_file *trace_file)
977 struct kretprobe_trace_entry_head *entry;
978 struct ring_buffer_event *event;
979 struct ring_buffer *buffer;
981 unsigned long irq_flags;
982 struct trace_event_call *call = &tk->tp.call;
984 WARN_ON(call != trace_file->event_call);
986 if (trace_trigger_soft_disabled(trace_file))
989 local_save_flags(irq_flags);
990 pc = preempt_count();
992 dsize = __get_data_size(&tk->tp, regs);
993 size = sizeof(*entry) + tk->tp.size + dsize;
995 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
997 size, irq_flags, pc);
1001 entry = ring_buffer_event_data(event);
1002 entry->func = (unsigned long)tk->rp.kp.addr;
1003 entry->ret_ip = (unsigned long)ri->ret_addr;
1004 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1006 event_trigger_unlock_commit_regs(trace_file, buffer, event,
1007 entry, irq_flags, pc, regs);
1011 kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1012 struct pt_regs *regs)
1014 struct event_file_link *link;
1016 list_for_each_entry_rcu(link, &tk->tp.files, list)
1017 __kretprobe_trace_func(tk, ri, regs, link->file);
1019 NOKPROBE_SYMBOL(kretprobe_trace_func);
1021 /* Event entry printers */
1022 static enum print_line_t
1023 print_kprobe_event(struct trace_iterator *iter, int flags,
1024 struct trace_event *event)
1026 struct kprobe_trace_entry_head *field;
1027 struct trace_seq *s = &iter->seq;
1028 struct trace_probe *tp;
1032 field = (struct kprobe_trace_entry_head *)iter->ent;
1033 tp = container_of(event, struct trace_probe, call.event);
1035 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1037 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1040 trace_seq_putc(s, ')');
1042 data = (u8 *)&field[1];
1043 for (i = 0; i < tp->nr_args; i++)
1044 if (!tp->args[i].type->print(s, tp->args[i].name,
1045 data + tp->args[i].offset, field))
1048 trace_seq_putc(s, '\n');
1050 return trace_handle_return(s);
1053 static enum print_line_t
1054 print_kretprobe_event(struct trace_iterator *iter, int flags,
1055 struct trace_event *event)
1057 struct kretprobe_trace_entry_head *field;
1058 struct trace_seq *s = &iter->seq;
1059 struct trace_probe *tp;
1063 field = (struct kretprobe_trace_entry_head *)iter->ent;
1064 tp = container_of(event, struct trace_probe, call.event);
1066 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1068 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1071 trace_seq_puts(s, " <- ");
1073 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1076 trace_seq_putc(s, ')');
1078 data = (u8 *)&field[1];
1079 for (i = 0; i < tp->nr_args; i++)
1080 if (!tp->args[i].type->print(s, tp->args[i].name,
1081 data + tp->args[i].offset, field))
1084 trace_seq_putc(s, '\n');
1087 return trace_handle_return(s);
1091 static int kprobe_event_define_fields(struct trace_event_call *event_call)
1094 struct kprobe_trace_entry_head field;
1095 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1097 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1098 /* Set argument names as fields */
1099 for (i = 0; i < tk->tp.nr_args; i++) {
1100 struct probe_arg *parg = &tk->tp.args[i];
1102 ret = trace_define_field(event_call, parg->type->fmttype,
1104 sizeof(field) + parg->offset,
1106 parg->type->is_signed,
1114 static int kretprobe_event_define_fields(struct trace_event_call *event_call)
1117 struct kretprobe_trace_entry_head field;
1118 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1120 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1121 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1122 /* Set argument names as fields */
1123 for (i = 0; i < tk->tp.nr_args; i++) {
1124 struct probe_arg *parg = &tk->tp.args[i];
1126 ret = trace_define_field(event_call, parg->type->fmttype,
1128 sizeof(field) + parg->offset,
1130 parg->type->is_signed,
1138 #ifdef CONFIG_PERF_EVENTS
1140 /* Kprobe profile handler */
1142 kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
1144 struct trace_event_call *call = &tk->tp.call;
1145 struct bpf_prog *prog = call->prog;
1146 struct kprobe_trace_entry_head *entry;
1147 struct hlist_head *head;
1148 int size, __size, dsize;
1151 if (prog && !trace_call_bpf(prog, regs))
1154 head = this_cpu_ptr(call->perf_events);
1155 if (hlist_empty(head))
1158 dsize = __get_data_size(&tk->tp, regs);
1159 __size = sizeof(*entry) + tk->tp.size + dsize;
1160 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1161 size -= sizeof(u32);
1163 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1167 entry->ip = (unsigned long)tk->rp.kp.addr;
1168 memset(&entry[1], 0, dsize);
1169 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1170 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1173 NOKPROBE_SYMBOL(kprobe_perf_func);
1175 /* Kretprobe profile handler */
1177 kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1178 struct pt_regs *regs)
1180 struct trace_event_call *call = &tk->tp.call;
1181 struct bpf_prog *prog = call->prog;
1182 struct kretprobe_trace_entry_head *entry;
1183 struct hlist_head *head;
1184 int size, __size, dsize;
1187 if (prog && !trace_call_bpf(prog, regs))
1190 head = this_cpu_ptr(call->perf_events);
1191 if (hlist_empty(head))
1194 dsize = __get_data_size(&tk->tp, regs);
1195 __size = sizeof(*entry) + tk->tp.size + dsize;
1196 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1197 size -= sizeof(u32);
1199 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1203 entry->func = (unsigned long)tk->rp.kp.addr;
1204 entry->ret_ip = (unsigned long)ri->ret_addr;
1205 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1206 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1209 NOKPROBE_SYMBOL(kretprobe_perf_func);
1210 #endif /* CONFIG_PERF_EVENTS */
1213 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1215 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1216 * lockless, but we can't race with this __init function.
1218 static int kprobe_register(struct trace_event_call *event,
1219 enum trace_reg type, void *data)
1221 struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
1222 struct trace_event_file *file = data;
1225 case TRACE_REG_REGISTER:
1226 return enable_trace_kprobe(tk, file);
1227 case TRACE_REG_UNREGISTER:
1228 return disable_trace_kprobe(tk, file);
1230 #ifdef CONFIG_PERF_EVENTS
1231 case TRACE_REG_PERF_REGISTER:
1232 return enable_trace_kprobe(tk, NULL);
1233 case TRACE_REG_PERF_UNREGISTER:
1234 return disable_trace_kprobe(tk, NULL);
1235 case TRACE_REG_PERF_OPEN:
1236 case TRACE_REG_PERF_CLOSE:
1237 case TRACE_REG_PERF_ADD:
1238 case TRACE_REG_PERF_DEL:
1245 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1247 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
1249 raw_cpu_inc(*tk->nhit);
1251 if (tk->tp.flags & TP_FLAG_TRACE)
1252 kprobe_trace_func(tk, regs);
1253 #ifdef CONFIG_PERF_EVENTS
1254 if (tk->tp.flags & TP_FLAG_PROFILE)
1255 kprobe_perf_func(tk, regs);
1257 return 0; /* We don't tweek kernel, so just return 0 */
1259 NOKPROBE_SYMBOL(kprobe_dispatcher);
1262 kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1264 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
1266 raw_cpu_inc(*tk->nhit);
1268 if (tk->tp.flags & TP_FLAG_TRACE)
1269 kretprobe_trace_func(tk, ri, regs);
1270 #ifdef CONFIG_PERF_EVENTS
1271 if (tk->tp.flags & TP_FLAG_PROFILE)
1272 kretprobe_perf_func(tk, ri, regs);
1274 return 0; /* We don't tweek kernel, so just return 0 */
1276 NOKPROBE_SYMBOL(kretprobe_dispatcher);
1278 static struct trace_event_functions kretprobe_funcs = {
1279 .trace = print_kretprobe_event
1282 static struct trace_event_functions kprobe_funcs = {
1283 .trace = print_kprobe_event
1286 static int register_kprobe_event(struct trace_kprobe *tk)
1288 struct trace_event_call *call = &tk->tp.call;
1291 /* Initialize trace_event_call */
1292 INIT_LIST_HEAD(&call->class->fields);
1293 if (trace_kprobe_is_return(tk)) {
1294 call->event.funcs = &kretprobe_funcs;
1295 call->class->define_fields = kretprobe_event_define_fields;
1297 call->event.funcs = &kprobe_funcs;
1298 call->class->define_fields = kprobe_event_define_fields;
1300 if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
1302 ret = register_trace_event(&call->event);
1304 kfree(call->print_fmt);
1307 call->flags = TRACE_EVENT_FL_KPROBE;
1308 call->class->reg = kprobe_register;
1310 ret = trace_add_event_call(call);
1312 pr_info("Failed to register kprobe event: %s\n",
1313 trace_event_name(call));
1314 kfree(call->print_fmt);
1315 unregister_trace_event(&call->event);
1320 static int unregister_kprobe_event(struct trace_kprobe *tk)
1324 /* tp->event is unregistered in trace_remove_event_call() */
1325 ret = trace_remove_event_call(&tk->tp.call);
1327 kfree(tk->tp.call.print_fmt);
1331 /* Make a tracefs interface for controlling probe points */
1332 static __init int init_kprobe_trace(void)
1334 struct dentry *d_tracer;
1335 struct dentry *entry;
1337 if (register_module_notifier(&trace_kprobe_module_nb))
1340 d_tracer = tracing_init_dentry();
1341 if (IS_ERR(d_tracer))
1344 entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
1345 NULL, &kprobe_events_ops);
1347 /* Event list interface */
1349 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1351 /* Profile interface */
1352 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
1353 NULL, &kprobe_profile_ops);
1356 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
1359 fs_initcall(init_kprobe_trace);
1362 #ifdef CONFIG_FTRACE_STARTUP_TEST
1364 * The "__used" keeps gcc from removing the function symbol
1365 * from the kallsyms table. 'noinline' makes sure that there
1366 * isn't an inlined version used by the test method below
1368 static __used __init noinline int
1369 kprobe_trace_selftest_target(int a1, int a2, int a3, int a4, int a5, int a6)
1371 return a1 + a2 + a3 + a4 + a5 + a6;
1374 static __init struct trace_event_file *
1375 find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
1377 struct trace_event_file *file;
1379 list_for_each_entry(file, &tr->events, list)
1380 if (file->event_call == &tk->tp.call)
1387 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
1388 * stage, we can do this lockless.
1390 static __init int kprobe_trace_self_tests_init(void)
1393 int (*target)(int, int, int, int, int, int);
1394 struct trace_kprobe *tk;
1395 struct trace_event_file *file;
1397 if (tracing_is_disabled())
1400 target = kprobe_trace_selftest_target;
1402 pr_info("Testing kprobe tracing: ");
1404 ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
1405 "$stack $stack0 +0($stack)",
1406 create_trace_kprobe);
1407 if (WARN_ON_ONCE(ret)) {
1408 pr_warn("error on probing function entry.\n");
1411 /* Enable trace point */
1412 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1413 if (WARN_ON_ONCE(tk == NULL)) {
1414 pr_warn("error on getting new probe.\n");
1417 file = find_trace_probe_file(tk, top_trace_array());
1418 if (WARN_ON_ONCE(file == NULL)) {
1419 pr_warn("error on getting probe file.\n");
1422 enable_trace_kprobe(tk, file);
1426 ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
1427 "$retval", create_trace_kprobe);
1428 if (WARN_ON_ONCE(ret)) {
1429 pr_warn("error on probing function return.\n");
1432 /* Enable trace point */
1433 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1434 if (WARN_ON_ONCE(tk == NULL)) {
1435 pr_warn("error on getting 2nd new probe.\n");
1438 file = find_trace_probe_file(tk, top_trace_array());
1439 if (WARN_ON_ONCE(file == NULL)) {
1440 pr_warn("error on getting probe file.\n");
1443 enable_trace_kprobe(tk, file);
1450 ret = target(1, 2, 3, 4, 5, 6);
1453 * Not expecting an error here, the check is only to prevent the
1454 * optimizer from removing the call to target() as otherwise there
1455 * are no side-effects and the call is never performed.
1460 /* Disable trace points before removing it */
1461 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1462 if (WARN_ON_ONCE(tk == NULL)) {
1463 pr_warn("error on getting test probe.\n");
1466 if (trace_kprobe_nhit(tk) != 1) {
1467 pr_warn("incorrect number of testprobe hits\n");
1471 file = find_trace_probe_file(tk, top_trace_array());
1472 if (WARN_ON_ONCE(file == NULL)) {
1473 pr_warn("error on getting probe file.\n");
1476 disable_trace_kprobe(tk, file);
1479 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1480 if (WARN_ON_ONCE(tk == NULL)) {
1481 pr_warn("error on getting 2nd test probe.\n");
1484 if (trace_kprobe_nhit(tk) != 1) {
1485 pr_warn("incorrect number of testprobe2 hits\n");
1489 file = find_trace_probe_file(tk, top_trace_array());
1490 if (WARN_ON_ONCE(file == NULL)) {
1491 pr_warn("error on getting probe file.\n");
1494 disable_trace_kprobe(tk, file);
1497 ret = traceprobe_command("-:testprobe", create_trace_kprobe);
1498 if (WARN_ON_ONCE(ret)) {
1499 pr_warn("error on deleting a probe.\n");
1503 ret = traceprobe_command("-:testprobe2", create_trace_kprobe);
1504 if (WARN_ON_ONCE(ret)) {
1505 pr_warn("error on deleting a probe.\n");
1510 release_all_trace_kprobes();
1512 pr_cont("NG: Some tests are failed. Please check them.\n");
1518 late_initcall(kprobe_trace_self_tests_init);