#! /bin/sh
#set -x
#=========================================================================
# Copyright (C) VMware, Inc. 1986-2011.  All Rights Reserved..
#
# Name - gemnetdebug
#
# Purpose - Script to run remote gem for debugging applications
#
#  This script is provided for debugging applications, such as
#  for debugging OutOfMemory situations.  This script can be edited
#  to set the debugging options below, without disturbing the
#  $GEMSTONE/sys/gemnetobject file.  
#  Then in GBS or topaz -r ,  use the gemnetdebug network
#  resource to login a debugging session, and the gemnetobject
#  resource to login a normal session.
#
#  If using a topaz -l, you can set any of the enviroment variables
#  discussed below interactively before invoking topaz -l , to
#  have them take effect in the linked session .
#
#	It is expressly permitted to make a copy of this file in some
#	other directory than $GEMSTONE/bin; hence help logic
#	is different here.
#
# $Id: gemnetdebug.sh,v 1.9 2008-01-09 22:50:52 stever Exp $
#
#=========================================================================

if [ "a$GEMSTONE" = "a" ]; then
  echo "ERROR: GemStone scripts require a GEMSTONE environment variable."
  echo "       Please set it to the directory where GemStone resides."
  exit 1
fi

if [ "x$PATH" = "x" ]; then PATH=:/bin:/usr/bin:/usr/ucb; export PATH; fi 

# error control - do not allow hup
trap '' 1

comid="gemnetdebug"			# this script's name
Syntax="Usage: gemnetdebug <protocol> <wellknown> <timeout> [ -h ]"
Hp="
	protocol	- network protocol for connection, such as \"TCP\"
	wellknown	- bound network address, such as a TCP/IP port number
	timeout		- how long, in seconds, to wait for connection"

Descript="
This Bourne shell script is invoked by a netldi to start a GemStone
session process, in response to a request of the netldi for a GemStone
session.  By default, the file $GEMSTONE/bin/services.dat translates the
GemStone service name gemnetdebug to a command that executes this
script.  If no such translation is found, the command
\$HOME/gemnetdebug is executed instead.

This script may  be  edited  to  change  the default gemname and/or gemdir
for custom gem executables.
"

# give a little help
if [ "x$1" = "xhelp" ] || [ "x$1" = "x-h" ] || [ "x$1" = "x-H" ]; then
  echo "$Syntax"; echo "$Hp"; echo "$Descript"; exit
fi

###########################################################################
################# User-definable symbols 
###########################################################################
gemname="gem"                   # name of the gem to execute
gemdir=""                       # directory where the gem lives
systemConfig=""                 # GEMSTONE_SYS_CONF
exeConfig=""                    # GEMSTONE_EXE_CONF
###########################################################################
#netldiSpec not set
#runpgsvrSpec not set
appName=$gemname		# application name, for configuration

###########################################################################
################# User-Changeable Option 
################# Uncomment this block of code to enable Kerberos authentication
###########################################################################
#if [ "x$runpgsvr" = "x" ]; then
#  runpgsvr=\!#krb#dbf\!runpgsvr ; export runpgsvr
#fi
###########################################################################

# Misc support...
. $GEMSTONE/bin/misc.sh

# set configuration args
. $GEMSTONE/bin/setconfig.sh	

# determine gemname's location
if [ "$gemdir" = "" ]; then
  gemdir=$GEMSTONE/sys
fi
requireExes $gemdir/$gemname

# show what we have
# say gemnet.summary $comid "`/bin/uname -n`" $GEMSTONE $gemname $gemdir \
#    "$GEMSTONE_SYS_CONF" "$GEMSTONE_EXE_CONF" "$1 $2 $3"
hName=`/bin/uname -n`
echo "${comId}[Info]:    the hostname is:  $hName"
echo "   GEMSTONE is:      \"${GEMSTONE}\""
echo "   ${gemname}\'s location is: $gemdir"
echo "   system config file is:     $GEMSTONE_SYS_CONF"
echo "   executable config file is: $GEMSTONE_EXE_CONF"
echo "   ${gemname}\'s arguments are: $1 $2 $3 "

# The default behaviour is to delete this process's log file if it exits
# normally. If you want to keep this process's log file even on normal exit
# then unset GEMSTONE_CHILD_LOG.
unset GEMSTONE_CHILD_LOG

# By default a gem will raise its number of descriptors to whatever the
# hard limit is. By setting GEMSTONE_MAX_FD to a positive number the
# gem will only raise its descriptors to that number.
# If GEMSTONE_MAX_FD is set to 0 then the gem will not modify its descriptor
# limit
# export GEMSTONE_MAX_FD=?

# Debugging controls

# wait 2 hours for debugger to attach before dumping core
GS_CORE_TIME_OUT=7200
export GS_CORE_TIME_OUT

# --------------------------------
# Following verify and print controls  are in terms of markSweep count
# and scavenge count .  Verification or printing will start at the 
# specified scavenge or mark sweep (counted from GciLogin) , and continue
# for the life of the session .  To enable a specific item,
# uncomment the definition and the export and edit the count value.

# Verification of object memory at scavenge, for use when
# memory corruption or GC bugs are suspected.
# Warning GS_DEBUG_VMGC_VERIFY_SCAV uses lots of cpu time.
# 
# GS_DEBUG_VMGC_VERIFY_SCAV=1
# export GS_DEBUG_VMGC_VERIFY_SCAV

# Verifycation of object memory at mark sweep, for use when
# memory corruption or GC bugs are suspected.
# GS_DEBUG_VMGC_VERIFY_MKSW=1
# export GS_DEBUG_VMGC_VERIFY_MKSW

# Print two line GC summary for each scavenge.
# warning, GS_DEBUG_VMGC_PRINT_SCAV can produce lots of output
# GS_DEBUG_VMGC_PRINT_SCAV=1
# export GS_DEBUG_VMGC_PRINT_SCAV

# Print two line GC summary for each MarkSweep .
#  enabled here
# GS_DEBUG_VMGC_PRINT_MKSW=1
# export GS_DEBUG_VMGC_PRINT_MKSW

# Print detailed memory usage (about 20 lines) for each mark sweep.
# GS_DEBUG_VMGC_PRINT_MKSW_MEMORY=1
# export GS_DEBUG_VMGC_PRINT_MKSW_MEMORY

# Print Smalltalk stack  at scavenge (warning lots of output)
# GS_DEBUG_VMGC_SCAV_PRINT_STACK=1
# export GS_DEBUG_VMGC_SCAV_PRINT_STACK

# Print Smalltalk stack at mark sweep.
# GS_DEBUG_VMGC_MKSW_PRINT_STACK=1
# export GS_DEBUG_VMGC_MKSW_PRINT_STACK

# Print C stack at scavenge
# warning, GS_DEBUG_VMGC_SCAV_PRINT_C_STACK can cause extreme slow downs,
#   2 seconds per scavenge. 
# GS_DEBUG_VMGC_SCAV_PRINT_C_STACK=1
# export GS_DEBUG_VMGC_SCAV_PRINT_C_STACK

# Print C stack at markSweep
# warning, GS_DEBUG_VMGC_MKSW_PRINT_C_STACK can cause extreme slow downs,
#   2 seconds per markSweep. 
# GS_DEBUG_VMGC_MKSW_PRINT_C_STACK=1
# export GS_DEBUG_VMGC_MKSW_PRINT_C_STACK

#  Print transaction boundaries (begin/commit/abort) , 1 line each
# GS_DEBUG_VM_PRINT_TRANS=1
# export GS_DEBUG_VM_PRINT_TRANS

#  Print Smalltalk stack and instance counts when OutOfMemory error occurs
# GS_DEBUG_VMGC_VERBOSE_OUTOFMEM=1
# export GS_DEBUG_VMGC_VERBOSE_OUTOFMEM


# --------------------------------
# GS_DEBUG_VMGC_PRINT_MKSW_MEMORY_USED specifies a percent of memory 
# used threshold.
# If the GS_DEBUG_VMGC_PRINT_MKSW_MEMORY_USED environment variable
# is not defined, the default is 75 percent.
# At the end of each mark sweep, if the percent used is greater than
# this threshold, the markSweep is printed, the Smalltalk stack 
# is printed to stdout, and the threshold is raised by 5 percent.  Thus in
# situation producing error 4067, OutOfMemory, you should get several
# Smalltalk stacks printed in the gem log file before the session dies.

# GS_DEBUG_VMGC_PRINT_MKSW_MEMORY_USED=75
# export GS_DEBUG_VMGC_PRINT_MKSW_MEMORY_USED

# --------------------------------

# GS_DEBUG_VMGC_MKSW_MEMORY_USED_SOFT_BREAK specifies a percent of memory
# used threshold.
# If the GS_DEBUG_VMGC_MKSW_MEMORY_USED_SOFT_BREAK environment variable
# is not defined, the default is infinity. 
# At the end of each mark sweep, if the percent used is greater than
# this threshold, a SoftBreak (error 6003) is generated, and
# the threshold is raised by 5 percent. 
#
# Suggested setting of 75% is enabled here.
GS_DEBUG_VMGC_MKSW_MEMORY_USED_SOFT_BREAK=75
export GS_DEBUG_VMGC_MKSW_MEMORY_USED_SOFT_BREAK


# --------------------------------
# start up gemname   
#  	sleep 3600 # enable for DEBUGGING
#       exit 0 # enable for DEBUGGING
exec $gemdir/$gemname $1 $2 $3
# with the above exec nothing after this is ever executed
