tools/bpf: do not use pahole if clang/llvm can generate BTF sections
Subsystems (7)
PATCH v4 10/13 tools/bpf: do not use pahole if clang/llvm can generate BTF sections
Add additional checks in tools/testing/selftests/bpf and samples/bpf such that if clang/llvm compiler can generate BTF sections, do not use pahole.
--- samples/bpf/Makefile | 8 ++++++++ tools/testing/selftests/bpf/Makefile | 8 ++++++++ 2 files changed, 16 insertions(+)
samples/bpf/Makefile +8 -0
@@ -208,12 +208,20 @@ endif BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris) BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF) BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm') +BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \ + $(CLANG) -target bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \ + readelf -S ./llvm_btf_verify.o | grep BTF; \ + /bin/rm -f ./llvm_btf_verify.o) +ifneq ($(BTF_LLVM_PROBE),) + EXTRA_CFLAGS += -g +else ifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),) EXTRA_CFLAGS += -g LLC_FLAGS += -mattr=dwarfris DWARF2BTF = y endif +endif # Trick to allow make to be run from this directory all:
tools/testing/selftests/bpf/Makefile +8 -0
@@ -124,7 +124,14 @@ $(OUTPUT)/test_stack_map.o: test_queue_stack_map.h BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris) BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF) BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm') +BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \ + $(CLANG) -target bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \ + readelf -S ./llvm_btf_verify.o | grep BTF; \ + /bin/rm -f ./llvm_btf_verify.o) +ifneq ($(BTF_LLVM_PROBE),) + CLANG_FLAGS += -g +else ifneq ($(BTF_LLC_PROBE),) ifneq ($(BTF_PAHOLE_PROBE),) ifneq ($(BTF_OBJCOPY_PROBE),)
@@ -134,6 +141,7 @@ ifneq ($(BTF_OBJCOPY_PROBE),) endif endif endif +endif $(OUTPUT)/%.o: %.c $(CLANG) $(CLANG_FLAGS) \
Signature · 1 line
2.17.1
PATCH v4 11/13 tools/bpf: refactor to implement btf_get_from_id() in lib/bpf
The function get_btf() is implemented in tools/bpf/bpftool/map.c to get a btf structure given a map_info. This patch refactored this function to be function btf_get_from_id() in tools/lib/bpf so that it can be used later.
--- tools/bpf/bpftool/map.c | 68 ++-------------------------------------- tools/lib/bpf/btf.c | 69 +++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 1 + 3 files changed, 72 insertions(+), 66 deletions(-)
tools/bpf/bpftool/map.c +2 -66
@@ -215,70 +215,6 @@ static int do_dump_btf(const struct btf_dumper *d,return ret; } -static int get_btf (struct bpf_map_info *map_info,struct btf **btf) -{ -struct bpf_btf_info btf_info = {0 }; -__u32 len =sizeof (btf_info); -__u32 last_size; -int btf_fd; -void *ptr; -int err; - - err =0 ; - *btf =NULL ; - btf_fd =bpf_btf_get_fd_by_id (map_info->btf_id); -if (btf_fd <0 ) -return 0 ; - -/* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so -* let's start with a sane default - 4KiB here - and resize it only if -* bpf_obj_get_info_by_fd() needs a bigger buffer. -*/ - btf_info.btf_size =4096 ; - last_size = btf_info.btf_size; - ptr =malloc (last_size); -if (!ptr) { - err = -ENOMEM ; - goto exit_free; - } - -bzero (ptr, last_size); - btf_info.btf =ptr_to_u64 (ptr); - err =bpf_obj_get_info_by_fd (btf_fd, &btf_info, &len); - -if (!err && btf_info.btf_size > last_size) { -void *temp_ptr; - - last_size = btf_info.btf_size; - temp_ptr =realloc (ptr, last_size); -if (!temp_ptr) { - err = -ENOMEM ; - goto exit_free; - } - ptr = temp_ptr; -bzero (ptr, last_size); - btf_info.btf =ptr_to_u64 (ptr); - err =bpf_obj_get_info_by_fd (btf_fd, &btf_info, &len); - } - -if (err || btf_info.btf_size > last_size) { - err = errno; - goto exit_free; - } - - *btf =btf__new ((__u8 *)btf_info.btf, btf_info.btf_size,NULL ); -if (IS_ERR (*btf)) { - err =PTR_ERR (*btf); - *btf =NULL ; - } - -exit_free: -close (btf_fd); -free (ptr); - -return err; -} -static json_writer_t *get_btf_writer (void ) {json_writer_t *jw =jsonw_new (stdout);
@@ -765,7 +701,7 @@ static int do_dump(int argc, char **argv) prev_key =NULL ; - err =get_btf (&info, &btf); + err =btf_get_from_id (info.btf_id, &btf);if (err) {p_err ("failed to get btf" ); goto exit_free;
@@ -909,7 +845,7 @@ static int do_lookup(int argc, char **argv) }/* here means bpf_map_lookup_elem() succeeded */ - err =get_btf (&info, &btf); + err =btf_get_from_id (info.btf_id, &btf);if (err) {p_err ("failed to get btf" ); goto exit_free;
tools/lib/bpf/btf.c +69 -0
81 lines not on this page · raw message
tools/lib/bpf/btf.h +1 -0
@@ -69,6 +69,7 @@ LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id); LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id); LIBBPF_API int btf__fd(const struct btf *btf); LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset); +LIBBPF_API int btf_get_from_id(__u32 id, struct btf **btf);struct btf_ext *btf_ext__new (__u8 *data,__u32 size,btf_print_fn_t err_log);void btf_ext__free (struct btf_ext *btf_ext);
Signature · 1 line
2.17.1
PATCH v4 12/13 tools/bpf: enhance test_btf file testing to test func info
In reply to Yonghong SongChange the bpf programs test_btf_haskv.c and test_btf_nokv.c to have two sections, and enhance test_btf.c test_file feature to test btf func_info returned by the kernel.
--- tools/testing/selftests/bpf/test_btf.c | 117 +++++++++++++++++-- tools/testing/selftests/bpf/test_btf_haskv.c | 16 ++- tools/testing/selftests/bpf/test_btf_nokv.c | 16 ++- 3 files changed, 136 insertions(+), 13 deletions(-)
tools/testing/selftests/bpf/test_btf.c +108 -9
@@ -2226,13 +2226,13 @@ static struct btf_file_test file_tests[] = { }, }; -static int file_has_btf_elf (const char *fn) +static int file_has_btf_elf (const char *fn,bool *has_btf_ext) {Elf_Scn *scn =NULL ;GElf_Ehdr ehdr; +int ret =0 ;int elf_fd;Elf *elf; -int ret;if (CHECK (elf_version (EV_CURRENT ) ==EV_NONE ,"elf_version(EV_CURRENT) == EV_NONE" ))
@@ -2264,14 +2264,12 @@ static int file_has_btf_elf(const char *fn) } sh_name =elf_strptr (elf, ehdr.e_shstrndx, sh.sh_name); -if (!strcmp (sh_name,BTF_ELF_SEC )) { +if (!strcmp (sh_name,BTF_ELF_SEC )) ret =1 ; - goto done; - } +if (!strcmp (sh_name,BTF_EXT_ELF_SEC )) + *has_btf_ext = true; } - ret =0 ; - done:close (elf_fd);elf_end (elf);
@@ -2281,15 +2279,24 @@ static int file_has_btf_elf(const char *fn)static int do_test_file (unsigned int test_num) {const struct btf_file_test *test = &file_tests[test_num -1 ]; +const char *expected_fnames[] = {"_dummy_tracepoint" , +"test_long_fname_1" , +"test_long_fname_2" }; +struct bpf_prog_info info = {};struct bpf_object *obj =NULL ; +struct bpf_func_info *finfo;struct bpf_program *prog; +__u32 info_len, rec_size; +bool has_btf_ext = false; +struct btf *btf =NULL ; +void *func_info =NULL ;struct bpf_map *map; -int err; +int i, err, prog_fd;fprintf (stderr,"BTF libbpf test[%u] (%s): " , test_num, test->file); - err =file_has_btf_elf (test->file); + err =file_has_btf_elf (test->file, &has_btf_ext);if (err ==-1 )return err;
@@ -2317,6 +2324,7 @@ static int do_test_file(unsigned int test_num) err =bpf_object__load (obj);if (CHECK (err <0 ,"bpf_object__load: %d" , err)) goto done; + prog_fd =bpf_program__fd (prog); map =bpf_object__find_map_by_name (obj,"btf_map" );if (CHECK (!map,"btf_map not found" )) {
@@ -2331,9 +2339,100 @@ static int do_test_file(unsigned int test_num) test->btf_kv_notfound)) goto done; +if (!jit_enabled || !has_btf_ext) + goto skip_jit; + +/* get necessary program info */ + info_len =sizeof (struct bpf_prog_info ); + err =bpf_obj_get_info_by_fd (prog_fd, &info, &info_len); + +if (CHECK (err ==-1 ,"invalid get info (1st) errno:%d" , errno)) { +fprintf (stderr,"%s\n" , btf_log_buf); + err =-1 ; + goto done; + } +if (CHECK (info.func_info_cnt !=3 , +"incorrect info.func_info_cnt (1st) %d" , + info.func_info_cnt)) { + err =-1 ; + goto done; + } + rec_size = info.func_info_rec_size; +if (CHECK (rec_size <4 , +"incorrect info.func_info_rec_size (1st) %d\n" , rec_size)) { + err =-1 ; + goto done; + } + + func_info =malloc (info.func_info_cnt * rec_size); +if (CHECK (!func_info,"out of memeory" )) { + err =-1 ; + goto done; + } + +/* reset info to only retrieve func_info related data */ +memset (&info,0 ,sizeof (info)); + info.func_info_cnt =3 ; + info.func_info_rec_size = rec_size; + info.func_info =ptr_to_u64 (func_info); + + err =bpf_obj_get_info_by_fd (prog_fd, &info, &info_len); + +if (CHECK (err ==-1 ,"invalid get info (2nd) errno:%d" , errno)) { +fprintf (stderr,"%s\n" , btf_log_buf); + err =-1 ; + goto done; + } +if (CHECK (info.func_info_cnt !=3 , +"incorrect info.func_info_cnt (2nd) %d" , + info.func_info_cnt)) { + err =-1 ; + goto done; + } +if (CHECK (info.func_info_rec_size != rec_size, +"incorrect info.func_info_rec_size (2nd) %d" , + info.func_info_rec_size)) { + err =-1 ; + goto done; + } + + err =btf_get_from_id (info.btf_id, &btf); +if (CHECK (err,"cannot get btf from kernel, err: %d" , err)) + goto done; + +/* check three functions */ + finfo = func_info; +for (i =0 ; i <3 ; i++) { +const struct btf_type *t; +const char *fname; + + t =btf__type_by_id (btf, finfo->type_id); +if (CHECK (!t,"btf__type_by_id failure: id %u" , + finfo->type_id)) { + err =-1 ; + goto done; + } + + fname =btf__name_by_offset (btf, t->name_off); + err =strcmp (fname, expected_fnames[i]); +/* for the second and third functions in .text section, +* the compiler may order them either way. +*/ +if (i && err) + err =strcmp (fname, expected_fnames[3 - i]); +if (CHECK (err,"incorrect fname %s" , fname ? :"" )) { + err =-1 ; + goto done; + } + + finfo = (void *)finfo + rec_size; + } + +skip_jit:fprintf (stderr,"OK" ); done: +free (func_info);bpf_object__close (obj);return err; }
tools/testing/selftests/bpf/test_btf_haskv.c +14 -2
@@ -24,8 +24,8 @@ struct dummy_tracepoint_args {struct sock *sock; }; -SEC("dummy_tracepoint") -int _dummy_tracepoint (struct dummy_tracepoint_args *arg) +__attribute__((noinline)) +static int test_long_fname_2 (struct dummy_tracepoint_args *arg) {struct ipv_counts *counts;int key =0 ;
@@ -42,4 +42,16 @@ int _dummy_tracepoint(struct dummy_tracepoint_args *arg)return 0 ; } +__attribute__((noinline)) +static int test_long_fname_1 (struct dummy_tracepoint_args *arg) +{ +return test_long_fname_2 (arg); +} + +SEC("dummy_tracepoint") +int _dummy_tracepoint (struct dummy_tracepoint_args *arg) +{ +return test_long_fname_1 (arg); +} + char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_btf_nokv.c +14 -2
@@ -22,8 +22,8 @@ struct dummy_tracepoint_args {struct sock *sock; }; -SEC("dummy_tracepoint") -int _dummy_tracepoint (struct dummy_tracepoint_args *arg) +__attribute__((noinline)) +static int test_long_fname_2 (struct dummy_tracepoint_args *arg) {struct ipv_counts *counts;int key =0 ;
@@ -40,4 +40,16 @@ int _dummy_tracepoint(struct dummy_tracepoint_args *arg)return 0 ; } +__attribute__((noinline)) +static int test_long_fname_1 (struct dummy_tracepoint_args *arg) +{ +return test_long_fname_2 (arg); +} + +SEC("dummy_tracepoint") +int _dummy_tracepoint (struct dummy_tracepoint_args *arg) +{ +return test_long_fname_1 (arg); +} + char _license[] SEC("license") = "GPL";
Signature · 1 line
2.17.1
PATCH v4 13/13 tools/bpf: bpftool: add support for func types
In reply to Yonghong SongThis patch added support to print function signature if btf func_info is available. Note that ksym now uses function name instead of prog_name as prog_name has a limit of 16 bytes including ending '\0'.
The following is a sample output for selftests test_btf with file test_btf_haskv.o for translated insns and jited insns respectively.
$ bpftool prog dump xlated id 1
int _dummy_tracepoint(struct dummy_tracepoint_args * arg):
0: (85) call pc+2#bpf_prog_2dcecc18072623fc_test_long_fname_1
1: (b7) r0 = 0
2: (95) exit
int test_long_fname_1(struct dummy_tracepoint_args * arg):
3: (85) call pc+1#bpf_prog_89d64e4abf0f0126_test_long_fname_2
4: (95) exit
int test_long_fname_2(struct dummy_tracepoint_args * arg):
5: (b7) r2 = 0
6: (63) *(u32 *)(r10 -4) = r2
7: (79) r1 = *(u64 *)(r1 +8)
...
22: (07) r1 += 1
23: (63) *(u32 *)(r0 +4) = r1
24: (95) exit $ bpftool prog dump jited id 1
int _dummy_tracepoint(struct dummy_tracepoint_args * arg):
bpf_prog_b07ccb89267cf242__dummy_tracepoint:
0: push %rbp
1: mov %rsp,%rbp
......
3c: add $0x28,%rbp
40: leaveq
41: retq int test_long_fname_1(struct dummy_tracepoint_args * arg):
bpf_prog_2dcecc18072623fc_test_long_fname_1:
0: push %rbp
1: mov %rsp,%rbp
......
3a: add $0x28,%rbp
3e: leaveq
3f: retq int test_long_fname_2(struct dummy_tracepoint_args * arg):
bpf_prog_89d64e4abf0f0126_test_long_fname_2:
0: push %rbp
1: mov %rsp,%rbp
......
80: add $0x28,%rbp
84: leaveq
85: retq--- tools/bpf/bpftool/btf_dumper.c | 98 +++++++++++++++++++++++++++++++ tools/bpf/bpftool/main.h | 2 + tools/bpf/bpftool/prog.c | 56 ++++++++++++++++++ tools/bpf/bpftool/xlated_dumper.c | 33 +++++++++++ tools/bpf/bpftool/xlated_dumper.h | 3 + 5 files changed, 192 insertions(+)
tools/bpf/bpftool/btf_dumper.c +98 -0
@@ -249,3 +249,101 @@ int btf_dumper_type(const struct btf_dumper *d, __u32 type_id, {return btf_dumper_do_type (d, type_id,0 , data); } + +#define BTF_PRINT_ARG (...) \ + do { \ + pos += snprintf(func_sig + pos, size - pos, \ + __VA_ARGS__); \ + if (pos >= size) \ + return -1; \ + } while (0) +#define BTF_PRINT_TYPE (type) \ + do { \ + pos = __btf_dumper_type_only(btf, type, func_sig, \ + pos, size); \ + if (pos == -1) \ + return -1; \ + } while (0) + +static int __btf_dumper_type_only (struct btf *btf,__u32 type_id, +char *func_sig,int pos,int size) +{ +const struct btf_type *t =btf__type_by_id (btf, type_id); +const struct btf_array *array; +int i, vlen; + +switch (BTF_INFO_KIND (t->info)) { +case BTF_KIND_INT : +BTF_PRINT_ARG ("%s " ,btf__name_by_offset (btf, t->name_off)); +break ; +case BTF_KIND_STRUCT : +BTF_PRINT_ARG ("struct %s " , +btf__name_by_offset (btf, t->name_off)); +break ; +case BTF_KIND_UNION : +BTF_PRINT_ARG ("union %s " , +btf__name_by_offset (btf, t->name_off)); +break ; +case BTF_KIND_ENUM : +BTF_PRINT_ARG ("enum %s " , +btf__name_by_offset (btf, t->name_off)); +break ; +case BTF_KIND_ARRAY : + array = (struct btf_array *)(t +1 ); +BTF_PRINT_TYPE (array->type); +BTF_PRINT_ARG ("[%d]" , array->nelems); +break ; +case BTF_KIND_PTR : +BTF_PRINT_TYPE (t->type); +BTF_PRINT_ARG ("* " ); +break ; +case BTF_KIND_UNKN : +case BTF_KIND_FWD : +case BTF_KIND_TYPEDEF : +return -1 ; +case BTF_KIND_VOLATILE : +BTF_PRINT_ARG ("volatile " ); +BTF_PRINT_TYPE (t->type); +break ; +case BTF_KIND_CONST : +BTF_PRINT_ARG ("const " ); +BTF_PRINT_TYPE (t->type); +break ; +case BTF_KIND_RESTRICT : +BTF_PRINT_ARG ("restrict " ); +BTF_PRINT_TYPE (t->type); +break ; +case BTF_KIND_FUNC : +BTF_PRINT_TYPE (t->type); +BTF_PRINT_ARG ("%s(" ,btf__name_by_offset (btf, t->name_off)); + vlen =BTF_INFO_VLEN (t->info); +for (i =0 ; i < vlen; i++) { +struct btf_param *arg = &((struct btf_param *)(t +1 ))[i]; + +if (i) +BTF_PRINT_ARG (", " ); +BTF_PRINT_TYPE (arg->type); +BTF_PRINT_ARG ("%s" ,btf__name_by_offset (btf, arg->name_off)); + } +BTF_PRINT_ARG (")" ); +break ; +default : +return -1 ; + } + +return pos; +} + +void btf_dumper_type_only (struct btf *btf,__u32 type_id,char *func_sig, +int size) +{ +int err; + + func_sig[0 ] ='\0' ; +if (!btf) +return ; + + err =__btf_dumper_type_only (btf, type_id, func_sig,0 , size); +if (err <0 ) + func_sig[0 ] ='\0' ; +}
tools/bpf/bpftool/main.h +2 -0
@@ -170,6 +170,8 @@ struct btf_dumper {*/ int btf_dumper_type (const struct btf_dumper *d,__u32 type_id,const void *data); +void btf_dumper_type_only (struct btf *btf,__u32 func_type_id,char *func_only, +int size);struct nlattr ;struct ifinfomsg ;
tools/bpf/bpftool/prog.c +56 -0
136 lines not on this page · raw message
tools/bpf/bpftool/xlated_dumper.c +33 -0
@@ -242,11 +242,15 @@ void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len, .cb_imm = print_imm, .private_data = dd, }; +struct bpf_func_info *record;struct bpf_insn *insn = buf; +struct btf *btf = dd->btf;bool double_insn = false; +char func_sig[1024 ];unsigned int i;jsonw_start_array (json_wtr); + record = dd->func_info;for (i =0 ; i < len /sizeof (*insn); i++) {if (double_insn) { double_insn = false;
@@ -255,6 +259,20 @@ void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len, double_insn = insn[i].code == (BPF_LD |BPF_IMM |BPF_DW );jsonw_start_object (json_wtr); + +if (btf && record) { +if (record->insn_offset == i) { +btf_dumper_type_only (btf, record->type_id, + func_sig, +sizeof (func_sig)); +if (func_sig[0 ] !='\0' ) { +jsonw_name (json_wtr,"proto" ); +jsonw_string (json_wtr, func_sig); + } + record = (void *)record + dd->finfo_rec_size; + } + } +jsonw_name (json_wtr,"disasm" );print_bpf_insn (&cbs, insn + i, true);
@@ -297,16 +315,31 @@ void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len, .cb_imm = print_imm, .private_data = dd, }; +struct bpf_func_info *record;struct bpf_insn *insn = buf; +struct btf *btf = dd->btf;bool double_insn = false; +char func_sig[1024 ];unsigned int i; + record = dd->func_info;for (i =0 ; i < len /sizeof (*insn); i++) {if (double_insn) { double_insn = false;continue ; } +if (btf && record) { +if (record->insn_offset == i) { +btf_dumper_type_only (btf, record->type_id, + func_sig, +sizeof (func_sig)); +if (func_sig[0 ] !='\0' ) +printf ("%s:\n" , func_sig); + record = (void *)record + dd->finfo_rec_size; + } + } + double_insn = insn[i].code == (BPF_LD |BPF_IMM |BPF_DW );printf ("% 4d: " , i);
tools/bpf/bpftool/xlated_dumper.h +3 -0
@@ -51,6 +51,9 @@ struct dump_data {__u32 sym_count;__u64 *jited_ksyms;__u32 nr_jited_ksyms; +struct btf *btf; +void *func_info; +__u32 finfo_rec_size;char scratch_buff[SYM_MAX_NAME +8 ]; };
Signature · 1 line
2.17.1