#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002,2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)28 1.3 src/avs/fs/mmfs/ts/admin/mmdumpfilocks.sh, mmfs, avs_rgpfs24, rgpfs240610b 10/16/02 23:26:49

sourceFile="mmdumpfilocks.sh"
[[ -n $DEBUG || -n $DEBUGmmdumpfilocks ]] && set -x

# full pathnames to dump commands
crash=/usr/sbin/crash
kdb=/usr/sbin/kdb
lcrash=/sbin/lcrash

if [[ $1 = '-?' || $1 = '-h' || $1 = '--help' || $1 = '--' ]]
then
  print -u2 "Usage:  mmdumpfilocks [dump [kernelfile]]"
  exit 1
fi

if [[ -x $lcrash ]]
then                               # Linux lcrash debugger
  # All the smarts are in the sial script
  print "load /usr/lpp/mmfs/bin/mmdumpfilocks.sial\\nmmdumpfilocks\\nquit" \
         | $lcrash 2>/dev/null \
         | /bin/awk '{ if (NR>3) { \
                         print $0; \
                       } \
                     }'


elif [[ -x $crash ]]
then                               # AIX pre-rel5 crash debugger
  # Stage 1: Select the GPFS vnodes, extract the gnode address and
  #          pass it forward as a comment to the next stage along
  #          with the offset of gn_filocks.  
  # Stage 2: Extract the value of gn_filocks passing it forward
  #          along with the gnode pointer.
  # Stage 3: If gn_filocks is not NULL, display the gnode and use
  #          the link command to follow the lock chain.
  print "vnode" | $crash $1 $2 2> /dev/null \
                | /usr/bin/awk '{ if ($5=="mmfs") { \
                                    print "! print", "gnode", $8; \
                                    print "calc", $8"+3C"; \
                                  } \
                                }' \
                | $crash $1 $2 2> /dev/null \
                | /usr/bin/awk '{ if ($1=="gnode") { \
                                    print "! print", "gnode", $2; \
                                  } else { \
                                    print "od", $1, "1 h"; \
                                  } \
                                }' \
                | $crash $1 $2 2> /dev/null \
                | /usr/bin/awk '{ if ($1=="gnode") { \
                                    GNODEP=$2; \
                                  } else if ($2!="00000000") { \
                                    print "! print GNODE"; \
                                    print "od", GNODEP, "24"; \
                                    print "! print FILOCKS"; \
                                    print "link", $2" 15 14 0"; \
                                  } \
                                }'  \
                | $crash $1 $2 2> /dev/null

elif [[ -x $kdb ]]
then                               # AIX rel5 kdb debugger
  # Stage 1: Select the vnode pointers from the file output.
  #          Note that we could start with "vnode" instead of going
  #          through the files, but the file list can be substantially
  #          shorter.  Also, don't start with inodes because they
  #          can be paged out.
  # Stage 2: Select the gnode pointers from the vnode output.
  #          Use "sort -u" to remove duplicates caused by two files
  #          associated with the same gnode.
  # Stage 3: It's hard to tell GPFS gnodes from others, but most
  #          others have symbolic gn_ops values (eg., "jfs_vops").
  #          If gn_ops is an address only, continue looking for locks.
  #          If gn_filocks is not NULL, display the gnode and use
  #          the "ext" command to follow the lock chain.

  print "file " | $kdb $1 $2 2> /dev/null \
                | /usr/bin/awk '{ if ($6=="VNODE" && $5!="00000000") { \
                                    print "vnode", $5 } \
                                }' \
                | $kdb $1 $2 2> /dev/null \
                | /usr/bin/awk '{ if ($3=="v_gnode..." && $4!="00000000") { \
                                    print "gnode", $4} \
                                }' \
                | /usr/bin/sort -u \
                | $kdb $1 $2 2> /dev/null \
                | /usr/bin/awk '{ if ($1=="GNODE............") { \
                                    GNODEP=$2; \
                                    getline;   \
                                  } else if ($5=="gn_ops........") { \
                                    VOPS=$6; \
                                    getline;   \
                                  } else if ($3=="gn_filocks...." && $4!="00000000" && \
                                             VOPS !~ /[g-z]/) { \
                                    print "! print GNODE"; \
                                    print "dw", GNODEP" 18"; \
                                    print "! print FILOCKS"; \
                                    print "ext -p", $4, "38 F 10"; \
                                  } \
                                }' \
                | $kdb $1 $2 2> /dev/null \
                | /usr/bin/awk '{ if (NR>20 && $1!="(0)>") { \
                                    print $0; \
                                  } \
                                }'

else
  print "Could not locate an appropriate dump viewing facility."
fi
exit 0
