#!/pkg/bin/ksh
#
# Take a pidin snapshot and store it in the pqmon_debug directory.
# If we have 20 or more snaps, delete all but the 20 newest snaps.
#
# Feb 2009, Chaks
# (Adapted from sysmgr_debug_script written by Tim LaBerge)
# 
# Copyright (c) 2009 by cisco Systems, Inc.
# All rights reserved.

usage="[-p pid_to_trace] [-D debug_cmd] [-r reason] pid_of_caller calling_process calling_function line_number"

unset pid
unset debug_cmd
unset reason

while getopts p:D:r:P: c
do
    case $c in
    p) pid=$OPTARG;;
    D) debug_cmd=$OPTARG;;
    r) reason=$OPTARG;;
    *) echo "Usage: $0 $usage"; exit 1;;
    esac
done

shift $(($OPTIND - 1))

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

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

calling_pid=$1
calling_process=$2
function=$3
line=$4

# Some of the operations we do in this script are expensive. Lower our prio
setprio 10

DEBUG_DIR=/tmp/pqmon_debug
DEBUG_FILE="$DEBUG_DIR/debug.$$"

# Define an array of debug files, sorted in time increasing order
set -A debug_files `ls -tr $DEBUG_DIR/debug.* 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 20 newest
    k=$(($i-20))
    j=0
    while [ $j -lt $k ]
    do
        rm ${debug_files[$j]} 2>/dev/null
	j=$(($j+1))
    done
fi
if [ -z "$reason" ]; then
    sysmgr_log "$0 invoked by ($calling_process) for pid ($pid). Output is in $DEBUG_FILE"
else
    sysmgr_log "$0 invoked for: $reason. Output is in $DEBUG_FILE"
fi

# Generate the snap
(
echo "Script invoked at `iosclock -d 0x0`"

if [ ! -z "$reason" ]; then
echo "Problem detected, Reason: $reason"
fi
echo "$0 invoked by ($calling_process)
Called by $function at line $line at `clock_chip -c`." 

echo "----------------------------------------------------------------"
echo "Output of: show_processes -b"
/pkg/bin/show_processes -b

echo "----------------------------------------------------------------"
echo "Output of: pidin"
pidin

# If we have a pid, get the attach_process output
if [ -n "$pid" ]
then
    echo "------------------------------------------------------------"
    echo "Output of: attach_process -A -p $pid -i 1"
    attach_process -A -p $pid -i 1
fi

if [ -n "$pid" ]
then
    echo "------------------------------------------------------------"
    echo "Output of: top_procs"
    top_procs -i 1 -c -D 
fi

if [ -n "$pid" ]
then
    echo "------------------------------------------------------------"
    echo "Output of: xipc_show -p $pid"
    xipc_show -p $pid
fi

if [ -n "$pid" ]
then
    echo "------------------------------------------------------------"
    echo "Output of: qad_show -b"
    qad_show -b
fi

echo "------------------------------------------------------------"
echo "Output of: pqmon_show_ltrace -D -T"
show_pqmon_ltrace -D -T

if [ ! -z "$debug_cmd" ]
then
    echo "------------------------------------------------------------"
    echo "Output of: $debug_cmd"
    eval $debug_cmd
fi

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

) > $DEBUG_FILE 2>&1
