bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Quentin Monnet <quentin@isovalent.com>
To: Alan Maguire <alan.maguire@oracle.com>,
	ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	acme@kernel.org
Cc: martin.lau@linux.dev, song@kernel.org, yhs@fb.com,
	john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com,
	haoluo@google.com, jolsa@kernel.org, mykolal@fb.com,
	bpf@vger.kernel.org
Subject: Re: [RFC bpf-next 7/8] bpftool: add BTF dump "format meta" to dump header/metadata
Date: Thu, 1 Jun 2023 17:33:59 +0100	[thread overview]
Message-ID: <7c8ecb4f-9db0-cbae-77ca-aea5d51196de@isovalent.com> (raw)
In-Reply-To: <20230531201936.1992188-8-alan.maguire@oracle.com>

2023-05-31 21:19 UTC+0100 ~ Alan Maguire <alan.maguire@oracle.com>
> Provide a way to dump BTF header and metadata info via
> bpftool; for example
> 
> $ bpftool btf dump file vmliux format meta

Typo: vmliux

> BTF: data size 4963656
> Header: magic 0xeb9f, version 1, flags 0x0, hdr_len 32
> Types: len 2927556, offset 0
> Strings: len 2035881, offset 2927556
> Metadata header found: len 184, offset 4963440, flags 0x1
> Description: 'generated by dwarves v1.25'
> CRC 0x6da2a930 ; base CRC 0x0
> Kind metadata for 20 kinds:
>        BTF_KIND_UNKN[ 0] flags 0x0    info_sz  0 elem_sz  0
>         BTF_KIND_INT[ 1] flags 0x0    info_sz  4 elem_sz  0
>         BTF_KIND_PTR[ 2] flags 0x0    info_sz  0 elem_sz  0
>       BTF_KIND_ARRAY[ 3] flags 0x0    info_sz 12 elem_sz  0
>      BTF_KIND_STRUCT[ 4] flags 0x0    info_sz  0 elem_sz 12
>       BTF_KIND_UNION[ 5] flags 0x0    info_sz  0 elem_sz 12
>        BTF_KIND_ENUM[ 6] flags 0x0    info_sz  0 elem_sz  8
>         BTF_KIND_FWD[ 7] flags 0x0    info_sz  0 elem_sz  0
>     BTF_KIND_TYPEDEF[ 8] flags 0x0    info_sz  0 elem_sz  0
>    BTF_KIND_VOLATILE[ 9] flags 0x0    info_sz  0 elem_sz  0
>       BTF_KIND_CONST[10] flags 0x0    info_sz  0 elem_sz  0
>    BTF_KIND_RESTRICT[11] flags 0x0    info_sz  0 elem_sz  0
>        BTF_KIND_FUNC[12] flags 0x0    info_sz  0 elem_sz  0
>  BTF_KIND_FUNC_PROTO[13] flags 0x0    info_sz  0 elem_sz  8
>         BTF_KIND_VAR[14] flags 0x0    info_sz  4 elem_sz  0
>     BTF_KIND_DATASEC[15] flags 0x0    info_sz  0 elem_sz 12
>       BTF_KIND_FLOAT[16] flags 0x0    info_sz  0 elem_sz  0
>    BTF_KIND_DECL_TAG[17] flags 0x1    info_sz  4 elem_sz  0
>    BTF_KIND_TYPE_TAG[18] flags 0x1    info_sz  0 elem_sz  0
>      BTF_KIND_ENUM64[19] flags 0x0    info_sz  0 elem_sz 12
> 

Thanks for this! For the non-RFC, can you please add the following:

- JSON output
- btf.c's do_help() update
- Documentation/bpftool-btf.rst update (cmd summary, and description)
- bash-completion/bpftool update (should be straightforward, we just
need to offer "metadata" after "format", like we already offer "c" and
"raw".

> Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
> ---
>  tools/bpf/bpftool/btf.c | 46 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 46 insertions(+)
> 
> diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
> index 91fcb75babe3..da4257e00ba8 100644
> --- a/tools/bpf/bpftool/btf.c
> +++ b/tools/bpf/bpftool/btf.c
> @@ -504,6 +504,47 @@ static int dump_btf_c(const struct btf *btf,
>  	return err;
>  }
>  
> +static int dump_btf_meta(const struct btf *btf)
> +{
> +	const struct btf_header *hdr;
> +	const struct btf_metadata *m;
> +	const void *data;
> +	__u32 data_sz;
> +	__u8 i;
> +
> +	data = btf__raw_data(btf, &data_sz);
> +	if (!data)
> +		return -ENOMEM;
> +	hdr = data;
> +	printf("BTF: data size %u\n", data_sz);
> +	printf("Header: magic 0x%x, version %d, flags 0x%x, hdr_len %u\n",
> +	       hdr->magic, hdr->version, hdr->flags, hdr->hdr_len);

Nit: bpftool's output fields don't usually start with a capital letter
(save for acronyms, obviously). I don't mind much, though.

> +	printf("Types: len %u, offset %u\n", hdr->type_len, hdr->type_off);
> +	printf("Strings: len %u, offset %u\n", hdr->str_len, hdr->str_off);
> +
> +	if (hdr->hdr_len < sizeof(struct btf_header) ||
> +	    hdr->meta_header.meta_len == 0 ||
> +	    hdr->meta_header.meta_off == 0)
> +		return 0;
> +
> +	m = (void *)hdr + hdr->hdr_len + hdr->meta_header.meta_off;
> +
> +	printf("Metadata header found: len %u, offset %u, flags 0x%x\n",
> +	       hdr->meta_header.meta_len, hdr->meta_header.meta_off, m->flags);
> +	if (m->description_off)
> +		printf("Description: '%s'\n", btf__name_by_offset(btf, m->description_off));
> +	printf("CRC 0x%x ; base CRC 0x%x\n", m->crc, m->base_crc);
> +	printf("Kind metadata for %d kinds:\n", m->kind_meta_cnt);
> +	for (i = 0; i < m->kind_meta_cnt; i++) {
> +		printf("%20s[%2d] flags 0x%-4x info_sz %2d elem_sz %2d\n",
> +		       btf__name_by_offset(btf, m->kind_meta[i].name_off),
> +		       i, m->kind_meta[i].flags, m->kind_meta[i].info_sz,
> +		       m->kind_meta[i].elem_sz);

Nit: I would maybe add a double space for separating the different
field, especially because we have some left padding for values < 10 in
your example and it looks strange to have numbers closer to the next
field name (on their right) rather than their own (on their left).

> +	}
> +
> +	return 0;
> +}
> +
>  static const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux";
>  
>  static struct btf *get_vmlinux_btf_from_sysfs(void)
> @@ -553,6 +594,7 @@ static int do_dump(int argc, char **argv)
>  	__u32 root_type_ids[2];
>  	int root_type_cnt = 0;
>  	bool dump_c = false;
> +	bool dump_meta = false;
>  	__u32 btf_id = -1;
>  	const char *src;
>  	int fd = -1;
> @@ -654,6 +696,8 @@ static int do_dump(int argc, char **argv)
>  			}
>  			if (strcmp(*argv, "c") == 0) {
>  				dump_c = true;
> +			} else if (strcmp(*argv, "meta") == 0) {
> +				dump_meta = true;

We could use is_prefix() instead of strcmp() (same for "raw" below, by
the way), to make it possible to pass the keyword by prefix (as in
"bpftool b d f vmlinux f m".

>  			} else if (strcmp(*argv, "raw") == 0) {
>  				dump_c = false;
>  			} else {
> @@ -692,6 +736,8 @@ static int do_dump(int argc, char **argv)
>  			goto done;
>  		}
>  		err = dump_btf_c(btf, root_type_ids, root_type_cnt);
> +	} else if (dump_meta) {
> +		err = dump_btf_meta(btf);
>  	} else {
>  		err = dump_btf_raw(btf, root_type_ids, root_type_cnt);
>  	}


  reply	other threads:[~2023-06-01 16:34 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-31 20:19 [RFC bpf-next 0/8] bpf: support BTF kind metadata to separate Alan Maguire
2023-05-31 20:19 ` [RFC bpf-next 1/8] btf: add kind metadata encoding to UAPI Alan Maguire
2023-06-01  3:53   ` Alexei Starovoitov
2023-06-01 10:36     ` Alan Maguire
2023-06-01 16:53       ` Alexei Starovoitov
2023-06-02 16:32         ` Andrii Nakryiko
2023-06-02 16:34           ` Andrii Nakryiko
2023-06-02 18:11           ` Alexei Starovoitov
2023-06-02 20:33             ` Andrii Nakryiko
2023-06-05 16:14               ` Alexei Starovoitov
2023-06-05 22:38                 ` Andrii Nakryiko
2023-06-06  2:46                   ` Alexei Starovoitov
2023-06-06 11:30                     ` Toke Høiland-Jørgensen
2023-06-07 11:55                       ` Eduard Zingerman
2023-06-07 15:29                         ` Yonghong Song
2023-06-07 16:14                           ` Eduard Zingerman
2023-06-07 21:47                             ` Andrii Nakryiko
2023-06-07 22:05                               ` Eduard Zingerman
2023-06-07 22:34                                 ` Andrii Nakryiko
2023-06-06 16:50                     ` Andrii Nakryiko
2023-06-07  1:16                       ` Alexei Starovoitov
2023-06-07 21:43                         ` Andrii Nakryiko
2023-05-31 20:19 ` [RFC bpf-next 2/8] libbpf: support handling of metadata section in BTF Alan Maguire
2023-06-05 11:01   ` Jiri Olsa
2023-06-05 21:40     ` Andrii Nakryiko
2023-05-31 20:19 ` [RFC bpf-next 3/8] libbpf: use metadata to compute an unknown kind size Alan Maguire
2023-05-31 20:19 ` [RFC bpf-next 4/8] btf: support kernel parsing of BTF with metadata, use it to parse BTF with unknown kinds Alan Maguire
2023-06-07 19:51   ` Eduard Zingerman
2023-05-31 20:19 ` [RFC bpf-next 5/8] libbpf: add metadata encoding support Alan Maguire
2023-05-31 20:19 ` [RFC bpf-next 6/8] btf: generate metadata for vmlinux/module BTF Alan Maguire
2023-05-31 20:19 ` [RFC bpf-next 7/8] bpftool: add BTF dump "format meta" to dump header/metadata Alan Maguire
2023-06-01 16:33   ` Quentin Monnet [this message]
2023-06-02 16:57   ` Andrii Nakryiko
2023-05-31 20:19 ` [RFC bpf-next 8/8] selftests/bpf: test kind encoding/decoding Alan Maguire
2023-05-31 20:19 ` [RFC dwarves] dwarves: encode BTF metadata if --btf_gen_meta is set Alan Maguire

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7c8ecb4f-9db0-cbae-77ca-aea5d51196de@isovalent.com \
    --to=quentin@isovalent.com \
    --cc=acme@kernel.org \
    --cc=alan.maguire@oracle.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mykolal@fb.com \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).