bpftool: btf: Support dumping a specific types from file
authorDaniel Xu <dxu@dxuuu.xyz>
Fri, 13 Dec 2024 19:44:11 +0000 (12:44 -0700)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 13 Dec 2024 22:35:42 +0000 (14:35 -0800)
Some projects, for example xdp-tools [0], prefer to check in a minimized
vmlinux.h rather than the complete file which can get rather large.

However, when you try to add a minimized version of a complex struct (eg
struct xfrm_state), things can get quite complex if you're trying to
manually untangle and deduplicate the dependencies.

This commit teaches bpftool to do a minimized dump of a specific types by
providing a optional root_id argument(s).

Example usage:

    $ ./bpftool btf dump file ~/dev/linux/vmlinux | rg "STRUCT 'xfrm_state'"
    [12643] STRUCT 'xfrm_state' size=912 vlen=58

    $ ./bpftool btf dump file ~/dev/linux/vmlinux root_id 12643 format c
    #ifndef __VMLINUX_H__
    #define __VMLINUX_H__

    [..]

    struct xfrm_type_offload;

    struct xfrm_sec_ctx;

    struct xfrm_state {
            possible_net_t xs_net;
            union {
                    struct hlist_node gclist;
                    struct hlist_node bydst;
            };
            union {
                    struct hlist_node dev_gclist;
                    struct hlist_node bysrc;
            };
            struct hlist_node byspi;
    [..]

[0]: https://github.com/xdp-project/xdp-tools/blob/master/headers/bpf/vmlinux.h

Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/04feb860c0a56a7da66f923551484e1483a72074.1734119028.git.dxu@dxuuu.xyz
tools/bpf/bpftool/Documentation/bpftool-btf.rst
tools/bpf/bpftool/btf.c

index 245569f430353c6c5a8ee81181c72049f18378ad..d47dddc2b4ee306a418a3e243a9bf8bd52e387a7 100644 (file)
@@ -24,7 +24,7 @@ BTF COMMANDS
 =============
 
 | **bpftool** **btf** { **show** | **list** } [**id** *BTF_ID*]
-| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*]
+| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*] [**root_id** *ROOT_ID*]
 | **bpftool** **btf help**
 |
 | *BTF_SRC* := { **id** *BTF_ID* | **prog** *PROG* | **map** *MAP* [{**key** | **value** | **kv** | **all**}] | **file** *FILE* }
@@ -43,7 +43,7 @@ bpftool btf { show | list } [id *BTF_ID*]
     that hold open file descriptors (FDs) against BTF objects. On such kernels
     bpftool will automatically emit this information as well.
 
-bpftool btf dump *BTF_SRC* [format *FORMAT*]
+bpftool btf dump *BTF_SRC* [format *FORMAT*] [root_id *ROOT_ID*]
     Dump BTF entries from a given *BTF_SRC*.
 
     When **id** is specified, BTF object with that ID will be loaded and all
@@ -67,6 +67,11 @@ bpftool btf dump *BTF_SRC* [format *FORMAT*]
     formatting, the output is sorted by default. Use the **unsorted** option
     to avoid sorting the output.
 
+    **root_id** option can be used to filter a dump to a single type and all
+    its dependent types. It cannot be used with any other types of filtering
+    (such as the "key", "value", or "kv" arguments when dumping BTF for a map).
+    It can be passed multiple times to dump multiple types.
+
 bpftool btf help
     Print short help message.
 
index 3e995faf9efa630aa19afe07de2021bef081d753..2636655ac18085eda92fd3bd0ab78fc6e89c44f7 100644 (file)
@@ -27,6 +27,8 @@
 #define KFUNC_DECL_TAG         "bpf_kfunc"
 #define FASTCALL_DECL_TAG      "bpf_fastcall"
 
+#define MAX_ROOT_IDS           16
+
 static const char * const btf_kind_str[NR_BTF_KINDS] = {
        [BTF_KIND_UNKN]         = "UNKNOWN",
        [BTF_KIND_INT]          = "INT",
@@ -880,7 +882,8 @@ static int do_dump(int argc, char **argv)
 {
        bool dump_c = false, sort_dump_c = true;
        struct btf *btf = NULL, *base = NULL;
-       __u32 root_type_ids[2];
+       __u32 root_type_ids[MAX_ROOT_IDS];
+       bool have_id_filtering;
        int root_type_cnt = 0;
        __u32 btf_id = -1;
        const char *src;
@@ -974,6 +977,8 @@ static int do_dump(int argc, char **argv)
                goto done;
        }
 
+       have_id_filtering = !!root_type_cnt;
+
        while (argc) {
                if (is_prefix(*argv, "format")) {
                        NEXT_ARG();
@@ -993,6 +998,36 @@ static int do_dump(int argc, char **argv)
                                goto done;
                        }
                        NEXT_ARG();
+               } else if (is_prefix(*argv, "root_id")) {
+                       __u32 root_id;
+                       char *end;
+
+                       if (have_id_filtering) {
+                               p_err("cannot use root_id with other type filtering");
+                               err = -EINVAL;
+                               goto done;
+                       } else if (root_type_cnt == MAX_ROOT_IDS) {
+                               p_err("only %d root_id are supported", MAX_ROOT_IDS);
+                               err = -E2BIG;
+                               goto done;
+                       }
+
+                       NEXT_ARG();
+                       root_id = strtoul(*argv, &end, 0);
+                       if (*end) {
+                               err = -1;
+                               p_err("can't parse %s as root ID", *argv);
+                               goto done;
+                       }
+                       for (i = 0; i < root_type_cnt; i++) {
+                               if (root_type_ids[i] == root_id) {
+                                       err = -EINVAL;
+                                       p_err("duplicate root_id %d supplied", root_id);
+                                       goto done;
+                               }
+                       }
+                       root_type_ids[root_type_cnt++] = root_id;
+                       NEXT_ARG();
                } else if (is_prefix(*argv, "unsorted")) {
                        sort_dump_c = false;
                        NEXT_ARG();
@@ -1403,7 +1438,7 @@ static int do_help(int argc, char **argv)
 
        fprintf(stderr,
                "Usage: %1$s %2$s { show | list } [id BTF_ID]\n"
-               "       %1$s %2$s dump BTF_SRC [format FORMAT]\n"
+               "       %1$s %2$s dump BTF_SRC [format FORMAT] [root_id ROOT_ID]\n"
                "       %1$s %2$s help\n"
                "\n"
                "       BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n"