#!/bin/sh
#Usage: nos_debug < -s | -r | -e >

mptool_conf=0
gcov_conf=0
vg_conf=0
slub_debug_conf=0
_debug_data_parse_config ()
{
    if [ ! -f /etc/fabos/nos_debug.conf ];
    then
        return
    fi

    /bin/cat /etc/fabos/nos_debug.conf | /bin/grep MP > /dev/null
    if [ $? = 0 ];
    then
        mptool_conf=1
    fi

    /bin/cat /etc/fabos/nos_debug.conf | /bin/grep GCOV > /dev/null
    if [ $? = 0 ];
    then
        gcov_conf=1
    fi

    /bin/cat /etc/fabos/nos_debug.conf | /bin/grep VG > /dev/null
    if [ $? = 0 ];
    then
        vg_conf=1
    fi

    #Clean valgrind enabled binaries from last run.
	ls /fabos/libexec/orig_bin/* > /dev/null  2>&1
    if [ $? = 0 ];
    then
        vg_conf=1
    fi

    /bin/cat /etc/fabos/nos_debug.conf | /bin/grep SLUB_DEBUG > /dev/null
    if [ $? = 0 ];
    then
        slub_debug_conf=1
    fi

	/sbin/bootenv | grep slub_debug > /dev/null
	slub_status=$?
	if [ $slub_status = 0 ];
    then
        slub_debug_conf=1
    fi
}

_debug_data_create_package ()
{
    SWBD=`/sbin/sin 2>/dev/null | /bin/grep SWBD | /usr/bin/cut -d ' ' -f 2 | /usr/bin/cut -d ',' -f 1`
    #No difference between 1000 and 1001 from number of MM/LC point of view.
    if [ $SWBD = "SWBD1001" ];
    then
        SWBD="SWBD1000"
    fi

    ifname="eth0"
    if [ $SWBD = "SWBD1000" ];
    then
        ifname="eth0:1"
    fi

    touch /tmp/nos_debug/${SWBD}

    swip=`/sbin/ifconfig $ifname | grep inet | awk '{print $2}' | cut -d ":" -f 2`
    var=`/bin/date +"%F-%H-%M"`
    mv /tmp/nos_debug /tmp/nos_debug_${swip}_${var}
    cd /tmp
    tar cfz nos_debug_${swip}_${var}.tar.gz nos_debug_${swip}_${var}
    echo "NOS debug data available at /tmp/nos_debug_${swip}_${var}.tar.gz"
}

if [ ! $# = 1 ];
then
    echo "Usage: nos_debug < -s | -v | -k | -r | -e >"
    echo "      -s: Start mptool"
    echo "      -v: Start valgrind"
    echo "      -k: Kernel slub_debug periodic check"
    echo "      -r: Collect debug data before reboot"
    echo "      -e: Collect debug data before ending sanity run"
    exit 1
fi

#simply exit if nos_debug not enabled either by gcov, mptool or valgrind
if [ ! -f /etc/fabos/nos_debug.conf ];
then
    exit 0
fi

_debug_data_parse_config

if [ $1 = "-s" ];
then
    if [ $mptool_conf = 1 ];
    then
        /fabos/bin/nos_mptool -s
    fi
fi

if [ $1 = "-v" ];
then
    if [ $vg_conf = 1 ];
    then
        /fabos/bin/nos_valgrind -f
    fi

	#start slabinfod.sh
    if [ $slub_debug_conf = 1 ];
    then
        /usr/bin/slabinfod.sh start
    fi
fi

if [ $1 = "-r" ];
then
    if [ $mptool_conf = 1 ];
    then
        /fabos/bin/nos_mptool -r
    fi

    if [ $gcov_conf = 1 ];
    then
        /fabos/bin/nos_gcov -d all
    fi

    if [ $slub_debug_conf = 1 ];
    then
        /fabos/bin/nos_slub_debug -r
    fi
fi

if [ $1 = "-e" ];
then
    \rm -fr /tmp/nos_debug
    mkdir /tmp/nos_debug

    if [ $mptool_conf = 1 ];
    then
        /fabos/bin/nos_mptool -e
        mv /var/log/mptool_*.tar.gz /tmp/nos_debug/
    fi

    if [ $gcov_conf = 1 ];
    then
        /fabos/bin/nos_gcov -d all
        /fabos/bin/nos_gcov -c
        mv /var/log/gcov_gcda_*.tar.gz /tmp/nos_debug/
    fi

    if [ $vg_conf = 1 ];
    then
        /fabos/bin/nos_valgrind -e
        mv /var/log/nos_valgrind_*.tar.gz /tmp/nos_debug/
    fi

	if [ $slub_debug_conf = 1 ];
	then
		echo "Collecting slub logs as part of nos_debug"
		/fabos/bin/nos_slub_debug -e
        mv /var/log/nos_slub_debug*.tar.gz /tmp/nos_debug/
	fi

    if [ $mptool_conf = 1 ] || [ $gcov_conf = 1 ] || [ $vg_conf = 1 ] || [ $slub_debug_conf = 1 ];
    then
        _debug_data_create_package
    fi
fi

exit 0
