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