#!/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="/usr"
exec_prefix="/usr"
VALGRIND="/usr/lib/valgrind"

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

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

# Valgrind options
vgopts=

# Tool to use, chosen by --tool=<foo>, defaults to Memcheck 
tool=memcheck

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

# Default core+tool to use are the installed ones
coredir=$VALGRIND
tooldir=$VALGRIND

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


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

# Setup tool shared object.
tool_so="vgskin_${tool}.so"
if [ ! -r "$tooldir/$tool_so" ] ; then
   echo
   echo "Tool error:"
   echo "  The shared library \`$tool_so' for the chosen"
   echo "  tool \`$tool' could not be found in"
   echo "  $tooldir"
   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 getconf GNU_LIBPTHREAD_VERSION 2>/dev/null | grep -qi NPTL 2>/dev/null; then
   LD_ASSUME_KERNEL=2.4.1
   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 ] && (echo "$1" | grep -q '/'); then
      which_prog=$1
   fi

   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 tool .so before valgrind.so to override template functions.
LD_PRELOAD=$tooldir/$tool_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 ---##
##--------------------------------------------------------------------##
