]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
libbpf: Add bpf_stream_printk() macro
authorKumar Kartikeya Dwivedi <memxor@gmail.com>
Thu, 3 Jul 2025 20:48:15 +0000 (13:48 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 4 Jul 2025 02:30:07 +0000 (19:30 -0700)
Add a convenience macro to print data to the BPF streams. BPF_STDOUT and
BPF_STDERR stream IDs in the vmlinux.h can be passed to the macro to
print to the respective streams.

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20250703204818.925464-10-memxor@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/bpf_helpers.h

index a50773d4616e6ce7d10e4468b042407d84cf9137..76b127a9f24d27a2cfaaf349646685506f627ba5 100644 (file)
@@ -314,6 +314,22 @@ enum libbpf_tristate {
                          ___param, sizeof(___param));          \
 })
 
+extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *args,
+                             __u32 len__sz, void *aux__prog) __weak __ksym;
+
+#define bpf_stream_printk(stream_id, fmt, args...)                             \
+({                                                                             \
+       static const char ___fmt[] = fmt;                                       \
+       unsigned long long ___param[___bpf_narg(args)];                         \
+                                                                               \
+       _Pragma("GCC diagnostic push")                                          \
+       _Pragma("GCC diagnostic ignored \"-Wint-conversion\"")                  \
+       ___bpf_fill(___param, args);                                            \
+       _Pragma("GCC diagnostic pop")                                           \
+                                                                               \
+       bpf_stream_vprintk(stream_id, ___fmt, ___param, sizeof(___param), NULL);\
+})
+
 /* Use __bpf_printk when bpf_printk call has 3 or fewer fmt args
  * Otherwise use __bpf_vprintk
  */