Skip to content

Commit 691ed59

Browse files
Rtoaxyonghong-song
authored andcommitted
tools/ttysnoop: Fix kfunc Unknown error
Check whether kfunc is supported, and decide whether to use kprobe or kfunc based on the test results. How to reproduce the error: $ sudo ./ttysnoop.py 7 bpf_attach_raw_tracepoint (kfunc): Unknown error 524 Traceback (most recent call last): File "/home/rongtao/Git/bcc/tools/./ttysnoop.py", line 175, in <module> b = BPF(text=bpf_text) File "/usr/lib/python3.9/site-packages/bcc/__init__.py", line 487, in __init__ self._trace_autoload() File "/usr/lib/python3.9/site-packages/bcc/__init__.py", line 1473, in _trace_autoload self.attach_kfunc(fn_name=func_name) File "/usr/lib/python3.9/site-packages/bcc/__init__.py", line 1138, in attach_kfunc raise Exception("Failed to attach BPF to entry kernel func") Exception: Failed to attach BPF to entry kernel func Signed-off-by: Rong Tao <[email protected]>
1 parent 24822c2 commit 691ed59

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

tools/ttysnoop.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# Idea: from ttywatcher.
1414
#
1515
# 15-Oct-2016 Brendan Gregg Created this.
16+
# 13-Dec-2022 Rong Tao Detect whether kfunc is supported.
1617

1718
from __future__ import print_function
1819
from bcc import BPF
@@ -130,7 +131,7 @@ def usage():
130131
return do_tty_write(ctx, buf, count);
131132
}
132133
#else
133-
KFUNC_PROBE(tty_write, struct kiocb *iocb, struct iov_iter *from)
134+
PROBE_TTY_WRITE
134135
{
135136
const char __user *buf;
136137
const struct kvec *kvec;
@@ -162,6 +163,21 @@ def usage():
162163
#endif
163164
"""
164165

166+
probe_tty_write_kfunc = """
167+
KFUNC_PROBE(tty_write, struct kiocb *iocb, struct iov_iter *from)
168+
"""
169+
170+
probe_tty_write_kprobe = """
171+
int kprobe__tty_write(struct pt_regs *ctx, struct kiocb *iocb,
172+
struct iov_iter *from)
173+
"""
174+
175+
is_support_kfunc = BPF.support_kfunc()
176+
if is_support_kfunc:
177+
bpf_text = bpf_text.replace('PROBE_TTY_WRITE', probe_tty_write_kfunc)
178+
else:
179+
bpf_text = bpf_text.replace('PROBE_TTY_WRITE', probe_tty_write_kprobe)
180+
165181
bpf_text = bpf_text.replace('PTS', str(pi.st_ino))
166182
if debug or args.ebpf:
167183
print(bpf_text)

0 commit comments

Comments
 (0)