On Mon, Apr 29, 2024 at 04:46:00PM -0600, Daniel Xu wrote: > This commit teaches pahole to parse symbols in .BTF_ids section in > vmlinux and discover exported kfuncs. Pahole then takes the list of > kfuncs and injects a BTF_KIND_DECL_TAG for each kfunc. > > Example of encoding: > > $ bpftool btf dump file .tmp_vmlinux.btf | rg "DECL_TAG 'bpf_kfunc'" | wc -l > 121 > > $ bpftool btf dump file .tmp_vmlinux.btf | rg 56337 > [56337] FUNC 'bpf_ct_change_timeout' type_id=56336 linkage=static > [127861] DECL_TAG 'bpf_kfunc' type_id=56337 component_idx=-1 > > This enables downstream users and tools to dynamically discover which > kfuncs are available on a system by parsing vmlinux or module BTF, both > available in /sys/kernel/btf. > > This feature is enabled with --btf_features=decl_tag,decl_tag_kfuncs. > > Acked-by: Jiri Olsa <jolsa@xxxxxxxxxx> > Tested-by: Jiri Olsa <jolsa@xxxxxxxxxx> > Reviewed-by: Alan Maguire <alan.maguire@xxxxxxxxxx> > Tested-by: Alan Maguire <alan.maguire@xxxxxxxxxx> > Signed-off-by: Daniel Xu <dxu@xxxxxxxxx> Here I needed this to avoid an strdup possibly followed by a free and then checking the strdup result, please Ack/revalidate tags. I'm dropping them as there are changes. - Arnaldo diff --git a/btf_encoder.c b/btf_encoder.c index e9d82e0af0e178fd..c2df2bc7a374447b 100644 --- a/btf_encoder.c +++ b/btf_encoder.c @@ -1434,11 +1434,13 @@ static char *get_func_name(const char *sym) return NULL; /* Strip prefix and handle malformed input such as __BTF_ID__func___ */ - func = strdup(sym + sizeof(BTF_ID_FUNC_PFX) - 1); - if (!strstr(func, "__")) { - free(func); + const char *func_sans_prefix = sym + sizeof(BTF_ID_FUNC_PFX) - 1; + if (!strstr(func_sans_prefix, "__")) return NULL; - } + + func = strdup(func_sans_prefix); + if (!func) + return NULL; /* Strip suffix */ end = strrchr(func, '_');