1#!/usr/bin/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 20 21# arguments 22examples = """examples: 23 ./killsnoop # trace all kill() signals 24 ./killsnoop -x # only show failed kills 25 ./killsnoop -p 181 # only trace PID 181 26 ./killsnoop -s 9 # only trace signal 9 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("-s", "--signal", 37 help="trace this signal only") 38parser.add_argument("--ebpf", action="store_true", 39 help=argparse.SUPPRESS) 40args = parser.parse_args() 41debug = 0 42 43# define BPF program 44bpf_text = """ 45#include <uapi/linux/ptrace.h> 46#include <linux/sched.h> 47 48struct val_t { 49 u64 pid; 50 int sig; 51 int tpid; 52 char comm[TASK_COMM_LEN]; 53}; 54 55struct data_t { 56 u64 pid; 57 int tpid; 58 int sig; 59 int ret; 60 char comm[TASK_COMM_LEN]; 61}; 62 63BPF_HASH(infotmp, u32, struct val_t); 64BPF_PERF_OUTPUT(events); 65 66int syscall__kill(struct pt_regs *ctx, int tpid, int sig) 67{ 68 u64 pid_tgid = bpf_get_current_pid_tgid(); 69 u32 pid = pid_tgid >> 32; 70 u32 tid = (u32)pid_tgid; 71 72 PID_FILTER 73 SIGNAL_FILTER 74 75 struct val_t val = {.pid = pid}; 76 if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) { 77 val.tpid = tpid; 78 val.sig = sig; 79 infotmp.update(&tid, &val); 80 } 81 82 return 0; 83}; 84 85int do_ret_sys_kill(struct pt_regs *ctx) 86{ 87 struct data_t data = {}; 88 struct val_t *valp; 89 u64 pid_tgid = bpf_get_current_pid_tgid(); 90 u32 pid = pid_tgid >> 32; 91 u32 tid = (u32)pid_tgid; 92 93 valp = infotmp.lookup(&tid); 94 if (valp == 0) { 95 // missed entry 96 return 0; 97 } 98 99 bpf_probe_read_kernel(&data.comm, sizeof(data.comm), valp->comm); 100 data.pid = pid; 101 data.tpid = valp->tpid; 102 data.ret = PT_REGS_RC(ctx); 103 data.sig = valp->sig; 104 105 events.perf_submit(ctx, &data, sizeof(data)); 106 infotmp.delete(&tid); 107 108 return 0; 109} 110""" 111if args.pid: 112 bpf_text = bpf_text.replace('PID_FILTER', 113 'if (pid != %s) { return 0; }' % args.pid) 114else: 115 bpf_text = bpf_text.replace('PID_FILTER', '') 116if args.signal: 117 bpf_text = bpf_text.replace('SIGNAL_FILTER', 118 'if (sig != %s) { return 0; }' % args.signal) 119else: 120 bpf_text = bpf_text.replace('SIGNAL_FILTER', '') 121if debug or args.ebpf: 122 print(bpf_text) 123 if args.ebpf: 124 exit() 125 126# initialize BPF 127b = BPF(text=bpf_text) 128kill_fnname = b.get_syscall_fnname("kill") 129b.attach_kprobe(event=kill_fnname, fn_name="syscall__kill") 130b.attach_kretprobe(event=kill_fnname, fn_name="do_ret_sys_kill") 131 132# header 133print("%-9s %-6s %-16s %-4s %-6s %s" % ( 134 "TIME", "PID", "COMM", "SIG", "TPID", "RESULT")) 135 136# process event 137def print_event(cpu, data, size): 138 event = b["events"].event(data) 139 140 if (args.failed and (event.ret >= 0)): 141 return 142 143 printb(b"%-9s %-6d %-16s %-4d %-6d %d" % (strftime("%H:%M:%S").encode('ascii'), 144 event.pid, event.comm, event.sig, event.tpid, event.ret)) 145 146# loop with callback to print_event 147b["events"].open_perf_buffer(print_event) 148while 1: 149 try: 150 b.perf_buffer_poll() 151 except KeyboardInterrupt: 152 exit() 153