bpftool: Adding support for BTF program names
`bpftool prog list` and other bpftool subcommands that show BPF program names currently get them from bpf_prog_info.name. That field is limited to 16 (BPF_OBJ_NAME_LEN) chars which leads to truncated names since many progs have much longer names. The idea of this change is to improve all bpftool commands that output prog name so that bpftool uses info from BTF to print program names if available. It tries bpf_prog_info.name first and fall back to btf only if the name is suspected to be truncated (has 15 chars length). Right now `bpftool p show id <id>` returns capped prog name <id>: kprobe name example_cap_cap tag 712e... ... With this change it would return <id>: kprobe name example_cap_capable tag 712e... ... Note, other commands that print prog names (e.g. "bpftool cgroup tree") are also addressed in this change. Signed-off-by: Raman Shukhau <ramasha@fb.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20220119100255.1068997-1-ramasha@fb.com
This commit is contained in:
committed by
Andrii Nakryiko
parent
eaa266d83a
commit
b662000aff
@ -424,8 +424,10 @@ out_free:
|
||||
free(value);
|
||||
}
|
||||
|
||||
static void print_prog_header_json(struct bpf_prog_info *info)
|
||||
static void print_prog_header_json(struct bpf_prog_info *info, int fd)
|
||||
{
|
||||
char prog_name[MAX_PROG_FULL_NAME];
|
||||
|
||||
jsonw_uint_field(json_wtr, "id", info->id);
|
||||
if (info->type < ARRAY_SIZE(prog_type_name))
|
||||
jsonw_string_field(json_wtr, "type",
|
||||
@ -433,8 +435,10 @@ static void print_prog_header_json(struct bpf_prog_info *info)
|
||||
else
|
||||
jsonw_uint_field(json_wtr, "type", info->type);
|
||||
|
||||
if (*info->name)
|
||||
jsonw_string_field(json_wtr, "name", info->name);
|
||||
if (*info->name) {
|
||||
get_prog_full_name(info, fd, prog_name, sizeof(prog_name));
|
||||
jsonw_string_field(json_wtr, "name", prog_name);
|
||||
}
|
||||
|
||||
jsonw_name(json_wtr, "tag");
|
||||
jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
|
||||
@ -455,7 +459,7 @@ static void print_prog_json(struct bpf_prog_info *info, int fd)
|
||||
char *memlock;
|
||||
|
||||
jsonw_start_object(json_wtr);
|
||||
print_prog_header_json(info);
|
||||
print_prog_header_json(info, fd);
|
||||
print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
|
||||
|
||||
if (info->load_time) {
|
||||
@ -507,16 +511,20 @@ static void print_prog_json(struct bpf_prog_info *info, int fd)
|
||||
jsonw_end_object(json_wtr);
|
||||
}
|
||||
|
||||
static void print_prog_header_plain(struct bpf_prog_info *info)
|
||||
static void print_prog_header_plain(struct bpf_prog_info *info, int fd)
|
||||
{
|
||||
char prog_name[MAX_PROG_FULL_NAME];
|
||||
|
||||
printf("%u: ", info->id);
|
||||
if (info->type < ARRAY_SIZE(prog_type_name))
|
||||
printf("%s ", prog_type_name[info->type]);
|
||||
else
|
||||
printf("type %u ", info->type);
|
||||
|
||||
if (*info->name)
|
||||
printf("name %s ", info->name);
|
||||
if (*info->name) {
|
||||
get_prog_full_name(info, fd, prog_name, sizeof(prog_name));
|
||||
printf("name %s ", prog_name);
|
||||
}
|
||||
|
||||
printf("tag ");
|
||||
fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
|
||||
@ -534,7 +542,7 @@ static void print_prog_plain(struct bpf_prog_info *info, int fd)
|
||||
{
|
||||
char *memlock;
|
||||
|
||||
print_prog_header_plain(info);
|
||||
print_prog_header_plain(info, fd);
|
||||
|
||||
if (info->load_time) {
|
||||
char buf[32];
|
||||
@ -972,10 +980,10 @@ static int do_dump(int argc, char **argv)
|
||||
|
||||
if (json_output && nb_fds > 1) {
|
||||
jsonw_start_object(json_wtr); /* prog object */
|
||||
print_prog_header_json(&info);
|
||||
print_prog_header_json(&info, fds[i]);
|
||||
jsonw_name(json_wtr, "insns");
|
||||
} else if (nb_fds > 1) {
|
||||
print_prog_header_plain(&info);
|
||||
print_prog_header_plain(&info, fds[i]);
|
||||
}
|
||||
|
||||
err = prog_dump(&info, mode, filepath, opcodes, visual, linum);
|
||||
|
Reference in New Issue
Block a user