#!/bin/bash
#
# Description: Show the process trace buffer of a specific PID or all processes with
#              a process trace buffer.
#
# Change Activity:
#   11/08/2004 P. Callaghan     - Initial version
#                               line of each process
#
#STARTUSAGE
#
# This script allows you to show the process trace buffer of a specific
# PID or all processes with a process trace buffer. Note, when the option
# to show all processes is used, the process trace buffer within processes
# with multiple threads is only displayed once.
#
# Usage:
#   showTraceBuf {pid | "all");
#          where pid specifies the process for which the process trace buffer is
#          to be shown and "all", specifies that all processes that
#          are using the trace library, libiqzdtrac.so, will be shown.
#          Note, "all" must be specified without the double quotes.
#          The process trace buffer is writtent to standard out (stdout).
#
#ENDUSAGE
#****************************************************************************

# Setup PATH so the showTrace binary, in the base tower, can be found.
if [ -n "$CONSOLE_PATH" ]; then
   # CONSOLE_PATH is set so use it.
   export PATH=${CONSOLE_PATH}/bin/base/:$PATH
else
   CCFW_BIN_BASE=/opt/ccfw/bin/base/
   CONSOLE_BIN_BASE=/console/bin/base/
   if [ -d $CCFW_BIN_BASE ]; then
      export PATH=$CCFW_BIN_BASE:$PATH
   elif [ -d $CONSOLE_BIN_BASE ]; then
      export PATH=$CONSOLE_BIN_BASE:$PATH
   else
      echo 'Cannot locate path for binaries in the CCFW base tower. Export the CONSOLE_PATH environment variable and try again.'
      exit 1
   fi
fi

# Setup LD_LIBRARY_PATH for the zSeries console.
if [ -n "$CONSOLE_PATH" ]; then
   # CONSOLE_PATH is set so use it.
   export LD_LIBRARY_PATH=${CONSOLE_PATH}/lib/framework/:$LD_LIBRARY_PATH
else
   CONSOLE_TRACE_LIBRARY_DIR=/console/lib/framework/
   if [ -d $CONSOLE_TRACE_LIBRARY_PATH ]; then
      export LD_LIBRARY_PATH=$CONSOLE_TRACE_LIBRARY_DIR:$LD_LIBRARY_PATH
   fi
fi

# Setup our main classpath to the base tower classes.
CCFW_CLASSPATH=/opt/ccfw/ccfw.jar
CONSOLE_CLASSPATH=/console/
if [ -f $CCFW_CLASSPATH ]; then
   OUR_CLASSPATH=$CCFW_CLASSPATH
elif [ -n "$CONSOLE_PATH" ]; then
   # CONSOLE_PATH is set so use it.
   OUR_CLASSPATH=$CONSOLE_PATH
elif [ -d $CONSOLE_CLASSPATH ]; then
   OUR_CLASSPATH=$CONSOLE_CLASSPATH
else
   echo 'Cannot locate classpath for the CCFW base tower.'
   exit 1
fi

javaProgram=`which java`
if [ $? -ne 0 ]; then
   # java is not in the path so use the best guess.
   javaProgram="/java/bin/java"
fi
${javaProgram} -cp $OUR_CLASSPATH com.ibm.hwmca.base.monitor.DumpTraceBuf $@

exit $?
