Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 4 | # killsnoop Trace signals issued by the kill() syscall. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 6 | # |
Junli Ou | 5eee5ff | 2016-08-13 17:12:45 +0800 | [diff] [blame] | 7 | # USAGE: killsnoop [-h] [-x] [-p PID] |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 8 | # |
| 9 | # Copyright (c) 2015 Brendan Gregg. |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 11 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 12 | # 20-Sep-2015 Brendan Gregg Created this. |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 13 | # 19-Feb-2016 Allan McAleavy migrated to BPF_PERF_OUTPUT |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 14 | |
| 15 | from __future__ import print_function |
| 16 | from bcc import BPF |
Brenden Blanco | e8001c3 | 2018-07-23 08:15:56 -0700 | [diff] [blame] | 17 | from bcc.utils import ArgString, printb |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 18 | import argparse |
Junli Ou | 5eee5ff | 2016-08-13 17:12:45 +0800 | [diff] [blame] | 19 | from time import strftime |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 20 | import ctypes as ct |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 21 | |
| 22 | # arguments |
| 23 | examples = """examples: |
| 24 | ./killsnoop # trace all kill() signals |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 25 | ./killsnoop -x # only show failed kills |
| 26 | ./killsnoop -p 181 # only trace PID 181 |
| 27 | """ |
| 28 | parser = argparse.ArgumentParser( |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 29 | description="Trace signals issued by the kill() syscall", |
| 30 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 31 | epilog=examples) |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 32 | parser.add_argument("-x", "--failed", action="store_true", |
Chris Down | 8ddcbdf | 2016-07-13 15:18:35 +0100 | [diff] [blame] | 33 | help="only show failed kill syscalls") |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 34 | parser.add_argument("-p", "--pid", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 35 | help="trace this PID only") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 36 | parser.add_argument("--ebpf", action="store_true", |
| 37 | help=argparse.SUPPRESS) |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 38 | args = parser.parse_args() |
| 39 | debug = 0 |
| 40 | |
| 41 | # define BPF program |
| 42 | bpf_text = """ |
| 43 | #include <uapi/linux/ptrace.h> |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 44 | #include <linux/sched.h> |
| 45 | |
| 46 | struct val_t { |
| 47 | u64 pid; |
mcaleavya | 6262f5d | 2016-02-19 23:48:20 +0000 | [diff] [blame] | 48 | int sig; |
| 49 | int tpid; |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 50 | char comm[TASK_COMM_LEN]; |
| 51 | }; |
| 52 | |
| 53 | struct data_t { |
| 54 | u64 pid; |
Brendan Gregg | 1298998 | 2016-09-14 08:15:09 -0700 | [diff] [blame] | 55 | int tpid; |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 56 | int sig; |
| 57 | int ret; |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 58 | char comm[TASK_COMM_LEN]; |
| 59 | }; |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 60 | |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 61 | BPF_HASH(infotmp, u32, struct val_t); |
| 62 | BPF_PERF_OUTPUT(events); |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 63 | |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 64 | int syscall__kill(struct pt_regs *ctx, int tpid, int sig) |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 65 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 66 | u32 pid = bpf_get_current_pid_tgid(); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 67 | FILTER |
Brendan Gregg | 1298998 | 2016-09-14 08:15:09 -0700 | [diff] [blame] | 68 | |
| 69 | struct val_t val = {.pid = pid}; |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 70 | if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) { |
mcaleavya | 6262f5d | 2016-02-19 23:48:20 +0000 | [diff] [blame] | 71 | val.tpid = tpid; |
| 72 | val.sig = sig; |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 73 | infotmp.update(&pid, &val); |
| 74 | } |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 75 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 76 | return 0; |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Yonghong Song | 6433569 | 2018-04-25 00:40:13 -0700 | [diff] [blame] | 79 | int do_ret_sys_kill(struct pt_regs *ctx) |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 80 | { |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 81 | struct data_t data = {}; |
| 82 | struct val_t *valp; |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 83 | u32 pid = bpf_get_current_pid_tgid(); |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 84 | |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 85 | valp = infotmp.lookup(&pid); |
| 86 | if (valp == 0) { |
| 87 | // missed entry |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm); |
| 92 | data.pid = pid; |
mcaleavya | 6262f5d | 2016-02-19 23:48:20 +0000 | [diff] [blame] | 93 | data.tpid = valp->tpid; |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 94 | data.ret = PT_REGS_RC(ctx); |
mcaleavya | 6262f5d | 2016-02-19 23:48:20 +0000 | [diff] [blame] | 95 | data.sig = valp->sig; |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 96 | |
| 97 | events.perf_submit(ctx, &data, sizeof(data)); |
| 98 | infotmp.delete(&pid); |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 99 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 100 | return 0; |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 101 | } |
| 102 | """ |
| 103 | if args.pid: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 104 | bpf_text = bpf_text.replace('FILTER', |
| 105 | 'if (pid != %s) { return 0; }' % args.pid) |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 106 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 107 | bpf_text = bpf_text.replace('FILTER', '') |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 108 | if debug or args.ebpf: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 109 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 110 | if args.ebpf: |
| 111 | exit() |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 112 | |
| 113 | # initialize BPF |
| 114 | b = BPF(text=bpf_text) |
Yonghong Song | 6433569 | 2018-04-25 00:40:13 -0700 | [diff] [blame] | 115 | kill_fnname = b.get_syscall_fnname("kill") |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 116 | b.attach_kprobe(event=kill_fnname, fn_name="syscall__kill") |
Yonghong Song | 6433569 | 2018-04-25 00:40:13 -0700 | [diff] [blame] | 117 | b.attach_kretprobe(event=kill_fnname, fn_name="do_ret_sys_kill") |
| 118 | |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 119 | |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 120 | TASK_COMM_LEN = 16 # linux/sched.h |
| 121 | |
| 122 | class Data(ct.Structure): |
| 123 | _fields_ = [ |
| 124 | ("pid", ct.c_ulonglong), |
Brendan Gregg | 1298998 | 2016-09-14 08:15:09 -0700 | [diff] [blame] | 125 | ("tpid", ct.c_int), |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 126 | ("sig", ct.c_int), |
| 127 | ("ret", ct.c_int), |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 128 | ("comm", ct.c_char * TASK_COMM_LEN) |
| 129 | ] |
| 130 | |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 131 | # header |
Junli Ou | 5eee5ff | 2016-08-13 17:12:45 +0800 | [diff] [blame] | 132 | print("%-9s %-6s %-16s %-4s %-6s %s" % ( |
| 133 | "TIME", "PID", "COMM", "SIG", "TPID", "RESULT")) |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 134 | |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 135 | # process event |
| 136 | def print_event(cpu, data, size): |
| 137 | event = ct.cast(data, ct.POINTER(Data)).contents |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 138 | |
Brendan Gregg | 1298998 | 2016-09-14 08:15:09 -0700 | [diff] [blame] | 139 | if (args.failed and (event.ret >= 0)): |
| 140 | return |
Brendan Gregg | d9e578b | 2015-09-21 11:59:42 -0700 | [diff] [blame] | 141 | |
Brenden Blanco | e8001c3 | 2018-07-23 08:15:56 -0700 | [diff] [blame] | 142 | printb(b"%-9s %-6d %-16s %-4d %-6d %d" % (strftime("%H:%M:%S"), |
| 143 | event.pid, event.comm, event.sig, event.tpid, event.ret)) |
mcaleavya | f5c40cb | 2016-02-19 23:02:39 +0000 | [diff] [blame] | 144 | |
| 145 | # loop with callback to print_event |
| 146 | b["events"].open_perf_buffer(print_event) |
| 147 | while 1: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 148 | b.perf_buffer_poll() |