#!/bin/sh
#
# Portable core of running processes
# author pancake
#

if [ -z "$1" ]; then
	echo "Usage: rsc core [pid]"
else
	gcore > /dev/null 2>&1
	if [ $? = 0 ]; then
		gcore $1
	else
		PID=$1
		CORE="core.$PID"
		CMDFILE="/tmp/.core-gen"
		echo "generate-core-file $CORE
		quit" > $CMDFILE
		gdb -x $CMDFILE /proc/$PID/exe $PID 2>&1 >/dev/null
		rm -f $CMDFILE
		echo $CORE
	fi
fi
