20 #include <linux/module.h>
25 #define KPROBE_EVENT_SYSTEM "kprobes"
44 #define SIZEOF_TRACE_PROBE(n) \
45 (offsetof(struct trace_probe, args) + \
46 (sizeof(struct probe_arg) * (n)))
51 return tp->
rp.handler !=
NULL;
61 return tp->
rp.kp.offset;
76 return !!(kprobe_gone(&tp->
rp.kp));
83 const char *
name = trace_probe_symbol(tp);
84 return strncmp(mod->
name, name, len) == 0 && name[len] ==
':';
89 return !!
strchr(trace_probe_symbol(tp),
':');
92 static int register_probe_event(
struct trace_probe *tp);
93 static void unregister_probe_event(
struct trace_probe *tp);
110 int nargs,
bool is_return)
129 tp->
rp.handler = kretprobe_dispatcher;
131 tp->
rp.kp.pre_handler = kprobe_dispatcher;
133 if (!event || !is_good_name(event)) {
143 if (!group || !is_good_name(group)) {
149 if (!tp->
class.system)
152 INIT_LIST_HEAD(&tp->
list);
161 static void free_trace_probe(
struct trace_probe *tp)
165 for (i = 0; i < tp->
nr_args; i++)
174 static struct trace_probe *find_trace_probe(
const char *event,
192 if (trace_probe_is_enabled(tp) && trace_probe_is_registered(tp) &&
193 !trace_probe_has_gone(tp)) {
194 if (trace_probe_is_return(tp))
195 ret = enable_kretprobe(&tp->rp);
204 static void disable_trace_probe(
struct trace_probe *tp,
int flag)
207 if (!trace_probe_is_enabled(tp) && trace_probe_is_registered(tp)) {
208 if (trace_probe_is_return(tp))
209 disable_kretprobe(&tp->
rp);
216 static int __register_trace_probe(
struct trace_probe *tp)
220 if (trace_probe_is_registered(tp))
223 for (i = 0; i < tp->
nr_args; i++)
227 if (trace_probe_is_enabled(tp))
232 if (trace_probe_is_return(tp))
240 pr_warning(
"Could not insert probe at %s+%lu: %d\n",
241 trace_probe_symbol(tp), trace_probe_offset(tp), ret);
242 if (ret == -
ENOENT && trace_probe_is_on_module(tp)) {
243 pr_warning(
"This probe might be able to register after"
244 "target module is loaded. Continue.\n");
246 }
else if (ret == -
EILSEQ) {
248 "instruction boundary.\n",
258 static void __unregister_trace_probe(
struct trace_probe *tp)
260 if (trace_probe_is_registered(tp)) {
261 if (trace_probe_is_return(tp))
267 if (tp->
rp.kp.symbol_name)
273 static int unregister_trace_probe(
struct trace_probe *tp)
276 if (trace_probe_is_enabled(tp))
279 __unregister_trace_probe(tp);
281 unregister_probe_event(tp);
287 static int register_trace_probe(
struct trace_probe *tp)
295 old_tp = find_trace_probe(tp->
call.name, tp->
call.class->system);
297 ret = unregister_trace_probe(old_tp);
300 free_trace_probe(old_tp);
304 ret = register_probe_event(tp);
306 pr_warning(
"Failed to register probe event(%d)\n", ret);
311 ret = __register_trace_probe(tp);
313 unregister_probe_event(tp);
330 if (val != MODULE_STATE_COMING)
336 if (trace_probe_within_module(tp, mod)) {
338 __unregister_trace_probe(tp);
339 ret = __register_trace_probe(tp);
341 pr_warning(
"Failed to re-register probe %s on"
352 .notifier_call = trace_probe_module_callback,
356 static int create_trace_probe(
int argc,
char **argv)
378 bool is_return =
false, is_delete =
false;
386 if (argv[0][0] ==
'p')
388 else if (argv[0][0] ==
'r')
390 else if (argv[0][0] ==
'-')
393 pr_info(
"Probe definition must be started with 'p', 'r' or"
398 if (argv[0][1] ==
':') {
402 event =
strchr(group,
'/') + 1;
405 pr_info(
"Group name is not specified\n");
410 pr_info(
"Event name is not specified\n");
419 pr_info(
"Delete command needs an event name.\n");
423 tp = find_trace_probe(event, group);
426 pr_info(
"Event %s/%s doesn't exist.\n", group, event);
430 ret = unregister_trace_probe(tp);
432 free_trace_probe(tp);
438 pr_info(
"Probe point is not specified.\n");
443 pr_info(
"Return probe point must be a symbol.\n");
449 pr_info(
"Failed to parse address.\n");
458 pr_info(
"Failed to parse symbol.\n");
461 if (offset && is_return) {
462 pr_info(
"Return probe must be used without offset.\n");
466 argc -= 2; argv += 2;
473 is_return ?
'r' :
'p', symbol, offset);
476 is_return ?
'r' :
'p', addr);
479 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
482 pr_info(
"Failed to allocate trace_probe.(%d)\n",
494 arg =
strchr(argv[i],
'=');
505 if (!tp->
args[i].name) {
506 pr_info(
"Failed to allocate argument[%d] name.\n", i);
511 if (!is_good_name(tp->
args[i].name)) {
512 pr_info(
"Invalid argument[%d] name: %s\n",
513 i, tp->
args[i].name);
520 pr_info(
"Argument[%d] name '%s' conflicts with "
521 "another field.\n", i, argv[i]);
530 pr_info(
"Parse error at argument[%d]. (%d)\n", i, ret);
535 ret = register_trace_probe(tp);
541 free_trace_probe(tp);
545 static int release_all_trace_probes(
void)
553 if (trace_probe_is_enabled(tp)) {
558 while (!list_empty(&probe_list)) {
560 unregister_trace_probe(tp);
561 free_trace_probe(tp);
571 static void *probes_seq_start(
struct seq_file *
m, loff_t *
pos)
577 static void *probes_seq_next(
struct seq_file *
m,
void *
v, loff_t *
pos)
582 static void probes_seq_stop(
struct seq_file *
m,
void *
v)
587 static int probes_seq_show(
struct seq_file *
m,
void *
v)
592 seq_printf(m,
"%c", trace_probe_is_return(tp) ?
'r' :
'p');
597 else if (tp->
rp.kp.offset)
598 seq_printf(m,
" %s+%u", trace_probe_symbol(tp),
603 for (i = 0; i < tp->
nr_args; i++)
611 .start = probes_seq_start,
612 .next = probes_seq_next,
613 .stop = probes_seq_stop,
614 .show = probes_seq_show
622 ret = release_all_trace_probes();
627 return seq_open(file, &probes_seq_op);
630 static ssize_t probes_write(
struct file *file,
const char __user *
buffer,
631 size_t count, loff_t *ppos)
643 .write = probes_write,
647 static int probes_profile_seq_show(
struct seq_file *m,
void *v)
658 .start = probes_seq_start,
659 .next = probes_seq_next,
660 .stop = probes_seq_stop,
661 .show = probes_profile_seq_show
664 static int profile_open(
struct inode *inode,
struct file *file)
666 return seq_open(file, &profile_seq_op);
671 .open = profile_open,
684 for (i = 0; i < tp->
nr_args; i++)
686 call_fetch(&tp->
args[i].fetch_size, regs, &len);
696 u8 *data,
int maxlen)
702 for (i = 0; i < tp->
nr_args; i++) {
708 dl = (
u32 *)(data + tp->
args[i].offset);
711 call_fetch(&tp->
args[i].fetch, regs, dl);
717 ent_size + tp->
args[i].offset);
720 call_fetch(&tp->
args[i].fetch, regs,
721 data + tp->
args[i].offset);
733 unsigned long irq_flags;
741 dsize = __get_data_size(tp, regs);
742 size =
sizeof(*entry) + tp->
size + dsize;
745 size, irq_flags, pc);
751 store_trace_args(
sizeof(*entry), tp,
regs, (
u8 *)&entry[1], dsize);
755 irq_flags, pc, regs);
767 unsigned long irq_flags;
773 dsize = __get_data_size(tp, regs);
774 size =
sizeof(*entry) + tp->
size + dsize;
777 size, irq_flags, pc);
784 store_trace_args(
sizeof(*entry), tp, regs, (
u8 *)&entry[1], dsize);
788 irq_flags, pc, regs);
814 data = (
u8 *)&field[1];
815 for (i = 0; i < tp->
nr_args; i++)
816 if (!tp->
args[i].type->print(s, tp->
args[i].name,
817 data + tp->
args[i].offset, field))
856 data = (
u8 *)&field[1];
857 for (i = 0; i < tp->
nr_args; i++)
858 if (!tp->
args[i].type->print(s, tp->
args[i].name,
859 data + tp->
args[i].offset, field))
879 for (i = 0; i < tp->
nr_args; i++) {
883 tp->
args[i].type->size,
884 tp->
args[i].type->is_signed,
901 for (i = 0; i < tp->
nr_args; i++) {
905 tp->
args[i].type->size,
906 tp->
args[i].type->is_signed,
914 static int __set_print_fmt(
struct trace_probe *tp,
char *buf,
int len)
921 if (!trace_probe_is_return(tp)) {
925 fmt =
"(%lx <- %lx)";
930 #define LEN_OR_ZERO (len ? len - pos : 0)
932 pos +=
snprintf(buf + pos, LEN_OR_ZERO,
"\"%s", fmt);
934 for (i = 0; i < tp->
nr_args; i++) {
935 pos +=
snprintf(buf + pos, LEN_OR_ZERO,
" %s=%s",
936 tp->
args[i].name, tp->
args[i].type->fmt);
939 pos +=
snprintf(buf + pos, LEN_OR_ZERO,
"\", %s", arg);
941 for (i = 0; i < tp->
nr_args; i++) {
942 if (
strcmp(tp->
args[i].type->name,
"string") == 0)
943 pos +=
snprintf(buf + pos, LEN_OR_ZERO,
947 pos +=
snprintf(buf + pos, LEN_OR_ZERO,
", REC->%s",
963 len = __set_print_fmt(tp, NULL, 0);
969 __set_print_fmt(tp, print_fmt, len + 1);
970 tp->
call.print_fmt = print_fmt;
975 #ifdef CONFIG_PERF_EVENTS
985 int size, __size, dsize;
988 dsize = __get_data_size(tp, regs);
989 __size =
sizeof(*entry) + tp->
size + dsize;
993 "profile buffer not large enough"))
1001 memset(&entry[1], 0, dsize);
1002 store_trace_args(
sizeof(*entry), tp, regs, (
u8 *)&entry[1], dsize);
1005 perf_trace_buf_submit(entry, size, rctx,
1006 entry->
ip, 1, regs, head, NULL);
1017 int size, __size, dsize;
1020 dsize = __get_data_size(tp, regs);
1021 __size =
sizeof(*entry) + tp->
size + dsize;
1023 size -=
sizeof(
u32);
1025 "profile buffer not large enough"))
1034 store_trace_args(
sizeof(*entry), tp, regs, (
u8 *)&entry[1], dsize);
1037 perf_trace_buf_submit(entry, size, rctx,
1038 entry->
ret_ip, 1, regs, head, NULL);
1055 #ifdef CONFIG_PERF_EVENTS
1056 case TRACE_REG_PERF_REGISTER:
1058 case TRACE_REG_PERF_UNREGISTER:
1061 case TRACE_REG_PERF_OPEN:
1062 case TRACE_REG_PERF_CLOSE:
1063 case TRACE_REG_PERF_ADD:
1064 case TRACE_REG_PERF_DEL:
1072 int kprobe_dispatcher(
struct kprobe *kp,
struct pt_regs *regs)
1077 kprobe_trace_func(kp, regs);
1078 #ifdef CONFIG_PERF_EVENTS
1080 kprobe_perf_func(kp, regs);
1091 kretprobe_trace_func(ri, regs);
1092 #ifdef CONFIG_PERF_EVENTS
1094 kretprobe_perf_func(ri, regs);
1107 static int register_probe_event(
struct trace_probe *tp)
1113 INIT_LIST_HEAD(&call->
class->fields);
1114 if (trace_probe_is_return(tp)) {
1115 call->
event.funcs = &kretprobe_funcs;
1116 call->
class->define_fields = kretprobe_event_define_fields;
1118 call->
event.funcs = &kprobe_funcs;
1119 call->
class->define_fields = kprobe_event_define_fields;
1121 if (set_print_fmt(tp) < 0)
1129 call->
class->reg = kprobe_register;
1133 pr_info(
"Failed to register kprobe event: %s\n", call->
name);
1140 static void unregister_probe_event(
struct trace_probe *tp)
1148 static __init int init_kprobe_trace(
void)
1161 NULL, &kprobe_events_ops);
1166 "'kprobe_events' entry\n");
1170 NULL, &kprobe_profile_ops);
1174 "'kprobe_profile' entry\n");
1180 #ifdef CONFIG_FTRACE_STARTUP_TEST
1186 static __used int kprobe_trace_selftest_target(
int a1,
int a2,
int a3,
1189 return a1 + a2 + a3 + a4 + a5 +
a6;
1192 static __init int kprobe_trace_self_tests_init(
void)
1198 target = kprobe_trace_selftest_target;
1200 pr_info(
"Testing kprobe tracing: ");
1203 "$stack $stack0 +0($stack)",
1204 create_trace_probe);
1206 pr_warning(
"error on probing function entry.\n");
1219 "$retval", create_trace_probe);
1221 pr_warning(
"error on probing function return.\n");
1236 ret =
target(1, 2, 3, 4, 5, 6);
1241 pr_warning(
"error on getting test probe.\n");
1248 pr_warning(
"error on getting 2nd test probe.\n");
1266 release_all_trace_probes();
1268 pr_cont(
"NG: Some tests are failed. Please check them.\n");