Skip to content

Commit

Permalink
bpftool: Print newline before '}' for struct with padding only fields
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Eduard Zingerman authored and Andrii Nakryiko committed Oct 5, 2022
1 parent 0326074 commit 44a726c
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tools/lib/bpf/btf_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,11 @@ static void btf_dump_emit_struct_def(struct btf_dump *d,
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)
Expand Down

0 comments on commit 44a726c

Please sign in to comment.