1 // SPDX-License-Identifier: GPL-2.0
3 * Fprobe-based tracing events
4 * Copyright (C) 2022 Google LLC.
6 #define pr_fmt(fmt) "trace_fprobe: " fmt
8 #include <linux/fprobe.h>
9 #include <linux/module.h>
10 #include <linux/rculist.h>
11 #include <linux/security.h>
12 #include <linux/tracepoint.h>
13 #include <linux/uaccess.h>
15 #include "trace_dynevent.h"
16 #include "trace_probe.h"
17 #include "trace_probe_kernel.h"
18 #include "trace_probe_tmpl.h"
20 #define FPROBE_EVENT_SYSTEM "fprobes"
21 #define TRACEPOINT_EVENT_SYSTEM "tracepoints"
22 #define RETHOOK_MAXACTIVE_MAX 4096
24 static int trace_fprobe_create(const char *raw_command);
25 static int trace_fprobe_show(struct seq_file *m, struct dyn_event *ev);
26 static int trace_fprobe_release(struct dyn_event *ev);
27 static bool trace_fprobe_is_busy(struct dyn_event *ev);
28 static bool trace_fprobe_match(const char *system, const char *event,
29 int argc, const char **argv, struct dyn_event *ev);
31 static struct dyn_event_operations trace_fprobe_ops = {
32 .create = trace_fprobe_create,
33 .show = trace_fprobe_show,
34 .is_busy = trace_fprobe_is_busy,
35 .free = trace_fprobe_release,
36 .match = trace_fprobe_match,
40 * Fprobe event core functions
43 struct dyn_event devent;
46 struct tracepoint *tpoint;
48 struct trace_probe tp;
51 static bool is_trace_fprobe(struct dyn_event *ev)
53 return ev->ops == &trace_fprobe_ops;
56 static struct trace_fprobe *to_trace_fprobe(struct dyn_event *ev)
58 return container_of(ev, struct trace_fprobe, devent);
62 * for_each_trace_fprobe - iterate over the trace_fprobe list
63 * @pos: the struct trace_fprobe * for each entry
64 * @dpos: the struct dyn_event * to use as a loop cursor
66 #define for_each_trace_fprobe(pos, dpos) \
67 for_each_dyn_event(dpos) \
68 if (is_trace_fprobe(dpos) && (pos = to_trace_fprobe(dpos)))
70 static bool trace_fprobe_is_return(struct trace_fprobe *tf)
72 return tf->fp.exit_handler != NULL;
75 static bool trace_fprobe_is_tracepoint(struct trace_fprobe *tf)
77 return tf->tpoint != NULL;
80 static const char *trace_fprobe_symbol(struct trace_fprobe *tf)
82 return tf->symbol ? tf->symbol : "unknown";
85 static bool trace_fprobe_is_busy(struct dyn_event *ev)
87 struct trace_fprobe *tf = to_trace_fprobe(ev);
89 return trace_probe_is_enabled(&tf->tp);
92 static bool trace_fprobe_match_command_head(struct trace_fprobe *tf,
93 int argc, const char **argv)
95 char buf[MAX_ARGSTR_LEN + 1];
100 snprintf(buf, sizeof(buf), "%s", trace_fprobe_symbol(tf));
101 if (strcmp(buf, argv[0]))
105 return trace_probe_match_command_args(&tf->tp, argc, argv);
108 static bool trace_fprobe_match(const char *system, const char *event,
109 int argc, const char **argv, struct dyn_event *ev)
111 struct trace_fprobe *tf = to_trace_fprobe(ev);
113 if (event[0] != '\0' && strcmp(trace_probe_name(&tf->tp), event))
116 if (system && strcmp(trace_probe_group_name(&tf->tp), system))
119 return trace_fprobe_match_command_head(tf, argc, argv);
122 static bool trace_fprobe_is_registered(struct trace_fprobe *tf)
124 return fprobe_is_registered(&tf->fp);
128 * Note that we don't verify the fetch_insn code, since it does not come
132 process_fetch_insn(struct fetch_insn *code, void *rec, void *dest,
135 struct pt_regs *regs = rec;
140 /* 1st stage: get value from context */
143 val = regs_get_kernel_stack_nth(regs, code->param);
145 case FETCH_OP_STACKP:
146 val = kernel_stack_pointer(regs);
148 case FETCH_OP_RETVAL:
149 val = regs_return_value(regs);
151 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
153 val = regs_get_kernel_argument(regs, code->param);
156 case FETCH_NOP_SYMBOL: /* Ignore a place holder */
160 ret = process_common_fetch_insn(code, &val);
166 return process_fetch_insn_bottom(code, val, dest, base);
168 NOKPROBE_SYMBOL(process_fetch_insn)
170 /* function entry handler */
171 static nokprobe_inline void
172 __fentry_trace_func(struct trace_fprobe *tf, unsigned long entry_ip,
173 struct pt_regs *regs,
174 struct trace_event_file *trace_file)
176 struct fentry_trace_entry_head *entry;
177 struct trace_event_call *call = trace_probe_event_call(&tf->tp);
178 struct trace_event_buffer fbuffer;
181 if (WARN_ON_ONCE(call != trace_file->event_call))
184 if (trace_trigger_soft_disabled(trace_file))
187 dsize = __get_data_size(&tf->tp, regs);
189 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
190 sizeof(*entry) + tf->tp.size + dsize);
195 entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
196 entry->ip = entry_ip;
197 store_trace_args(&entry[1], &tf->tp, regs, sizeof(*entry), dsize);
199 trace_event_buffer_commit(&fbuffer);
203 fentry_trace_func(struct trace_fprobe *tf, unsigned long entry_ip,
204 struct pt_regs *regs)
206 struct event_file_link *link;
208 trace_probe_for_each_link_rcu(link, &tf->tp)
209 __fentry_trace_func(tf, entry_ip, regs, link->file);
211 NOKPROBE_SYMBOL(fentry_trace_func);
213 /* Kretprobe handler */
214 static nokprobe_inline void
215 __fexit_trace_func(struct trace_fprobe *tf, unsigned long entry_ip,
216 unsigned long ret_ip, struct pt_regs *regs,
217 struct trace_event_file *trace_file)
219 struct fexit_trace_entry_head *entry;
220 struct trace_event_buffer fbuffer;
221 struct trace_event_call *call = trace_probe_event_call(&tf->tp);
224 if (WARN_ON_ONCE(call != trace_file->event_call))
227 if (trace_trigger_soft_disabled(trace_file))
230 dsize = __get_data_size(&tf->tp, regs);
232 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
233 sizeof(*entry) + tf->tp.size + dsize);
238 entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
239 entry->func = entry_ip;
240 entry->ret_ip = ret_ip;
241 store_trace_args(&entry[1], &tf->tp, regs, sizeof(*entry), dsize);
243 trace_event_buffer_commit(&fbuffer);
247 fexit_trace_func(struct trace_fprobe *tf, unsigned long entry_ip,
248 unsigned long ret_ip, struct pt_regs *regs)
250 struct event_file_link *link;
252 trace_probe_for_each_link_rcu(link, &tf->tp)
253 __fexit_trace_func(tf, entry_ip, ret_ip, regs, link->file);
255 NOKPROBE_SYMBOL(fexit_trace_func);
257 #ifdef CONFIG_PERF_EVENTS
259 static int fentry_perf_func(struct trace_fprobe *tf, unsigned long entry_ip,
260 struct pt_regs *regs)
262 struct trace_event_call *call = trace_probe_event_call(&tf->tp);
263 struct fentry_trace_entry_head *entry;
264 struct hlist_head *head;
265 int size, __size, dsize;
268 head = this_cpu_ptr(call->perf_events);
269 if (hlist_empty(head))
272 dsize = __get_data_size(&tf->tp, regs);
273 __size = sizeof(*entry) + tf->tp.size + dsize;
274 size = ALIGN(__size + sizeof(u32), sizeof(u64));
277 entry = perf_trace_buf_alloc(size, NULL, &rctx);
281 entry->ip = entry_ip;
282 memset(&entry[1], 0, dsize);
283 store_trace_args(&entry[1], &tf->tp, regs, sizeof(*entry), dsize);
284 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
288 NOKPROBE_SYMBOL(fentry_perf_func);
291 fexit_perf_func(struct trace_fprobe *tf, unsigned long entry_ip,
292 unsigned long ret_ip, struct pt_regs *regs)
294 struct trace_event_call *call = trace_probe_event_call(&tf->tp);
295 struct fexit_trace_entry_head *entry;
296 struct hlist_head *head;
297 int size, __size, dsize;
300 head = this_cpu_ptr(call->perf_events);
301 if (hlist_empty(head))
304 dsize = __get_data_size(&tf->tp, regs);
305 __size = sizeof(*entry) + tf->tp.size + dsize;
306 size = ALIGN(__size + sizeof(u32), sizeof(u64));
309 entry = perf_trace_buf_alloc(size, NULL, &rctx);
313 entry->func = entry_ip;
314 entry->ret_ip = ret_ip;
315 store_trace_args(&entry[1], &tf->tp, regs, sizeof(*entry), dsize);
316 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
319 NOKPROBE_SYMBOL(fexit_perf_func);
320 #endif /* CONFIG_PERF_EVENTS */
322 static int fentry_dispatcher(struct fprobe *fp, unsigned long entry_ip,
323 unsigned long ret_ip, struct pt_regs *regs,
326 struct trace_fprobe *tf = container_of(fp, struct trace_fprobe, fp);
329 if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE))
330 fentry_trace_func(tf, entry_ip, regs);
331 #ifdef CONFIG_PERF_EVENTS
332 if (trace_probe_test_flag(&tf->tp, TP_FLAG_PROFILE))
333 ret = fentry_perf_func(tf, entry_ip, regs);
337 NOKPROBE_SYMBOL(fentry_dispatcher);
339 static void fexit_dispatcher(struct fprobe *fp, unsigned long entry_ip,
340 unsigned long ret_ip, struct pt_regs *regs,
343 struct trace_fprobe *tf = container_of(fp, struct trace_fprobe, fp);
345 if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE))
346 fexit_trace_func(tf, entry_ip, ret_ip, regs);
347 #ifdef CONFIG_PERF_EVENTS
348 if (trace_probe_test_flag(&tf->tp, TP_FLAG_PROFILE))
349 fexit_perf_func(tf, entry_ip, ret_ip, regs);
352 NOKPROBE_SYMBOL(fexit_dispatcher);
354 static void free_trace_fprobe(struct trace_fprobe *tf)
357 trace_probe_cleanup(&tf->tp);
364 * Allocate new trace_probe and initialize it (including fprobe).
366 static struct trace_fprobe *alloc_trace_fprobe(const char *group,
369 struct tracepoint *tpoint,
371 int nargs, bool is_return)
373 struct trace_fprobe *tf;
376 tf = kzalloc(struct_size(tf, tp.args, nargs), GFP_KERNEL);
380 tf->symbol = kstrdup(symbol, GFP_KERNEL);
385 tf->fp.exit_handler = fexit_dispatcher;
387 tf->fp.entry_handler = fentry_dispatcher;
390 tf->fp.nr_maxactive = maxactive;
392 ret = trace_probe_init(&tf->tp, event, group, false);
396 dyn_event_init(&tf->devent, &trace_fprobe_ops);
399 free_trace_fprobe(tf);
403 static struct trace_fprobe *find_trace_fprobe(const char *event,
406 struct dyn_event *pos;
407 struct trace_fprobe *tf;
409 for_each_trace_fprobe(tf, pos)
410 if (strcmp(trace_probe_name(&tf->tp), event) == 0 &&
411 strcmp(trace_probe_group_name(&tf->tp), group) == 0)
416 static inline int __enable_trace_fprobe(struct trace_fprobe *tf)
418 if (trace_fprobe_is_registered(tf))
419 enable_fprobe(&tf->fp);
424 static void __disable_trace_fprobe(struct trace_probe *tp)
426 struct trace_fprobe *tf;
428 list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) {
429 if (!trace_fprobe_is_registered(tf))
431 disable_fprobe(&tf->fp);
437 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
439 static int enable_trace_fprobe(struct trace_event_call *call,
440 struct trace_event_file *file)
442 struct trace_probe *tp;
443 struct trace_fprobe *tf;
447 tp = trace_probe_primary_from_call(call);
448 if (WARN_ON_ONCE(!tp))
450 enabled = trace_probe_is_enabled(tp);
452 /* This also changes "enabled" state */
454 ret = trace_probe_add_file(tp, file);
458 trace_probe_set_flag(tp, TP_FLAG_PROFILE);
461 list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) {
462 /* TODO: check the fprobe is gone */
463 __enable_trace_fprobe(tf);
471 * Disable trace_probe
472 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
474 static int disable_trace_fprobe(struct trace_event_call *call,
475 struct trace_event_file *file)
477 struct trace_probe *tp;
479 tp = trace_probe_primary_from_call(call);
480 if (WARN_ON_ONCE(!tp))
484 if (!trace_probe_get_file_link(tp, file))
486 if (!trace_probe_has_single_file(tp))
488 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
490 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
492 if (!trace_probe_is_enabled(tp))
493 __disable_trace_fprobe(tp);
498 * Synchronization is done in below function. For perf event,
499 * file == NULL and perf_trace_event_unreg() calls
500 * tracepoint_synchronize_unregister() to ensure synchronize
501 * event. We don't need to care about it.
503 trace_probe_remove_file(tp, file);
508 /* Event entry printers */
509 static enum print_line_t
510 print_fentry_event(struct trace_iterator *iter, int flags,
511 struct trace_event *event)
513 struct fentry_trace_entry_head *field;
514 struct trace_seq *s = &iter->seq;
515 struct trace_probe *tp;
517 field = (struct fentry_trace_entry_head *)iter->ent;
518 tp = trace_probe_primary_from_call(
519 container_of(event, struct trace_event_call, event));
520 if (WARN_ON_ONCE(!tp))
523 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
525 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
528 trace_seq_putc(s, ')');
530 if (trace_probe_print_args(s, tp->args, tp->nr_args,
531 (u8 *)&field[1], field) < 0)
534 trace_seq_putc(s, '\n');
536 return trace_handle_return(s);
539 static enum print_line_t
540 print_fexit_event(struct trace_iterator *iter, int flags,
541 struct trace_event *event)
543 struct fexit_trace_entry_head *field;
544 struct trace_seq *s = &iter->seq;
545 struct trace_probe *tp;
547 field = (struct fexit_trace_entry_head *)iter->ent;
548 tp = trace_probe_primary_from_call(
549 container_of(event, struct trace_event_call, event));
550 if (WARN_ON_ONCE(!tp))
553 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
555 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
558 trace_seq_puts(s, " <- ");
560 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
563 trace_seq_putc(s, ')');
565 if (trace_probe_print_args(s, tp->args, tp->nr_args,
566 (u8 *)&field[1], field) < 0)
569 trace_seq_putc(s, '\n');
572 return trace_handle_return(s);
575 static int fentry_event_define_fields(struct trace_event_call *event_call)
578 struct fentry_trace_entry_head field;
579 struct trace_probe *tp;
581 tp = trace_probe_primary_from_call(event_call);
582 if (WARN_ON_ONCE(!tp))
585 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
587 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
590 static int fexit_event_define_fields(struct trace_event_call *event_call)
593 struct fexit_trace_entry_head field;
594 struct trace_probe *tp;
596 tp = trace_probe_primary_from_call(event_call);
597 if (WARN_ON_ONCE(!tp))
600 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
601 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
603 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
606 static struct trace_event_functions fentry_funcs = {
607 .trace = print_fentry_event
610 static struct trace_event_functions fexit_funcs = {
611 .trace = print_fexit_event
614 static struct trace_event_fields fentry_fields_array[] = {
615 { .type = TRACE_FUNCTION_TYPE,
616 .define_fields = fentry_event_define_fields },
620 static struct trace_event_fields fexit_fields_array[] = {
621 { .type = TRACE_FUNCTION_TYPE,
622 .define_fields = fexit_event_define_fields },
626 static int fprobe_register(struct trace_event_call *event,
627 enum trace_reg type, void *data);
629 static inline void init_trace_event_call(struct trace_fprobe *tf)
631 struct trace_event_call *call = trace_probe_event_call(&tf->tp);
633 if (trace_fprobe_is_return(tf)) {
634 call->event.funcs = &fexit_funcs;
635 call->class->fields_array = fexit_fields_array;
637 call->event.funcs = &fentry_funcs;
638 call->class->fields_array = fentry_fields_array;
641 call->flags = TRACE_EVENT_FL_FPROBE;
642 call->class->reg = fprobe_register;
645 static int register_fprobe_event(struct trace_fprobe *tf)
647 init_trace_event_call(tf);
649 return trace_probe_register_event_call(&tf->tp);
652 static int unregister_fprobe_event(struct trace_fprobe *tf)
654 return trace_probe_unregister_event_call(&tf->tp);
657 /* Internal register function - just handle fprobe and flags */
658 static int __register_trace_fprobe(struct trace_fprobe *tf)
662 /* Should we need new LOCKDOWN flag for fprobe? */
663 ret = security_locked_down(LOCKDOWN_KPROBES);
667 if (trace_fprobe_is_registered(tf))
670 for (i = 0; i < tf->tp.nr_args; i++) {
671 ret = traceprobe_update_arg(&tf->tp.args[i]);
676 /* Set/clear disabled flag according to tp->flag */
677 if (trace_probe_is_enabled(&tf->tp))
678 tf->fp.flags &= ~FPROBE_FL_DISABLED;
680 tf->fp.flags |= FPROBE_FL_DISABLED;
682 if (trace_fprobe_is_tracepoint(tf)) {
683 struct tracepoint *tpoint = tf->tpoint;
684 unsigned long ip = (unsigned long)tpoint->probestub;
686 * Here, we do 2 steps to enable fprobe on a tracepoint.
687 * At first, put __probestub_##TP function on the tracepoint
688 * and put a fprobe on the stub function.
690 ret = tracepoint_probe_register_prio_may_exist(tpoint,
691 tpoint->probestub, NULL, 0);
694 return register_fprobe_ips(&tf->fp, &ip, 1);
697 /* TODO: handle filter, nofilter or symbol list */
698 return register_fprobe(&tf->fp, tf->symbol, NULL);
701 /* Internal unregister function - just handle fprobe and flags */
702 static void __unregister_trace_fprobe(struct trace_fprobe *tf)
704 if (trace_fprobe_is_registered(tf)) {
705 unregister_fprobe(&tf->fp);
706 memset(&tf->fp, 0, sizeof(tf->fp));
707 if (trace_fprobe_is_tracepoint(tf)) {
708 tracepoint_probe_unregister(tf->tpoint,
709 tf->tpoint->probestub, NULL);
716 /* TODO: make this trace_*probe common function */
717 /* Unregister a trace_probe and probe_event */
718 static int unregister_trace_fprobe(struct trace_fprobe *tf)
720 /* If other probes are on the event, just unregister fprobe */
721 if (trace_probe_has_sibling(&tf->tp))
724 /* Enabled event can not be unregistered */
725 if (trace_probe_is_enabled(&tf->tp))
728 /* If there's a reference to the dynamic event */
729 if (trace_event_dyn_busy(trace_probe_event_call(&tf->tp)))
732 /* Will fail if probe is being used by ftrace or perf */
733 if (unregister_fprobe_event(tf))
737 __unregister_trace_fprobe(tf);
738 dyn_event_remove(&tf->devent);
739 trace_probe_unlink(&tf->tp);
744 static bool trace_fprobe_has_same_fprobe(struct trace_fprobe *orig,
745 struct trace_fprobe *comp)
747 struct trace_probe_event *tpe = orig->tp.event;
750 list_for_each_entry(orig, &tpe->probes, tp.list) {
751 if (strcmp(trace_fprobe_symbol(orig),
752 trace_fprobe_symbol(comp)))
756 * trace_probe_compare_arg_type() ensured that nr_args and
757 * each argument name and type are same. Let's compare comm.
759 for (i = 0; i < orig->tp.nr_args; i++) {
760 if (strcmp(orig->tp.args[i].comm,
761 comp->tp.args[i].comm))
765 if (i == orig->tp.nr_args)
772 static int append_trace_fprobe(struct trace_fprobe *tf, struct trace_fprobe *to)
776 if (trace_fprobe_is_return(tf) != trace_fprobe_is_return(to) ||
777 trace_fprobe_is_tracepoint(tf) != trace_fprobe_is_tracepoint(to)) {
778 trace_probe_log_set_index(0);
779 trace_probe_log_err(0, DIFF_PROBE_TYPE);
782 ret = trace_probe_compare_arg_type(&tf->tp, &to->tp);
784 /* Note that argument starts index = 2 */
785 trace_probe_log_set_index(ret + 1);
786 trace_probe_log_err(0, DIFF_ARG_TYPE);
789 if (trace_fprobe_has_same_fprobe(to, tf)) {
790 trace_probe_log_set_index(0);
791 trace_probe_log_err(0, SAME_PROBE);
795 /* Append to existing event */
796 ret = trace_probe_append(&tf->tp, &to->tp);
800 ret = __register_trace_fprobe(tf);
802 trace_probe_unlink(&tf->tp);
804 dyn_event_add(&tf->devent, trace_probe_event_call(&tf->tp));
809 /* Register a trace_probe and probe_event */
810 static int register_trace_fprobe(struct trace_fprobe *tf)
812 struct trace_fprobe *old_tf;
815 mutex_lock(&event_mutex);
817 old_tf = find_trace_fprobe(trace_probe_name(&tf->tp),
818 trace_probe_group_name(&tf->tp));
820 ret = append_trace_fprobe(tf, old_tf);
824 /* Register new event */
825 ret = register_fprobe_event(tf);
827 if (ret == -EEXIST) {
828 trace_probe_log_set_index(0);
829 trace_probe_log_err(0, EVENT_EXIST);
831 pr_warn("Failed to register probe event(%d)\n", ret);
835 /* Register fprobe */
836 ret = __register_trace_fprobe(tf);
838 unregister_fprobe_event(tf);
840 dyn_event_add(&tf->devent, trace_probe_event_call(&tf->tp));
843 mutex_unlock(&event_mutex);
847 #ifdef CONFIG_MODULES
848 static int __tracepoint_probe_module_cb(struct notifier_block *self,
849 unsigned long val, void *data)
851 struct tp_module *tp_mod = data;
852 struct trace_fprobe *tf;
853 struct dyn_event *pos;
855 if (val != MODULE_STATE_GOING)
858 mutex_lock(&event_mutex);
859 for_each_trace_fprobe(tf, pos) {
860 if (tp_mod->mod == tf->mod) {
861 tracepoint_probe_unregister(tf->tpoint,
862 tf->tpoint->probestub, NULL);
867 mutex_unlock(&event_mutex);
872 static struct notifier_block tracepoint_module_nb = {
873 .notifier_call = __tracepoint_probe_module_cb,
875 #endif /* CONFIG_MODULES */
877 struct __find_tracepoint_cb_data {
879 struct tracepoint *tpoint;
882 static void __find_tracepoint_cb(struct tracepoint *tp, void *priv)
884 struct __find_tracepoint_cb_data *data = priv;
886 if (!data->tpoint && !strcmp(data->tp_name, tp->name))
890 static struct tracepoint *find_tracepoint(const char *tp_name)
892 struct __find_tracepoint_cb_data data = {
896 for_each_kernel_tracepoint(__find_tracepoint_cb, &data);
901 static int parse_symbol_and_return(int argc, const char *argv[],
902 char **symbol, bool *is_return,
905 char *tmp = strchr(argv[1], '%');
909 int len = tmp - argv[1];
911 if (!is_tracepoint && !strcmp(tmp, "%return")) {
914 trace_probe_log_err(len, BAD_ADDR_SUFFIX);
917 *symbol = kmemdup_nul(argv[1], len, GFP_KERNEL);
919 *symbol = kstrdup(argv[1], GFP_KERNEL);
926 /* If there is $retval, this should be a return fprobe. */
927 for (i = 2; i < argc; i++) {
928 tmp = strstr(argv[i], "$retval");
929 if (tmp && !isalnum(tmp[7]) && tmp[7] != '_') {
932 * NOTE: Don't check is_tracepoint here, because it will
933 * be checked when the argument is parsed.
941 static int __trace_fprobe_create(int argc, const char *argv[])
945 * - Add fentry probe:
946 * f[:[GRP/][EVENT]] [MOD:]KSYM [FETCHARGS]
948 * f[N][:[GRP/][EVENT]] [MOD:]KSYM%return [FETCHARGS]
949 * - Add tracepoint probe:
950 * t[:[GRP/][EVENT]] TRACEPOINT [FETCHARGS]
953 * $retval : fetch return value
954 * $stack : fetch stack address
955 * $stackN : fetch Nth entry of stack (N:0-)
956 * $argN : fetch Nth argument (N:1-)
957 * $comm : fetch current task comm
958 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
959 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
960 * Dereferencing memory fetch:
961 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
962 * Alias name of args:
963 * NAME=FETCHARG : set NAME as alias of FETCHARG.
965 * FETCHARG:TYPE : use TYPE instead of unsigned long.
967 struct trace_fprobe *tf = NULL;
968 int i, len, new_argc = 0, ret = 0;
969 bool is_return = false;
971 const char *event = NULL, *group = FPROBE_EVENT_SYSTEM;
972 const char **new_argv = NULL;
974 char buf[MAX_EVENT_NAME_LEN];
975 char gbuf[MAX_EVENT_NAME_LEN];
976 char sbuf[KSYM_NAME_LEN];
977 char abuf[MAX_BTF_ARGS_LEN];
978 bool is_tracepoint = false;
979 struct tracepoint *tpoint = NULL;
980 struct traceprobe_parse_context ctx = {
981 .flags = TPARG_FL_KERNEL | TPARG_FL_FPROBE,
984 if ((argv[0][0] != 'f' && argv[0][0] != 't') || argc < 2)
987 if (argv[0][0] == 't') {
988 is_tracepoint = true;
989 group = TRACEPOINT_EVENT_SYSTEM;
992 trace_probe_log_init("trace_fprobe", argc, argv);
994 event = strchr(&argv[0][1], ':');
998 if (isdigit(argv[0][1])) {
1000 len = event - &argv[0][1] - 1;
1002 len = strlen(&argv[0][1]);
1003 if (len > MAX_EVENT_NAME_LEN - 1) {
1004 trace_probe_log_err(1, BAD_MAXACT);
1007 memcpy(buf, &argv[0][1], len);
1009 ret = kstrtouint(buf, 0, &maxactive);
1010 if (ret || !maxactive) {
1011 trace_probe_log_err(1, BAD_MAXACT);
1014 /* fprobe rethook instances are iterated over via a list. The
1015 * maximum should stay reasonable.
1017 if (maxactive > RETHOOK_MAXACTIVE_MAX) {
1018 trace_probe_log_err(1, MAXACT_TOO_BIG);
1023 trace_probe_log_set_index(1);
1025 /* a symbol(or tracepoint) must be specified */
1026 ret = parse_symbol_and_return(argc, argv, &symbol, &is_return, is_tracepoint);
1030 if (!is_return && maxactive) {
1031 trace_probe_log_set_index(0);
1032 trace_probe_log_err(1, BAD_MAXACT_TYPE);
1036 trace_probe_log_set_index(0);
1038 ret = traceprobe_parse_event_name(&event, &group, gbuf,
1045 /* Make a new event name */
1047 snprintf(buf, MAX_EVENT_NAME_LEN, "%s%s",
1048 isdigit(*symbol) ? "_" : "", symbol);
1050 snprintf(buf, MAX_EVENT_NAME_LEN, "%s__%s", symbol,
1051 is_return ? "exit" : "entry");
1052 sanitize_event_name(buf);
1057 ctx.flags |= TPARG_FL_RETURN;
1059 ctx.flags |= TPARG_FL_FENTRY;
1061 if (is_tracepoint) {
1062 ctx.flags |= TPARG_FL_TPOINT;
1063 tpoint = find_tracepoint(symbol);
1065 trace_probe_log_set_index(1);
1066 trace_probe_log_err(0, NO_TRACEPOINT);
1069 ctx.funcname = kallsyms_lookup(
1070 (unsigned long)tpoint->probestub,
1071 NULL, NULL, NULL, sbuf);
1073 ctx.funcname = symbol;
1075 argc -= 2; argv += 2;
1076 new_argv = traceprobe_expand_meta_args(argc, argv, &new_argc,
1077 abuf, MAX_BTF_ARGS_LEN, &ctx);
1078 if (IS_ERR(new_argv)) {
1079 ret = PTR_ERR(new_argv);
1089 tf = alloc_trace_fprobe(group, event, symbol, tpoint, maxactive,
1093 /* This must return -ENOMEM, else there is a bug */
1094 WARN_ON_ONCE(ret != -ENOMEM);
1095 goto out; /* We know tf is not allocated */
1099 tf->mod = __module_text_address(
1100 (unsigned long)tf->tpoint->probestub);
1102 /* parse arguments */
1103 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
1104 trace_probe_log_set_index(i + 2);
1106 ret = traceprobe_parse_probe_arg(&tf->tp, i, argv[i], &ctx);
1108 goto error; /* This can be -ENOMEM */
1111 ret = traceprobe_set_print_fmt(&tf->tp,
1112 is_return ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL);
1116 ret = register_trace_fprobe(tf);
1118 trace_probe_log_set_index(1);
1120 trace_probe_log_err(0, BAD_INSN_BNDRY);
1121 else if (ret == -ENOENT)
1122 trace_probe_log_err(0, BAD_PROBE_ADDR);
1123 else if (ret != -ENOMEM && ret != -EEXIST)
1124 trace_probe_log_err(0, FAIL_REG_PROBE);
1129 traceprobe_finish_parse(&ctx);
1130 trace_probe_log_clear();
1138 free_trace_fprobe(tf);
1142 static int trace_fprobe_create(const char *raw_command)
1144 return trace_probe_create(raw_command, __trace_fprobe_create);
1147 static int trace_fprobe_release(struct dyn_event *ev)
1149 struct trace_fprobe *tf = to_trace_fprobe(ev);
1150 int ret = unregister_trace_fprobe(tf);
1153 free_trace_fprobe(tf);
1157 static int trace_fprobe_show(struct seq_file *m, struct dyn_event *ev)
1159 struct trace_fprobe *tf = to_trace_fprobe(ev);
1162 if (trace_fprobe_is_tracepoint(tf))
1166 if (trace_fprobe_is_return(tf) && tf->fp.nr_maxactive)
1167 seq_printf(m, "%d", tf->fp.nr_maxactive);
1168 seq_printf(m, ":%s/%s", trace_probe_group_name(&tf->tp),
1169 trace_probe_name(&tf->tp));
1171 seq_printf(m, " %s%s", trace_fprobe_symbol(tf),
1172 trace_fprobe_is_return(tf) ? "%return" : "");
1174 for (i = 0; i < tf->tp.nr_args; i++)
1175 seq_printf(m, " %s=%s", tf->tp.args[i].name, tf->tp.args[i].comm);
1182 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1184 static int fprobe_register(struct trace_event_call *event,
1185 enum trace_reg type, void *data)
1187 struct trace_event_file *file = data;
1190 case TRACE_REG_REGISTER:
1191 return enable_trace_fprobe(event, file);
1192 case TRACE_REG_UNREGISTER:
1193 return disable_trace_fprobe(event, file);
1195 #ifdef CONFIG_PERF_EVENTS
1196 case TRACE_REG_PERF_REGISTER:
1197 return enable_trace_fprobe(event, NULL);
1198 case TRACE_REG_PERF_UNREGISTER:
1199 return disable_trace_fprobe(event, NULL);
1200 case TRACE_REG_PERF_OPEN:
1201 case TRACE_REG_PERF_CLOSE:
1202 case TRACE_REG_PERF_ADD:
1203 case TRACE_REG_PERF_DEL:
1211 * Register dynevent at core_initcall. This allows kernel to setup fprobe
1212 * events in postcore_initcall without tracefs.
1214 static __init int init_fprobe_trace_early(void)
1218 ret = dyn_event_register(&trace_fprobe_ops);
1222 #ifdef CONFIG_MODULES
1223 ret = register_tracepoint_module_notifier(&tracepoint_module_nb);
1230 core_initcall(init_fprobe_trace_early);