Re: [PATCH bpf-next 1/2] libbpf: Skip BTF_KIND_FWD when counting duplicated type names

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Feb 21, 2022 at 11:34 PM Xu Kuohai <xukuohai@xxxxxxxxxx> wrote:
>
> If a FWD appears in the BTF before a STRUCT with the same name, the
> STRUCT is dumped with a conflicted name:
>
>     $ bpftool btf dump file vmlinux format raw | grep "'unix_sock'"
>     [81287] FWD 'unix_sock' fwd_kind=struct
>     [89336] STRUCT 'unix_sock' size=1024 vlen=14
>
>     $ bpftool btf dump file vmlinux format c | grep "struct unix_sock"
>     struct unix_sock;
>     struct unix_sock___2 {      <--- conflict, the "___2" is unexpected
>                     struct unix_sock___2 *unix_sk;
>
> Fixes: 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion")
> Signed-off-by: Xu Kuohai <xukuohai@xxxxxxxxxx>
> ---
>  tools/lib/bpf/btf_dump.c | 16 +++++++++-------
>  1 file changed, 9 insertions(+), 7 deletions(-)
>
> diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
> index 07ebe70d3a30..55079efbd8f1 100644
> --- a/tools/lib/bpf/btf_dump.c
> +++ b/tools/lib/bpf/btf_dump.c
> @@ -1505,13 +1505,15 @@ static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id,
>         if (s->name_resolved)
>                 return *cached_name ? *cached_name : orig_name;
>
> -       dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
> -       if (dup_cnt > 1) {
> -               const size_t max_len = 256;
> -               char new_name[max_len];
> -
> -               snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
> -               *cached_name = strdup(new_name);
> +       if (!btf_is_fwd(t)) {

let's handle FWD as a special case and not touch existing logic. Maybe
something like this?

if (btf_is_fwd(t) || (btf_is_enum(t) && btf_vlen(t) == 0)) {
    s->name_resolved = 1;
    return orig_name;

}


btf_is_enum() part should handle enum forward declarations (they are
represented as enums with no values defined)


> +               dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
> +               if (dup_cnt > 1) {
> +                       const size_t max_len = 256;
> +                       char new_name[max_len];
> +
> +                       snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
> +                       *cached_name = strdup(new_name);
> +               }
>         }
>
>         s->name_resolved = 1;
> --
> 2.30.2
>



[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux