xref: /linux-6.15/include/linux/fprobe.h (revision 76d0de57)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Simple ftrace probe wrapper */
3 #ifndef _LINUX_FPROBE_H
4 #define _LINUX_FPROBE_H
5 
6 #include <linux/compiler.h>
7 #include <linux/ftrace.h>
8 #include <linux/rethook.h>
9 
10 /**
11  * struct fprobe - ftrace based probe.
12  * @ops: The ftrace_ops.
13  * @nmissed: The counter for missing events.
14  * @flags: The status flag.
15  * @rethook: The rethook data structure. (internal data)
16  * @entry_data_size: The private data storage size.
17  * @entry_handler: The callback function for function entry.
18  * @exit_handler: The callback function for function exit.
19  */
20 struct fprobe {
21 #ifdef CONFIG_FUNCTION_TRACER
22 	/*
23 	 * If CONFIG_FUNCTION_TRACER is not set, CONFIG_FPROBE is disabled too.
24 	 * But user of fprobe may keep embedding the struct fprobe on their own
25 	 * code. To avoid build error, this will keep the fprobe data structure
26 	 * defined here, but remove ftrace_ops data structure.
27 	 */
28 	struct ftrace_ops	ops;
29 #endif
30 	unsigned long		nmissed;
31 	unsigned int		flags;
32 	struct rethook		*rethook;
33 	size_t			entry_data_size;
34 
35 	void (*entry_handler)(struct fprobe *fp, unsigned long entry_ip,
36 			      struct pt_regs *regs, void *entry_data);
37 	void (*exit_handler)(struct fprobe *fp, unsigned long entry_ip,
38 			     struct pt_regs *regs, void *entry_data);
39 };
40 
41 /* This fprobe is soft-disabled. */
42 #define FPROBE_FL_DISABLED	1
43 
44 /*
45  * This fprobe handler will be shared with kprobes.
46  * This flag must be set before registering.
47  */
48 #define FPROBE_FL_KPROBE_SHARED	2
49 
50 static inline bool fprobe_disabled(struct fprobe *fp)
51 {
52 	return (fp) ? fp->flags & FPROBE_FL_DISABLED : false;
53 }
54 
55 static inline bool fprobe_shared_with_kprobes(struct fprobe *fp)
56 {
57 	return (fp) ? fp->flags & FPROBE_FL_KPROBE_SHARED : false;
58 }
59 
60 #ifdef CONFIG_FPROBE
61 int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter);
62 int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num);
63 int register_fprobe_syms(struct fprobe *fp, const char **syms, int num);
64 int unregister_fprobe(struct fprobe *fp);
65 #else
66 static inline int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
67 {
68 	return -EOPNOTSUPP;
69 }
70 static inline int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
71 {
72 	return -EOPNOTSUPP;
73 }
74 static inline int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
75 {
76 	return -EOPNOTSUPP;
77 }
78 static inline int unregister_fprobe(struct fprobe *fp)
79 {
80 	return -EOPNOTSUPP;
81 }
82 #endif
83 
84 /**
85  * disable_fprobe() - Disable fprobe
86  * @fp: The fprobe to be disabled.
87  *
88  * This will soft-disable @fp. Note that this doesn't remove the ftrace
89  * hooks from the function entry.
90  */
91 static inline void disable_fprobe(struct fprobe *fp)
92 {
93 	if (fp)
94 		fp->flags |= FPROBE_FL_DISABLED;
95 }
96 
97 /**
98  * enable_fprobe() - Enable fprobe
99  * @fp: The fprobe to be enabled.
100  *
101  * This will soft-enable @fp.
102  */
103 static inline void enable_fprobe(struct fprobe *fp)
104 {
105 	if (fp)
106 		fp->flags &= ~FPROBE_FL_DISABLED;
107 }
108 
109 #endif
110