Skip to content

Commit 0d1e026

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpftool: improve split BTF support'
Andrii Nakryiko says: ==================== Few follow up improvements to bpftool for split BTF support: - emit "name <anon>" for non-named BTFs in `bpftool btf show` command; - when dumping /sys/kernel/btf/<module> use /sys/kernel/btf/vmlinux as the base BTF, unless base BTF is explicitly specified with -B flag. This patch set also adds btf__base_btf() getter to access base BTF of the struct btf. ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 97306be + fa45283 commit 0d1e026

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

tools/bpf/bpftool/btf.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,13 @@ static int dump_btf_raw(const struct btf *btf,
357357
dump_btf_type(btf, root_type_ids[i], t);
358358
}
359359
} else {
360+
const struct btf *base;
360361
int cnt = btf__get_nr_types(btf);
361362
int start_id = 1;
362363

363-
if (base_btf)
364-
start_id = btf__get_nr_types(base_btf) + 1;
364+
base = btf__base_btf(btf);
365+
if (base)
366+
start_id = btf__get_nr_types(base) + 1;
365367

366368
for (i = start_id; i <= cnt; i++) {
367369
t = btf__type_by_id(btf, i);
@@ -428,7 +430,7 @@ static int dump_btf_c(const struct btf *btf,
428430

429431
static int do_dump(int argc, char **argv)
430432
{
431-
struct btf *btf = NULL;
433+
struct btf *btf = NULL, *base = NULL;
432434
__u32 root_type_ids[2];
433435
int root_type_cnt = 0;
434436
bool dump_c = false;
@@ -502,7 +504,21 @@ static int do_dump(int argc, char **argv)
502504
}
503505
NEXT_ARG();
504506
} else if (is_prefix(src, "file")) {
505-
btf = btf__parse_split(*argv, base_btf);
507+
const char sysfs_prefix[] = "/sys/kernel/btf/";
508+
const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux";
509+
510+
if (!base_btf &&
511+
strncmp(*argv, sysfs_prefix, sizeof(sysfs_prefix) - 1) == 0 &&
512+
strcmp(*argv, sysfs_vmlinux) != 0) {
513+
base = btf__parse(sysfs_vmlinux, NULL);
514+
if (libbpf_get_error(base)) {
515+
p_err("failed to parse vmlinux BTF at '%s': %ld\n",
516+
sysfs_vmlinux, libbpf_get_error(base));
517+
base = NULL;
518+
}
519+
}
520+
521+
btf = btf__parse_split(*argv, base ?: base_btf);
506522
if (IS_ERR(btf)) {
507523
err = -PTR_ERR(btf);
508524
btf = NULL;
@@ -567,6 +583,7 @@ static int do_dump(int argc, char **argv)
567583
done:
568584
close(fd);
569585
btf__free(btf);
586+
btf__free(base);
570587
return err;
571588
}
572589

@@ -750,6 +767,8 @@ show_btf_plain(struct bpf_btf_info *info, int fd,
750767
printf("name [%s] ", name);
751768
else if (name && name[0])
752769
printf("name %s ", name);
770+
else
771+
printf("name <anon> ");
753772
printf("size %uB", info->btf_size);
754773

755774
n = 0;

tools/lib/bpf/btf.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,11 @@ __u32 btf__get_nr_types(const struct btf *btf)
432432
return btf->start_id + btf->nr_types - 1;
433433
}
434434

435+
const struct btf *btf__base_btf(const struct btf *btf)
436+
{
437+
return btf->base_btf;
438+
}
439+
435440
/* internal helper returning non-const pointer to a type */
436441
static struct btf_type *btf_type_by_id(struct btf *btf, __u32 type_id)
437442
{

tools/lib/bpf/btf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
5151
LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf,
5252
const char *type_name, __u32 kind);
5353
LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);
54+
LIBBPF_API const struct btf *btf__base_btf(const struct btf *btf);
5455
LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
5556
__u32 id);
5657
LIBBPF_API size_t btf__pointer_size(const struct btf *btf);

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ LIBBPF_0.2.0 {
340340

341341
LIBBPF_0.3.0 {
342342
global:
343+
btf__base_btf;
343344
btf__parse_elf_split;
344345
btf__parse_raw_split;
345346
btf__parse_split;

0 commit comments

Comments
 (0)