* [PATCH bpf-next] bpftool: btf: Support dumping a single type from file
@ 2024-12-06 23:29 Daniel Xu
2024-12-07 1:50 ` Martin KaFai Lau
0 siblings, 1 reply; 3+ messages in thread
From: Daniel Xu @ 2024-12-06 23:29 UTC (permalink / raw)
To: ast, kuba, andrii, hawk, qmo, john.fastabend, davem, daniel
Cc: martin.lau, eddyz87, song, yonghong.song, kpsingh, sdf, haoluo,
jolsa, bpf, linux-kernel, netdev, antony, toke
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
Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
---
tools/bpf/bpftool/Documentation/bpftool-btf.rst | 5 +++--
tools/bpf/bpftool/btf.c | 12 ++++++++++++
2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/tools/bpf/bpftool/Documentation/bpftool-btf.rst b/tools/bpf/bpftool/Documentation/bpftool-btf.rst
index 3f6bca03ad2e..5abd0e99022f 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-btf.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-btf.rst
@@ -27,7 +27,7 @@ BTF COMMANDS
| **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*]
| **bpftool** **btf help**
|
-| *BTF_SRC* := { **id** *BTF_ID* | **prog** *PROG* | **map** *MAP* [{**key** | **value** | **kv** | **all**}] | **file** *FILE* }
+| *BTF_SRC* := { **id** *BTF_ID* | **prog** *PROG* | **map** *MAP* [{**key** | **value** | **kv** | **all**}] | **file** *FILE* [**root_id** *ROOT_ID*] }
| *FORMAT* := { **raw** | **c** [**unsorted**] }
| *MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
| *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *PROG_NAME* }
@@ -60,7 +60,8 @@ bpftool btf dump *BTF_SRC*
When specifying *FILE*, an ELF file is expected, containing .BTF section
with well-defined BTF binary format data, typically produced by clang or
- pahole.
+ pahole. You can choose to dump a single type and all its dependent types
+ by providing an optional *ROOT_ID*.
**format** option can be used to override default (raw) output format. Raw
(**raw**) or C-syntax (**c**) output formats are supported. With C-style
diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
index d005e4fd6128..668ff0d10469 100644
--- a/tools/bpf/bpftool/btf.c
+++ b/tools/bpf/bpftool/btf.c
@@ -953,6 +953,7 @@ static int do_dump(int argc, char **argv)
NEXT_ARG();
} else if (is_prefix(src, "file")) {
const char sysfs_prefix[] = "/sys/kernel/btf/";
+ char *end;
if (!base_btf &&
strncmp(*argv, sysfs_prefix, sizeof(sysfs_prefix) - 1) == 0 &&
@@ -967,6 +968,17 @@ static int do_dump(int argc, char **argv)
goto done;
}
NEXT_ARG();
+
+ if (argc && is_prefix(*argv, "root_id")) {
+ NEXT_ARG();
+ root_type_ids[root_type_cnt++] = strtoul(*argv, &end, 0);
+ if (*end) {
+ err = -1;
+ p_err("can't parse %s as root ID", *argv);
+ goto done;
+ }
+ NEXT_ARG();
+ }
} else {
err = -1;
p_err("unrecognized BTF source specifier: '%s'", src);
--
2.46.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH bpf-next] bpftool: btf: Support dumping a single type from file
2024-12-06 23:29 [PATCH bpf-next] bpftool: btf: Support dumping a single type from file Daniel Xu
@ 2024-12-07 1:50 ` Martin KaFai Lau
2024-12-07 23:15 ` Daniel Xu
0 siblings, 1 reply; 3+ messages in thread
From: Martin KaFai Lau @ 2024-12-07 1:50 UTC (permalink / raw)
To: Daniel Xu
Cc: ast, kuba, andrii, hawk, qmo, john.fastabend, davem, daniel,
eddyz87, song, yonghong.song, kpsingh, sdf, haoluo, jolsa, bpf,
linux-kernel, netdev, antony, toke
On 12/6/24 3:29 PM, Daniel Xu wrote:
> diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
> index d005e4fd6128..668ff0d10469 100644
> --- a/tools/bpf/bpftool/btf.c
> +++ b/tools/bpf/bpftool/btf.c
> @@ -953,6 +953,7 @@ static int do_dump(int argc, char **argv)
> NEXT_ARG();
> } else if (is_prefix(src, "file")) {
> const char sysfs_prefix[] = "/sys/kernel/btf/";
> + char *end;
>
> if (!base_btf &&
> strncmp(*argv, sysfs_prefix, sizeof(sysfs_prefix) - 1) == 0 &&
> @@ -967,6 +968,17 @@ static int do_dump(int argc, char **argv)
> goto done;
> }
> NEXT_ARG();
> +
> + if (argc && is_prefix(*argv, "root_id")) {
> + NEXT_ARG();
> + root_type_ids[root_type_cnt++] = strtoul(*argv, &end, 0);
I only looked at the do_dump(). Other existing root_type_ids are from the kernel
map_get_info and they should be valid. I haven't looked at the dump_btf_*, so
ask a lazy question, is an invalid root_id handled properly?
Others lgtm.
> + if (*end) {
> + err = -1;
> + p_err("can't parse %s as root ID", *argv);
> + goto done;
> + }
> + NEXT_ARG();
> + }
> } else {
> err = -1;
> p_err("unrecognized BTF source specifier: '%s'", src);
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH bpf-next] bpftool: btf: Support dumping a single type from file
2024-12-07 1:50 ` Martin KaFai Lau
@ 2024-12-07 23:15 ` Daniel Xu
0 siblings, 0 replies; 3+ messages in thread
From: Daniel Xu @ 2024-12-07 23:15 UTC (permalink / raw)
To: Martin KaFai Lau
Cc: Alexei Starovoitov, Jakub Kicinski, Andrii Nakryiko,
Jesper Dangaard Brouer, qmo, John Fastabend, David Miller,
Daniel Borkmann, Eduard Zingerman, Song Liu, Yonghong Song,
KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
bpf@vger.kernel.org, linux-kernel, netdev, Antony Antony,
Toke Høiland-Jørgensen
On Fri, Dec 6, 2024, at 5:50 PM, Martin KaFai Lau wrote:
> On 12/6/24 3:29 PM, Daniel Xu wrote:
>> diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
>> index d005e4fd6128..668ff0d10469 100644
>> --- a/tools/bpf/bpftool/btf.c
>> +++ b/tools/bpf/bpftool/btf.c
>> @@ -953,6 +953,7 @@ static int do_dump(int argc, char **argv)
>> NEXT_ARG();
>> } else if (is_prefix(src, "file")) {
>> const char sysfs_prefix[] = "/sys/kernel/btf/";
>> + char *end;
>>
>> if (!base_btf &&
>> strncmp(*argv, sysfs_prefix, sizeof(sysfs_prefix) - 1) == 0 &&
>> @@ -967,6 +968,17 @@ static int do_dump(int argc, char **argv)
>> goto done;
>> }
>> NEXT_ARG();
>> +
>> + if (argc && is_prefix(*argv, "root_id")) {
>> + NEXT_ARG();
>> + root_type_ids[root_type_cnt++] = strtoul(*argv, &end, 0);
>
> I only looked at the do_dump(). Other existing root_type_ids are from
> the kernel
> map_get_info and they should be valid. I haven't looked at the
> dump_btf_*, so
> ask a lazy question, is an invalid root_id handled properly?
>
> Others lgtm.
Good question. Passing an invalid btf ID results in half the
boilerplate being printed to terminal before an early exit and
an unclean return code.
Probably not be the best way to error. I'll send v2 with an
earlier error check.
Thanks,
Daniel
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-12-07 23:15 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-06 23:29 [PATCH bpf-next] bpftool: btf: Support dumping a single type from file Daniel Xu
2024-12-07 1:50 ` Martin KaFai Lau
2024-12-07 23:15 ` Daniel Xu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox