blob: 6022cd0848d327a80fef3b0a23e86a03e87a660c [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#
Junli Ou5eee5ff2016-08-13 17:12:45 +08007# USAGE: killsnoop [-h] [-x] [-p PID]
Brendan Greggd9e578b2015-09-21 11:59:42 -07008#
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
Junli Ou5eee5ff2016-08-13 17:12:45 +080018from time import strftime
mcaleavyaf5c40cb2016-02-19 23:02:39 +000019import ctypes as ct
Brendan Greggd9e578b2015-09-21 11:59:42 -070020
21# arguments
22examples = """examples:
23 ./killsnoop # trace all kill() signals
Brendan Greggd9e578b2015-09-21 11:59:42 -070024 ./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("-x", "--failed", action="store_true",
Chris Down8ddcbdf2016-07-13 15:18:35 +010032 help="only show failed kill syscalls")
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")
Nathan Scottcf0792f2018-02-02 16:56:50 +110035parser.add_argument("--ebpf", action="store_true",
36 help=argparse.SUPPRESS)
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;
mcaleavya6262f5d2016-02-19 23:48:20 +000047 int sig;
48 int tpid;
mcaleavyaf5c40cb2016-02-19 23:02:39 +000049 char comm[TASK_COMM_LEN];
50};
51
52struct data_t {
53 u64 pid;
Brendan Gregg12989982016-09-14 08:15:09 -070054 int tpid;
mcaleavyaf5c40cb2016-02-19 23:02:39 +000055 int sig;
56 int ret;
mcaleavyaf5c40cb2016-02-19 23:02:39 +000057 char comm[TASK_COMM_LEN];
58};
Brendan Greggd9e578b2015-09-21 11:59:42 -070059
mcaleavyaf5c40cb2016-02-19 23:02:39 +000060BPF_HASH(infotmp, u32, struct val_t);
61BPF_PERF_OUTPUT(events);
Brendan Greggd9e578b2015-09-21 11:59:42 -070062
yonghong-song2da34262018-06-13 06:12:22 -070063int syscall__kill(struct pt_regs *ctx, int tpid, int sig)
Brendan Greggd9e578b2015-09-21 11:59:42 -070064{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080065 u32 pid = bpf_get_current_pid_tgid();
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080066 FILTER
Brendan Gregg12989982016-09-14 08:15:09 -070067
68 struct val_t val = {.pid = pid};
mcaleavyaf5c40cb2016-02-19 23:02:39 +000069 if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
mcaleavya6262f5d2016-02-19 23:48:20 +000070 val.tpid = tpid;
71 val.sig = sig;
mcaleavyaf5c40cb2016-02-19 23:02:39 +000072 infotmp.update(&pid, &val);
73 }
Brendan Greggd9e578b2015-09-21 11:59:42 -070074
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080075 return 0;
Brendan Greggd9e578b2015-09-21 11:59:42 -070076};
77
Yonghong Song64335692018-04-25 00:40:13 -070078int do_ret_sys_kill(struct pt_regs *ctx)
Brendan Greggd9e578b2015-09-21 11:59:42 -070079{
mcaleavyaf5c40cb2016-02-19 23:02:39 +000080 struct data_t data = {};
81 struct val_t *valp;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080082 u32 pid = bpf_get_current_pid_tgid();
Brendan Greggd9e578b2015-09-21 11:59:42 -070083
mcaleavyaf5c40cb2016-02-19 23:02:39 +000084 valp = infotmp.lookup(&pid);
85 if (valp == 0) {
86 // missed entry
87 return 0;
88 }
89
90 bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm);
91 data.pid = pid;
mcaleavya6262f5d2016-02-19 23:48:20 +000092 data.tpid = valp->tpid;
Naveen N. Rao4afa96a2016-05-03 14:54:21 +053093 data.ret = PT_REGS_RC(ctx);
mcaleavya6262f5d2016-02-19 23:48:20 +000094 data.sig = valp->sig;
mcaleavyaf5c40cb2016-02-19 23:02:39 +000095
96 events.perf_submit(ctx, &data, sizeof(data));
97 infotmp.delete(&pid);
Brendan Greggd9e578b2015-09-21 11:59:42 -070098
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080099 return 0;
Brendan Greggd9e578b2015-09-21 11:59:42 -0700100}
101"""
102if args.pid:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800103 bpf_text = bpf_text.replace('FILTER',
104 'if (pid != %s) { return 0; }' % args.pid)
Brendan Greggd9e578b2015-09-21 11:59:42 -0700105else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800106 bpf_text = bpf_text.replace('FILTER', '')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100107if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800108 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100109 if args.ebpf:
110 exit()
Brendan Greggd9e578b2015-09-21 11:59:42 -0700111
112# initialize BPF
113b = BPF(text=bpf_text)
Yonghong Song64335692018-04-25 00:40:13 -0700114kill_fnname = b.get_syscall_fnname("kill")
yonghong-song2da34262018-06-13 06:12:22 -0700115b.attach_kprobe(event=kill_fnname, fn_name="syscall__kill")
Yonghong Song64335692018-04-25 00:40:13 -0700116b.attach_kretprobe(event=kill_fnname, fn_name="do_ret_sys_kill")
117
Brendan Greggd9e578b2015-09-21 11:59:42 -0700118
mcaleavyaf5c40cb2016-02-19 23:02:39 +0000119TASK_COMM_LEN = 16 # linux/sched.h
120
121class Data(ct.Structure):
122 _fields_ = [
123 ("pid", ct.c_ulonglong),
Brendan Gregg12989982016-09-14 08:15:09 -0700124 ("tpid", ct.c_int),
mcaleavyaf5c40cb2016-02-19 23:02:39 +0000125 ("sig", ct.c_int),
126 ("ret", ct.c_int),
mcaleavyaf5c40cb2016-02-19 23:02:39 +0000127 ("comm", ct.c_char * TASK_COMM_LEN)
128 ]
129
Brendan Greggd9e578b2015-09-21 11:59:42 -0700130# header
Junli Ou5eee5ff2016-08-13 17:12:45 +0800131print("%-9s %-6s %-16s %-4s %-6s %s" % (
132 "TIME", "PID", "COMM", "SIG", "TPID", "RESULT"))
Brendan Greggd9e578b2015-09-21 11:59:42 -0700133
mcaleavyaf5c40cb2016-02-19 23:02:39 +0000134# process event
135def print_event(cpu, data, size):
136 event = ct.cast(data, ct.POINTER(Data)).contents
Brendan Greggd9e578b2015-09-21 11:59:42 -0700137
Brendan Gregg12989982016-09-14 08:15:09 -0700138 if (args.failed and (event.ret >= 0)):
139 return
Brendan Greggd9e578b2015-09-21 11:59:42 -0700140
Junli Ou5eee5ff2016-08-13 17:12:45 +0800141 print("%-9s %-6d %-16s %-4d %-6d %d" % (strftime("%H:%M:%S"),
Rafael F78948e42017-03-26 14:54:25 +0200142 event.pid, event.comm.decode(), event.sig, event.tpid, event.ret))
mcaleavyaf5c40cb2016-02-19 23:02:39 +0000143
144# loop with callback to print_event
145b["events"].open_perf_buffer(print_event)
146while 1:
Teng Qindbf00292018-02-28 21:47:50 -0800147 b.perf_buffer_poll()