home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3547 < prev    next >
Encoding:
Internet Message Format  |  1991-06-26  |  3.4 KB

  1. From: ian@ipse2pt5.cs.man.ac.uk (Ian Cottam)
  2. Newsgroups: alt.sources
  3. Subject: Simple sh script that runs dbx (UNIX only)
  4. Message-ID: <2791@m1.cs.man.ac.uk>
  5. Date: 25 Jun 91 16:57:37 GMT
  6.  
  7. I've had a lot of requests for my simple little UNIX sh script
  8. that automatically prints out a post-mortem dump after running
  9. a program that memory faults and core dumps.
  10.  
  11. Quite a few peoples' email addresses don't work from here I'm afraid.
  12. Anyway, it gone to comp.unix.sources - where it may or may not appear
  13. one day. Since several people asked for it here too:.....
  14.  
  15. ___________________simply cut here please____________________
  16. #!/bin/sh
  17. #
  18. # usage: pmd [-v] prog args...
  19. # shell script to run a binary prog and, if it dumps,
  20. # runs a debugger to tell you why!
  21. # The flag -v means print more verbose explanatory text.
  22. #
  23. # Ian Cottam, June 91 release 6, friday.
  24. # Donated to the public domain :-)
  25.  
  26. if test $# -eq  0; then
  27.    echo "`basename $0`: usage: `basename $0` [-v] prog args..." 1>&2 ; exit 1;
  28. fi
  29.  
  30. if test $1 = "-v"
  31. then
  32.     SPEAKUP=1
  33.     shift
  34.     if test $# -eq  0; then
  35.           echo "`basename $0`: usage: `basename $0` [-v] prog args..." 1>&2 ; exit 1;
  36.     fi
  37. fi
  38.  
  39. # don't be confused by random core files
  40. if test -f core; then rm -f core; fi
  41.  
  42. # execute prog args...
  43. $*
  44. if test ! -f core; then exit $?; fi
  45.  
  46. # only do rest of script if prog dumped on us
  47. # '\n'
  48. NL='
  49. '
  50. # number of lines to print either side of fault
  51. WINDOW=3
  52.  
  53. trap "rm -f /tmp/pmd$$; exit 1" 1 2 15
  54.  
  55. # find out how deeply nested we were when fault occured
  56. { echo up 10000; echo quit; } |  dbx $1 core 2> /tmp/pmd$$ > /dev/null
  57. read MOVED UP N LEVELS < /tmp/pmd$$
  58.  
  59. # find out what line number, approximately, we crashed at
  60. { echo list; echo quit; } | dbx $1 core 2>/dev/null | sed '1,2d' > /tmp/pmd$$
  61. read LINENO JUNK < /tmp/pmd$$
  62. rm -f /tmp/pmd$$
  63. # be careful re occasional junk from dbx
  64. expr $LINENO + 42 > /dev/null 2>&1
  65. if test $? = 2; then
  66.   echo "`basename $0`: confused! (was -g given to compiler?), run debugger by hand!" 1>&2
  67.   exit 1
  68. fi
  69.  
  70. if test $SPEAKUP; then
  71.    echo
  72.    echo Your command "($*)" has aborted.
  73.    echo A source-related analysis, via dbx\(1\), follows.
  74.    echo You are cautioned to read the Bugs section of the dbx man page.
  75. fi
  76.  
  77. # sort out line number range to print around fault area
  78. echo "($*) -- fault on or about line $LINENO"
  79. if test $LINENO -le $WINDOW; then LINENO=`expr $WINDOW + 1`; fi
  80.  
  81. # check if crash in main
  82. if test $SPEAKUP; then
  83.    echo "A few lines either side of the fault line are also printed."
  84.    if test "$LEVELS" = "top call level"; then
  85.     echo "The arguments and local variables to main follow."
  86.    else
  87.     echo "The arguments and local variables to the offending function follow."
  88.     echo "Subsequent output shows the call trace back up to main."
  89.    fi
  90. fi
  91.  
  92. # print for each function level from fault point backwards
  93. { echo file
  94.   echo func
  95.   echo list `expr $LINENO - $WINDOW` "," `expr $LINENO + $WINDOW`
  96.   echo dump
  97.   if test "$LEVELS" && test "$LEVELS" != "top call level"; then
  98.     while test $N -ne 0; do
  99.       echo up; echo file; echo dump
  100.       N=`expr $N - 1`
  101.     done
  102.   fi
  103. }| dbx $1 core| sed "1,2d;s/^Current function is/\\${NL}Called from function:/"
  104.  
  105. exit 0
  106.  
  107. ___________________simply cut here please____________________
  108.  
  109. No warranty, but let me know if you like it or modify it, please.
  110. -Ian
  111.  
  112. --
  113. Ian Cottam, Room IT209, Department of Computer Science,
  114. University of Manchester, Oxford Road, Manchester, M13 9PL, U.K.
  115. Tel: (+44) 61-275 6157         FAX: (+44) 61-275-6236
  116. Internet: ian%cs.man.ac.uk;  JANET: ian@uk.ac.man.cs
  117.