#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/bde/bmem/etc/bmemrun.in              */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Sun Apr 20 09:35:07 2003                          */
#*    Last change :  Mon Oct 23 16:47:04 2017 (serrano)                */
#*    Copyright   :  2003-17 Manuel Serrano                            */
#*    -------------------------------------------------------------    */
#*    The script shell to monitor memory allocations of Bigloo prgms   */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    Configuration                                                    */
#*---------------------------------------------------------------------*/
safety=s
lib=
libextra=
exe=a.out
bmon=

#*---------------------------------------------------------------------*/
#*    Argument parsing                                                 */
#*---------------------------------------------------------------------*/
while : ; do
  case $1 in
    "")
      break;;

    -h|--help)
      echo "bglmemrun: [options] exe [arg1] [arg2]..." >&2;
      echo "  -h|--help            -- This message" >&2;
      echo "  -s|-safe|--safe      -- Preload safe libraries" >&2;
      echo "  -u|-unsafe|--unsafe  -- Preload unsafe libraries" >&2;
      echo "  -t|--thread          -- Profile multithreaded program" >&2;
      echo "  --lib-extra <lib>    -- Add an extra profiling lib" >&2;
      echo "" >&2;
      echo "To be profiled, programs have to be compiled with" >&2;
      echo "  -g [-gtraceN]" >&2;
      echo "" >&2;
      echo "Normally bmem finds automatically the correct libraries to use" >&2;
      echo "however, if needed, to force the unsafe multithread libaries" >&2;
      echo "use the following options" >&2;
      echo "  -u -t" >&2;
      echo "in that order" >&2;
      exit 0;;

    -s|-safe|--safe)
      safety=s;
      lib=/usr/lib/bigloo/4.3e/bmem/bmem_$safety.so;;
    
    -u|-unsafe|--unsafe)
      safety=u;
      lib=/usr/lib/bigloo/4.3e/bmem/bmem_$safety.so;;

    --lib-extra)
      shift;
      libextra="$libextra $1";
      shift;
      break;;

    -t|--thread)
      lib=/usr/lib/bigloo/4.3e/bmem/bmem_fth_$safety.so;;
    
    *)
      exe=$1;
      shift;
      args=$*;
      if [ "$bmon " = " " ]; then
        bmon="`echo $exe | sed 's/[.][^.]*$//'`.bmem";
        bmon=`basename $bmon`;
      fi;
      break;;

  esac
  shift
done


#*---------------------------------------------------------------------*/
#*    autoconfiguration                                                */
#*---------------------------------------------------------------------*/
if [ "$lib " = " " ]; then
  # safety
  g=`ldd $exe | grep libbigloo_u`
  if [ "$g " = " " ]; then
    safety=s
  else
    safety=u
  fi

  # gc
  g=`ldd $exe | grep libbigloogc_fth`
  if [ "$g " = " " ]; then
    gc=
  else
    gc=_fth
  fi
    
  lib=/usr/lib/bigloo/4.3e/bmem/bmem"$gc"_"$safety".so
fi


#*---------------------------------------------------------------------*/
#*    Run the program                                                  */
#*---------------------------------------------------------------------*/
export BMEMUNSAFE=_$safety
LD_PRELOAD="$lib$libextra" $exe $args
