Skip to content

Commit df8ff35

Browse files
anakryikoAlexei Starovoitov
authored and
Alexei Starovoitov
committed
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 <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 396f544 commit df8ff35

16 files changed

+131
-135
lines changed

tools/lib/bpf/bpf_tracing.h

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,122 @@ struct pt_regs;
192192
(void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
193193
#endif
194194

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+
195313
#endif

tools/testing/selftests/bpf/bpf_tcp_helpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <linux/types.h>
77
#include <bpf/bpf_helpers.h>
88
#include <bpf/bpf_core_read.h>
9-
#include "bpf_trace_helpers.h"
9+
#include <bpf/bpf_tracing.h>
1010

1111
#define BPF_STRUCT_OPS(name, args...) \
1212
SEC("struct_ops/"#name) \

tools/testing/selftests/bpf/bpf_trace_helpers.h

Lines changed: 0 additions & 121 deletions
This file was deleted.

tools/testing/selftests/bpf/progs/bpf_dctcp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <linux/bpf.h>
1010
#include <linux/types.h>
1111
#include <bpf/bpf_helpers.h>
12-
#include "bpf_trace_helpers.h"
12+
#include <bpf/bpf_tracing.h>
1313
#include "bpf_tcp_helpers.h"
1414

1515
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/progs/fentry_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* Copyright (c) 2019 Facebook */
33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5-
#include "bpf_trace_helpers.h"
5+
#include <bpf/bpf_tracing.h>
66

77
char _license[] SEC("license") = "GPL";
88

tools/testing/selftests/bpf/progs/fexit_bpf2bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <linux/bpf.h>
66
#include <bpf/bpf_helpers.h>
77
#include <bpf/bpf_endian.h>
8-
#include "bpf_trace_helpers.h"
8+
#include <bpf/bpf_tracing.h>
99

1010
struct sk_buff {
1111
unsigned int len;

tools/testing/selftests/bpf/progs/fexit_bpf2bpf_simple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* Copyright (c) 2019 Facebook */
33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5-
#include "bpf_trace_helpers.h"
5+
#include <bpf/bpf_tracing.h>
66

77
struct sk_buff {
88
unsigned int len;

tools/testing/selftests/bpf/progs/fexit_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* Copyright (c) 2019 Facebook */
33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5-
#include "bpf_trace_helpers.h"
5+
#include <bpf/bpf_tracing.h>
66

77
char _license[] SEC("license") = "GPL";
88

tools/testing/selftests/bpf/progs/kfree_skb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <stdbool.h>
55
#include <bpf/bpf_helpers.h>
66
#include <bpf/bpf_endian.h>
7-
#include "bpf_trace_helpers.h"
7+
#include <bpf/bpf_tracing.h>
88

99
char _license[] SEC("license") = "GPL";
1010
struct {

tools/testing/selftests/bpf/progs/test_attach_probe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <linux/ptrace.h>
55
#include <linux/bpf.h>
66
#include <bpf/bpf_helpers.h>
7-
#include "bpf_trace_helpers.h"
7+
#include <bpf/bpf_tracing.h>
88

99
int kprobe_res = 0;
1010
int kretprobe_res = 0;

tools/testing/selftests/bpf/progs/test_overhead.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <linux/ptrace.h>
77
#include <bpf/bpf_helpers.h>
88
#include <bpf/bpf_tracing.h>
9-
#include "bpf_trace_helpers.h"
109

1110
struct task_struct;
1211

tools/testing/selftests/bpf/progs/test_perf_branches.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <linux/ptrace.h>
66
#include <linux/bpf.h>
77
#include <bpf/bpf_helpers.h>
8-
#include "bpf_trace_helpers.h"
8+
#include <bpf/bpf_tracing.h>
99

1010
int valid = 0;
1111
int required_size_out = 0;

tools/testing/selftests/bpf/progs/test_perf_buffer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <linux/ptrace.h>
55
#include <linux/bpf.h>
66
#include <bpf/bpf_helpers.h>
7-
#include "bpf_trace_helpers.h"
7+
#include <bpf/bpf_tracing.h>
88

99
struct {
1010
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);

tools/testing/selftests/bpf/progs/test_probe_user.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <bpf/bpf_helpers.h>
99
#include <bpf/bpf_tracing.h>
10-
#include "bpf_trace_helpers.h"
1110

1211
static struct sockaddr_in old;
1312

tools/testing/selftests/bpf/progs/test_trampoline_count.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#include <stdbool.h>
33
#include <stddef.h>
44
#include <linux/bpf.h>
5-
#include "bpf_trace_helpers.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
67

78
struct task_struct;
89

tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <linux/bpf.h>
3+
#include <bpf/bpf_tracing.h>
34
#include <bpf/bpf_helpers.h>
4-
#include "bpf_trace_helpers.h"
55

66
struct net_device {
77
/* Structure does not need to contain all entries,

0 commit comments

Comments
 (0)