@@ -171,4 +171,74 @@ static EBPF_INLINE UNUSED int bpf_usdt_arg(struct pt_regs *ctx, u64 arg_num, lon
171171 return 0 ;
172172}
173173
174+ // clang-format off
175+ // Individual argument extraction macros
176+ // Usage: s32 arg0 = bpf_usdt_arg0(ctx);
177+ #define bpf_usdt_arg0 (ctx ) ({ long _arg; bpf_usdt_arg(ctx, 0, &_arg); _arg; })
178+ #define bpf_usdt_arg1 (ctx ) ({ long _arg; bpf_usdt_arg(ctx, 1, &_arg); _arg; })
179+ #define bpf_usdt_arg2 (ctx ) ({ long _arg; bpf_usdt_arg(ctx, 2, &_arg); _arg; })
180+ #define bpf_usdt_arg3 (ctx ) ({ long _arg; bpf_usdt_arg(ctx, 3, &_arg); _arg; })
181+ #define bpf_usdt_arg4 (ctx ) ({ long _arg; bpf_usdt_arg(ctx, 4, &_arg); _arg; })
182+ #define bpf_usdt_arg5 (ctx ) ({ long _arg; bpf_usdt_arg(ctx, 5, &_arg); _arg; })
183+ #define bpf_usdt_arg6 (ctx ) ({ long _arg; bpf_usdt_arg(ctx, 6, &_arg); _arg; })
184+ #define bpf_usdt_arg7 (ctx ) ({ long _arg; bpf_usdt_arg(ctx, 7, &_arg); _arg; })
185+ #define bpf_usdt_arg8 (ctx ) ({ long _arg; bpf_usdt_arg(ctx, 8, &_arg); _arg; })
186+ #define bpf_usdt_arg9 (ctx ) ({ long _arg; bpf_usdt_arg(ctx, 9, &_arg); _arg; })
187+ #define bpf_usdt_arg10 (ctx ) ({ long _arg; bpf_usdt_arg(ctx, 10, &_arg); _arg; })
188+ #define bpf_usdt_arg11 (ctx ) ({ long _arg; bpf_usdt_arg(ctx, 11, &_arg); _arg; })
189+
190+ // The rest of this code is from libbpf
191+ #ifndef ___bpf_concat
192+ #define ___bpf_concat (a , b ) a##b
193+ #endif
194+ #ifndef ___bpf_apply
195+ #define ___bpf_apply (fn , n ) ___bpf_concat(fn, n)
196+ #endif
197+ #ifndef ___bpf_nth
198+ #define ___bpf_nth (_ , _1 , _2 , _3 , _4 , _5 , _6 , _7 , _8 , _9 , _a , _b , _c , N , ...) N
199+ #endif
200+ #ifndef ___bpf_narg
201+ #define ___bpf_narg (...) \
202+ ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
203+ #endif
204+
205+ #define ___bpf_usdt_args0 () ctx
206+ #define ___bpf_usdt_args1 (x ) ___bpf_usdt_args0(), ({ long _x; bpf_usdt_arg(ctx, 0, &_x); _x; })
207+ #define ___bpf_usdt_args2 (x , args ...) ___bpf_usdt_args1(args), ({ long _x; bpf_usdt_arg(ctx, 1, &_x); _x; })
208+ #define ___bpf_usdt_args3 (x , args ...) ___bpf_usdt_args2(args), ({ long _x; bpf_usdt_arg(ctx, 2, &_x); _x; })
209+ #define ___bpf_usdt_args4 (x , args ...) ___bpf_usdt_args3(args), ({ long _x; bpf_usdt_arg(ctx, 3, &_x); _x; })
210+ #define ___bpf_usdt_args5 (x , args ...) ___bpf_usdt_args4(args), ({ long _x; bpf_usdt_arg(ctx, 4, &_x); _x; })
211+ #define ___bpf_usdt_args6 (x , args ...) ___bpf_usdt_args5(args), ({ long _x; bpf_usdt_arg(ctx, 5, &_x); _x; })
212+ #define ___bpf_usdt_args7 (x , args ...) ___bpf_usdt_args6(args), ({ long _x; bpf_usdt_arg(ctx, 6, &_x); _x; })
213+ #define ___bpf_usdt_args8 (x , args ...) ___bpf_usdt_args7(args), ({ long _x; bpf_usdt_arg(ctx, 7, &_x); _x; })
214+ #define ___bpf_usdt_args9 (x , args ...) ___bpf_usdt_args8(args), ({ long _x; bpf_usdt_arg(ctx, 8, &_x); _x; })
215+ #define ___bpf_usdt_args10 (x , args ...) ___bpf_usdt_args9(args), ({ long _x; bpf_usdt_arg(ctx, 9, &_x); _x; })
216+ #define ___bpf_usdt_args11 (x , args ...) ___bpf_usdt_args10(args), ({ long _x; bpf_usdt_arg(ctx, 10, &_x); _x; })
217+ #define ___bpf_usdt_args12 (x , args ...) ___bpf_usdt_args11(args), ({ long _x; bpf_usdt_arg(ctx, 11, &_x); _x; })
218+ #define ___bpf_usdt_args (args ...) ___bpf_apply(___bpf_usdt_args, ___bpf_narg(args))(args)
219+
220+ /*
221+ * BPF_USDT serves the same purpose for USDT handlers as BPF_PROG for
222+ * tp_btf/fentry/fexit BPF programs and BPF_KPROBE for kprobes.
223+ * Original struct pt_regs * context is preserved as 'ctx' argument.
224+ */
225+ #define BPF_USDT (name , args ...) \
226+ name(struct pt_regs *ctx); \
227+ static EBPF_INLINE typeof(name(0)) \
228+ ____##name(UNUSED struct pt_regs *ctx, ##args); \
229+ typeof(name(0)) name(struct pt_regs *ctx) \
230+ { \
231+ _Pragma("GCC diagnostic push") \
232+ _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
233+ return ____##name(___bpf_usdt_args(args)); \
234+ _Pragma("GCC diagnostic pop") \
235+ } \
236+ static EBPF_INLINE typeof(name(0)) \
237+ ____##name(UNUSED struct pt_regs *ctx, ##args)
238+
239+ #define BPF_USDT_CALL (name , args ...) \
240+ ____##name(___bpf_usdt_args(args))
241+
242+ // clang-format on
243+
174244#endif // OPTI_USDT_ARGS_H
0 commit comments