Skip to content

Commit a812d92

Browse files
danobianakryiko
authored andcommitted
bpftool: btf: Support dumping a specific types from file
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 <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/04feb860c0a56a7da66f923551484e1483a72074.1734119028.git.dxu@dxuuu.xyz
1 parent 7f5819e commit a812d92

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

tools/bpf/bpftool/Documentation/bpftool-btf.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ BTF COMMANDS
2424
=============
2525

2626
| **bpftool** **btf** { **show** | **list** } [**id** *BTF_ID*]
27-
| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*]
27+
| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*] [**root_id** *ROOT_ID*]
2828
| **bpftool** **btf help**
2929
|
3030
| *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*]
4343
that hold open file descriptors (FDs) against BTF objects. On such kernels
4444
bpftool will automatically emit this information as well.
4545

46-
bpftool btf dump *BTF_SRC* [format *FORMAT*]
46+
bpftool btf dump *BTF_SRC* [format *FORMAT*] [root_id *ROOT_ID*]
4747
Dump BTF entries from a given *BTF_SRC*.
4848

4949
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*]
6767
formatting, the output is sorted by default. Use the **unsorted** option
6868
to avoid sorting the output.
6969

70+
**root_id** option can be used to filter a dump to a single type and all
71+
its dependent types. It cannot be used with any other types of filtering
72+
(such as the "key", "value", or "kv" arguments when dumping BTF for a map).
73+
It can be passed multiple times to dump multiple types.
74+
7075
bpftool btf help
7176
Print short help message.
7277

tools/bpf/bpftool/btf.c

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#define KFUNC_DECL_TAG "bpf_kfunc"
2828
#define FASTCALL_DECL_TAG "bpf_fastcall"
2929

30+
#define MAX_ROOT_IDS 16
31+
3032
static const char * const btf_kind_str[NR_BTF_KINDS] = {
3133
[BTF_KIND_UNKN] = "UNKNOWN",
3234
[BTF_KIND_INT] = "INT",
@@ -880,7 +882,8 @@ static int do_dump(int argc, char **argv)
880882
{
881883
bool dump_c = false, sort_dump_c = true;
882884
struct btf *btf = NULL, *base = NULL;
883-
__u32 root_type_ids[2];
885+
__u32 root_type_ids[MAX_ROOT_IDS];
886+
bool have_id_filtering;
884887
int root_type_cnt = 0;
885888
__u32 btf_id = -1;
886889
const char *src;
@@ -974,6 +977,8 @@ static int do_dump(int argc, char **argv)
974977
goto done;
975978
}
976979

980+
have_id_filtering = !!root_type_cnt;
981+
977982
while (argc) {
978983
if (is_prefix(*argv, "format")) {
979984
NEXT_ARG();
@@ -993,6 +998,36 @@ static int do_dump(int argc, char **argv)
993998
goto done;
994999
}
9951000
NEXT_ARG();
1001+
} else if (is_prefix(*argv, "root_id")) {
1002+
__u32 root_id;
1003+
char *end;
1004+
1005+
if (have_id_filtering) {
1006+
p_err("cannot use root_id with other type filtering");
1007+
err = -EINVAL;
1008+
goto done;
1009+
} else if (root_type_cnt == MAX_ROOT_IDS) {
1010+
p_err("only %d root_id are supported", MAX_ROOT_IDS);
1011+
err = -E2BIG;
1012+
goto done;
1013+
}
1014+
1015+
NEXT_ARG();
1016+
root_id = strtoul(*argv, &end, 0);
1017+
if (*end) {
1018+
err = -1;
1019+
p_err("can't parse %s as root ID", *argv);
1020+
goto done;
1021+
}
1022+
for (i = 0; i < root_type_cnt; i++) {
1023+
if (root_type_ids[i] == root_id) {
1024+
err = -EINVAL;
1025+
p_err("duplicate root_id %d supplied", root_id);
1026+
goto done;
1027+
}
1028+
}
1029+
root_type_ids[root_type_cnt++] = root_id;
1030+
NEXT_ARG();
9961031
} else if (is_prefix(*argv, "unsorted")) {
9971032
sort_dump_c = false;
9981033
NEXT_ARG();
@@ -1403,7 +1438,7 @@ static int do_help(int argc, char **argv)
14031438

14041439
fprintf(stderr,
14051440
"Usage: %1$s %2$s { show | list } [id BTF_ID]\n"
1406-
" %1$s %2$s dump BTF_SRC [format FORMAT]\n"
1441+
" %1$s %2$s dump BTF_SRC [format FORMAT] [root_id ROOT_ID]\n"
14071442
" %1$s %2$s help\n"
14081443
"\n"
14091444
" BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n"

0 commit comments

Comments
 (0)