#!/pkg/bin/ksh
#
# Collect debug information on the supplied pid before it is killed.
# If we have 10 or more snaps, delete all but the 5 oldest and 4 newest snaps.
#
# July 2006, Gurudatt Shenoy
# 
#  Copyright (c) 2006-2007, 2009-2015 by cisco Systems, Inc.
#  All rights reserved.
#

usage="[-C console_node] pid_of_caller calling_process calling_function line_number nodename pid jid"
GETAPPMEDIA="/pkg/sbin/getappmedia"

unset CONSOLE_NODE
while getopts C: c
do
    case $c in
    C) CONSOLE_NODE=$OPTARG;;
    *) echo "Usage: $0 $usage" >&2; exit 1;;
    esac
done

# Move forward in the argument list
shift $(($OPTIND - 1))


# We expect 7 arguments. 
if [ -z $7 ]
then
    echo "Usage: $0 $usage" >&2
    exit 1
fi

calling_pid=$1
shift
calling_process=$1
shift 
function=$1
shift
line=$1
shift
nodename=$1
shift
pid=$1
shift
jid=$1
shift
nodeid=$1
shift

[[ ! -x $GETAPPMEDIA ]] && {
        echo "Could not find $GETAPPMEDIA"
        return
}

if [ -n "$CONSOLE_NODE" ]; then
DEV=`$GETAPPMEDIA -C $CONSOLE_NODE diag`
else
DEV=`$GETAPPMEDIA diag`
fi

[[ ! -d $DEV ]] && {
	echo "Destination directory not found ($DEV)"
	return
}

DEBUG_DIR=${DEV}/resmon_debug
if [[ $DEV != "/dev/shmem" && ! -d $DEBUG_DIR ]]
then
    echo "Making $DEBUG_DIR" 2>&1 
    mkdir $DEBUG_DIR
fi

DEBUG_FILE="$DEBUG_DIR/debug_filedesc.$nodename.$pid.log"

# Define an array of debug files, sorted in time increasing order
#set -A debug_files `ls -tr $DEBUG_DIR/debug_filedesc.* 2>/dev/null`
debug_files=`ls -tr $DEBUG_DIR/debug_filedesc.* 2>/dev/null`

# 'i' is the index of the last array element
i=${#debug_files[*]} 

# If 10 or more files in the directory...
if [ $i -ge 9 ]
then
    # Keep the 5 oldest and 4 newest
    k=$(($i-4))
    j=5
    while [ $j -lt $k ]
    do
        rm ${debug_files[$j]} 2>/dev/null
	j=$(($j+1))
    done
fi

/pkg/sbin/sysmgr_log "$0 invoked by pid $calling_pid ($calling_process) for pid $pid, jid $jid.
Output is in $DEBUG_FILE"

# Generate the snap
(
echo "----------------------------------------------------------------"
echo "Gathering info about potential FD leak. Time `iosclock -d 0x0`"
echo "----------------------------------------------------------------"

proc_name=`cat /proc/${pid}/stat | cut -d')' -f1 | cut -d'(' -f2`
core_location="/misc/scratch/core"

echo "----------------------------------------------------------------"
echo "Dumping running core for pid:$pid process:$proc_name at $core_location Time `iosclock -d 0x0`"
echo "----------------------------------------------------------------"
    # core_location=$nodename

    /pkg/bin/dump_core $core_location ${proc_name}
#    corehelper_gen -o running -p $proc_name -n $nodeid

echo "----------------------------------------------------------------"
echo "Output from gstack for pid:$pid Time `iosclock -d 0x0`"
echo "----------------------------------------------------------------"
  NO_DLLMAIN=1 gstack $pid
#attach_process -A -p $pid -i 1

echo "----------------------------------------------------------------"
echo "Output from show process file <jid> details: Time `iosclock -d 0x0`"
echo "----------------------------------------------------------------"
  NO_DLLMAIN=1 sh_proc_files -j $jid -v 
echo "----------------------------------------------------------------"
echo "Exiting at Time `iosclock -d 0x0`."

) > $DEBUG_FILE 2>&1
