Simple sh script that runs dbx (UNIX only)

Ian Cottam ian at ipse2pt5.cs.man.ac.uk
Wed Jun 26 02:57:37 AEST 1991


I've had a lot of requests for my simple little UNIX sh script
that automatically prints out a post-mortem dump after running
a program that memory faults and core dumps.

Quite a few peoples' email addresses don't work from here I'm afraid.
Anyway, it gone to comp.unix.sources - where it may or may not appear
one day. Since several people asked for it here too:.....

___________________simply cut here please____________________
#!/bin/sh
#
# usage: pmd [-v] prog args...
# shell script to run a binary prog and, if it dumps,
# runs a debugger to tell you why!
# The flag -v means print more verbose explanatory text.
#
# Ian Cottam, June 91 release 6, friday.
# Donated to the public domain :-)

if test $# -eq  0; then
   echo "`basename $0`: usage: `basename $0` [-v] prog args..." 1>&2 ; exit 1;
fi

if test $1 = "-v"
then
	SPEAKUP=1
	shift
	if test $# -eq  0; then
          echo "`basename $0`: usage: `basename $0` [-v] prog args..." 1>&2 ; exit 1;
	fi
fi

# don't be confused by random core files
if test -f core; then rm -f core; fi

# execute prog args...
$*
if test ! -f core; then exit $?; fi

# only do rest of script if prog dumped on us
# '\n'
NL='
'
# number of lines to print either side of fault
WINDOW=3

trap "rm -f /tmp/pmd$$; exit 1" 1 2 15

# find out how deeply nested we were when fault occured
{ echo up 10000; echo quit; } |  dbx $1 core 2> /tmp/pmd$$ > /dev/null
read MOVED UP N LEVELS < /tmp/pmd$$

# find out what line number, approximately, we crashed at
{ echo list; echo quit; } | dbx $1 core 2>/dev/null | sed '1,2d' > /tmp/pmd$$
read LINENO JUNK < /tmp/pmd$$
rm -f /tmp/pmd$$
# be careful re occasional junk from dbx
expr $LINENO + 42 > /dev/null 2>&1
if test $? = 2; then
  echo "`basename $0`: confused! (was -g given to compiler?), run debugger by hand!" 1>&2
  exit 1
fi

if test $SPEAKUP; then
   echo
   echo Your command "($*)" has aborted.
   echo A source-related analysis, via dbx\(1\), follows.
   echo You are cautioned to read the Bugs section of the dbx man page.
fi

# sort out line number range to print around fault area
echo "($*) -- fault on or about line $LINENO"
if test $LINENO -le $WINDOW; then LINENO=`expr $WINDOW + 1`; fi

# check if crash in main
if test $SPEAKUP; then
   echo "A few lines either side of the fault line are also printed."
   if test "$LEVELS" = "top call level"; then
    echo "The arguments and local variables to main follow."
   else
    echo "The arguments and local variables to the offending function follow."
    echo "Subsequent output shows the call trace back up to main."
   fi
fi

# print for each function level from fault point backwards
{ echo file
  echo func
  echo list `expr $LINENO - $WINDOW` "," `expr $LINENO + $WINDOW`
  echo dump
  if test "$LEVELS" && test "$LEVELS" != "top call level"; then
	while test $N -ne 0; do
	  echo up; echo file; echo dump
	  N=`expr $N - 1`
	done
  fi
}| dbx $1 core| sed "1,2d;s/^Current function is/\\${NL}Called from function:/"

exit 0

___________________simply cut here please____________________

No warranty, but let me know if you like it or modify it, please.
-Ian

--
Ian Cottam, Room IT209, Department of Computer Science,
University of Manchester, Oxford Road, Manchester, M13 9PL, U.K.
Tel: (+44) 61-275 6157         FAX: (+44) 61-275-6236
Internet: ian%cs.man.ac.uk;  JANET: ian at uk.ac.man.cs



More information about the Alt.sources mailing list