On Fri, Apr 26, 2024 at 5:17 AM Viktor Malik <vmalik@xxxxxxxxxx> wrote: > > In some situations, it is useful to explicitly specify a kernel module > to search for a tracing program target (e.g. when a function of the same > name exists in multiple modules or in vmlinux). > > This patch enables that by allowing the "module:function" syntax for the > find_kernel_btf_id function. Thanks to this, the syntax can be used both > from a SEC macro (i.e. `SEC(fentry/module:function)`) and via the > bpf_program__set_attach_target API call. > > Signed-off-by: Viktor Malik <vmalik@xxxxxxxxxx> > --- > tools/lib/bpf/libbpf.c | 33 ++++++++++++++++++++++++--------- > 1 file changed, 24 insertions(+), 9 deletions(-) > Looks good, just stylistic nits below. > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index 97eb6e5dd7c8..5a136876cd1c 100644 > --- a/tools/lib/bpf/libbpf.c > +++ b/tools/lib/bpf/libbpf.c > @@ -9858,16 +9858,28 @@ static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name, > enum bpf_attach_type attach_type, > int *btf_obj_fd, int *btf_type_id) > { > - int ret, i; > + int ret, i, mod_len; > + const char *fun_name, *mod_name = NULL; bikeshedding nit, but let's call it fn_name ("fun" doesn't associate with "function" in my head at all) > > - ret = find_attach_btf_id(obj->btf_vmlinux, attach_name, attach_type); > - if (ret > 0) { > - *btf_obj_fd = 0; /* vmlinux BTF */ > - *btf_type_id = ret; > - return 0; > + fun_name = strchr(attach_name, ':'); > + if (fun_name) { > + mod_name = attach_name; > + mod_len = fun_name - mod_name; > + fun_name++; > + } > + > + if (!mod_name || strncmp(mod_name, "vmlinux", mod_len) == 0) { > + ret = find_attach_btf_id(obj->btf_vmlinux, > + mod_name ? fun_name : attach_name, > + attach_type); > + if (ret > 0) { > + *btf_obj_fd = 0; /* vmlinux BTF */ > + *btf_type_id = ret; > + return 0; > + } > + if (ret != -ENOENT) > + return ret; > } > - if (ret != -ENOENT) > - return ret; > > ret = load_module_btfs(obj); > if (ret) > @@ -9876,7 +9888,10 @@ static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name, > for (i = 0; i < obj->btf_module_cnt; i++) { > const struct module_btf *mod = &obj->btf_modules[i]; > > - ret = find_attach_btf_id(mod->btf, attach_name, attach_type); > + if (mod_name && strncmp(mod->name, mod_name, mod_len)) please add explicit `== 0` after strncmp(), like you did above for vmlinux > + continue; > + > + ret = find_attach_btf_id(mod->btf, mod_name ? fun_name : attach_name, attach_type); > if (ret > 0) { > *btf_obj_fd = mod->fd; > *btf_type_id = ret; > -- > 2.44.0 >