Skip to content

Commit 6a10d2d

Browse files
committed
Merge branch 'bpftool-btf-support-dumping-a-single-type-from-file'
Daniel Xu says: ==================== bpftool: btf: Support dumping a single type 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 single type by providing an optional root_id argument. 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 === Changelog === Changes in v5: * Update bash-completion to support repeating root_id * Update man page to mention root_id NAND map key/value/kv/all Changes in v4: * Support multiple instances of root_id Changes in v3: * Make `root_id` a top level btf-dump argument rather than attached to `file` * Update bash completion script * Refactor root_type_ids checking to after btf handle creation * Update help messages and fix existing man page inconsistency Changes in v2: * Add early error check for invalid BTF ID ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents 5506b7d + 9d294f6 commit 6a10d2d

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
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*
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*
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/bash-completion/bpftool

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,19 +930,24 @@ _bpftool()
930930
format)
931931
COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) )
932932
;;
933+
root_id)
934+
return 0;
935+
;;
933936
c)
934-
COMPREPLY=( $( compgen -W "unsorted" -- "$cur" ) )
937+
COMPREPLY=( $( compgen -W "unsorted root_id" -- "$cur" ) )
935938
;;
936939
*)
937940
# emit extra options
938941
case ${words[3]} in
939942
id|file)
943+
COMPREPLY=( $( compgen -W "root_id" -- "$cur" ) )
940944
_bpftool_once_attr 'format'
941945
;;
942946
map|prog)
943947
if [[ ${words[3]} == "map" ]] && [[ $cword == 6 ]]; then
944948
COMPREPLY+=( $( compgen -W "key value kv all" -- "$cur" ) )
945949
fi
950+
COMPREPLY=( $( compgen -W "root_id" -- "$cur" ) )
946951
_bpftool_once_attr 'format'
947952
;;
948953
*)

tools/bpf/bpftool/btf.c

Lines changed: 49 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,12 +882,14 @@ 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;
887890
int fd = -1;
888891
int err = 0;
892+
int i;
889893

890894
if (!REQ_ARGS(2)) {
891895
usage();
@@ -973,6 +977,8 @@ static int do_dump(int argc, char **argv)
973977
goto done;
974978
}
975979

980+
have_id_filtering = !!root_type_cnt;
981+
976982
while (argc) {
977983
if (is_prefix(*argv, "format")) {
978984
NEXT_ARG();
@@ -992,6 +998,36 @@ static int do_dump(int argc, char **argv)
992998
goto done;
993999
}
9941000
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();
9951031
} else if (is_prefix(*argv, "unsorted")) {
9961032
sort_dump_c = false;
9971033
NEXT_ARG();
@@ -1017,6 +1053,17 @@ static int do_dump(int argc, char **argv)
10171053
}
10181054
}
10191055

1056+
/* Invalid root IDs causes half emitted boilerplate and then unclean
1057+
* exit. It's an ugly user experience, so handle common error here.
1058+
*/
1059+
for (i = 0; i < root_type_cnt; i++) {
1060+
if (root_type_ids[i] >= btf__type_cnt(btf)) {
1061+
err = -EINVAL;
1062+
p_err("invalid root ID: %u", root_type_ids[i]);
1063+
goto done;
1064+
}
1065+
}
1066+
10201067
if (dump_c) {
10211068
if (json_output) {
10221069
p_err("JSON output for C-syntax dump is not supported");
@@ -1391,7 +1438,7 @@ static int do_help(int argc, char **argv)
13911438

13921439
fprintf(stderr,
13931440
"Usage: %1$s %2$s { show | list } [id BTF_ID]\n"
1394-
" %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"
13951442
" %1$s %2$s help\n"
13961443
"\n"
13971444
" BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n"

0 commit comments

Comments
 (0)