#! /bin/bash
# Copyright (C) VMware, Inc. 1986-2011.  All Rights Reserved.
#
# GemStone pstack -- print the stack for a given PID using GDB.
# usage: pstack <pid>
#
# GDB must be in the user's PATH, and $GEMSTONE must be valid

if [ $# -ne 1 ] ; then
  echo "Usage: pstack <pid>"
  exit 1
fi

pid=$1

gdb=gdb
type $gdb
errcode=$?
if [ $errcode -ne 0 ] ; then
    echo "Cannot find $gdb in path $PATH"
    exit $errcode
fi

## Invoke GDB, filter output
# --nw prevents GUI interface to GDB, if installed
# --nx prevents user's .gdbinit from being run
# --batch suppresses version/copyright header and terminates GDB when
#   input file exhausted
# --pid attaches to the given process
# --command runs commands from the given file
$gdb --nw --nx --batch --pid=$pid --command=$GEMSTONE/bin/pstack.gdb | \
  grep -Ev '^\(gdb\)|^Reading|^Loaded|No such file or directory\.$'
#"No such file or directory" regularly appears on Mac, not a real problem
