Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bcc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
bcc
Commits
f5c40cb6
Commit
f5c40cb6
authored
Feb 19, 2016
by
mcaleavya
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrated killsnoop to bpf_perf_event
parent
ca6ac5ad
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
201 additions
and
14 deletions
+201
-14
man/man8/killsnoop.8
man/man8/killsnoop.8
+4
-0
tools/killsnoop.py
tools/killsnoop.py
+91
-14
tools/old/killsnoop.py
tools/old/killsnoop.py
+106
-0
No files found.
man/man8/killsnoop.8
View file @
f5c40cb6
...
...
@@ -11,6 +11,10 @@ is sending signals.
This works by tracing the kernel sys_kill() function using dynamic tracing, and
will need updating to match any changes to this function.
This makes use of a Linux 4.5 feature (bpf_perf_event_output());
for kernels older than 4.5, see the version under tools/old,
which uses an older mechanism.
Since this uses BPF, only the root user can use this tool.
.SH REQUIREMENTS
CONFIG_BPF and bcc.
...
...
tools/killsnoop.py
View file @
f5c40cb6
...
...
@@ -10,10 +10,12 @@
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 20-Sep-2015 Brendan Gregg Created this.
# 19-Feb-2016 Allan McAleavy migrated to BPF_PERF_OUTPUT
from
__future__
import
print_function
from
bcc
import
BPF
import
argparse
import
ctypes
as
ct
# arguments
examples
=
"""examples:
...
...
@@ -38,15 +40,40 @@ debug = 0
# define BPF program
bpf_text
=
"""
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
struct val_t {
u64 pid;
u64 ts;
char comm[TASK_COMM_LEN];
};
struct data_t {
u64 pid;
u64 tpid;
int sig;
int ret;
u64 ts;
u64 delta;
char comm[TASK_COMM_LEN];
};
BPF_HASH(args_pid, u32, int);
BPF_HASH(args_sig, u32, int);
BPF_HASH(infotmp, u32, struct val_t);
BPF_PERF_OUTPUT(events);
int kprobe__sys_kill(struct pt_regs *ctx, int tpid, int sig)
{
struct val_t val = {};
u32 pid = bpf_get_current_pid_tgid();
FILTER
if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
val.pid = bpf_get_current_pid_tgid();
val.ts = bpf_ktime_get_ns();
infotmp.update(&pid, &val);
}
args_pid.update(&pid, &tpid);
args_sig.update(&pid, &sig);
...
...
@@ -55,8 +82,11 @@ int kprobe__sys_kill(struct pt_regs *ctx, int tpid, int sig)
int kretprobe__sys_kill(struct pt_regs *ctx)
{
int *tpidp, *sigp, ret = ctx->ax;
struct data_t data = {};
struct val_t *valp;
int *tpidp, *sigp;
u32 pid = bpf_get_current_pid_tgid();
u64 tsp = bpf_ktime_get_ns();
tpidp = args_pid.lookup(&pid);
sigp = args_sig.lookup(&pid);
...
...
@@ -64,7 +94,22 @@ int kretprobe__sys_kill(struct pt_regs *ctx)
return 0; // missed entry
}
bpf_trace_printk("%d %d %d
\
\
n", *tpidp, *sigp, ret);
valp = infotmp.lookup(&pid);
if (valp == 0) {
// missed entry
return 0;
}
bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm);
data.pid = pid;
data.delta = tsp - valp->ts;
data.ts = tsp / 1000;
data.tpid = *tpidp;
data.ret = ctx->ax;
data.sig = *sigp;
events.perf_submit(ctx, &data, sizeof(data));
infotmp.delete(&pid);
args_pid.delete(&pid);
args_sig.delete(&pid);
...
...
@@ -82,25 +127,57 @@ if debug:
# initialize BPF
b
=
BPF
(
text
=
bpf_text
)
TASK_COMM_LEN
=
16
# linux/sched.h
class
Data
(
ct
.
Structure
):
_fields_
=
[
(
"pid"
,
ct
.
c_ulonglong
),
(
"tpid"
,
ct
.
c_ulonglong
),
(
"sig"
,
ct
.
c_int
),
(
"ret"
,
ct
.
c_int
),
(
"ts"
,
ct
.
c_ulonglong
),
(
"delta"
,
ct
.
c_ulonglong
),
(
"comm"
,
ct
.
c_char
*
TASK_COMM_LEN
)
]
start_ts
=
0
prev_ts
=
0
delta
=
0
# header
if
args
.
timestamp
:
print
(
"%-14s"
%
(
"TIME(s)"
),
end
=
""
)
print
(
"%-6s %-16s %-4s %-6s %s"
%
(
"PID"
,
"COMM"
,
"SIG"
,
"TPID"
,
"RESULT"
))
start_ts
=
0
# process event
def
print_event
(
cpu
,
data
,
size
):
event
=
ct
.
cast
(
data
,
ct
.
POINTER
(
Data
)).
contents
global
start_ts
global
prev_ts
global
delta
# format output
while
1
:
(
task
,
pid
,
cpu
,
flags
,
ts
,
msg
)
=
b
.
trace_fields
()
(
tpid_s
,
sig_s
,
ret_s
)
=
msg
.
split
(
" "
)
if
start_ts
==
0
:
prev_ts
=
start_ts
if
start_ts
==
1
:
delta
=
float
(
delta
)
+
(
event
.
ts
-
prev_ts
)
ret
=
int
(
ret_s
)
if
(
args
.
failed
and
(
ret
>=
0
)):
continue
if
(
args
.
failed
and
(
event
.
ret
>=
0
)):
start_ts
=
1
prev_ts
=
event
.
ts
return
# print columns
if
args
.
timestamp
:
if
start_ts
==
0
:
start_ts
=
ts
print
(
"%-14.9f"
%
(
ts
-
start_ts
),
end
=
""
)
print
(
"%-6d %-16s %-4s %-6s %s"
%
(
pid
,
task
,
sig_s
,
tpid_s
,
ret_s
))
print
(
"%-14.9f"
%
(
delta
/
1000000
),
end
=
""
)
print
(
"%-6d %-16s %-4d %-6d %d"
%
(
event
.
pid
,
event
.
comm
,
event
.
sig
,
event
.
tpid
,
event
.
ret
))
prev_ts
=
event
.
ts
start_ts
=
1
# loop with callback to print_event
b
[
"events"
].
open_perf_buffer
(
print_event
)
while
1
:
b
.
kprobe_poll
()
tools/old/killsnoop.py
0 → 100755
View file @
f5c40cb6
#!/usr/bin/python
# @lint-avoid-python-3-compatibility-imports
#
# killsnoop Trace signals issued by the kill() syscall.
# For Linux, uses BCC, eBPF. Embedded C.
#
# USAGE: killsnoop [-h] [-t] [-x] [-p PID]
#
# Copyright (c) 2015 Brendan Gregg.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 20-Sep-2015 Brendan Gregg Created this.
from
__future__
import
print_function
from
bcc
import
BPF
import
argparse
# arguments
examples
=
"""examples:
./killsnoop # trace all kill() signals
./killsnoop -t # include timestamps
./killsnoop -x # only show failed kills
./killsnoop -p 181 # only trace PID 181
"""
parser
=
argparse
.
ArgumentParser
(
description
=
"Trace signals issued by the kill() syscall"
,
formatter_class
=
argparse
.
RawDescriptionHelpFormatter
,
epilog
=
examples
)
parser
.
add_argument
(
"-t"
,
"--timestamp"
,
action
=
"store_true"
,
help
=
"include timestamp on output"
)
parser
.
add_argument
(
"-x"
,
"--failed"
,
action
=
"store_true"
,
help
=
"only show failed opens"
)
parser
.
add_argument
(
"-p"
,
"--pid"
,
help
=
"trace this PID only"
)
args
=
parser
.
parse_args
()
debug
=
0
# define BPF program
bpf_text
=
"""
#include <uapi/linux/ptrace.h>
BPF_HASH(args_pid, u32, int);
BPF_HASH(args_sig, u32, int);
int kprobe__sys_kill(struct pt_regs *ctx, int tpid, int sig)
{
u32 pid = bpf_get_current_pid_tgid();
FILTER
args_pid.update(&pid, &tpid);
args_sig.update(&pid, &sig);
return 0;
};
int kretprobe__sys_kill(struct pt_regs *ctx)
{
int *tpidp, *sigp, ret = ctx->ax;
u32 pid = bpf_get_current_pid_tgid();
tpidp = args_pid.lookup(&pid);
sigp = args_sig.lookup(&pid);
if (tpidp == 0 || sigp == 0) {
return 0; // missed entry
}
bpf_trace_printk("%d %d %d
\
\
n", *tpidp, *sigp, ret);
args_pid.delete(&pid);
args_sig.delete(&pid);
return 0;
}
"""
if
args
.
pid
:
bpf_text
=
bpf_text
.
replace
(
'FILTER'
,
'if (pid != %s) { return 0; }'
%
args
.
pid
)
else
:
bpf_text
=
bpf_text
.
replace
(
'FILTER'
,
''
)
if
debug
:
print
(
bpf_text
)
# initialize BPF
b
=
BPF
(
text
=
bpf_text
)
# header
if
args
.
timestamp
:
print
(
"%-14s"
%
(
"TIME(s)"
),
end
=
""
)
print
(
"%-6s %-16s %-4s %-6s %s"
%
(
"PID"
,
"COMM"
,
"SIG"
,
"TPID"
,
"RESULT"
))
start_ts
=
0
# format output
while
1
:
(
task
,
pid
,
cpu
,
flags
,
ts
,
msg
)
=
b
.
trace_fields
()
(
tpid_s
,
sig_s
,
ret_s
)
=
msg
.
split
(
" "
)
ret
=
int
(
ret_s
)
if
(
args
.
failed
and
(
ret
>=
0
)):
continue
# print columns
if
args
.
timestamp
:
if
start_ts
==
0
:
start_ts
=
ts
print
(
"%-14.9f"
%
(
ts
-
start_ts
),
end
=
""
)
print
(
"%-6d %-16s %-4s %-6s %s"
%
(
pid
,
task
,
sig_s
,
tpid_s
,
ret_s
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment