#!/bin/bash [ "$BASH" ] || { echo "Use bash >= 4"; exit 2; } ((BASH_VERSINFO[0] >= 4)) || { echo "Use bash >= 4"; exit 2; } declare -A kill_time declare -A exit_time declare -a exit_seq declare -A pid_comm declare -A wait_time declare -A killer declare -A recv_sig # ts, pid, signo set_kill_time() { if [[ "${kill_time[$2]}" ]]; then return fi case "$3" in 0 | 1 | 2 | 3 | 6 | 9 | 15) kill_time[$2]=$1 ;; esac } # ts, pid set_exit_time() { if [[ "${exit_time[$2]}" ]]; then return fi exit_time[$2]=$1 exit_seq+=("$pid") } # pid, target_pid, signo add_killer() { killer[$1]+=" $3>$target_pid" } #ts, pid, signo add_signal() { recv_sig[$2]+=" $3($1)" } # https://kernel.org/doc/Documentation/trace/ftrace.txt declare -i lineno=0 while read -r line; do let lineno++ if [[ $line == '' || $line == '#'* ]]; then continue fi read -r -a cols <<< "$line" comm=${cols[0]%-*} pid=${cols[0]#$comm-} ts=${cols[3]%:} ts=${ts//./} func=${cols[4]%(*} func=${func%:} if [[ -z "$shut_time" ]]; then shut_time=$ts ts=0 else zeros=000000 ((ts-=shut_time)) if (( 6 >= ${#ts} )); then ts=${zeros:0:7-${#ts}}$ts fi ts=${ts:0:${#ts}-6}.${ts:${#ts}-6} fi case "$func" in sched_process_exit) set_exit_time "$ts" "$pid" comm="${cols[5]#comm=}" ;; sys_kill | sys_tgkill) if [[ '->' == "${cols[5]}" ]]; then if [[ '0x0' != "${cols[6]}" ]]; then target_pid=${killer[$pid]##*>} if [[ "$target_pid" ]]; then add_killer "$pid" "$target_pid" "$signo" set_exit_time "$ts" "$target_pid" fi fi else let target_pid=0x${cols[5]%,} if [[ 'sys_kill' == "$func" ]]; then let signo=0x${cols[7]%)} else let signo=0x${cols[9]%)} fi set_kill_time "$ts" "$target_pid" "$signo" add_killer "$pid" "$target_pid" "$signo" fi ;; signal_deliver) let signo=${cols[5]#sig=} add_signal "$ts" "$pid" "$signo" ;; sys_exit) set_kill_time "$ts" "$pid" - ;; esac if [[ -z "${pid_comm[$pid]}" || '<...>' == "${pid_comm[$pid]}" ]]; then pid_comm[$pid]=$comm fi done for pid in "${exit_seq[@]}"; do printf "%15s(%4s) exited at %10ss, got signals: %s\n" "${pid_comm[$pid]}" "$pid" "${exit_time[$pid]}" "${recv_sig[$pid]}" done