1#! /bin/sh 2 3linux_check_ptrace_scope() 4{ 5 if grep -q '1' </proc/sys/kernel/yama/ptrace_scope; then 6 cat <<EOF 7Your system prevents the use of PTRACE to attach to non-child processes. The core file 8cannot be generated. Please reset /proc/sys/kernel/yama/ptrace_scope to 0 (requires root 9privileges) to enable core generation via gcore. 10EOF 11 exit 1 12 fi 13} 14 15set -e -x 16 17OS=$(uname -s) 18if [ "$OS" = Linux ]; then 19 linux_check_ptrace_scope 20fi 21 22rm -f a.out 23make -f main.mk 24 25cat <<EOF 26Executable file is in a.out. 27Core file will be saved as core.<pid>. 28EOF 29 30stack_size=`ulimit -s` 31 32# Decrease stack size to 16k => smaller core files. 33# gcore won't run with the smaller stack 34ulimit -Ss 16 35 36core_dump_filter=`cat /proc/self/coredump_filter` 37echo 0 > /proc/self/coredump_filter 38 39./a.out & 40 41pid=$! 42 43echo $core_dump_filter > /proc/self/coredump_filter 44 45# Reset stack size as so there's enough space to run gcore. 46ulimit -s $stack_size 47 48echo "Sleeping for 5 seconds to wait for $pid" 49 50sleep 5 51echo "Taking core from process $pid" 52 53gcore -o core $pid 54 55echo "Killing process $pid" 56kill -9 $pid 57