#!/usr/bin/sh
#
# Copyright (c) 1999, 2001 by Sun Microsystems, Inc.
# All rights reserved.
#
#pragma ident	"@(#)rgmd_debug.sh	1.4	01/02/26 SMI"
#
# This script is an undocumented utility which facilitates
# debugging of the rgmd at customer sites.  This script permits
# the verbose debugging mode of the rgmd to be turned on or off
# on the local node.  To enable or disable verbose debugging mode
# on multiple nodes, the script has to be invoked on each node.
#
# It also allows the trace buffer in the rgmd to be dumped out.
# The trace buffer is a ring buffer that has essentially the same
# information as the verbose debugging messages generated when
# debugging mode is turned on. When the trace buffer gets full,
# the oldest messages get dropped.
#
# Each trace buffer entry has the thread ID of the rgmd thread that
# generated the message and the last 8 digits of a microsecond
# timestamp.
#
# It also provides functionality for printing the node number of the current
# president node.
#
# No references should be made in gate code to this script.
#
# Usage:	$0 { on | off | printbuf | pres }
#
# When verbose debugging mode is turned on, the rgmd will syslog a
# large number of messages which will appear on the console and
# in /var/adm/messages.  These messages are intended for the use
# of product sustaining engineers within Sun; they are not intended
# to be used by customers.
#
PGREP=/usr/bin/pgrep
ID=/usr/bin/id
TEMPFILE=/tmp/`basename $0`.$$
rm -f $TEMPFILE

print_pres=0

case "$1" in
on|ON|On|1)
	flag=1
	newstate=ON
	change_state=1
	;;

off|OFF|Off|0)
	flag=0
	newstate=OFF
	change_state=1
	;;

printbuf|PRINTBUF|Printbuf)
	change_state=0
	;;

pres|PRES|Pres)
	print_pres=1
	change_state=0
	;;

*)
	echo "Usage:	$0 { on | off | printbuf | pres }"
	exit 1
	;;
esac

res=`$ID`
if [ $? -ne 0 ]; then
	echo "Failed: cannot execute $ID command."
	exit 1
fi
set -- $res
if [ "$1" != "uid=0(root)" ]; then
	echo "Failed: this program must be executed as root."
	exit 1
fi

procid=`$PGREP -x rgmd`
numprocs=`echo $procid|wc -w`
if [ $numprocs -eq 0 ]; then
	echo 'Failed: cannot obtain process id of rgmd.'
	exit 1
fi
if [ $numprocs -gt 1 ]; then
	echo 'Failed: more than one rgmd process is running.'
	exit 1
fi

# if we're supposed to print the president, do it first and then exit so
# we don't execute any of the other functionality
if [ $print_pres -eq 1 ]; then
	rgm_state_adr=`echo "0t${procid}:A\nRgm_state/K\n:R\n$q" | adb - - | \
	    awk '/Rgm_state:[a-z0-9 	]/ {print $2}'`

	if [ $? -ne 0 ]; then
	    echo "Command failed."
	    exit 1
	fi

	# Note: the next line depends on a hard-coded byte offset into an
	# rgmd data structure and will need to be kept in sync with future
	# changes to the rgmd such as modification of the data structure or
	# compilation as a 64 bit application.
	node=`echo "0t${procid}:A\n${rgm_state_adr}+18/D\n:R\n$q" | adb - - | \
	    nawk -F: '/^[0-9a-fx]+:[1-9	 ]+/ {print $2}' | nawk \
	    '{gsub(/	/,"")}{print}'`

	if [ $? -ne 0 ]; then
	    echo "Command failed."
	    exit 1
	fi
	    
	echo $node
	exit 0
fi

if [ $change_state -eq 1 ]; then
	echo 0t${procid}':A\nDebugflag/W '${flag}'\n:R\n$q' | \
	    adb - - >$TEMPFILE 2>&1
else
	echo 0t${procid}':A\n*rgm_dbg_buf/s\n:R\n$q' | adb - -
fi

res=$?
if [ $res -ne 0 -a $change_state -eq 1 ]; then
	echo "adb command execution failed, output was:"
	cat $TEMPFILE 1>&2
fi

rm -f $TEMPFILE
if [ $res -eq 0 -a $change_state -eq 1 ]; then
	echo "rgmd verbose debugging is $newstate"
fi
exit $res
