Skip to content

Commit dbeb619

Browse files
chenhengqiNobody
authored andcommitted
libbpf: Allow kprobe attach using legacy debugfs interface
On some old kernels, kprobe auto-attach may fail when attach to symbols like udp_send_skb.isra.52 . This is because the kernel has kprobe PMU but don't allow attach to a symbol with '.' ([0]). Add a new option to bpf_kprobe_opts to allow using the legacy kprobe attach directly. This way, users can use bpf_program__attach_kprobe_opts in a dedicated custom sec handler to handle such case. [0]: https://github.com/torvalds/linux/blob/v4.18/kernel/trace/trace_kprobe.c#L340-L343 Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
1 parent 97db7e1 commit dbeb619

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10097,9 +10097,16 @@ static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
1009710097
const char *kfunc_name, size_t offset)
1009810098
{
1009910099
static int index = 0;
10100+
int i;
1010010101

1010110102
snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name, offset,
1010210103
__sync_fetch_and_add(&index, 1));
10104+
10105+
/* sanitize .isra.$n symbols */
10106+
for (i = 0; buf[i]; i++) {
10107+
if (!isalnum(buf[i]))
10108+
buf[i] = '_';
10109+
}
1010310110
}
1010410111

1010510112
static int add_kprobe_event_legacy(const char *probe_name, bool retprobe,
@@ -10189,7 +10196,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
1018910196
offset = OPTS_GET(opts, offset, 0);
1019010197
pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
1019110198

10192-
legacy = determine_kprobe_perf_type() < 0;
10199+
legacy = OPTS_GET(opts, legacy, false) || determine_kprobe_perf_type() < 0;
1019310200
if (!legacy) {
1019410201
pfd = perf_event_open_probe(false /* uprobe */, retprobe,
1019510202
func_name, offset,

tools/lib/bpf/libbpf.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,11 @@ struct bpf_kprobe_opts {
413413
size_t offset;
414414
/* kprobe is return probe */
415415
bool retprobe;
416+
/* use the legacy debugfs interface */
417+
bool legacy;
416418
size_t :0;
417419
};
418-
#define bpf_kprobe_opts__last_field retprobe
420+
#define bpf_kprobe_opts__last_field legacy
419421

420422
LIBBPF_API struct bpf_link *
421423
bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,

0 commit comments

Comments
 (0)