• Andrii Nakryiko's avatar
    libbpf: Ensure btf_dump__new() and btf_dump_opts are future-proof · 6084f5dc
    Andrii Nakryiko authored
    Change btf_dump__new() and corresponding struct btf_dump_ops structure
    to be extensible by using OPTS "framework" ([0]). Given we don't change
    the names, we use a similar approach as with bpf_prog_load(), but this
    time we ended up with two APIs with the same name and same number of
    arguments, so overloading based on number of arguments with
    ___libbpf_override() doesn't work.
    
    Instead, use "overloading" based on types. In this particular case,
    print callback has to be specified, so we detect which argument is
    a callback. If it's 4th (last) argument, old implementation of API is
    used by user code. If not, it must be 2nd, and thus new implementation
    is selected. The rest is handled by the same symbol versioning approach.
    
    btf_ext argument is dropped as it was never used and isn't necessary
    either. If in the future we'll need btf_ext, that will be added into
    OPTS-based struct btf_dump_opts.
    
    struct btf_dump_opts is reused for both old API and new APIs. ctx field
    is marked deprecated in v0.7+ and it's put at the same memory location
    as OPTS's sz field. Any user of new-style btf_dump__new() will have to
    set sz field and doesn't/shouldn't use ctx, as ctx is now passed along
    the callback as mandatory input argument, following the other APIs in
    libbpf that accept callbacks consistently.
    
    Again, this is quite ugly in implementation, but is done in the name of
    backwards compatibility and uniform and extensible future APIs (at the
    same time, sigh). And it will be gone in libbpf 1.0.
    
      [0] Closes: https://github.com/libbpf/libbpf/issues/283Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211111053624.190580-5-andrii@kernel.org
    6084f5dc
btf_dump.c 62.1 KB