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