Re: [RFC bpf-next 7/8] bpftool: add BTF dump "format meta" to dump header/metadata

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2023-05-31 21:19 UTC+0100 ~ Alan Maguire <alan.maguire@xxxxxxxxxx>
> 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@xxxxxxxxxx>
> ---
>  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);
>  	}





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux