#!/pkg/bin/ksh
#
# Collect debug information on the supplied jobid 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, 2012-2016, 2020 by cisco Systems, Inc.
#  All rights reserved.
#

usage="[-C console_node] pid_to_trace  pid_of_caller processtokill 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

pid=$1
calling_pid=$2
process_name=$3
function=$4
line=$5
nodename=$6

# We want to save data locally to a disk.
[[ ! -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_mem.$nodename.$pid.log"
DEBUG_LTRACE="$DEBUG_DIR/debug_mem.ltrace.$nodename.$pid"

# 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`
  debug_files=`ls -tr $DEBUG_DIR/debug_mem.* 2>/dev/null`

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

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

#Copy LTRACE directory of process to be killed.
LTRACE_DIR="/dev/shmem/ltrace/$process_name"

if [-e $LTRACE_DIR ]
then 
    mkdir $DEBUG_LTRACE
    cp -r $LTRACE_DIR $DEBUG_LTRACE
/pkg/sbin/sysmgr_log "$0 invoked by (Resmon). Raw LTRACE dump saved to $DEBUG_LTRACE"
else
/pkg/sbin/sysmgr_log "$0 invoked by (Resmon). No LTRACE dump for $process_name"
fi

/pkg/sbin/sysmgr_log "$0 invoked by pid $calling_pid (Resmon). Output is in $DEBUG_FILE"

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

# echo "----------------------------------------------------------------"
# echo "Output from show proc mem $pid: at Time `iosclock -d 0x0`"
# NO_DLLMAIN=1 sh_proc_memory -p $pid

echo "----------------------------------------------------------------"
echo "Output from show dll pid $pid: at Time `iosclock -d 0x0`"
echo "----------------------------------------------------------------"
 NO_DLLMAIN=1 show_dll -p $pid

# echo "----------------------------------------------------------------"
# echo "Output from malloc_dump for pid $pid: at Time `iosclock -d 0x0`"
# echo "----------------------------------------------------------------"
# malloc_dump -A -p $pid -d

echo "----------------------------------------------------------------"
echo "Output from malloc_dump -c $pid  at Time `iosclock -d 0x0`"
echo "----------------------------------------------------------------"
 NO_DLLMAIN=1 malloc_dump -c $pid

echo "----------------------------------------------------------------"
echo "----------------------------------------------------------------"
echo "Output from free -m at Time `iosclock -d 0x0`"
echo "----------------------------------------------------------------"
 NO_DLLMAIN=1 free -m
echo "----------------------------------------------------------------"
echo "----------------------------------------------------------------"
echo "Output from smap /proc/* at Time `iosclock -d 0x0`"
echo "----------------------------------------------------------------"
 NO_DLLMAIN=1 smap /proc/*
echo "----------------------------------------------------------------"
echo "Exiting at Time `iosclock -d 0x0`."
echo "----------------------------------------------------------------"
echo "----------------------------------------------------------------"
echo "Output from df -t tmpfs at Time `iosclock -d 0x0`"
echo "----------------------------------------------------------------"
 NO_DLLMAIN=1 df -t tmpfs
echo "----------------------------------------------------------------"
echo "Exiting at Time `iosclock -d 0x0`."
echo "----------------------------------------------------------------"
echo "----------------------------------------------------------------"
echo "Output from ls -la /tmp/ at Time `iosclock -d 0x0`"
echo "----------------------------------------------------------------"
 NO_DLLMAIN=1 ls -la /tmp/
echo "----------------------------------------------------------------"
echo "Exiting at Time `iosclock -d 0x0`."



) > $DEBUG_FILE 2>&1
