#!/bin/sh
##--------------------------------------------------------------------##
##--- The startup script.                                 valgrind ---##
##--------------------------------------------------------------------##

#  This file is part of Valgrind, an extensible x86 protected-mode
#  emulator for monitoring program execution on x86-Unixes.
#
#  Copyright (C) 2002-2003 Julian Seward
#     jseward@acm.org
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
#  02111-1307, USA.
#
#  The GNU General Public License is contained in the file COPYING.


# Should point to the installation directory
prefix="/home/csprofs/neal/local"
exec_prefix="${prefix}"
VALGRIND="${exec_prefix}/lib/valgrind"
nptl_threading="yes"

# Other stuff ...
version="2.0.0"
emailto="jseward@acm.org"

# The default name of the suppressions file
vgsupp="--suppressions=$VALGRIND/default.supp"

# Valgrind options
vgopts=

# --skin=<foo> arg, specifying skin used
skin_arg=

# --in-place=<dir> arg, for using non-installed version
in_place_arg=

# Default core+skin to use are the installed ones
coredir=$VALGRIND
skindir=$VALGRIND

# Collect up args for Valgrind.  Only some are intercepted here;
# the rest are passed to vg_main.c.
while [ $# != 0 ]
do
  arg=$1
  case "$arg" in
    --version)              echo "valgrind-$version"; exit 1 ;;
    --skin=*)               skin_arg=$arg;            shift;;
    --in-place=*)           in_place_arg=$arg;        shift;;
    -*)                     vgopts="$vgopts $arg";    shift;;
    *)                      break;;
  esac
done


# Decide on the skin.  Default to memory checking if not specified.
if [ z"$skin_arg" = z ]; then
   skin=memcheck
else
   # Hack off the "--skin=" prefix.
   skin=`echo $skin_arg | sed 's/--skin=//'`
fi

# If running uninstalled version in-place...
if [ z"$in_place_arg" != z ]; then
   in_place_dir=`echo $in_place_arg | sed 's/--in-place=//'`
   skindir="$in_place_dir/$skin"
   coredir="$in_place_dir/coregrind/.in_place"
   vgsupp="--suppressions=$in_place_dir/default.supp"
fi

# Setup skin shared object.
skin_so="vgskin_${skin}.so"
if [ ! -r "$skindir/$skin_so" ] ; then
   echo
   echo "Skin error:"
   echo "  The shared library \`$skin_so' for the chosen"
   echo "  skin \`$skin' could not be found in"
   echo "  $skindir"
   echo
   exit 1
fi

VG_ARGS="$VALGRIND_OPTS $vgsupp $vgopts"

export VG_ARGS

# Red Hat Linux 9 uses NPTL, which has a kernel interface 
# unlike the linuxthreads interface valgrind expects.  We can
# tell the dynamic loader to disable this interface using
# an environment variable.

if [ z"$nptl_threading" = zyes ]; then
   LD_ASSUME_KERNEL=2.2.5
   export LD_ASSUME_KERNEL 
fi

# Check that the program looks ok
is_prog=0

if [ $# != 0 ] ; then

   # Ensure the program exists.  Ignore any error messages from 'which'.
   which_prog=`which $1 2> /dev/null`
   if [ z$which_prog = z ] ; then
      echo "$0: '$1' not found in \$PATH, aborting."
      exit
   fi

   if [ $# != 0 ] ; then
      case `file -L "$which_prog"` in       # must follow symlinks, hence -L
      # Ensure the program isn't statically linked.
      *"statically linked"*)
        echo "\`$which_prog' is statically linked"
        echo "Valgrind only works on dynamically linked executables;  your"
        echo "program must rely on at least one shared object for Valgrind"
        echo "to work with it.  Read FAQ #5 for more information."
        exit 1 ;;
      # Ensure that there are no setuid or gid flags
      *:\ set?id\ ELF*)
        echo "\`$which_prog' is suid/sgid."
        echo "Valgrind can't handle these executables, as it"
        echo "requires the LD_PRELOAD feature in order to work."
        echo ""
        echo "Remove those flags and try again."
        echo ""
        exit 1
        ;;
      esac
   fi

   is_prog=1
fi

# A bit subtle.  The LD_PRELOAD added entry must be absolute
# and not depend on LD_LIBRARY_PATH.  This is so that we can
# mess with LD_LIBRARY_PATH for child processes, which makes
# libpthread.so fall out of visibility, independently of
# whether valgrind.so is visible.

LD_LIBRARY_PATH=$coredir:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

# Insert skin .so before valgrind.so to override template functions.
LD_PRELOAD=$skindir/$skin_so:$coredir/valgrind.so:$LD_PRELOAD
export LD_PRELOAD
#LD_DEBUG=files
#LD_DEBUG=symbols
#export LD_DEBUG
    
# Actually run the program, under Valgrind's control
if [ $is_prog = 1 ] ; then
   exec "$@"
else
   # If no command given, act like -h was given so vg_main.c prints out the
   # usage string.  And pass to 'exec' the name of any program -- it doesn't
   # matter which -- because it won't be run anyway (we use 'true').
   VG_ARGS="$VG_ARGS -h"
   exec true
fi

##--------------------------------------------------------------------##
##--- end                                                 valgrind ---##
##--------------------------------------------------------------------##