#!/bin/sh
# Make terminal ansi-color
export TVOPT=ansi
# Fix screwed-up LD environment on power machines.
# export LD_LIBRARY_PATH=/uf1/jdh8d/RA/NICECAP/idapro_lib/
export LD_LIBRARY_PATH=${IDAROOT}
# Set IDA path.
export IDADIR=$IDAROOT
# Make sure that the idal executable can be reached from PATH.
export PATH=$PATH:$IDADIR
export STARS_ROOT_FILENAME=$1
export IDALOG=$1.log
# export IDALOG_SILENT=1
# Extract the code addresses with objdump.
objdump -d --prefix-addresses $1 | grep "^[0-9]" > $1.SMPobjdump

# Extract the exception-handling function target addresses.
# dwarfdump -F $1 | grep "^<" | cut --delimiter="<" --field=3 | cut --delimiter=":" --field=1 | cut --bytes 4-10 > $1.eh_frame_addrs
# Make readelf do the same thing as the above dwarfdump command, to 
#  eliminate configuration dependence on dwarfdump.
readelf -wF $1 | grep "pc=" | cut --delimiter="=" --field=3 | cut --delimiter="." --field=1 > $1.eh_frame_addrs

if [ "${IDAROOT}"X = "X"  ];  then echo Please set IDAROOT; exit 1; fi
if [ ! -f ${IDAROOT}/libida.so ]; then echo IDAROOT is set poorly, please fix.; exit 1; fi


# remove tmp files
rm -f $1.id? $1.{i64,nam,til,log,syms}
rm -f `basename $1 .psexe`.{i64,nam,til} `basename $1 .psexe`.id?
rm -f `basename $1 .ncexe`.{i64,nam,til} `basename $1 .ncexe`.id?
rm -f `basename $1 .exe`.{i64,nam,til} `basename $1 .exe`.id?

# old_stty=`stty -g`



# Run the linux IDA Pro with the SMPStaticAnalyzer plugin.
# jdh - added -B for batch mode, avoids any prompts.
# jdh - note:  can't use -B, as the plugin doesn't run!

# TVHEADLESS=1 time ${IDAROOT}/idat64  -A -Telf -OSMPStaticAnalyzer:run $1 2>&1 >/dev/null

# TVHEADLESS=1 time ${IDAROOT}/idat64  -A -SSMP.idc -OSMPStaticAnalyzer:ShadowFuncPtrs:ShadowArgs:ImproveCFG:AnalyzeLoops:SCCP:NoGenAsm $1 > $1.idaoutput

TVHEADLESS=1 time ${IDAROOT}/idat64  -A -SSMP7noasm.idc -OSMPStaticAnalyzer:ShadowFuncPtrs:ShadowArgs:ImproveCFG:AnalyzeLoops:SCCP:NoGenAsm $1 > $1.idaoutput

# TVHEADLESS=1 time ${IDAROOT}/idat64  -A -SSMP7.idc -OSMPStaticAnalyzer:ShadowFuncPtrs:ShadowArgs:ImproveCFG:AnalyzeLoops:SCCP:NoGenAsm $1 > $1.idaoutput

# TVHEADLESS=1 valgrind -v --leak-check=full --show-reachable=yes --track-origins=yes --log-file=$1.valgrind.log ${IDAROOT}/idat64  -A -SSMP7.idc $1 


# SMP seems to mess up the terminal, using this to fix it.
# /usr/bin/reset 
# stty $old_stty

# Get the output file SMP.syms into $1.syms.
# mv SMP.annot $1.annot