libbpf: Add BTF field iterator
authorAndrii Nakryiko <andrii@kernel.org>
Wed, 5 Jun 2024 00:16:25 +0000 (17:16 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 5 Jun 2024 14:54:26 +0000 (16:54 +0200)
commit68153bb2fffbe59804370e514482f95c4b2053ff
tree130e88dd4f4f856a770497b71bb4a05fa565ed98
parent898ac74c5b5f8b551a377d6a60ca4e30023ac9d2
libbpf: Add BTF field iterator

Implement iterator-based type ID and string offset BTF field iterator.
This is used extensively in BTF-handling code and BPF linker code for
various sanity checks, rewriting IDs/offsets, etc. Currently this is
implemented as visitor pattern calling custom callbacks, which makes the
logic (especially in simple cases) unnecessarily obscure and harder to
follow.

Having equivalent functionality using iterator pattern makes for simpler
to understand and maintain code. As we add more code for BTF processing
logic in libbpf, it's best to switch to iterator pattern before adding
more callback-based code.

The idea for iterator-based implementation is to record offsets of
necessary fields within fixed btf_type parts (which should be iterated
just once), and, for kinds that have multiple members (based on vlen
field), record where in each member necessary fields are located.

Generic iteration code then just keeps track of last offset that was
returned and handles N members correctly. Return type is just u32
pointer, where NULL is returned when all relevant fields were already
iterated.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Alan Maguire <alan.maguire@oracle.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20240605001629.4061937-2-andrii@kernel.org
tools/lib/bpf/btf.c
tools/lib/bpf/libbpf_internal.h