Update.
[platform/upstream/glibc.git] / debug / catchsegv.sh
1 #! /bin/sh
2
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.
6
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.
11
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.
16
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.
21
22 prog="$1"
23 shift
24 args="$*"
25
26 if test $# -eq 0; then
27   case "$args" in
28     --h | --he | --hel | --help)
29       echo 'Usage: catchsegv PROGRAM ARGS...'
30       echo '  --help      print this help, then exit'
31       echo '  --version   print version number, then exit'
32       echo "Report bugs using the \`glibcbug' script to <bugs@gnu.org>."
33       exit 0
34       ;;
35     --v | --ve | --ver | --vers | --versi | --versio | --version)
36       echo 'catchsegv (GNU libc) @VERSION@'
37       echo 'Copyright (C) 1998 Free Software Foundation, Inc.
38 This is free software; see the source for copying conditions.  There is NO
39 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
40 Written by Ulrich Drepper.'
41       exit 0
42       ;;
43     *)
44       ;;
45   esac
46 fi
47
48 LD_PRELOAD="${LD_PRELOAD:+${LD_PRELOAD}:}@SLIB@/libSegFault.so"
49 export LD_PRELOAD
50 SEGFAULT_USE_ALTSTACK=1
51 export SEGFAULT_USE_ALTSTACK
52 SEGFAULT_OUTPUT_NAME="$prog.segv.$$"
53 export SEGFAULT_OUTPUT_NAME
54
55 $prog $args
56 exval=$?
57
58 unset LD_PRELOAD
59 # Check for an segmentation fault.
60 if test $exval -eq 139; then
61   # We caught a segmentation error.  The output is in the file with the
62   # name we have in SEGFAULT_OUTPUT_NAME.  In the output the names of
63   # functions in shared objects are available, but names in the static
64   # part of the program are not.  We use addr2line to get this information.
65   (read line; echo "$line"
66    read line; echo "$line"
67    while read line; do
68      case "$line" in
69        [*) addr="`echo $line | sed 's/^[\(.*\)]$/\1/'`"
70            complete="`addr2line -f -e $prog $addr 2>/dev/null|`"
71            if $? -eq 0; then
72              echo "`echo $complete|sed 'N;s/\(.*\)\n\(.*\)/\2(\1)/;'`$line"
73            else
74              echo "$line"
75            fi
76            ;;
77         *) echo "$line"
78            ;;
79      esac
80    done) < $SEGFAULT_OUTPUT_NAME
81    rm $SEGFAULT_OUTPUT_NAME
82 fi
83
84 exit $exval