Update copyright dates not handled by scripts/update-copyrights.
[glibc.git] / debug / catchsegv.sh
blobf8328eee69b2566779ae460d1df2bbb46f5bc7b1
1 #!/bin/sh
2 # Copyright (C) 1998-2022 Free Software Foundation, Inc.
3 # This file is part of the GNU C Library.
5 # The GNU C Library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # The GNU C Library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with the GNU C Library; if not, see
17 # <https://www.gnu.org/licenses/>.
19 if test $# -eq 0; then
20 echo "$0: missing program name" >&2
21 echo "Try \`$0 --help' for more information." >&2
22 exit 1
25 prog="$1"
26 shift
28 if test $# -eq 0; then
29 case "$prog" in
30 --h | --he | --hel | --help)
31 echo 'Usage: catchsegv PROGRAM ARGS...'
32 echo ' --help print this help, then exit'
33 echo ' --version print version number, then exit'
34 echo 'For bug reporting instructions, please see:'
35 cat <<\EOF
36 @REPORT_BUGS_TO@.
37 EOF
38 exit 0
40 --v | --ve | --ver | --vers | --versi | --versio | --version)
41 echo 'catchsegv @PKGVERSION@@VERSION@'
42 echo 'Copyright (C) 2022 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=`mktemp ${TMPDIR:-/tmp}/segv_output.XXXXXX` || exit
55 # Redirect stderr to avoid termination message from shell.
56 (exec 3>&2 2>/dev/null
57 LD_PRELOAD=${LD_PRELOAD:+${LD_PRELOAD}:}@SLIB@/libSegFault.so \
58 SEGFAULT_USE_ALTSTACK=1 \
59 SEGFAULT_OUTPUT_NAME=$segv_output \
60 "$prog" ${1+"$@"} 2>&3 3>&-)
61 exval=$?
63 # Check for output. Even if the program terminated correctly it might
64 # be that a minor process (clone) failed. Therefore we do not check the
65 # exit code.
66 if test -s "$segv_output"; then
67 # The program caught a signal. The output is in the file with the
68 # name we have in SEGFAULT_OUTPUT_NAME. In the output the names of
69 # functions in shared objects are available, but names in the static
70 # part of the program are not. We use addr2line to get this information.
71 case $prog in
72 */*) ;;
74 old_IFS=$IFS
75 IFS=:
76 for p in $PATH; do
77 test -n "$p" || p=.
78 if test -f "$p/$prog"; then
79 prog=$p/$prog
80 break
82 done
83 IFS=$old_IFS
85 esac
86 sed '/Backtrace/q' "$segv_output"
87 sed '1,/Backtrace/d' "$segv_output" |
88 (while read line; do
89 line=`echo $line | sed "s@^$prog\\(\\[.*\\)@\1@"`
90 case "$line" in
91 \[*) addr=`echo "$line" | sed 's/^\[\(.*\)\]$/\1/'`
92 complete=`addr2line -f -e "$prog" $addr 2>/dev/null`
93 if test $? -eq 0; then
94 echo "`echo "$complete"|sed 'N;s/\(.*\)\n\(.*\)/\2(\1)/;'`$line"
95 else
96 echo "$line"
99 *) echo "$line"
101 esac
102 done)
104 rm -f "$segv_output"
106 exit $exval