bpftool: Use BTF field iterator in btfgen
authorAndrii Nakryiko <andrii@kernel.org>
Wed, 5 Jun 2024 00:16:28 +0000 (17:16 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 5 Jun 2024 14:54:41 +0000 (16:54 +0200)
Switch bpftool's code which is using libbpf-internal
btf_type_visit_type_ids() helper to new btf_field_iter functionality.

This makes bpftool code simpler, but also unblocks removing libbpf's
btf_type_visit_type_ids() helper completely.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Alan Maguire <alan.maguire@oracle.com>
Reviewed-by: Quentin Monnet <qmo@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20240605001629.4061937-5-andrii@kernel.org
tools/bpf/bpftool/gen.c

index b3979ddc01895abb571d54ba2e2f2779f0b31b17..d244a7de387e7f318e6a22128d381da2da83fdcb 100644 (file)
@@ -2379,15 +2379,6 @@ out:
        return err;
 }
 
-static int btfgen_remap_id(__u32 *type_id, void *ctx)
-{
-       unsigned int *ids = ctx;
-
-       *type_id = ids[*type_id];
-
-       return 0;
-}
-
 /* Generate BTF from relocation information previously recorded */
 static struct btf *btfgen_get_btf(struct btfgen_info *info)
 {
@@ -2467,10 +2458,15 @@ static struct btf *btfgen_get_btf(struct btfgen_info *info)
        /* second pass: fix up type ids */
        for (i = 1; i < btf__type_cnt(btf_new); i++) {
                struct btf_type *btf_type = (struct btf_type *) btf__type_by_id(btf_new, i);
+               struct btf_field_iter it;
+               __u32 *type_id;
 
-               err = btf_type_visit_type_ids(btf_type, btfgen_remap_id, ids);
+               err = btf_field_iter_init(&it, btf_type, BTF_FIELD_ITER_IDS);
                if (err)
                        goto err_out;
+
+               while ((type_id = btf_field_iter_next(&it)))
+                       *type_id = ids[*type_id];
        }
 
        free(ids);