Skip to content

Commit f07cbb7

Browse files
authored
tools/killsnoop: migrate to tracepoints (#5546)
This commit updates the python killsnoop tool to use tracepoints for the kill, tkill, and tgkill syscalls. Previously, it used kprobes on sys_kill, which broke on Linux 5.11 due to kernel changes. This brings the python tool in parity with the libbpf-tools C version (sigsnoop), providing a more robust and stable tracing mechanism. Fixes #3592
1 parent 6d0a964 commit f07cbb7

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

tests/python/test_tools_smoke.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def test_gethostlatency(self):
222222
def test_hardirqs(self):
223223
self.run_with_duration("hardirqs.py 1 1")
224224

225-
@skipUnless(kernel_version_ge(4,4), "requires kernel >= 4.4")
225+
@skipUnless(kernel_version_ge(4,7), "requires kernel >= 4.7")
226226
def test_killsnoop(self):
227227
# Because killsnoop intercepts signals, if we send it a SIGINT we we
228228
# we likely catch it while it is handling the data packet from the

tools/killsnoop.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
BPF_HASH(infotmp, u32, struct val_t);
6868
BPF_PERF_OUTPUT(events);
6969
70-
int syscall__kill(struct pt_regs *ctx, int tpid, int sig)
70+
71+
static int probe_entry(u32 tpid, int sig)
7172
{
7273
u64 pid_tgid = bpf_get_current_pid_tgid();
7374
u32 pid = pid_tgid >> 32;
@@ -85,9 +86,24 @@
8586
}
8687
8788
return 0;
88-
};
89+
}
90+
91+
TRACEPOINT_PROBE(syscalls, sys_enter_kill)
92+
{
93+
return probe_entry(args->pid, args->sig);
94+
}
95+
96+
TRACEPOINT_PROBE(syscalls, sys_enter_tkill)
97+
{
98+
return probe_entry(args->pid, args->sig);
99+
}
100+
101+
TRACEPOINT_PROBE(syscalls, sys_enter_tgkill)
102+
{
103+
return probe_entry(args->pid, args->sig);
104+
}
89105
90-
int do_ret_sys_kill(struct pt_regs *ctx)
106+
static int probe_exit(void *ctx, int ret)
91107
{
92108
struct data_t data = {};
93109
struct val_t *valp;
@@ -104,14 +120,30 @@
104120
bpf_probe_read_kernel(&data.comm, sizeof(data.comm), valp->comm);
105121
data.pid = pid;
106122
data.tpid = valp->tpid;
107-
data.ret = PT_REGS_RC(ctx);
123+
data.ret = ret;
108124
data.sig = valp->sig;
109125
110126
events.perf_submit(ctx, &data, sizeof(data));
111127
infotmp.delete(&tid);
112128
113129
return 0;
114130
}
131+
132+
TRACEPOINT_PROBE(syscalls, sys_exit_kill)
133+
{
134+
return probe_exit(args, args->ret);
135+
}
136+
137+
TRACEPOINT_PROBE(syscalls, sys_exit_tkill)
138+
{
139+
return probe_exit(args, args->ret);
140+
}
141+
142+
TRACEPOINT_PROBE(syscalls, sys_exit_tgkill)
143+
{
144+
return probe_exit(args, args->ret);
145+
}
146+
115147
"""
116148

117149
if args.tpid:
@@ -141,9 +173,6 @@
141173

142174
# initialize BPF
143175
b = BPF(text=bpf_text)
144-
kill_fnname = b.get_syscall_fnname("kill")
145-
b.attach_kprobe(event=kill_fnname, fn_name="syscall__kill")
146-
b.attach_kretprobe(event=kill_fnname, fn_name="do_ret_sys_kill")
147176

148177
# detect the length of PID column
149178
pid_bytes = 6

0 commit comments

Comments
 (0)