Blob Blame History Raw
From: Eduard Zingerman <eddyz87@gmail.com>
Date: Sat, 1 Oct 2022 13:44:24 +0300
Subject: bpftool: Print newline before '}' for struct with padding only fields
Patch-mainline: v6.2-rc1
Git-commit: 44a726c3f23cf762ef4ce3c1709aefbcbe97f62c
References: bsc#1211220 jsc#PED-3924
X-Info: add context for upstream commit ea2ce1ba99aa "libbpf: fix BTF-to-C converter's padding logic"

btf_dump_emit_struct_def attempts to print empty structures at a
single line, e.g. `struct empty {}`. However, it has to account for a
case when there are no regular but some padding fields in the struct.
In such case `vlen` would be zero, but size would be non-zero.

E.g. here is struct bpf_timer from vmlinux.h before this patch:

 struct bpf_timer {
 	long: 64;
	long: 64;};

And after this patch:

 struct bpf_dynptr {
 	long: 64;
	long: 64;
 };

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20221001104425.415768-1-eddyz87@gmail.com
Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
---
 tools/lib/bpf/btf_dump.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -950,7 +950,11 @@ static void btf_dump_emit_struct_def(str
 					  lvl + 1);
 	}
 
-	if (vlen)
+	/*
+	 * Keep `struct empty {}` on a single line,
+	 * only print newline when there are regular or padding fields.
+	 */
+	if (vlen || t->size)
 		btf_dump_printf(d, "\n");
 	btf_dump_printf(d, "%s}", pfx(lvl));
 	if (packed)