Skip to content

Commit 93701ec

Browse files
anakryikokernel-patches-bot
authored andcommitted
tools/bpftool: add bpftool support for split BTF
Add ability to work with split BTF by providing extra -B flag, which allows to specify the path to the base BTF file. Signed-off-by: Andrii Nakryiko <[email protected]>
1 parent fcedce6 commit 93701ec

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

tools/bpf/bpftool/btf.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,12 @@ static int dump_btf_raw(const struct btf *btf,
358358
}
359359
} else {
360360
int cnt = btf__get_nr_types(btf);
361+
int start_id = 1;
361362

362-
for (i = 1; i <= cnt; i++) {
363+
if (base_btf)
364+
start_id = btf__get_nr_types(base_btf) + 1;
365+
366+
for (i = start_id; i <= cnt; i++) {
363367
t = btf__type_by_id(btf, i);
364368
dump_btf_type(btf, i, t);
365369
}
@@ -438,7 +442,6 @@ static int do_dump(int argc, char **argv)
438442
return -1;
439443
}
440444
src = GET_ARG();
441-
442445
if (is_prefix(src, "map")) {
443446
struct bpf_map_info info = {};
444447
__u32 len = sizeof(info);
@@ -499,7 +502,7 @@ static int do_dump(int argc, char **argv)
499502
}
500503
NEXT_ARG();
501504
} else if (is_prefix(src, "file")) {
502-
btf = btf__parse(*argv, NULL);
505+
btf = btf__parse_split(*argv, base_btf);
503506
if (IS_ERR(btf)) {
504507
err = -PTR_ERR(btf);
505508
btf = NULL;

tools/bpf/bpftool/main.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <bpf/bpf.h>
1313
#include <bpf/libbpf.h>
14+
#include <bpf/btf.h>
1415

1516
#include "main.h"
1617

@@ -28,6 +29,7 @@ bool show_pinned;
2829
bool block_mount;
2930
bool verifier_logs;
3031
bool relaxed_maps;
32+
struct btf *base_btf;
3133
struct pinned_obj_table prog_table;
3234
struct pinned_obj_table map_table;
3335
struct pinned_obj_table link_table;
@@ -391,6 +393,7 @@ int main(int argc, char **argv)
391393
{ "mapcompat", no_argument, NULL, 'm' },
392394
{ "nomount", no_argument, NULL, 'n' },
393395
{ "debug", no_argument, NULL, 'd' },
396+
{ "base-btf", required_argument, NULL, 'B' },
394397
{ 0 }
395398
};
396399
int opt, ret;
@@ -407,7 +410,7 @@ int main(int argc, char **argv)
407410
hash_init(link_table.table);
408411

409412
opterr = 0;
410-
while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
413+
while ((opt = getopt_long(argc, argv, "VhpjfmndB:",
411414
options, NULL)) >= 0) {
412415
switch (opt) {
413416
case 'V':
@@ -441,6 +444,15 @@ int main(int argc, char **argv)
441444
libbpf_set_print(print_all_levels);
442445
verifier_logs = true;
443446
break;
447+
case 'B':
448+
base_btf = btf__parse(optarg, NULL);
449+
if (libbpf_get_error(base_btf)) {
450+
p_err("failed to parse base BTF at '%s': %ld\n",
451+
optarg, libbpf_get_error(base_btf));
452+
base_btf = NULL;
453+
return -1;
454+
}
455+
break;
444456
default:
445457
p_err("unrecognized option '%s'", argv[optind - 1]);
446458
if (json_output)
@@ -465,6 +477,7 @@ int main(int argc, char **argv)
465477
delete_pinned_obj_table(&map_table);
466478
delete_pinned_obj_table(&link_table);
467479
}
480+
btf__free(base_btf);
468481

469482
return ret;
470483
}

tools/bpf/bpftool/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ extern bool show_pids;
9090
extern bool block_mount;
9191
extern bool verifier_logs;
9292
extern bool relaxed_maps;
93+
extern struct btf *base_btf;
9394
extern struct pinned_obj_table prog_table;
9495
extern struct pinned_obj_table map_table;
9596
extern struct pinned_obj_table link_table;

0 commit comments

Comments
 (0)