Commit 8a3fe76f authored by Jackie Liu's avatar Jackie Liu Committed by Andrii Nakryiko

libbpf: Cross-join available_filter_functions and kallsyms for multi-kprobes

When using regular expression matching with "kprobe multi", it scans all
the functions under "/proc/kallsyms" that can be matched. However, not all
of them can be traced by kprobe.multi. If any one of the functions fails
to be traced, it will result in the failure of all functions. The best
approach is to filter out the functions that cannot be traced to ensure
proper tracking of the functions.

Closes: https://lore.kernel.org/oe-kbuild-all/202307030355.TdXOHklM-lkp@intel.com/Reported-by: default avatarkernel test robot <lkp@intel.com>
Suggested-by: default avatarJiri Olsa <jolsa@kernel.org>
Suggested-by: default avatarAndrii Nakryiko <andrii.nakryiko@gmail.com>
Signed-off-by: default avatarJackie Liu <liuyun01@kylinos.cn>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20230705091209.3803873-1-liu.yun@linux.dev
parent e76a0143
......@@ -10228,6 +10228,12 @@ static const char *tracefs_uprobe_events(void)
return use_debugfs() ? DEBUGFS"/uprobe_events" : TRACEFS"/uprobe_events";
}
static const char *tracefs_available_filter_functions(void)
{
return use_debugfs() ? DEBUGFS"/available_filter_functions"
: TRACEFS"/available_filter_functions";
}
static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
const char *kfunc_name, size_t offset)
{
......@@ -10543,25 +10549,107 @@ struct kprobe_multi_resolve {
size_t cnt;
};
static int
resolve_kprobe_multi_cb(unsigned long long sym_addr, char sym_type,
const char *sym_name, void *ctx)
struct avail_kallsyms_data {
char **syms;
size_t cnt;
struct kprobe_multi_resolve *res;
};
static int avail_func_cmp(const void *a, const void *b)
{
struct kprobe_multi_resolve *res = ctx;
return strcmp(*(const char **)a, *(const char **)b);
}
static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type,
const char *sym_name, void *ctx)
{
struct avail_kallsyms_data *data = ctx;
struct kprobe_multi_resolve *res = data->res;
int err;
if (!glob_match(sym_name, res->pattern))
if (!bsearch(&sym_name, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp))
return 0;
err = libbpf_ensure_mem((void **) &res->addrs, &res->cap, sizeof(unsigned long),
res->cnt + 1);
err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, sizeof(*res->addrs), res->cnt + 1);
if (err)
return err;
res->addrs[res->cnt++] = (unsigned long) sym_addr;
res->addrs[res->cnt++] = (unsigned long)sym_addr;
return 0;
}
static int libbpf_available_kallsyms_parse(struct kprobe_multi_resolve *res)
{
const char *available_functions_file = tracefs_available_filter_functions();
struct avail_kallsyms_data data;
char sym_name[500];
FILE *f;
int err = 0, ret, i;
char **syms = NULL;
size_t cap = 0, cnt = 0;
f = fopen(available_functions_file, "re");
if (!f) {
err = -errno;
pr_warn("failed to open %s: %d\n", available_functions_file, err);
return err;
}
while (true) {
char *name;
ret = fscanf(f, "%499s%*[^\n]\n", sym_name);
if (ret == EOF && feof(f))
break;
if (ret != 1) {
pr_warn("failed to parse available_filter_functions entry: %d\n", ret);
err = -EINVAL;
goto cleanup;
}
if (!glob_match(sym_name, res->pattern))
continue;
err = libbpf_ensure_mem((void **)&syms, &cap, sizeof(*syms), cnt + 1);
if (err)
goto cleanup;
name = strdup(sym_name);
if (!name) {
err = -errno;
goto cleanup;
}
syms[cnt++] = name;
}
/* no entries found, bail out */
if (cnt == 0) {
err = -ENOENT;
goto cleanup;
}
/* sort available functions */
qsort(syms, cnt, sizeof(*syms), avail_func_cmp);
data.syms = syms;
data.res = res;
data.cnt = cnt;
libbpf_kallsyms_parse(avail_kallsyms_cb, &data);
if (res->cnt == 0)
err = -ENOENT;
cleanup:
for (i = 0; i < cnt; i++)
free((char *)syms[i]);
free(syms);
fclose(f);
return err;
}
struct bpf_link *
bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
const char *pattern,
......@@ -10598,13 +10686,9 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
return libbpf_err_ptr(-EINVAL);
if (pattern) {
err = libbpf_kallsyms_parse(resolve_kprobe_multi_cb, &res);
err = libbpf_available_kallsyms_parse(&res);
if (err)
goto error;
if (!res.cnt) {
err = -ENOENT;
goto error;
}
addrs = res.addrs;
cnt = res.cnt;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment