#!/pkg/bin/ksh
#
# Collect debug information on the supplied Toppid before it's killed.
# If we have 10 or more snaps, delete all but the 5 oldest and 4 newest snaps.
#
# August 2003, Tim LaBerge 
# 
#  Copyright (c) 2003-2007, 2010, 2015-2016 by cisco Systems, Inc.
#  All rights reserved.
#

usage="[-C console_node] toppid_to_trace  pid_of_caller calling_process calling_function line_number nodename"
unset pid
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 exactly 6 arguments. No more, no less. 6 shall be the number of the
# arguments and the number of the arguments shall be 6...
if [ -z $6 ]
then
    echo "Usage: $0 $usage" >&2
    exit 1
fi

if [ -n "$7" ]
then
    echo "Usage: $0 $usage" >&2
    exit 1
fi

toppid=$1
calling_pid=$2
calling_process=$3
function=$4
line=$5
nodename=$6

# We want to save data locally, so as not to rely on qnet.
[[ ! -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/wdsysmon_debug
if [[ $DEV != "/dev/shmem" && ! -d $DEBUG_DIR ]]
then
    mkdir $DEBUG_DIR
fi

DEBUG_FILE="$DEBUG_DIR/debug_mem.$nodename.$$"

# Define an array of debug files, sorted in time increasing order
# FIXME: the ls can fail in low mem (it's a spawn of a subshell)
set -A debug_files `ls -tr $DEBUG_DIR/debug_mem.* 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

echo "$0 invoked by pid $calling_pid ($calling_process). Output is in $DEBUG_FILE" > /dev/syslog/LOG_DEBUG/wdsysmon

# Some of what goes on here is expensive. Set our priority down.
setprio 10
# Generate the snap
(
echo "$0 invoked by pid $calling_pid ($calling_process). 
Called by $function at line $line at `iosclock -d 0x0`." 

echo "----------------------------------------------------------------"
echo "Output from show proc mem $pid:"
sh_proc_memory_sort "sh_proc_mem_cli -p $toppid"
echo "----------------------------------------------------------------"
echo "Output from show dll pid $pid:"
show_dll -p $toppid
echo "----------------------------------------------------------------"
#
# "malloc_dump -A -p $jobid -d" was getting called earlier but removed as part of 
# CSCsg44933 since this was leading to problems if process had allocated lot
# of memory
#
echo "Output from show mem $pid:"
show_memory_ng -p $toppid

echo "+++++++++++++++++++++ show version start ++++++++++++++++++++++++++"
ng_show_version
echo "--------------------- show version end  ---------------------------"


echo "+++++++++++++++++++++ show logging start ++++++++++++++++++++++++++"
show_logging
echo "--------------------- show logging end  ---------------------------"

echo "+++++++++++++++++++++ show memory summary start +++++++++++++++++++"
show_memory_ng -s
echo "--------------------- show  memory summary end  -------------------"


echo "+++++++++++++++++++++ show proc memory  start +++++++++++++++++++++"
sh_proc_memory_sort "sh_proc_mem_cli"
echo "--------------------- show proc memory end  -----------------------"


echo "+++++++++++++++++++++ show watchdog trace  start +++++++++++++++++++"
show_watchdog_trace
echo "--------------------- show watchdog trace end  ---------------------"

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



) > $DEBUG_FILE 2>&1
