Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Merge selftests' bpf_trace_helpers.h into libbpf's bpf_tracing.h

Move BPF_PROG, BPF_KPROBE, and BPF_KRETPROBE macro into libbpf's bpf_tracing.h
header to make it available for non-selftests users.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200229231112.1240137-5-andriin@fb.com

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
df8ff353 396f544e

+131 -135
+118
tools/lib/bpf/bpf_tracing.h
··· 192 192 (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) 193 193 #endif 194 194 195 + #define ___bpf_concat(a, b) a ## b 196 + #define ___bpf_apply(fn, n) ___bpf_concat(fn, n) 197 + #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N 198 + #define ___bpf_narg(...) \ 199 + ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 200 + #define ___bpf_empty(...) \ 201 + ___bpf_nth(_, ##__VA_ARGS__, N, N, N, N, N, N, N, N, N, N, 0) 202 + 203 + #define ___bpf_ctx_cast0() ctx 204 + #define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0] 205 + #define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1] 206 + #define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2] 207 + #define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3] 208 + #define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4] 209 + #define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5] 210 + #define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6] 211 + #define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7] 212 + #define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8] 213 + #define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9] 214 + #define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10] 215 + #define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11] 216 + #define ___bpf_ctx_cast(args...) \ 217 + ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args) 218 + 219 + /* 220 + * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and 221 + * similar kinds of BPF programs, that accept input arguments as a single 222 + * pointer to untyped u64 array, where each u64 can actually be a typed 223 + * pointer or integer of different size. Instead of requring user to write 224 + * manual casts and work with array elements by index, BPF_PROG macro 225 + * allows user to declare a list of named and typed input arguments in the 226 + * same syntax as for normal C function. All the casting is hidden and 227 + * performed transparently, while user code can just assume working with 228 + * function arguments of specified type and name. 229 + * 230 + * Original raw context argument is preserved as well as 'ctx' argument. 231 + * This is useful when using BPF helpers that expect original context 232 + * as one of the parameters (e.g., for bpf_perf_event_output()). 233 + */ 234 + #define BPF_PROG(name, args...) \ 235 + name(unsigned long long *ctx); \ 236 + static __attribute__((always_inline)) typeof(name(0)) \ 237 + ____##name(unsigned long long *ctx, ##args); \ 238 + typeof(name(0)) name(unsigned long long *ctx) \ 239 + { \ 240 + _Pragma("GCC diagnostic push") \ 241 + _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 242 + return ____##name(___bpf_ctx_cast(args)); \ 243 + _Pragma("GCC diagnostic pop") \ 244 + } \ 245 + static __attribute__((always_inline)) typeof(name(0)) \ 246 + ____##name(unsigned long long *ctx, ##args) 247 + 248 + struct pt_regs; 249 + 250 + #define ___bpf_kprobe_args0() ctx 251 + #define ___bpf_kprobe_args1(x) \ 252 + ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx) 253 + #define ___bpf_kprobe_args2(x, args...) \ 254 + ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx) 255 + #define ___bpf_kprobe_args3(x, args...) \ 256 + ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx) 257 + #define ___bpf_kprobe_args4(x, args...) \ 258 + ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx) 259 + #define ___bpf_kprobe_args5(x, args...) \ 260 + ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx) 261 + #define ___bpf_kprobe_args(args...) \ 262 + ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args) 263 + 264 + /* 265 + * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for 266 + * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific 267 + * low-level way of getting kprobe input arguments from struct pt_regs, and 268 + * provides a familiar typed and named function arguments syntax and 269 + * semantics of accessing kprobe input paremeters. 270 + * 271 + * Original struct pt_regs* context is preserved as 'ctx' argument. This might 272 + * be necessary when using BPF helpers like bpf_perf_event_output(). 273 + */ 274 + #define BPF_KPROBE(name, args...) \ 275 + name(struct pt_regs *ctx); \ 276 + static __attribute__((always_inline)) typeof(name(0)) \ 277 + ____##name(struct pt_regs *ctx, ##args); \ 278 + typeof(name(0)) name(struct pt_regs *ctx) \ 279 + { \ 280 + _Pragma("GCC diagnostic push") \ 281 + _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 282 + return ____##name(___bpf_kprobe_args(args)); \ 283 + _Pragma("GCC diagnostic pop") \ 284 + } \ 285 + static __attribute__((always_inline)) typeof(name(0)) \ 286 + ____##name(struct pt_regs *ctx, ##args) 287 + 288 + #define ___bpf_kretprobe_args0() ctx 289 + #define ___bpf_kretprobe_args1(x) \ 290 + ___bpf_kretprobe_args0(), (void *)PT_REGS_RET(ctx) 291 + #define ___bpf_kretprobe_args(args...) \ 292 + ___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args) 293 + 294 + /* 295 + * BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional 296 + * return value (in addition to `struct pt_regs *ctx`), but no input 297 + * arguments, because they will be clobbered by the time probed function 298 + * returns. 299 + */ 300 + #define BPF_KRETPROBE(name, args...) \ 301 + name(struct pt_regs *ctx); \ 302 + static __attribute__((always_inline)) typeof(name(0)) \ 303 + ____##name(struct pt_regs *ctx, ##args); \ 304 + typeof(name(0)) name(struct pt_regs *ctx) \ 305 + { \ 306 + _Pragma("GCC diagnostic push") \ 307 + _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 308 + return ____##name(___bpf_kretprobe_args(args)); \ 309 + _Pragma("GCC diagnostic pop") \ 310 + } \ 311 + static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args) 312 + 195 313 #endif
+1 -1
tools/testing/selftests/bpf/bpf_tcp_helpers.h
··· 6 6 #include <linux/types.h> 7 7 #include <bpf/bpf_helpers.h> 8 8 #include <bpf/bpf_core_read.h> 9 - #include "bpf_trace_helpers.h" 9 + #include <bpf/bpf_tracing.h> 10 10 11 11 #define BPF_STRUCT_OPS(name, args...) \ 12 12 SEC("struct_ops/"#name) \
-121
tools/testing/selftests/bpf/bpf_trace_helpers.h
··· 1 - /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 - #ifndef __BPF_TRACE_HELPERS_H 3 - #define __BPF_TRACE_HELPERS_H 4 - 5 - #include <bpf/bpf_helpers.h> 6 - 7 - #define ___bpf_concat(a, b) a ## b 8 - #define ___bpf_apply(fn, n) ___bpf_concat(fn, n) 9 - #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N 10 - #define ___bpf_narg(...) \ 11 - ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 12 - #define ___bpf_empty(...) \ 13 - ___bpf_nth(_, ##__VA_ARGS__, N, N, N, N, N, N, N, N, N, N, 0) 14 - 15 - #define ___bpf_ctx_cast0() ctx 16 - #define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0] 17 - #define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1] 18 - #define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2] 19 - #define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3] 20 - #define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4] 21 - #define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5] 22 - #define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6] 23 - #define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7] 24 - #define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8] 25 - #define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9] 26 - #define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10] 27 - #define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11] 28 - #define ___bpf_ctx_cast(args...) \ 29 - ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args) 30 - 31 - /* 32 - * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and 33 - * similar kinds of BPF programs, that accept input arguments as a single 34 - * pointer to untyped u64 array, where each u64 can actually be a typed 35 - * pointer or integer of different size. Instead of requring user to write 36 - * manual casts and work with array elements by index, BPF_PROG macro 37 - * allows user to declare a list of named and typed input arguments in the 38 - * same syntax as for normal C function. All the casting is hidden and 39 - * performed transparently, while user code can just assume working with 40 - * function arguments of specified type and name. 41 - * 42 - * Original raw context argument is preserved as well as 'ctx' argument. 43 - * This is useful when using BPF helpers that expect original context 44 - * as one of the parameters (e.g., for bpf_perf_event_output()). 45 - */ 46 - #define BPF_PROG(name, args...) \ 47 - name(unsigned long long *ctx); \ 48 - static __always_inline typeof(name(0)) \ 49 - ____##name(unsigned long long *ctx, ##args); \ 50 - typeof(name(0)) name(unsigned long long *ctx) \ 51 - { \ 52 - _Pragma("GCC diagnostic push") \ 53 - _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 54 - return ____##name(___bpf_ctx_cast(args)); \ 55 - _Pragma("GCC diagnostic pop") \ 56 - } \ 57 - static __always_inline typeof(name(0)) \ 58 - ____##name(unsigned long long *ctx, ##args) 59 - 60 - struct pt_regs; 61 - 62 - #define ___bpf_kprobe_args0() ctx 63 - #define ___bpf_kprobe_args1(x) \ 64 - ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx) 65 - #define ___bpf_kprobe_args2(x, args...) \ 66 - ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx) 67 - #define ___bpf_kprobe_args3(x, args...) \ 68 - ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx) 69 - #define ___bpf_kprobe_args4(x, args...) \ 70 - ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx) 71 - #define ___bpf_kprobe_args5(x, args...) \ 72 - ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx) 73 - #define ___bpf_kprobe_args(args...) \ 74 - ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args) 75 - 76 - /* 77 - * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for 78 - * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific 79 - * low-level way of getting kprobe input arguments from struct pt_regs, and 80 - * provides a familiar typed and named function arguments syntax and 81 - * semantics of accessing kprobe input paremeters. 82 - * 83 - * Original struct pt_regs* context is preserved as 'ctx' argument. This might 84 - * be necessary when using BPF helpers like bpf_perf_event_output(). 85 - */ 86 - #define BPF_KPROBE(name, args...) \ 87 - name(struct pt_regs *ctx); \ 88 - static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args);\ 89 - typeof(name(0)) name(struct pt_regs *ctx) \ 90 - { \ 91 - _Pragma("GCC diagnostic push") \ 92 - _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 93 - return ____##name(___bpf_kprobe_args(args)); \ 94 - _Pragma("GCC diagnostic pop") \ 95 - } \ 96 - static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args) 97 - 98 - #define ___bpf_kretprobe_args0() ctx 99 - #define ___bpf_kretprobe_args1(x) \ 100 - ___bpf_kretprobe_args0(), (void *)PT_REGS_RET(ctx) 101 - #define ___bpf_kretprobe_args(args...) \ 102 - ___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args) 103 - 104 - /* 105 - * BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional 106 - * return value (in addition to `struct pt_regs *ctx`), but no input 107 - * arguments, because they will be clobbered by the time probed function 108 - * returns. 109 - */ 110 - #define BPF_KRETPROBE(name, args...) \ 111 - name(struct pt_regs *ctx); \ 112 - static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args);\ 113 - typeof(name(0)) name(struct pt_regs *ctx) \ 114 - { \ 115 - _Pragma("GCC diagnostic push") \ 116 - _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 117 - return ____##name(___bpf_kretprobe_args(args)); \ 118 - _Pragma("GCC diagnostic pop") \ 119 - } \ 120 - static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args) 121 - #endif
+1 -1
tools/testing/selftests/bpf/progs/bpf_dctcp.c
··· 9 9 #include <linux/bpf.h> 10 10 #include <linux/types.h> 11 11 #include <bpf/bpf_helpers.h> 12 - #include "bpf_trace_helpers.h" 12 + #include <bpf/bpf_tracing.h> 13 13 #include "bpf_tcp_helpers.h" 14 14 15 15 char _license[] SEC("license") = "GPL";
+1 -1
tools/testing/selftests/bpf/progs/fentry_test.c
··· 2 2 /* Copyright (c) 2019 Facebook */ 3 3 #include <linux/bpf.h> 4 4 #include <bpf/bpf_helpers.h> 5 - #include "bpf_trace_helpers.h" 5 + #include <bpf/bpf_tracing.h> 6 6 7 7 char _license[] SEC("license") = "GPL"; 8 8
+1 -1
tools/testing/selftests/bpf/progs/fexit_bpf2bpf.c
··· 5 5 #include <linux/bpf.h> 6 6 #include <bpf/bpf_helpers.h> 7 7 #include <bpf/bpf_endian.h> 8 - #include "bpf_trace_helpers.h" 8 + #include <bpf/bpf_tracing.h> 9 9 10 10 struct sk_buff { 11 11 unsigned int len;
+1 -1
tools/testing/selftests/bpf/progs/fexit_bpf2bpf_simple.c
··· 2 2 /* Copyright (c) 2019 Facebook */ 3 3 #include <linux/bpf.h> 4 4 #include <bpf/bpf_helpers.h> 5 - #include "bpf_trace_helpers.h" 5 + #include <bpf/bpf_tracing.h> 6 6 7 7 struct sk_buff { 8 8 unsigned int len;
+1 -1
tools/testing/selftests/bpf/progs/fexit_test.c
··· 2 2 /* Copyright (c) 2019 Facebook */ 3 3 #include <linux/bpf.h> 4 4 #include <bpf/bpf_helpers.h> 5 - #include "bpf_trace_helpers.h" 5 + #include <bpf/bpf_tracing.h> 6 6 7 7 char _license[] SEC("license") = "GPL"; 8 8
+1 -1
tools/testing/selftests/bpf/progs/kfree_skb.c
··· 4 4 #include <stdbool.h> 5 5 #include <bpf/bpf_helpers.h> 6 6 #include <bpf/bpf_endian.h> 7 - #include "bpf_trace_helpers.h" 7 + #include <bpf/bpf_tracing.h> 8 8 9 9 char _license[] SEC("license") = "GPL"; 10 10 struct {
+1 -1
tools/testing/selftests/bpf/progs/test_attach_probe.c
··· 4 4 #include <linux/ptrace.h> 5 5 #include <linux/bpf.h> 6 6 #include <bpf/bpf_helpers.h> 7 - #include "bpf_trace_helpers.h" 7 + #include <bpf/bpf_tracing.h> 8 8 9 9 int kprobe_res = 0; 10 10 int kretprobe_res = 0;
-1
tools/testing/selftests/bpf/progs/test_overhead.c
··· 6 6 #include <linux/ptrace.h> 7 7 #include <bpf/bpf_helpers.h> 8 8 #include <bpf/bpf_tracing.h> 9 - #include "bpf_trace_helpers.h" 10 9 11 10 struct task_struct; 12 11
+1 -1
tools/testing/selftests/bpf/progs/test_perf_branches.c
··· 5 5 #include <linux/ptrace.h> 6 6 #include <linux/bpf.h> 7 7 #include <bpf/bpf_helpers.h> 8 - #include "bpf_trace_helpers.h" 8 + #include <bpf/bpf_tracing.h> 9 9 10 10 int valid = 0; 11 11 int required_size_out = 0;
+1 -1
tools/testing/selftests/bpf/progs/test_perf_buffer.c
··· 4 4 #include <linux/ptrace.h> 5 5 #include <linux/bpf.h> 6 6 #include <bpf/bpf_helpers.h> 7 - #include "bpf_trace_helpers.h" 7 + #include <bpf/bpf_tracing.h> 8 8 9 9 struct { 10 10 __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
-1
tools/testing/selftests/bpf/progs/test_probe_user.c
··· 7 7 8 8 #include <bpf/bpf_helpers.h> 9 9 #include <bpf/bpf_tracing.h> 10 - #include "bpf_trace_helpers.h" 11 10 12 11 static struct sockaddr_in old; 13 12
+2 -1
tools/testing/selftests/bpf/progs/test_trampoline_count.c
··· 2 2 #include <stdbool.h> 3 3 #include <stddef.h> 4 4 #include <linux/bpf.h> 5 - #include "bpf_trace_helpers.h" 5 + #include <bpf/bpf_helpers.h> 6 + #include <bpf/bpf_tracing.h> 6 7 7 8 struct task_struct; 8 9
+1 -1
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 #include <linux/bpf.h> 3 + #include <bpf/bpf_tracing.h> 3 4 #include <bpf/bpf_helpers.h> 4 - #include "bpf_trace_helpers.h" 5 5 6 6 struct net_device { 7 7 /* Structure does not need to contain all entries,