blob: f5f96a007923dc0d70f078cc6d13b3e4dce9ec39 [file] [log] [blame]
Brendan Greggd9e578b2015-09-21 11:59:42 -07001#!/usr/bin/python
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08002# @lint-avoid-python-3-compatibility-imports
Brendan Greggd9e578b2015-09-21 11:59:42 -07003#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08004# killsnoop Trace signals issued by the kill() syscall.
5# For Linux, uses BCC, eBPF. Embedded C.
Brendan Greggd9e578b2015-09-21 11:59:42 -07006#
7# USAGE: killsnoop [-h] [-t] [-x] [-p PID]
8#
9# Copyright (c) 2015 Brendan Gregg.
10# Licensed under the Apache License, Version 2.0 (the "License")
11#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080012# 20-Sep-2015 Brendan Gregg Created this.
mcaleavyaf5c40cb2016-02-19 23:02:39 +000013# 19-Feb-2016 Allan McAleavy migrated to BPF_PERF_OUTPUT
Brendan Greggd9e578b2015-09-21 11:59:42 -070014
15from __future__ import print_function
16from bcc import BPF
17import argparse
mcaleavyaf5c40cb2016-02-19 23:02:39 +000018import ctypes as ct
Brendan Greggd9e578b2015-09-21 11:59:42 -070019
20# arguments
21examples = """examples:
22 ./killsnoop # trace all kill() signals
23 ./killsnoop -t # include timestamps
24 ./killsnoop -x # only show failed kills
25 ./killsnoop -p 181 # only trace PID 181
26"""
27parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080028 description="Trace signals issued by the kill() syscall",
29 formatter_class=argparse.RawDescriptionHelpFormatter,
30 epilog=examples)
Brendan Greggd9e578b2015-09-21 11:59:42 -070031parser.add_argument("-t", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080032 help="include timestamp on output")
Brendan Greggd9e578b2015-09-21 11:59:42 -070033parser.add_argument("-x", "--failed", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080034 help="only show failed opens")
Brendan Greggd9e578b2015-09-21 11:59:42 -070035parser.add_argument("-p", "--pid",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080036 help="trace this PID only")
Brendan Greggd9e578b2015-09-21 11:59:42 -070037args = parser.parse_args()
38debug = 0
39
40# define BPF program
41bpf_text = """
42#include <uapi/linux/ptrace.h>
mcaleavyaf5c40cb2016-02-19 23:02:39 +000043#include <linux/sched.h>
44
45struct val_t {
46 u64 pid;
47 u64 ts;
mcaleavya6262f5d2016-02-19 23:48:20 +000048 int sig;
49 int tpid;
mcaleavyaf5c40cb2016-02-19 23:02:39 +000050 char comm[TASK_COMM_LEN];
51};
52
53struct data_t {
54 u64 pid;
55 u64 tpid;
56 int sig;
57 int ret;
58 u64 ts;
59 u64 delta;
60 char comm[TASK_COMM_LEN];
61};
Brendan Greggd9e578b2015-09-21 11:59:42 -070062
mcaleavyaf5c40cb2016-02-19 23:02:39 +000063BPF_HASH(infotmp, u32, struct val_t);
64BPF_PERF_OUTPUT(events);
Brendan Greggd9e578b2015-09-21 11:59:42 -070065
66int kprobe__sys_kill(struct pt_regs *ctx, int tpid, int sig)
67{
mcaleavyaf5c40cb2016-02-19 23:02:39 +000068 struct val_t val = {};
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080069 u32 pid = bpf_get_current_pid_tgid();
Brendan Greggd9e578b2015-09-21 11:59:42 -070070
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080071 FILTER
mcaleavyaf5c40cb2016-02-19 23:02:39 +000072 if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
73 val.pid = bpf_get_current_pid_tgid();
74 val.ts = bpf_ktime_get_ns();
mcaleavya6262f5d2016-02-19 23:48:20 +000075 val.tpid = tpid;
76 val.sig = sig;
mcaleavyaf5c40cb2016-02-19 23:02:39 +000077 infotmp.update(&pid, &val);
78 }
Brendan Greggd9e578b2015-09-21 11:59:42 -070079
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080080 return 0;
Brendan Greggd9e578b2015-09-21 11:59:42 -070081};
82
83int kretprobe__sys_kill(struct pt_regs *ctx)
84{
mcaleavyaf5c40cb2016-02-19 23:02:39 +000085 struct data_t data = {};
86 struct val_t *valp;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080087 u32 pid = bpf_get_current_pid_tgid();
mcaleavyaf5c40cb2016-02-19 23:02:39 +000088 u64 tsp = bpf_ktime_get_ns();
Brendan Greggd9e578b2015-09-21 11:59:42 -070089
mcaleavyaf5c40cb2016-02-19 23:02:39 +000090 valp = infotmp.lookup(&pid);
91 if (valp == 0) {
92 // missed entry
93 return 0;
94 }
95
96 bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm);
97 data.pid = pid;
98 data.delta = tsp - valp->ts;
99 data.ts = tsp / 1000;
mcaleavya6262f5d2016-02-19 23:48:20 +0000100 data.tpid = valp->tpid;
mcaleavyaf5c40cb2016-02-19 23:02:39 +0000101 data.ret = ctx->ax;
mcaleavya6262f5d2016-02-19 23:48:20 +0000102 data.sig = valp->sig;
mcaleavyaf5c40cb2016-02-19 23:02:39 +0000103
104 events.perf_submit(ctx, &data, sizeof(data));
105 infotmp.delete(&pid);
Brendan Greggd9e578b2015-09-21 11:59:42 -0700106
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800107 return 0;
Brendan Greggd9e578b2015-09-21 11:59:42 -0700108}
109"""
110if args.pid:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800111 bpf_text = bpf_text.replace('FILTER',
112 'if (pid != %s) { return 0; }' % args.pid)
Brendan Greggd9e578b2015-09-21 11:59:42 -0700113else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800114 bpf_text = bpf_text.replace('FILTER', '')
Brendan Greggd9e578b2015-09-21 11:59:42 -0700115if debug:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800116 print(bpf_text)
Brendan Greggd9e578b2015-09-21 11:59:42 -0700117
118# initialize BPF
119b = BPF(text=bpf_text)
120
mcaleavyaf5c40cb2016-02-19 23:02:39 +0000121TASK_COMM_LEN = 16 # linux/sched.h
122
123class Data(ct.Structure):
124 _fields_ = [
125 ("pid", ct.c_ulonglong),
126 ("tpid", ct.c_ulonglong),
127 ("sig", ct.c_int),
128 ("ret", ct.c_int),
129 ("ts", ct.c_ulonglong),
130 ("delta", ct.c_ulonglong),
131 ("comm", ct.c_char * TASK_COMM_LEN)
132 ]
133
134start_ts = 0
135prev_ts = 0
136delta = 0
137
Brendan Greggd9e578b2015-09-21 11:59:42 -0700138# header
139if args.timestamp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800140 print("%-14s" % ("TIME(s)"), end="")
Brendan Greggd9e578b2015-09-21 11:59:42 -0700141print("%-6s %-16s %-4s %-6s %s" % ("PID", "COMM", "SIG", "TPID", "RESULT"))
142
mcaleavyaf5c40cb2016-02-19 23:02:39 +0000143# process event
144def print_event(cpu, data, size):
145 event = ct.cast(data, ct.POINTER(Data)).contents
146 global start_ts
147 global prev_ts
148 global delta
Brendan Greggd9e578b2015-09-21 11:59:42 -0700149
mcaleavyaf5c40cb2016-02-19 23:02:39 +0000150 if start_ts == 0:
151 prev_ts = start_ts
Brendan Greggd9e578b2015-09-21 11:59:42 -0700152
mcaleavyaf5c40cb2016-02-19 23:02:39 +0000153 if start_ts == 1:
154 delta = float(delta) + (event.ts - prev_ts)
155
156 if (args.failed and (event.ret >= 0)):
157 start_ts = 1
158 prev_ts = event.ts
159 return
Brendan Greggd9e578b2015-09-21 11:59:42 -0700160
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800161 # print columns
162 if args.timestamp:
mcaleavyaf5c40cb2016-02-19 23:02:39 +0000163 print("%-14.9f" % (delta / 1000000), end="")
164
165 print("%-6d %-16s %-4d %-6d %d" % (event.pid, event.comm, event.sig,
166 event.tpid, event.ret))
167
168 prev_ts = event.ts
169 start_ts = 1
170
171# loop with callback to print_event
172b["events"].open_perf_buffer(print_event)
173while 1:
174 b.kprobe_poll()