xref: /linux/include/trace/bpf_probe.h (revision 9f2c9170934eace462499ba0bfe042cc72900173)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #undef TRACE_SYSTEM_VAR
4 
5 #ifdef CONFIG_BPF_EVENTS
6 
7 #undef __entry
8 #define __entry entry
9 
10 #undef __get_dynamic_array
11 #define __get_dynamic_array(field)	\
12 		((void *)__entry + (__entry->__data_loc_##field & 0xffff))
13 
14 #undef __get_dynamic_array_len
15 #define __get_dynamic_array_len(field)	\
16 		((__entry->__data_loc_##field >> 16) & 0xffff)
17 
18 #undef __get_str
19 #define __get_str(field) ((char *)__get_dynamic_array(field))
20 
21 #undef __get_bitmask
22 #define __get_bitmask(field) (char *)__get_dynamic_array(field)
23 
24 #undef __get_cpumask
25 #define __get_cpumask(field) (char *)__get_dynamic_array(field)
26 
27 #undef __get_sockaddr
28 #define __get_sockaddr(field) ((struct sockaddr *)__get_dynamic_array(field))
29 
30 #undef __get_rel_dynamic_array
31 #define __get_rel_dynamic_array(field)	\
32 		((void *)(&__entry->__rel_loc_##field) +	\
33 		 sizeof(__entry->__rel_loc_##field) +		\
34 		 (__entry->__rel_loc_##field & 0xffff))
35 
36 #undef __get_rel_dynamic_array_len
37 #define __get_rel_dynamic_array_len(field)	\
38 		((__entry->__rel_loc_##field >> 16) & 0xffff)
39 
40 #undef __get_rel_str
41 #define __get_rel_str(field) ((char *)__get_rel_dynamic_array(field))
42 
43 #undef __get_rel_bitmask
44 #define __get_rel_bitmask(field) (char *)__get_rel_dynamic_array(field)
45 
46 #undef __get_rel_cpumask
47 #define __get_rel_cpumask(field) (char *)__get_rel_dynamic_array(field)
48 
49 #undef __get_rel_sockaddr
50 #define __get_rel_sockaddr(field) ((struct sockaddr *)__get_rel_dynamic_array(field))
51 
52 #undef __perf_count
53 #define __perf_count(c)	(c)
54 
55 #undef __perf_task
56 #define __perf_task(t)	(t)
57 
58 /* cast any integer, pointer, or small struct to u64 */
59 #define UINTTYPE(size) \
60 	__typeof__(__builtin_choose_expr(size == 1,  (u8)1, \
61 		   __builtin_choose_expr(size == 2, (u16)2, \
62 		   __builtin_choose_expr(size == 4, (u32)3, \
63 		   __builtin_choose_expr(size == 8, (u64)4, \
64 					 (void)5)))))
65 #define __CAST_TO_U64(x) ({ \
66 	typeof(x) __src = (x); \
67 	UINTTYPE(sizeof(x)) __dst; \
68 	memcpy(&__dst, &__src, sizeof(__dst)); \
69 	(u64)__dst; })
70 
71 #define __CAST1(a,...) __CAST_TO_U64(a)
72 #define __CAST2(a,...) __CAST_TO_U64(a), __CAST1(__VA_ARGS__)
73 #define __CAST3(a,...) __CAST_TO_U64(a), __CAST2(__VA_ARGS__)
74 #define __CAST4(a,...) __CAST_TO_U64(a), __CAST3(__VA_ARGS__)
75 #define __CAST5(a,...) __CAST_TO_U64(a), __CAST4(__VA_ARGS__)
76 #define __CAST6(a,...) __CAST_TO_U64(a), __CAST5(__VA_ARGS__)
77 #define __CAST7(a,...) __CAST_TO_U64(a), __CAST6(__VA_ARGS__)
78 #define __CAST8(a,...) __CAST_TO_U64(a), __CAST7(__VA_ARGS__)
79 #define __CAST9(a,...) __CAST_TO_U64(a), __CAST8(__VA_ARGS__)
80 #define __CAST10(a,...) __CAST_TO_U64(a), __CAST9(__VA_ARGS__)
81 #define __CAST11(a,...) __CAST_TO_U64(a), __CAST10(__VA_ARGS__)
82 #define __CAST12(a,...) __CAST_TO_U64(a), __CAST11(__VA_ARGS__)
83 /* tracepoints with more than 12 arguments will hit build error */
84 #define CAST_TO_U64(...) CONCATENATE(__CAST, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)
85 
86 #define __BPF_DECLARE_TRACE(call, proto, args)				\
87 static notrace void							\
88 __bpf_trace_##call(void *__data, proto)					\
89 {									\
90 	struct bpf_prog *prog = __data;					\
91 	CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(prog, CAST_TO_U64(args));	\
92 }
93 
94 #undef DECLARE_EVENT_CLASS
95 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
96 	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))
97 
98 /*
99  * This part is compiled out, it is only here as a build time check
100  * to make sure that if the tracepoint handling changes, the
101  * bpf probe will fail to compile unless it too is updated.
102  */
103 #define __DEFINE_EVENT(template, call, proto, args, size)		\
104 static inline void bpf_test_probe_##call(void)				\
105 {									\
106 	check_trace_callback_type_##call(__bpf_trace_##template);	\
107 }									\
108 typedef void (*btf_trace_##call)(void *__data, proto);			\
109 static union {								\
110 	struct bpf_raw_event_map event;					\
111 	btf_trace_##call handler;					\
112 } __bpf_trace_tp_map_##call __used					\
113 __section("__bpf_raw_tp_map") = {					\
114 	.event = {							\
115 		.tp		= &__tracepoint_##call,			\
116 		.bpf_func	= __bpf_trace_##template,		\
117 		.num_args	= COUNT_ARGS(args),			\
118 		.writable_size	= size,					\
119 	},								\
120 };
121 
122 #define FIRST(x, ...) x
123 
124 #define __CHECK_WRITABLE_BUF_SIZE(call, proto, args, size)		\
125 static inline void bpf_test_buffer_##call(void)				\
126 {									\
127 	/* BUILD_BUG_ON() is ignored if the code is completely eliminated, but \
128 	 * BUILD_BUG_ON_ZERO() uses a different mechanism that is not	\
129 	 * dead-code-eliminated.					\
130 	 */								\
131 	FIRST(proto);							\
132 	(void)BUILD_BUG_ON_ZERO(size != sizeof(*FIRST(args)));		\
133 }
134 
135 #undef DEFINE_EVENT_WRITABLE
136 #define DEFINE_EVENT_WRITABLE(template, call, proto, args, size) \
137 	__CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
138 	__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size)
139 
140 #undef DEFINE_EVENT
141 #define DEFINE_EVENT(template, call, proto, args)			\
142 	__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), 0)
143 
144 #undef DEFINE_EVENT_PRINT
145 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
146 	DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
147 
148 #undef DECLARE_TRACE
149 #define DECLARE_TRACE(call, proto, args)				\
150 	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))		\
151 	__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0)
152 
153 #undef DECLARE_TRACE_WRITABLE
154 #define DECLARE_TRACE_WRITABLE(call, proto, args, size) \
155 	__CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
156 	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
157 	__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), size)
158 
159 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
160 
161 #undef DECLARE_TRACE_WRITABLE
162 #undef DEFINE_EVENT_WRITABLE
163 #undef __CHECK_WRITABLE_BUF_SIZE
164 #undef __DEFINE_EVENT
165 #undef FIRST
166 
167 #endif /* CONFIG_BPF_EVENTS */
168