2021-03-10 22:04:57

by Florent Revest

[permalink] [raw]
Subject: [PATCH bpf-next 4/5] libbpf: Introduce a BPF_SNPRINTF helper macro

Similarly to BPF_SEQ_PRINTF, this macro turns variadic arguments into an
array of u64, making it more natural to call the bpf_snprintf helper.

Signed-off-by: Florent Revest <[email protected]>
---
tools/lib/bpf/bpf_tracing.h | 15 +++++++++++++++
1 file changed, 15 insertions(+)

diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
index f6a2deb3cd5b..89e82da9b8a0 100644
--- a/tools/lib/bpf/bpf_tracing.h
+++ b/tools/lib/bpf/bpf_tracing.h
@@ -457,4 +457,19 @@ static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args)
___ret; \
})

+/*
+ * BPF_SNPRINTF wraps the bpf_snprintf helper with variadic arguments instead of
+ * an array of u64.
+ */
+#define BPF_SNPRINTF(out, out_size, fmt, args...) \
+ ({ \
+ _Pragma("GCC diagnostic push") \
+ _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
+ ___bpf_build_param(args); \
+ _Pragma("GCC diagnostic pop") \
+ int ___ret = bpf_snprintf(out, out_size, fmt, \
+ ___param, sizeof(___param)); \
+ ___ret; \
+ })
+
#endif
--
2.30.1.766.gb4fecdf3b7-goog


2021-03-16 08:04:49

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH bpf-next 4/5] libbpf: Introduce a BPF_SNPRINTF helper macro

On Wed, Mar 10, 2021 at 2:02 PM Florent Revest <[email protected]> wrote:
>
> Similarly to BPF_SEQ_PRINTF, this macro turns variadic arguments into an
> array of u64, making it more natural to call the bpf_snprintf helper.
>
> Signed-off-by: Florent Revest <[email protected]>
> ---
> tools/lib/bpf/bpf_tracing.h | 15 +++++++++++++++
> 1 file changed, 15 insertions(+)
>
> diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
> index f6a2deb3cd5b..89e82da9b8a0 100644
> --- a/tools/lib/bpf/bpf_tracing.h
> +++ b/tools/lib/bpf/bpf_tracing.h
> @@ -457,4 +457,19 @@ static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args)
> ___ret; \
> })
>
> +/*
> + * BPF_SNPRINTF wraps the bpf_snprintf helper with variadic arguments instead of
> + * an array of u64.
> + */
> +#define BPF_SNPRINTF(out, out_size, fmt, args...) \
> + ({ \
> + _Pragma("GCC diagnostic push") \
> + _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
> + ___bpf_build_param(args); \
> + _Pragma("GCC diagnostic pop") \
> + int ___ret = bpf_snprintf(out, out_size, fmt, \
> + ___param, sizeof(___param)); \

same problem, mixing variable declarations and code

> + ___ret; \
> + })
> +
> #endif
> --
> 2.30.1.766.gb4fecdf3b7-goog
>