#!/pkg/bin/ksh
#
# Collect debug information on the supplied pids before they are killed.
# If we have 10 or more snaps, delete all but the 5 oldest and 4 newest snaps.
#
# December 2004, Tim LaBerge 
# 
#  Copyright (c) 2004-2007, 2010, 2012-2014 by cisco Systems, Inc.
#  All rights reserved.
#


usage="[-C console_node] pid_of_caller calling_process calling_function line_number nodename pids_to_trace  "
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 at least 6 arguments. 
if [ -z $6 ]
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
nodeid=$1
shift


pids=$@
echo "Tracing pids ${pids[*]}"

[[ ! -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
    mkdir $DEBUG_DIR
fi

DEBUG_FILE="$DEBUG_DIR/debug_deadlock.$nodename.${pids[*]}log"

# Define an array of debug files, sorted in time increasing order
# set -A debug_files `ls -tr $DEBUG_DIR/debug_deadlock.* 2>/dev/null`
  debug_files=`ls -tr $DEBUG_DIR/debug_deadlock.* 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(s) ${pids[*]}. Output is in $DEBUG_FILE"


# Generate the snap
(
echo "----------------------------------------------------------------"
echo "Called by function $function from line $line at `iosclock -d 0x0`"
echo "----------------------------------------------------------------"

echo "----------------------------------------------------------------"
echo "Output from ps at Time `iosclock -d 0x0`"
echo "----------------------------------------------------------------"
  NO_DLLMAIN=1 ps

echo "----------------------------------------------------------------"
echo "Output from lwm_debug_proc -p all -b  at Time `iosclock -d 0x0`"
echo "----------------------------------------------------------------"
  NO_DLLMAIN=1 lwm_debug_proc -p all -b


for pid in `echo ${pids[*]}`
do
    echo "----------------------------------------------------------------"
    echo "Tracing pid $pid at Time `iosclock -d 0x0`"
    echo "----------------------------------------------------------------"
    echo "Output from gstack pid:$pid at Time `iosclock -d 0x0`"
    NO_DLLMAIN=1 gstack $pid
    echo "----------------------------------------------------------------"

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

#    echo "Dumping running core for pid:$pid process:$proc_name at $core_locationat Time `iosclock -d 0x0` "
    # /pkg/bin/dump_core $core_location ${proc_name}
#    corehelper_gen -o running -p $proc_name -n $nodeid

done
echo "----------------------------------------------------------------"
echo "Exiting at Time `iosclock -d 0x0`."

) > $DEBUG_FILE 2>&1
