blob: ddf9d5af0fc79c539145476e0111a93666882324 [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.
Brendan Greggd9e578b2015-09-21 11:59:42 -070013
14from __future__ import print_function
15from bcc import BPF
16import argparse
17
18# arguments
19examples = """examples:
20 ./killsnoop # trace all kill() signals
21 ./killsnoop -t # include timestamps
22 ./killsnoop -x # only show failed kills
23 ./killsnoop -p 181 # only trace PID 181
24"""
25parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080026 description="Trace signals issued by the kill() syscall",
27 formatter_class=argparse.RawDescriptionHelpFormatter,
28 epilog=examples)
Brendan Greggd9e578b2015-09-21 11:59:42 -070029parser.add_argument("-t", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080030 help="include timestamp on output")
Brendan Greggd9e578b2015-09-21 11:59:42 -070031parser.add_argument("-x", "--failed", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080032 help="only show failed opens")
Brendan Greggd9e578b2015-09-21 11:59:42 -070033parser.add_argument("-p", "--pid",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080034 help="trace this PID only")
Brendan Greggd9e578b2015-09-21 11:59:42 -070035args = parser.parse_args()
36debug = 0
37
38# define BPF program
39bpf_text = """
40#include <uapi/linux/ptrace.h>
41
42BPF_HASH(args_pid, u32, int);
43BPF_HASH(args_sig, u32, int);
44
45int kprobe__sys_kill(struct pt_regs *ctx, int tpid, int sig)
46{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080047 u32 pid = bpf_get_current_pid_tgid();
Brendan Greggd9e578b2015-09-21 11:59:42 -070048
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080049 FILTER
50 args_pid.update(&pid, &tpid);
51 args_sig.update(&pid, &sig);
Brendan Greggd9e578b2015-09-21 11:59:42 -070052
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080053 return 0;
Brendan Greggd9e578b2015-09-21 11:59:42 -070054};
55
56int kretprobe__sys_kill(struct pt_regs *ctx)
57{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080058 int *tpidp, *sigp, ret = ctx->ax;
59 u32 pid = bpf_get_current_pid_tgid();
Brendan Greggd9e578b2015-09-21 11:59:42 -070060
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080061 tpidp = args_pid.lookup(&pid);
62 sigp = args_sig.lookup(&pid);
63 if (tpidp == 0 || sigp == 0) {
64 return 0; // missed entry
65 }
Brendan Greggd9e578b2015-09-21 11:59:42 -070066
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080067 bpf_trace_printk("%d %d %d\\n", *tpidp, *sigp, ret);
68 args_pid.delete(&pid);
69 args_sig.delete(&pid);
Brendan Greggd9e578b2015-09-21 11:59:42 -070070
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080071 return 0;
Brendan Greggd9e578b2015-09-21 11:59:42 -070072}
73"""
74if args.pid:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080075 bpf_text = bpf_text.replace('FILTER',
76 'if (pid != %s) { return 0; }' % args.pid)
Brendan Greggd9e578b2015-09-21 11:59:42 -070077else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080078 bpf_text = bpf_text.replace('FILTER', '')
Brendan Greggd9e578b2015-09-21 11:59:42 -070079if debug:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080080 print(bpf_text)
Brendan Greggd9e578b2015-09-21 11:59:42 -070081
82# initialize BPF
83b = BPF(text=bpf_text)
84
85# header
86if args.timestamp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080087 print("%-14s" % ("TIME(s)"), end="")
Brendan Greggd9e578b2015-09-21 11:59:42 -070088print("%-6s %-16s %-4s %-6s %s" % ("PID", "COMM", "SIG", "TPID", "RESULT"))
89
90start_ts = 0
91
92# format output
93while 1:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080094 (task, pid, cpu, flags, ts, msg) = b.trace_fields()
95 (tpid_s, sig_s, ret_s) = msg.split(" ")
Brendan Greggd9e578b2015-09-21 11:59:42 -070096
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080097 ret = int(ret_s)
98 if (args.failed and (ret >= 0)):
99 continue
Brendan Greggd9e578b2015-09-21 11:59:42 -0700100
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800101 # print columns
102 if args.timestamp:
103 if start_ts == 0:
104 start_ts = ts
105 print("%-14.9f" % (ts - start_ts), end="")
106 print("%-6d %-16s %-4s %-6s %s" % (pid, task, sig_s, tpid_s, ret_s))