Update.
[glibc.git] / debug / catchsegv.sh
blob48c447b039e4b9078ae57869e20719882a8cdb0b
1 #! /bin/sh
3 # Copyright (C) 1998 Free Software Foundation, Inc.
4 # This file is part of the GNU C Library.
5 # Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
7 # The GNU C Library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Library General Public License as
9 # published by the Free Software Foundation; either version 2 of the
10 # License, or (at your option) any later version.
12 # The GNU C Library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Library General Public License for more details.
17 # You should have received a copy of the GNU Library General Public
18 # License along with the GNU C Library; see the file COPYING.LIB. If not,
19 # write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 # Boston, MA 02111-1307, USA.
22 if test $# -eq 0; then
23 echo "$0: missing programm name" >&2
24 echo "Try \`$0 --help' for more information." >&2
25 exit 1
28 prog="$1"
29 shift
31 if test $# -eq 0; then
32 case "$prog" in
33 --h | --he | --hel | --help)
34 echo 'Usage: catchsegv PROGRAM ARGS...'
35 echo ' --help print this help, then exit'
36 echo ' --version print version number, then exit'
37 echo "Report bugs using the \`glibcbug' script to <bugs@gnu.org>."
38 exit 0
40 --v | --ve | --ver | --vers | --versi | --versio | --version)
41 echo 'catchsegv (GNU libc) @VERSION@'
42 echo 'Copyright (C) 1998 Free Software Foundation, Inc.
43 This is free software; see the source for copying conditions. There is NO
44 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
45 Written by Ulrich Drepper.'
46 exit 0
50 esac
53 segv_output=`basename "$prog"`.segv.$$
55 LD_PRELOAD=${LD_PRELOAD:+${LD_PRELOAD}:}@SLIB@/libSegFault.so \
56 SEGFAULT_USE_ALTSTACK=1 \
57 SEGFAULT_OUTPUT_NAME=$segv_output \
58 "$prog" ${1+"$@"}
59 exval=$?
61 # Check for a segmentation error.
62 if test $exval -eq 139 && test -f "$segv_output"; then
63 # We caught a segmentation error. The output is in the file with the
64 # name we have in SEGFAULT_OUTPUT_NAME. In the output the names of
65 # functions in shared objects are available, but names in the static
66 # part of the program are not. We use addr2line to get this information.
67 case $prog in
68 */*) ;;
70 old_IFS=$IFS
71 IFS=:
72 for p in $PATH; do
73 test -n "$p" || p=.
74 if test -f "$p/$prog"; then
75 prog=$p/$prog
76 break
78 done
79 IFS=$old_IFS
81 esac
82 (read line; echo "$line"
83 read line; echo "$line"
84 while read line; do
85 case "$line" in
86 [*) addr=`echo $line | sed 's/^\[\(.*\)\]$/\1/'`
87 complete=`addr2line -f -e "$prog" $addr 2>/dev/null`
88 if test $? -eq 0; then
89 echo "`echo "$complete"|sed 'N;s/\(.*\)\n\(.*\)/\2(\1)/;'`$line"
90 else
91 echo "$line"
94 *) echo "$line"
96 esac
97 done) < "$segv_output"
98 rm -f "$segv_output"
101 exit $exval